Ejemplo n.º 1
0
        public void EnumParsingRules()
        {
            IXmlParserSettings parserSettings = new XmlParserSettings();

            Property <Sex> enumProp   = new Property <Sex>("Sex");
            var            enumParser = parserSettings.ParserRules.GetParserRule(enumProp).Parser as IValueParser;

            enumParser.Should().NotBeNull();

            CheckValue(enumProp, enumParser, "Male", true, Sex.Male);
            CheckValue(enumProp, enumParser, "???", false, null);
            CheckValue(enumProp, enumParser, null, false, null);

            Property <Sex?> nullableEnumProp   = new Property <Sex?>("OptionalSex");
            var             nullableEnumParser = parserSettings.ParserRules.GetParserRule(nullableEnumProp).Parser as IValueParser;

            nullableEnumParser.Should().NotBeNull();

            CheckValue(nullableEnumProp, nullableEnumParser, "Male", true, Sex.Male);
            CheckValue(nullableEnumProp, nullableEnumParser, "???", false, null);
            CheckValue(nullableEnumProp, nullableEnumParser, null, true, null);

            void CheckValue(IProperty property, IValueParser valueParser, string value, bool shouldBeSuccess, object shouldBeValue)
            {
                var parseResult = valueParser.ParseUntyped(value);

                parseResult.IsSuccess.Should().Be(shouldBeSuccess);
                if (shouldBeSuccess)
                {
                    IPropertyValue propertyValue = PropertyValue.Create(property, parseResult.ValueUntyped);
                    propertyValue.ValueUntyped.Should().Be(shouldBeValue);
                }
            }
        }
Ejemplo n.º 2
0
        public void TestStringSerialize()
        {
            var prop = PropertyValue.Create("abc", "def");
            var tm0  = TypeModel.Create(false, ProtoCompatibilitySettingsValue.FullCompatibility);

            tm0.DeepClone(prop);
            string hex;

            using (MemoryStream ms = new MemoryStream())
            {
                var tm = TypeModel.Create(false, ProtoCompatibilitySettingsValue.FullCompatibility);
                tm.Serialize(ms, prop);
                hex = Util.GetHex(ms.ToArray());
            }
            Assert.AreEqual(
                "32"       // field 6, string
                + "05"     // 5 bytes
                + "0A"     // field 1, string
                + "03"     // 3 bytes
                + "646566" // "def"
                + "0A"     // field 1, string
                + "03"     // 3 bytes
                + "616263" // "abc"
                , hex);
        }
Ejemplo n.º 3
0
        public void TestInt32RoundTrip()
        {
            var orig     = PropertyValue.Create("abc", 123);
            var intClone = Serializer.DeepClone(orig);

            Assert.Equal("abc", intClone.Name);
            Assert.Equal(123, intClone.Value);
        }
Ejemplo n.º 4
0
        public void TestStringRoundTrip()
        {
            var stringClone = Serializer.DeepClone(
                PropertyValue.Create("abc", "def"));

            Assert.Equal("abc", stringClone.Name);
            Assert.Equal("def", stringClone.Value);

            Serializer.PrepareSerializer <PropertyValue>();

            stringClone = Serializer.DeepClone(
                PropertyValue.Create("abc", "def"));
            Assert.Equal("abc", stringClone.Name);
            Assert.Equal("def", stringClone.Value);
        }
Ejemplo n.º 5
0
        public static IPropertyValue ToModel(this PropertyValueContract propertyValueContract, IMapperSettings mapperSettings, IMutableMessageList <Message>?messages = null)
        {
            Type      propertyType = mapperSettings.GetTypeByName(propertyValueContract.Type);
            string    propertyName = propertyValueContract.Name;
            IProperty property     = Property.Create(propertyType, propertyName);

            var propertyValueResult = mapperSettings.DeserializeValue(propertyType, propertyValueContract.Value);

            object?value = propertyValueResult.GetValueOrDefault(message =>
            {
                messages?.Add(message);
                return(null);
            });

            IPropertyValue propertyValue = PropertyValue.Create(property, value: value, valueSource: propertyValueResult.IsSuccess ? ValueSource.Defined : ValueSource.NotDefined);

            return(propertyValue);
        }
Ejemplo n.º 6
0
        public void TestStringSerialize()
        {
            var    prop = PropertyValue.Create("abc", "def");
            string hex;

            using (MemoryStream ms = new MemoryStream())
            {
                Serializer.Serialize(ms, prop);
                hex = Util.GetHex(ms.ToArray());
            }

            Assert.Equal(
                "32"       // field 6, string
                + "05"     // 5 bytes
                + "0A"     // field 1, string
                + "03"     // 3 bytes
                + "646566" // "def"
                + "0A"     // field 1, string
                + "03"     // 3 bytes
                + "616263" // "abc"
                , hex);
        }