Ejemplo n.º 1
0
 public Token(Type type, String value, StreamLocation location)
     : this()
 {
     TokenType = type;
     Value = value;
     Location = location;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Copy constructor
 /// </summary>
 /// <param name="that">Source instance</param>
 public StreamLocation(StreamLocation that)
     : this()
 {
     Offset = that.Offset;
     Line = that.Line;
     Column = that.Column;
 }
Ejemplo n.º 3
0
        public void AdvanceTest_FromStart()
        {
            StreamLocation expected = new StreamLocation(1, 1, 2);

            StreamLocationTracker sut = new StreamLocationTracker();
            sut.Advance();

            Assert.AreEqual(expected, sut.Location);
        }
Ejemplo n.º 4
0
        public void AdvanceTest_WithNewLineTrue()
        {
            StreamLocation startState = new StreamLocation(7, 5, 3);
            StreamLocation expected = new StreamLocation(8, 6, 1);

            StreamLocationTracker sut = new StreamLocationTracker(startState);
            sut.Advance(true);

            Assert.AreEqual(expected, sut.Location);
        }
Ejemplo n.º 5
0
        public void AdvanceTest_FromArbitraryState()
        {
            StreamLocation startState = new StreamLocation(7, 5, 3);
            StreamLocation expected = new StreamLocation(8, 5, 4);

            StreamLocationTracker sut = new StreamLocationTracker(startState);
            sut.Advance();

            Assert.AreEqual(expected, sut.Location);
        }
Ejemplo n.º 6
0
        public void CharacterStreamConstructorTest_StringReader()
        {
            StringReader testInput = new StringReader("Test");
            StreamLocation expectedLocation = new StreamLocation(0, 1, 1);

            CharacterScanner sut = new CharacterScanner(testInput);

            Assert.AreEqual(false, sut.IsEOF(), "Should not be at EOF");
            Assert.AreEqual(true, sut.HasNext(), "Should have next");
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.AreEqual('T', sut.Peek());
        }
Ejemplo n.º 7
0
        public void ConjunctionNodeTest_OR()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            Expression expectedLeft = Expression.Constant(false);
            Expression expectedRight = Expression.Constant(true);

            ConjunctionNode sut = new ConjunctionNode(ConjunctionNode.Conjunction.OR, new TestNode(expectedLeft), new TestNode(expectedRight), expectedLocation);

            Assert.AreEqual(TNode.CONJUNCTION, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("OR"));
            Assert.IsInstanceOfType(sut.SubExpression, typeof(BinaryExpression));
            Assert.AreEqual(ExpressionType.Or, sut.SubExpression.NodeType);
            Assert.AreEqual(typeof(bool), sut.SubExpression.Type);
            Assert.AreEqual(expectedLeft, ((BinaryExpression)sut.SubExpression).Left);
            Assert.AreEqual(expectedRight, ((BinaryExpression)sut.SubExpression).Right);
        }
Ejemplo n.º 8
0
        public void DateNodeTest()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            DateTime testDate = new DateTime(2013, 10, 3);

            DateNode sut = new DateNode(testDate.Year.ToString(), testDate.Month.ToString(), testDate.Day.ToString(), expectedLocation);

            Assert.AreEqual(TNode.DATE, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("DATE"));
            Assert.AreEqual(FieldValueType.DATE, sut.ValueType);

            Assert.AreEqual(ExpressionType.Constant, sut.SubExpression.NodeType);
            Assert.IsInstanceOfType(sut.SubExpression, typeof(ConstantExpression));
            Assert.AreEqual(typeof(DateTime?), ((ConstantExpression)sut.SubExpression).Type);
            Assert.AreEqual(testDate, ((ConstantExpression)sut.SubExpression).Value);
        }
Ejemplo n.º 9
0
        public void IdentifierNodeTest()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            Field<string> retriever = new IntegerField<string>(s => int.Parse(s));
            ParameterExpression arg = Expression.Parameter(typeof(string), "arg");

            ValueNode sut = new IdentifierNode<string>("identifier", retriever, arg, expectedLocation);

            Assert.AreEqual(TNode.IDENTIFIER, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("IDENTIFIER"));
            Assert.AreEqual(FieldValueType.INTEGER, sut.ValueType);
            Assert.IsInstanceOfType(sut.SubExpression, typeof(UnaryExpression));
            Assert.AreEqual(ExpressionType.Convert, sut.SubExpression.NodeType);
            Assert.AreEqual(typeof(long?), sut.SubExpression.Type);
            {
                Expression invokeExpr = ((UnaryExpression)sut.SubExpression).Operand;
                Assert.IsInstanceOfType(invokeExpr, typeof(InvocationExpression));
                Assert.AreEqual(ExpressionType.Invoke, invokeExpr.NodeType);
                Assert.AreEqual(arg, ((InvocationExpression)invokeExpr).Arguments[0]);
            }
        }
Ejemplo n.º 10
0
 public Token(Type type, StreamLocation location)
     : this(type, null, location)
 {
 }
Ejemplo n.º 11
0
        public void RangeNodeTest_CoercedTypes()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            SimpleValueNode testLowBound = new NumberNode("1", new StreamLocation());
            SimpleValueNode testUpperBound = new NumberNode("10.", new StreamLocation());
            bool testInclusiveLeft = false;
            bool testInclusiveRight = true;

            RangeNode sut = new RangeNode(testLowBound, testInclusiveLeft, testUpperBound, testInclusiveRight, expectedLocation);

            Assert.AreEqual(TNode.RANGE, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("RANGE"));
            Assert.AreEqual(FieldValueType.FLOAT, sut.ValueType);
            Assert.IsNull(sut.SubExpression);
            Assert.AreEqual(testLowBound, sut.GetLeft());
            Assert.AreEqual(testInclusiveLeft, sut.IsInclusiveLeft());
            Assert.AreEqual(testUpperBound, sut.GetRight());
            Assert.AreEqual(testInclusiveRight, sut.IsInclusiveRight());
        }
Ejemplo n.º 12
0
        public void CharacterStreamIsEOFHasNextTest_NotEOF()
        {
            string testInput = "a";
            bool expectEOF = false;
            StreamLocation expectedLocation = new StreamLocation(0, 1, 1);

            CharacterScanner sut = new CharacterScanner(testInput);
            sut.Start();

            Assert.AreEqual(expectEOF, sut.IsEOF(), "EOF unexpected");
            Assert.AreEqual(!expectEOF, sut.HasNext(), "HasNext expected");
            Assert.AreEqual(expectedLocation, sut.Location);
        }
Ejemplo n.º 13
0
        public void NewLineTest_FromStart()
        {
            StreamLocation expected = new StreamLocation(1, 2, 1);

            StreamLocationTracker sut = new StreamLocationTracker();
            sut.NewLine();

            Assert.AreEqual(expected, sut.Location);
        }
Ejemplo n.º 14
0
        public void RelationNodeTest_TypeCoercion()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            Expression operandA = Expression.Constant(43L);
            Expression operandB = Expression.Constant(3.4D);

            RelationNode sut = new RelationNode(RelationNode.Operator.EQ,
                                                new SimpleValueTestNode(operandA, FieldValueType.INTEGER),
                                                new SimpleValueTestNode(operandB, FieldValueType.FLOAT),
                                                expectedLocation);

            Assert.AreEqual(TNode.RELATION, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("RELATION"));
            Assert.IsInstanceOfType(sut.SubExpression, typeof(BinaryExpression));
            Assert.AreEqual(ExpressionType.Equal, sut.SubExpression.NodeType);
            Assert.AreEqual(typeof(bool), sut.SubExpression.Type);
            Assert.IsInstanceOfType(((BinaryExpression)sut.SubExpression).Left, typeof(UnaryExpression));
            UnaryExpression cast = (UnaryExpression)((BinaryExpression)sut.SubExpression).Left;
            Assert.AreEqual(ExpressionType.Convert, cast.NodeType);
            Assert.AreEqual(operandA, cast.Operand);
            Assert.AreEqual(operandB, ((BinaryExpression)sut.SubExpression).Right);
        }
Ejemplo n.º 15
0
        public void RelationNodeTest_NE()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            Expression operandA = Expression.Constant(43L);
            Expression operandB = Expression.Constant(34L);

            RelationNode sut = new RelationNode(RelationNode.Operator.NE,
                                                new SimpleValueTestNode(operandA, FieldValueType.INTEGER),
                                                new SimpleValueTestNode(operandB, FieldValueType.INTEGER),
                                                expectedLocation);

            Assert.AreEqual(TNode.RELATION, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("RELATION"));
            Assert.IsInstanceOfType(sut.SubExpression, typeof(BinaryExpression));
            Assert.AreEqual(ExpressionType.NotEqual, sut.SubExpression.NodeType);
            Assert.AreEqual(typeof(bool), sut.SubExpression.Type);
            Assert.AreEqual(operandA, ((BinaryExpression)sut.SubExpression).Left);
            Assert.AreEqual(operandB, ((BinaryExpression)sut.SubExpression).Right);
        }
Ejemplo n.º 16
0
        public void RelationNodeTest_IN_Set()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            Expression operandA = Expression.Constant(43L, typeof(long?));
            List<ConstantExpression> testExprs = new List<ConstantExpression>() { Expression.Constant(34L, typeof(long?)),
                                                                                  Expression.Constant(64L, typeof(long?)) };
            ISet<SimpleValueNode> testSet = new HashSet<SimpleValueNode>();
            foreach (ConstantExpression expr in testExprs)
            {
                testSet.Add(new SimpleValueTestNode(expr, FieldValueType.INTEGER));
            }

            RelationNode sut = new RelationNode(RelationNode.Operator.IN,
                                                new SimpleValueTestNode(operandA, FieldValueType.INTEGER),
                                                new SetNode(testSet, new StreamLocation()),
                                                expectedLocation);

            Assert.AreEqual(TNode.RELATION, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("RELATION"));
            Assert.IsInstanceOfType(sut.SubExpression, typeof(BinaryExpression));
            Assert.AreEqual(ExpressionType.Or, sut.SubExpression.NodeType);
            Assert.AreEqual(typeof(bool), sut.SubExpression.Type);
            List<ConstantExpression> resultExprs = new List<ConstantExpression>();
            {
                Expression left = ((BinaryExpression)sut.SubExpression).Left;
                Assert.IsInstanceOfType(left, typeof(BinaryExpression));
                Assert.AreEqual(ExpressionType.Or, sut.SubExpression.NodeType);
                Assert.AreEqual(typeof(bool), sut.SubExpression.Type);
                {
                    Expression leftLeft = ((BinaryExpression)left).Left;
                    Assert.IsInstanceOfType(leftLeft, typeof(ConstantExpression));
                    Assert.AreEqual(ExpressionType.Constant, leftLeft.NodeType);
                    Assert.AreEqual(typeof(bool), leftLeft.Type);
                    Assert.AreEqual(false, ((ConstantExpression)leftLeft).Value);
                }
                {
                    Expression leftRight = ((BinaryExpression)left).Right;
                    Assert.IsInstanceOfType(leftRight, typeof(BinaryExpression));
                    Assert.AreEqual(ExpressionType.Equal, leftRight.NodeType);
                    Assert.AreEqual(operandA, ((BinaryExpression)leftRight).Left);
                    resultExprs.Add((ConstantExpression)((BinaryExpression)leftRight).Right);
                }
            }
            {
                Expression right = ((BinaryExpression)sut.SubExpression).Right;
                Assert.IsInstanceOfType(right, typeof(BinaryExpression));
                Assert.AreEqual(ExpressionType.Equal, right.NodeType);
                Assert.AreEqual(operandA, ((BinaryExpression)right).Left);
                resultExprs.Add((ConstantExpression)((BinaryExpression)right).Right);
            }
            CollectionAssert.AreEquivalent(testExprs, resultExprs);
        }
Ejemplo n.º 17
0
        public void RelationNodeTest_IN_Range_Inclusive()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            Expression operandA = Expression.Constant(43L, typeof(long?));
            SimpleValueNode rangeLow = new NumberNode("34", new StreamLocation());
            SimpleValueNode rangeHigh = new NumberNode("68", new StreamLocation());

            RelationNode sut = new RelationNode(RelationNode.Operator.IN,
                                                new SimpleValueTestNode(operandA, FieldValueType.INTEGER),
                                                new RangeNode(rangeLow, true, rangeHigh, true, new StreamLocation()),
                                                expectedLocation);

            Assert.AreEqual(TNode.RELATION, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("RELATION"));
            Assert.IsInstanceOfType(sut.SubExpression, typeof(BinaryExpression));
            Assert.AreEqual(ExpressionType.And, sut.SubExpression.NodeType);
            Assert.AreEqual(typeof(bool), sut.SubExpression.Type);
            {
                Expression left = ((BinaryExpression)sut.SubExpression).Left;
                Assert.IsInstanceOfType(left, typeof(BinaryExpression));
                Assert.AreEqual(ExpressionType.GreaterThanOrEqual, left.NodeType);
                Assert.AreEqual(operandA, ((BinaryExpression)left).Left);
                Assert.AreEqual(rangeLow.SubExpression, ((BinaryExpression)left).Right);
            }
            {
                Expression right = ((BinaryExpression)sut.SubExpression).Right;
                Assert.IsInstanceOfType(right, typeof(BinaryExpression));
                Assert.AreEqual(ExpressionType.LessThanOrEqual, right.NodeType);
                Assert.AreEqual(operandA, ((BinaryExpression)right).Left);
                Assert.AreEqual(rangeHigh.SubExpression, ((BinaryExpression)right).Right);
            }
        }
Ejemplo n.º 18
0
        public void RangeNodeTest_IncompatibleTypes()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            SimpleValueNode testLowBound = new NumberNode("1", new StreamLocation());
            SimpleValueNode testUpperBound = new StringNode("10", new StreamLocation());
            bool testInclusiveLeft = true;
            bool testInclusiveRight = true;

            RangeNode sut = new RangeNode(testLowBound, testInclusiveLeft, testUpperBound, testInclusiveRight, expectedLocation);
        }
Ejemplo n.º 19
0
        public void RelationNodeTest_TypeMismatch()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            Expression operandA = Expression.Constant(43L);
            Expression operandB = Expression.Constant("34");

            RelationNode sut = new RelationNode(RelationNode.Operator.EQ,
                                                new SimpleValueTestNode(operandA, FieldValueType.INTEGER),
                                                new SimpleValueTestNode(operandB, FieldValueType.TEXT),
                                                expectedLocation);
        }
Ejemplo n.º 20
0
        public void RelationNodeTest_IN_EmptySet()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            Expression operandA = Expression.Constant(43L, typeof(long?));
            List<ConstantExpression> testExprs = new List<ConstantExpression>();
            ISet<SimpleValueNode> testSet = new HashSet<SimpleValueNode>();
            foreach (ConstantExpression expr in testExprs)
            {
                testSet.Add(new SimpleValueTestNode(expr, FieldValueType.INTEGER));
            }

            RelationNode sut = new RelationNode(RelationNode.Operator.IN,
                                                new SimpleValueTestNode(operandA, FieldValueType.INTEGER),
                                                new SetNode(testSet, new StreamLocation()),
                                                expectedLocation);

            Assert.AreEqual(TNode.RELATION, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("RELATION"));
            Assert.IsInstanceOfType(sut.SubExpression, typeof(ConstantExpression));
            Assert.AreEqual(ExpressionType.Constant, sut.SubExpression.NodeType);
            Assert.AreEqual(typeof(bool), sut.SubExpression.Type);
            Assert.AreEqual(false, ((ConstantExpression)sut.SubExpression).Value);
        }
Ejemplo n.º 21
0
        public void StreamLocationTrackerConstructorTest()
        {
            StreamLocation expected = new StreamLocation(0, 1, 1);

            StreamLocationTracker sut = new StreamLocationTracker();

            Assert.AreEqual(expected, sut.Location);
        }
Ejemplo n.º 22
0
        public void SetNodeTest_Empty()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            ISet<SimpleValueNode> testSet = new HashSet<SimpleValueNode>();

            ValueNode sut = new SetNode(testSet, expectedLocation);

            Assert.AreEqual(TNode.SET, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("SET"));
            Assert.AreEqual(FieldValueType.NONE, sut.ValueType);
            Assert.IsNull(sut.SubExpression);
            Assert.AreEqual(testSet, ((SetNode)sut).GetSet());
        }
Ejemplo n.º 23
0
        public void SetNodeTest_MixedCompatibleTypes()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            ISet<SimpleValueNode> testSet = new HashSet<SimpleValueNode>()
                {
                    new NumberNode("34", new StreamLocation()),
                    new NumberNode("3.4", new StreamLocation()),
                    new NumberNode("34e-2", new StreamLocation()),
                };

            ValueNode sut = new SetNode(testSet, expectedLocation);

            Assert.AreEqual(TNode.SET, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("SET"));
            Assert.AreEqual(FieldValueType.FLOAT, sut.ValueType);
            Assert.IsNull(sut.SubExpression);
            Assert.AreEqual(testSet, ((SetNode)sut).GetSet());
        }
Ejemplo n.º 24
0
        public void SetNodeTest_MixedIncompatibleTypes()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            ISet<SimpleValueNode> testSet = new HashSet<SimpleValueNode>()
                {
                    new NumberNode("34", new StreamLocation()),
                    new StringNode("3.4", new StreamLocation()),
                    new NumberNode("34e-2", new StreamLocation()),
                };

            ValueNode sut = new SetNode(testSet, expectedLocation);
        }
Ejemplo n.º 25
0
        public void CharacterStreamIsEOFHasNextTest_AtEOF()
        {
            string testInput = "a";
            bool expectEOF = true;
            StreamLocation expectedLocation = new StreamLocation(1, 1, 2);

            CharacterScanner sut = new CharacterScanner(testInput);
            sut.Start();
            sut.Consume();

            Assert.AreEqual(expectEOF, sut.IsEOF(), "EOF expected");
            Assert.AreEqual(!expectEOF, sut.HasNext(), "HasNext unexpected");
            Assert.AreEqual(expectedLocation, sut.Location);
        }
Ejemplo n.º 26
0
        public void StringNodeTest()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            string testString = "hello world";

            ValueNode sut = new StringNode(testString, expectedLocation);

            Assert.AreEqual(TNode.STRING, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("STRING"));
            Assert.AreEqual(FieldValueType.TEXT, sut.ValueType);
            Assert.IsInstanceOfType(sut.SubExpression, typeof(ConstantExpression));
            Assert.AreEqual(ExpressionType.Constant, sut.SubExpression.NodeType);
            Assert.AreEqual(typeof(string), sut.SubExpression.Type);
            Assert.AreEqual(testString, ((ConstantExpression)sut.SubExpression).Value);
        }
Ejemplo n.º 27
0
        public void CharacterStreamStartTest()
        {
            StreamLocation expectedLocation = new StreamLocation(0, 1, 1);
            CharacterScanner sut = new CharacterScanner("Test");

            sut.Start();

            Assert.IsTrue(IsStarted(sut), "Should not be able to set new location if started");
            Assert.AreEqual(expectedLocation, sut.Location);
        }
Ejemplo n.º 28
0
        public void TrueNodeTest()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);

            TrueNode sut = new TrueNode(expectedLocation);

            Assert.AreEqual(TNode.TRUE, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("TRUE"));

            Assert.AreEqual(ExpressionType.Constant, sut.SubExpression.NodeType);
            Assert.IsInstanceOfType(sut.SubExpression, typeof(ConstantExpression));
            Assert.AreEqual(typeof(bool), ((ConstantExpression)sut.SubExpression).Type);
            Assert.AreEqual(true, ((ConstantExpression)sut.SubExpression).Value);
        }
Ejemplo n.º 29
0
 public ParseException(string msg, StreamLocation loc, Exception e)
     : base(String.Format("Syntax Error: {0} @ {1}", msg, loc), e)
 {
 }
Ejemplo n.º 30
0
        public void NumberNodeTest_Integer()
        {
            StreamLocation expectedLocation = new StreamLocation(3, 2, 1);
            string testNumber = "34";
            long? expectedValue = (long?)long.Parse(testNumber);

            ValueNode sut = new NumberNode(testNumber, expectedLocation);

            Assert.AreEqual(TNode.NUMBER, sut.NodeType);
            Assert.AreEqual(expectedLocation, sut.Location);
            Assert.IsTrue(sut.Description.Contains("NUMBER"));
            Assert.AreEqual(FieldValueType.INTEGER, sut.ValueType);
            Assert.IsInstanceOfType(sut.SubExpression, typeof(ConstantExpression));
            Assert.AreEqual(ExpressionType.Constant, sut.SubExpression.NodeType);
            Assert.AreEqual(typeof(long?), sut.SubExpression.Type);
            Assert.AreEqual(expectedValue, ((ConstantExpression)sut.SubExpression).Value);
        }