Esempio n. 1
0
        public void ParsePrimitiveConstant()
        {
            Expression <Func <int> > f            = () => 5;
            ConstantNode             constantNode = NaturalExpressionParser.Parse(f.Body) as ConstantNode;

            Assert.AreEqual("5", constantNode.Text);
        }
Esempio n. 2
0
        public void ParseArrayCreateAndIndex()
        {
            Expression <Func <int> > f = () => new int[] { 1, 2, 3 }[1];
            var node = NaturalExpressionParser.Parse(f.Body);

            var expected = new ArrayIndexNode
            {
                Array = new NewArrayNode
                {
                    Type  = "int",
                    Items = new List <Node>
                    {
                        new ConstantNode {
                            Text = "1"
                        },
                        new ConstantNode {
                            Text = "2"
                        },
                        new ConstantNode {
                            Text = "3"
                        },
                    }
                },
                Index = new ConstantNode {
                    Text = "1"
                },
                Value = "2"
            };

            Assert.AreEqual(expected, node);
        }
Esempio n. 3
0
        public void ParseStringConstant()
        {
            Expression <Func <string> > f = () => "foo";
            ConstantNode constantNode     = NaturalExpressionParser.Parse(f.Body) as ConstantNode;

            Assert.AreEqual("\"foo\"", constantNode.Text);
        }
Esempio n. 4
0
        public void ParsePrimitiveStaticField()
        {
            Expression <Func <int> > f            = () => field;
            ConstantNode             constantNode = NaturalExpressionParser.Parse(f.Body) as ConstantNode;

            Assert.AreEqual("field", constantNode.Text);
            Assert.AreEqual("5", constantNode.Value);
        }
Esempio n. 5
0
        public static Exception CreateException(LambdaExpression expression, string message)
        {
            Node constantNode = NaturalExpressionParser.Parse(expression.Body);

            string[] lines = NodeFormatter.Format(constantNode);
            string   nl    = Environment.NewLine;

            return(new Exception(message + ", expression was:" + nl + nl + String.Join(nl, lines)));
        }
Esempio n. 6
0
        public void ParseMember()
        {
            int x = 5;
            Expression <Func <int> > f = () => x;
            Node constantNode          = NaturalExpressionParser.Parse(f.Body);

            Assert.AreEqual(new ConstantNode {
                Text = "x", Value = "5"
            }, constantNode);
        }
Esempio n. 7
0
 public void PrintResults()
 {
     int x = 11;
     int y = 6;
     DateTime d = new DateTime(2010, 3, 1);
     Expression<Func<bool>> expression = () => x + 5 == d.Month * y;
     Node constantNode = NaturalExpressionParser.Parse(expression.Body);
     string[] strings = NodeFormatter.Format(constantNode);
     string s = string.Join(Environment.NewLine, strings);
     Console.Out.WriteLine(s);
 }
Esempio n. 8
0
        public void ParseEnumerableWithNulls()
        {
            var v = new List <int?> {
                1, 2, null, 4
            };
            Expression <Func <List <int?> > > f = () => v;
            var node = NaturalExpressionParser.Parse(f.Body);

            var expected = new ConstantNode()
            {
                Text  = "v",
                Value = "{1, 2, null, 4}"
            };

            Assert.AreEqual(expected, node);
        }
Esempio n. 9
0
        public void ParseMethodWithException()
        {
            Expression <Func <int> > f = () => ThrowException();
            var node = NaturalExpressionParser.Parse(f.Body);

            var expected = new MethodCallNode
            {
                Container = new ConstantNode {
                    Text = "PowerAssertTests.ParserTest"
                },
                MemberName  = "ThrowException",
                MemberValue = @"DivideByZeroException: Attempted to divide by zero.",
            };

            Assert.AreEqual(expected, node);
        }
Esempio n. 10
0
        public void ParseMemberAccess()
        {
            DateTime d = new DateTime(2010, 12, 25);
            Expression <Func <int> > f    = () => d.Day;
            MemberAccessNode         node = (MemberAccessNode)NaturalExpressionParser.Parse(f.Body);

            MemberAccessNode expected = new MemberAccessNode
            {
                Container = new ConstantNode {
                    Text = "d", Value = d.ToString()
                },
                MemberName  = "Day",
                MemberValue = "25"
            };

            Assert.AreEqual(expected, node);
        }
Esempio n. 11
0
        public void ParseUnaryNegate()
        {
            var v = 5;
            Expression <Func <int> > f = () => - v;
            var node = NaturalExpressionParser.Parse(f.Body);

            var expected = new UnaryNode
            {
                Prefix      = "-",
                PrefixValue = "-5",
                Operand     = new ConstantNode()
                {
                    Text = "v", Value = "5"
                },
            };

            Assert.AreEqual(expected, node);
        }
Esempio n. 12
0
        public void ParseUnaryNot()
        {
            var v = true;
            Expression <Func <bool> > f = () => !v;
            var node = NaturalExpressionParser.Parse(f.Body);

            var expected = new UnaryNode
            {
                Prefix      = "!",
                PrefixValue = "False",
                Operand     = new ConstantNode()
                {
                    Text = "v", Value = "True"
                },
            };

            Assert.AreEqual(expected, node);
        }
Esempio n. 13
0
        public void ParseCast()
        {
            double x = 5.1;
            Expression <Func <int> > f = () => (int)x;
            var node = NaturalExpressionParser.Parse(f.Body);

            var expected = new UnaryNode
            {
                Prefix      = "(int)(",
                PrefixValue = "5",
                Operand     = new ConstantNode()
                {
                    Text = "x", Value = 5.1M.ToString()
                },
                Suffix = ")"
            };

            Assert.AreEqual(expected, node);
        }
Esempio n. 14
0
        public void ParseBinaryWithNullRightHandSide()
        {
            string rightHandSide        = null;
            Expression <Func <bool> > f = () => "foo" == rightHandSide;
            var node     = NaturalExpressionParser.Parse(f.Body);
            var expected = new BinaryNode
            {
                Left = new ConstantNode {
                    Text = "\"foo\""
                },
                Right = new ConstantNode {
                    Text = "rightHandSide", Value = "null"
                },
                Operator = "==",
                Value    = "False"
            };

            Assert.AreEqual(expected, node);
        }
Esempio n. 15
0
        public void ParseConditional()
        {
            bool b = false;
            Expression <Func <int> > f = () => b ? 1 : 2;
            var node = NaturalExpressionParser.Parse(f.Body);

            var expected = new ConditionalNode
            {
                Condition = new ConstantNode {
                    Text = "b", Value = "False"
                },
                TrueValue = new ConstantNode {
                    Text = "1"
                },
                FalseValue = new ConstantNode {
                    Text = "2"
                },
            };

            Assert.AreEqual(expected, node);
        }
Esempio n. 16
0
        public void ParseIsOperator()
        {
            object x = "xValue";
            Expression <Func <bool> > f = () => x is string;

            var node     = NaturalExpressionParser.Parse(f.Body);
            var expected = new BinaryNode()
            {
                Left = new ConstantNode()
                {
                    Text = "x", Value = "\"xValue\""
                },
                Operator = "is",
                Right    = new ConstantNode()
                {
                    Text = "string"
                },
                Value = "True"
            };

            Assert.AreEqual(expected, node);
        }
Esempio n. 17
0
        public void ParseMethodAccess()
        {
            string s = "hello";
            Expression <Func <string> > f = () => s.Substring(1);
            var node = NaturalExpressionParser.Parse(f.Body);

            var expected = new MethodCallNode
            {
                Container = new ConstantNode {
                    Text = "s", Value = @"""hello"""
                },
                MemberName  = "Substring",
                MemberValue = @"""ello""",
                Parameters  = new List <Node>()
                {
                    new ConstantNode {
                        Text = "1"
                    }
                }
            };

            Assert.AreEqual(expected, node);
        }
Esempio n. 18
0
        public static string CreateSimpleFormatFor(LambdaExpression expression)
        {
            Node constantNode = NaturalExpressionParser.Parse(expression.Body);

            return(NodeFormatter.SimpleFormat(constantNode));
        }