public void ShouldNotMapSameTermNameMoreThanOnce()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();

            nameAndTypeMappings.MapTermNameToType <FileInfo>("term1");

            // Act & Assert
            Assert.Throws <Exception>(() =>
            {
                nameAndTypeMappings.MapTermNameToType <DateTime>("term1");
            });
        }
Beispiel #2
0
        private static void InheritanceAndNaming()
        {
            Console.WriteLine("Inheritance and naming ...");
            var nameMappings = new NameAndTypeMappings();
            var serializer   = new StringSerializer(nameAndTypeMappings: nameMappings);

            nameMappings.MapTermNameToType <Circle>("circle");
            nameMappings.MapTermNameToType <Square>("square");
            IShape circle = serializer.Deserialize <IShape>("root: circle<Radius>(10);");
            IShape square = serializer.Deserialize <IShape>("root: square<Width>(10);");

            Console.WriteLine(circle.GetType().Name);
            Console.WriteLine(square.GetType().Name);
            Console.WriteLine();
        }
Beispiel #3
0
        private static void ExpresionTree()
        {
            Console.WriteLine("Expression trees ...");

            var nameMappings = new NameAndTypeMappings();
            var serializer   = new StringSerializer(nameAndTypeMappings: nameMappings);

            nameMappings.MapTermNameToType <Add>("add");
            nameMappings.MapTermNameToType <Sub>("sub");
            nameMappings.MapTermNameToType <Mul>("mul");
            nameMappings.MapTermNameToType <Div>("div");

            string      INPUT_DESERIALIZE = "root: div(mul(4,sub(add(3,2),1)),5);";
            IExpression expr = serializer.Deserialize <IExpression>(INPUT_DESERIALIZE);

            Console.WriteLine($"Result = {expr.Calculate()}");
        }
        public void ShouldTestTypeAssignability()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();

            nameAndTypeMappings.MapTermNameToType <FileInfo>("term1");

            // Act & Assert
            Assert.Throws <Exception>(() =>
            {
                _ = nameAndTypeMappings.GetTypeForTermName("term1", typeof(DateTime));
            });
        }
        public void ShouldCallDefaultConstructorZeroArgsNoFieldMappingsGiven()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();
            var serializer          = new StringSerializer(nameAndTypeMappings: nameAndTypeMappings);
            var input = "root: person();";

            // Act
            nameAndTypeMappings.MapTermNameToType <Person>("person");
            var output = serializer.Deserialize <Person>(input);

            // Assert
            Assert.NotNull(output);
        }
        public void ShouldSubstituteMappedTypeOnDeserialize()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();
            var serializer          = new StringSerializer(nameAndTypeMappings: nameAndTypeMappings);

            // Act
            var input = "root: Person<ContactInfo>(ContactInfo<Name>(\"Sarel\"));";

            nameAndTypeMappings.MapTermNameToType <PhoneInfo>("ContactInfo");
            var output = serializer.Deserialize <Person>(input);

            // Assert
            Assert.IsType <PhoneInfo>(output.ContactInfo);
            Assert.True(StringComparer.InvariantCulture.Equals(output.ContactInfo.Name, "Sarel"));
        }
        public void ShouldUseLongestDeconstructorToCreateNonAcTermForExtensionMethod()
        {
            // Arrange
            var mappings   = new NameAndTypeMappings();
            var serializer = new StringSerializer(nameAndTypeMappings: mappings);

            mappings.MapExtensionMethodDestructorsFromType(typeof(DateTimeExtensions));
            mappings.MapTermNameToType <DateTime>("datetime");
            var input = new DateTime(2020, 10, 13);

            // Act
            string deserialized = serializer.Serialize <DateTime>(input);

            // Assert
            Assert.Equal("root: datetime(2020,10,13);", deserialized);
        }
        public void ShouldCallNonDefaultConstructorNoFieldMappingsGiven()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();
            var serializer          = new StringSerializer(nameAndTypeMappings: nameAndTypeMappings);
            var input = "root: datetime(2020,10,10);";

            // Act
            nameAndTypeMappings.MapTermNameToType <DateTime>("datetime");
            var output = serializer.Deserialize <DateTime>(input);

            // Assert
            Assert.Equal(2020, output.Year);
            Assert.Equal(10, output.Month);
            Assert.Equal(10, output.Day);
        }
        public void ShouldSubstituteMappedTypeOnSerialize()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();
            var serializer          = new StringSerializer(nameAndTypeMappings: nameAndTypeMappings);

            // Act
            var input = new PhoneInfo
            {
                Name = "Sarel"
            };

            nameAndTypeMappings.MapTermNameToType <PhoneInfo>("ContactInfoRenamed");
            var output = serializer.Serialize(input);

            // Assert
            Assert.True(StringComparer.InvariantCulture.Equals("root: ContactInfoRenamed(null,\"Sarel\");", output));
        }
        public void ShouldUseLongestDeconstructorToCreateNonAcTermForNonExtensionMethod()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();
            var serializer          = new StringSerializer(nameAndTypeMappings: nameAndTypeMappings);
            var input = new PhoneInfo
            {
                Name        = "Test T",
                PhoneNumber = "1234567890"
            };

            // Act
            nameAndTypeMappings.MapTermNameToType <PhoneInfo>("phone");
            var output = serializer.Serialize(input);

            // Assert
            Assert.Equal("root: phone(\"1234567890\",\"Test T\");", output);
        }
Beispiel #11
0
        private static void ConstructorsAndDeconstructors()
        {
            var nameMappings = new NameAndTypeMappings();
            var serializer   = new StringSerializer(nameAndTypeMappings: nameMappings);

            nameMappings.MapExtensionMethodDestructorsFromType(typeof(DateTimeExtensions));
            nameMappings.MapTermNameToType <DateTime>("datetime");

            Console.WriteLine("Build terms with constructors ...");

            string INPUT_SERIALIZE = "root: datetime(2020,10,13);";
            var    output          = serializer.Deserialize <DateTime>(INPUT_SERIALIZE);

            Console.WriteLine($"Deserialized datetime = {output}");

            Console.WriteLine("Build terms with deconstructors ...");

            DateTime INPUT_DESERIALIZE = new DateTime(2020, 7, 8);
            var      outputSerialized  = serializer.Serialize(INPUT_DESERIALIZE);

            Console.WriteLine($"Deserialized datetime = {outputSerialized}");
        }