예제 #1
0
        public void ParseName()
        {
            Parser parser = new Parser("foo");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(VariableExpression));
            Assert.AreEqual("foo", ((VariableExpression)result).Name);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #2
0
        public void ParseGlobalVariableAssignment()
        {
            Parser parser = new Parser("$foo = 42");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(GlobalVariableAssignmentExpression));
            Assert.AreEqual("foo", ((GlobalVariableAssignmentExpression)result).Name);

            var expr = ((GlobalVariableAssignmentExpression)result).Expression;

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(ConstantExpression));
            Assert.AreEqual(42, ((ConstantExpression)expr).Value);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #3
0
        public void ParseAddIntegersUsingParenthesis()
        {
            Parser parser = new Parser("(1+2)");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(AddExpression));

            var addexpr = (AddExpression)result;

            var lexpr = addexpr.LeftExpression;
            var rexpr = addexpr.RightExpression;

            Assert.IsNotNull(lexpr);
            Assert.IsInstanceOfType(lexpr, typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)lexpr).Value);

            Assert.IsNotNull(rexpr);
            Assert.IsInstanceOfType(rexpr, typeof(ConstantExpression));
            Assert.AreEqual(2, ((ConstantExpression)rexpr).Value);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #4
0
        public void ParseNil()
        {
            Parser parser = new Parser("nil");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ConstantExpression));
            Assert.IsNull(((ConstantExpression)result).Value);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #5
0
        public void ParseFalse()
        {
            Parser parser = new Parser("false");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ConstantExpression));
            Assert.AreEqual(false, ((ConstantExpression)result).Value);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #6
0
        public void ParseNullString()
        {
            Parser parser = new Parser(null);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #7
0
        public void ParseLogicalNotInteger()
        {
            Parser parser = new Parser("!false");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(LogicalNotExpression));

            var notexpr = (LogicalNotExpression)result;

            var expr = notexpr.Expression;

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(ConstantExpression));
            Assert.AreEqual(false, ((ConstantExpression)expr).Value);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #8
0
        public void ParseDotDotExpression()
        {
            Parser parser = new Parser("foo.bar.zoo");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DotExpression));

            var dotexpr = (DotExpression)result;

            Assert.IsNotNull(dotexpr.Expression);
            Assert.IsInstanceOfType(dotexpr.Expression, typeof(DotExpression));

            var dexpr = (DotExpression)dotexpr.Expression;

            Assert.IsNotNull(dexpr.Expression);
            Assert.IsInstanceOfType(dexpr.Expression, typeof(VariableExpression));

            var vexpr = (VariableExpression)dexpr.Expression;

            Assert.AreEqual("foo", vexpr.Name);
            Assert.AreEqual("bar", dexpr.Name);
            Assert.AreEqual("zoo", dotexpr.Name);
        }
예제 #9
0
        public void ParseNamedTuple()
        {
            Parser parser = new Parser("{ name: \"Crystal\", year: 2011 }");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(NamedTupleExpression));

            var ntupleexpr = (NamedTupleExpression)result;

            Assert.IsNotNull(ntupleexpr.Names);
            Assert.AreEqual(2, ntupleexpr.Names.Count);

            Assert.IsNotNull(ntupleexpr.Expressions);
            Assert.AreEqual(2, ntupleexpr.Expressions.Count);

            Assert.IsInstanceOfType(ntupleexpr.Expressions[0], typeof(ConstantExpression));
            Assert.AreEqual("Crystal", ((ConstantExpression)ntupleexpr.Expressions[0]).Value);

            Assert.IsInstanceOfType(ntupleexpr.Expressions[1], typeof(ConstantExpression));
            Assert.AreEqual(2011, ((ConstantExpression)ntupleexpr.Expressions[1]).Value);
        }
예제 #10
0
        public void ParseDotExpressionWithArgumentBtwParentheses()
        {
            Parser parser = new Parser("foo.bar(42)");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DotExpression));

            var dotexpr = (DotExpression)result;

            Assert.IsNotNull(dotexpr.Expression);
            Assert.IsInstanceOfType(dotexpr.Expression, typeof(VariableExpression));

            var vexpr = (VariableExpression)dotexpr.Expression;

            Assert.AreEqual("foo", vexpr.Name);

            Assert.AreEqual("bar", dotexpr.Name);

            Assert.IsNotNull(dotexpr.Arguments);
            Assert.AreEqual(1, dotexpr.Arguments.Count);
            Assert.IsNotNull(dotexpr.Arguments[0]);
            Assert.IsInstanceOfType(dotexpr.Arguments[0], typeof(ConstantExpression));
            Assert.AreEqual(42, ((ConstantExpression)dotexpr.Arguments[0]).Value);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #11
0
        public void ParseArray()
        {
            Parser parser = new Parser("[ 1, 2, 3 ]");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ArrayExpression));

            var arrexpr = (ArrayExpression)result;

            Assert.IsNotNull(arrexpr.Expressions);
            Assert.AreEqual(3, arrexpr.Expressions.Count);

            Assert.IsInstanceOfType(arrexpr.Expressions[0], typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)arrexpr.Expressions[0]).Value);

            Assert.IsInstanceOfType(arrexpr.Expressions[1], typeof(ConstantExpression));
            Assert.AreEqual(2, ((ConstantExpression)arrexpr.Expressions[1]).Value);

            Assert.IsInstanceOfType(arrexpr.Expressions[2], typeof(ConstantExpression));
            Assert.AreEqual(3, ((ConstantExpression)arrexpr.Expressions[2]).Value);
        }
예제 #12
0
        public void ParseTuple()
        {
            Parser parser = new Parser("{ 1, 2, 3 }");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(TupleExpression));

            var tuplexpr = (TupleExpression)result;

            Assert.IsNotNull(tuplexpr.Expressions);
            Assert.AreEqual(3, tuplexpr.Expressions.Count);

            Assert.IsInstanceOfType(tuplexpr.Expressions[0], typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)tuplexpr.Expressions[0]).Value);

            Assert.IsInstanceOfType(tuplexpr.Expressions[1], typeof(ConstantExpression));
            Assert.AreEqual(2, ((ConstantExpression)tuplexpr.Expressions[1]).Value);

            Assert.IsInstanceOfType(tuplexpr.Expressions[2], typeof(ConstantExpression));
            Assert.AreEqual(3, ((ConstantExpression)tuplexpr.Expressions[2]).Value);
        }
예제 #13
0
        public void ParseLogicalOrBooleans()
        {
            Parser parser = new Parser("true||false");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(LogicalOrExpression));

            var subexpr = (LogicalOrExpression)result;

            var lexpr = subexpr.LeftExpression;
            var rexpr = subexpr.RightExpression;

            Assert.IsNotNull(lexpr);
            Assert.IsInstanceOfType(lexpr, typeof(ConstantExpression));
            Assert.AreEqual(true, ((ConstantExpression)lexpr).Value);

            Assert.IsNotNull(rexpr);
            Assert.IsInstanceOfType(rexpr, typeof(ConstantExpression));
            Assert.AreEqual(false, ((ConstantExpression)rexpr).Value);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #14
0
        public void ParseMultiplyAddThreeIntegersUsingParenthesis()
        {
            Parser parser = new Parser("1*(2+3)");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(MultiplyExpression));

            var multexpr = (MultiplyExpression)result;

            var lexpr = multexpr.LeftExpression;
            var rexpr = multexpr.RightExpression;

            Assert.IsNotNull(lexpr);
            Assert.IsInstanceOfType(lexpr, typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)lexpr).Value);

            Assert.IsNotNull(rexpr);
            Assert.IsInstanceOfType(rexpr, typeof(AddExpression));

            var addexpr = (AddExpression)rexpr;

            lexpr = addexpr.LeftExpression;
            rexpr = addexpr.RightExpression;

            Assert.AreEqual(2, ((ConstantExpression)lexpr).Value);

            Assert.IsNotNull(rexpr);
            Assert.IsInstanceOfType(rexpr, typeof(ConstantExpression));
            Assert.AreEqual(3, ((ConstantExpression)rexpr).Value);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #15
0
        public void ParseEmptyString()
        {
            Parser parser = new Parser(string.Empty);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #16
0
        public void ParseDivideAddThreeIntegers()
        {
            Parser parser = new Parser("1/2+3");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(AddExpression));

            var addexpr = (AddExpression)result;

            var lexpr = addexpr.LeftExpression;
            var rexpr = addexpr.RightExpression;

            Assert.IsNotNull(rexpr);
            Assert.IsInstanceOfType(rexpr, typeof(ConstantExpression));
            Assert.AreEqual(3, ((ConstantExpression)rexpr).Value);

            Assert.IsNotNull(lexpr);
            Assert.IsInstanceOfType(lexpr, typeof(DivideExpression));

            var divexpr = (DivideExpression)lexpr;

            lexpr = divexpr.LeftExpression;
            rexpr = divexpr.RightExpression;

            Assert.AreEqual(1, ((ConstantExpression)lexpr).Value);

            Assert.IsNotNull(rexpr);
            Assert.IsInstanceOfType(rexpr, typeof(ConstantExpression));
            Assert.AreEqual(2, ((ConstantExpression)rexpr).Value);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #17
0
        public void ParseString()
        {
            Parser parser = new Parser("\"foo\"");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ConstantExpression));
            Assert.AreEqual("foo", ((ConstantExpression)result).Value);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #18
0
        public void ParseLessEqualsIntegers()
        {
            Parser parser = new Parser("1<=2");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(LessEqualsExpression));

            var subexpr = (LessEqualsExpression)result;

            var lexpr = subexpr.LeftExpression;
            var rexpr = subexpr.RightExpression;

            Assert.IsNotNull(lexpr);
            Assert.IsInstanceOfType(lexpr, typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)lexpr).Value);

            Assert.IsNotNull(rexpr);
            Assert.IsInstanceOfType(rexpr, typeof(ConstantExpression));
            Assert.AreEqual(2, ((ConstantExpression)rexpr).Value);

            Assert.IsNull(parser.ParseExpression());
        }
예제 #19
0
        public void ParseUnaryPlusInteger()
        {
            Parser parser = new Parser("+3");

            var result = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(UnaryPlusExpression));

            var plusexpr = (UnaryPlusExpression)result;

            var expr = plusexpr.Expression;

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(ConstantExpression));
            Assert.AreEqual(3, ((ConstantExpression)expr).Value);

            Assert.IsNull(parser.ParseExpression());
        }