public void ShouldThrowExceptionWhenLoadExtensionMethodDestructorsFromTypeNotExtensionClass()
        {
            // Arrange
            NameAndTypeMappings mappings = new NameAndTypeMappings();

            // Act & Assert
            Assert.Throws <Exception>(() => mappings.MapExtensionMethodDestructorsFromType(typeof(DateTime)));
        }
        public void ShouldReturnNullIfConstantNotKnown()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();

            // Act
            var value = nameAndTypeMappings.GetIdentifierForConstantValue(123);

            // Assert
            Assert.Null(value);
        }
        public void ShouldNotMapSameConstantIdentifierMultipleTimes()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();

            // Act & Assert
            Assert.Throws <Exception>(() =>
            {
                nameAndTypeMappings.MapIdentifierNameToConstant("Pi", Math.PI);
                nameAndTypeMappings.MapIdentifierNameToConstant("Pi", Math.PI);
            });
        }
        public void ShouldGetBestDeconstructorFromExtensionMethods()
        {
            // Arrange
            NameAndTypeMappings mappings = new NameAndTypeMappings();

            // Act
            mappings.MapExtensionMethodDestructorsFromType(typeof(DateTimeExtensions));
            var deconstructor = mappings.GetBestDeconstructorMethodForAcTerm(typeof(DateTime));

            // Assert
            Assert.Equal(4, deconstructor.GetParameters().Length);
        }
Example #5
0
        private static void NamedConstants()
        {
            Console.WriteLine("Named constants ...");
            var nameMappings = new NameAndTypeMappings();
            var serializer   = new StringSerializer(nameAndTypeMappings: nameMappings);

            nameMappings.MapIdentifierNameToConstant("Pi", Math.PI);
            var output = serializer.Serialize(Math.PI);

            Console.WriteLine(output);
            Console.WriteLine();
        }
        public void ShouldSerializeConstant(object value, string identifier)
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();
            var serializer          = new StringSerializer(nameAndTypeMappings: nameAndTypeMappings);

            // Act
            nameAndTypeMappings.MapIdentifierNameToConstant(identifier, value);
            var output = serializer.Serialize(value);

            // Assert
            Assert.True(StringComparer.InvariantCulture.Equals($"root: {identifier};", output));
        }
        public void ShouldTestTypeAssignability()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();

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

            // Act & Assert
            Assert.Throws <Exception>(() =>
            {
                _ = nameAndTypeMappings.GetTypeForTermName("term1", typeof(DateTime));
            });
        }
        public void ShouldNotMapSameTermNameMoreThanOnce()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();

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

            // Act & Assert
            Assert.Throws <Exception>(() =>
            {
                nameAndTypeMappings.MapTermNameToType <DateTime>("term1");
            });
        }
        public void ShouldSupportDeconstructorWithZeroArgs()
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();
            var serializer          = new StringSerializer(nameAndTypeMappings: nameAndTypeMappings);
            var input = new RandomValue();

            // Act
            var output = serializer.Serialize(input);

            // Assert
            Assert.Equal("root: RandomValue();", output);
        }
        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 ShouldDeserializeConstant(object value, string identifier)
        {
            // Arrange
            var nameAndTypeMappings = new NameAndTypeMappings();
            var serializer          = new StringSerializer(nameAndTypeMappings: nameAndTypeMappings);
            var input = $"root: {identifier};";

            // Act
            nameAndTypeMappings.MapIdentifierNameToConstant(identifier, value);
            var output = serializer.Deserialize <object>(input);

            // Assert
            Assert.Equal(value, output);
        }
Example #12
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();
        }
        public void ShouldGenerateMultipleTerms()
        {
            var nameAndTypeMappings = new NameAndTypeMappings();
            var translator          = new ObjectToAstTranslator(nameAndTypeMappings);

            RunTest("val(count_to_3());", "val(1);val(2);val(3);", "count_to_3", SymbolType.NonAcTerm, (inputTerm, database) =>
            {
                return(new[]
                {
                    translator.BuildTermForObject(1, database),
                    translator.BuildTermForObject(2, database),
                    translator.BuildTermForObject(3, database)
                });
            });
        }
        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 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 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"));
        }
Example #17
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 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);
        }
        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));
        }
Example #20
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}");
        }
 public ObjectToAstTranslator(NameAndTypeMappings nameAndTypeMappings)
 {
     _nameAndTypeMappings = nameAndTypeMappings;
 }
 internal StringToObjectTranslator(NameAndTypeMappings nameAndTypeMappings)
 {
     _parser              = new TrlParser();
     _objectCache         = new Dictionary <(ulong, string), object>(EqualityComparer <ValueTuple <ulong, string> > .Default);
     _nameAndTypeMappings = nameAndTypeMappings;
 }