Exemplo n.º 1
0
        public void TestLiteral()
        {
            Literal v = new Literal();

            v.Raw = "5";
            ExecutionEnvironment e = new ExecutionEnvironment();
            var r = v.Execute(e);

            Assert.AreEqual(r.ReturnValue, 5);
            {
                Literal a = new Literal()
                {
                    Raw = "6.0"
                };
                var ra = a.Execute(e);
                Assert.AreEqual(ra.Type, CompletionType.Value);
                Assert.AreEqual(ra.ReturnValue, 6.0f);
            }
            {
                Literal a = new Literal()
                {
                    Raw = "\"6.0\""
                };
                var ra = a.Execute(e);
                Assert.AreEqual(ra.Type, CompletionType.Value);
                Assert.AreEqual(ra.ReturnValue, "6.0");
            }
        }
Exemplo n.º 2
0
        public void LiteralTests()
        {
            string            txt               = "1";
            AntlrInputStream  inputStream       = new AntlrInputStream(txt);
            BogieLangLexer    lexer             = new BogieLangLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            BogieLangParser   parser            = new BogieLangParser(commonTokenStream);

            BogieLangParser.LiteralContext literalContext = parser.literal();
            BogieLangBaseVisitor <object>  visitor        = new BogieLangBaseVisitor <object>();

            visitor.Visit(literalContext);
            Literal literal = Literal.Compile(literalContext);

            Assert.True(literal.Execute() is int);
            Assert.True((int)literal.Execute() == 1);


            txt         = "1.0";
            inputStream = new AntlrInputStream(txt);
            lexer       = new BogieLangLexer(inputStream);
            lexer.AddErrorListener(new ParserErrorHandler <int>());
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            parser.AddErrorListener(new ParserErrorHandler <object>());
            literalContext = parser.literal();
            visitor        = new BogieLangBaseVisitor <object>();
            visitor.Visit(literalContext);
            literal = Literal.Compile(literalContext);
            Assert.True(literal.Execute() is double);
            Assert.True((double)literal.Execute() == 1.0);


            txt         = "false";
            inputStream = new AntlrInputStream(txt);
            lexer       = new BogieLangLexer(inputStream);
            lexer.AddErrorListener(new ParserErrorHandler <int>());
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            parser.AddErrorListener(new ParserErrorHandler <object>());
            literalContext = parser.literal();
            visitor        = new BogieLangBaseVisitor <object>();
            visitor.Visit(literalContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);
            literal = Literal.Compile(literalContext);
            Assert.True(literal.Execute() is bool);
            Assert.True((bool)literal.Execute() == false);


            txt         = "\"asd8 99asd\"";
            inputStream = new AntlrInputStream(txt);
            lexer       = new BogieLangLexer(inputStream);
            lexer.AddErrorListener(new ParserErrorHandler <int>());
            commonTokenStream = new CommonTokenStream(lexer);
            parser            = new BogieLangParser(commonTokenStream);
            parser.AddErrorListener(new ParserErrorHandler <object>());
            literalContext = parser.literal();
            visitor        = new BogieLangBaseVisitor <object>();
            visitor.Visit(literalContext);
            Assert.True(parser.NumberOfSyntaxErrors == 0);
            literal = Literal.Compile(literalContext);
            Assert.True(literal.Execute() is string);
            Assert.True((string)literal.Execute() == "asd8 99asd");
        }