Esempio n. 1
0
        public void PrefixExpressionsParseTest()
        {
            var prefixTests = new PrefixTest[]
            {
                new() { Input = "!5", Operator = "!", IntegerValue = 5 },
                new() { Input = "-50", Operator = "-", IntegerValue = 50 }
            };

            foreach (var prefixTest in prefixTests)
            {
                var lexer   = new Lexer(prefixTest.Input);
                var parser  = new Parser(lexer);
                var program = parser.ParseCode();
                StaticTests.CheckParserErrors(parser);

                Assert.AreEqual(1, program.Statements.Count,
                                $"program should have 1 statement, got={program.Statements.Count}");

                var stmt = program.Statements.First() as ExpressionStatement;

                Assert.AreEqual("ExpressionStatement", stmt.GetType().Name,
                                $"first statement not of type 'ExpressionStatement', got={stmt.GetType().Name}");

                Assert.AreEqual("PrefixExpression", stmt.Expression.GetType().Name,
                                $"expression not of type 'PrefixExpression', got={stmt.Expression.GetType().Name}");

                var exp = stmt.Expression as PrefixExpression;

                Assert.AreEqual(prefixTest.Operator, exp.Operator,
                                $"operator is not {prefixTest.Operator}, got={exp.Operator}");

                StaticTests.TestIntegerLiteral(exp.Right, prefixTest.IntegerValue);
            }
        }
    }
Esempio n. 2
0
        public void ArrayLiteralParsingTest()
        {
            const string input = "[1, 2 * 2, 3 + 3]";

            var lexer   = new Lexer(input);
            var parser  = new Parser(lexer);
            var program = parser.ParseCode();

            StaticTests.CheckParserErrors(parser);

            Assert.AreEqual(nameof(ExpressionStatement), program.Statements.First().GetType().Name);

            var stmt = (ExpressionStatement)program.Statements.First();

            Assert.AreEqual(nameof(ArrayLiteral), stmt.Expression.GetType().Name);

            var array = (ArrayLiteral)stmt.Expression;

            Assert.AreEqual(3, array.Elements.Count);

            StaticTests.TestIntegerLiteral(array.Elements.First(), 1);
            IntegerLiteral two   = 2;
            IntegerLiteral three = 3;

            StaticTests.TestInfixExpression(array.Elements[1], two, "*", two);
            StaticTests.TestInfixExpression(array.Elements[2], three, "+", three);
        }
Esempio n. 3
0
        public void HashLiteralParsingStringKeysTest()
        {
            const string input   = "{'one': 1, 'two': 2, 'three': 3}";
            var          lexer   = new Lexer(input);
            var          parser  = new Parser(lexer);
            var          program = parser.ParseCode();

            StaticTests.CheckParserErrors(parser);

            Assert.AreEqual(nameof(ExpressionStatement), program.Statements.First().GetType().Name);

            var stmt = (ExpressionStatement)program.Statements.First();

            Assert.AreEqual(nameof(HashLiteral), stmt.Expression.GetType().Name);

            var hash = (HashLiteral)stmt.Expression;

            Assert.AreEqual(3, hash.Pairs.Count);

            var expected = new Dictionary <string, long>
            {
                { "one", 1 },
                { "two", 2 },
                { "three", 3 }
            };

            foreach (var pairsKey in hash.Pairs.Keys)
            {
                Assert.AreEqual(nameof(StringLiteral), pairsKey.GetType().Name);
                var literal = (StringLiteral)pairsKey;

                var expectedValue = expected[literal.Str()];

                StaticTests.TestIntegerLiteral(hash.Pairs[pairsKey], expectedValue);
            }
        }