public void testCompleteExp()
        {
            setup("log(5+6*7/8)");

            ExpressionContext context = parser.expression();

            DoubleSpreadsheetVisitor visitor = new DoubleSpreadsheetVisitor();
            double result = visitor.Visit(context);

            Assert.Equal("1.0107238653917732", result.ToString(System.Globalization.CultureInfo.GetCultureInfo("en-US").NumberFormat));
        }
        public void testVisitPowerExp()
        {
            setup("4^3^2");

            PowerExpContext context = parser.expression() as PowerExpContext;

            DoubleSpreadsheetVisitor visitor = new DoubleSpreadsheetVisitor();
            double result = visitor.VisitPowerExp(context);

            Assert.Equal(double.Parse("262144"), result);
        }
        public void testVisitFunctionExp()
        {
            setup("log(100)");

            FunctionExpContext context = parser.expression() as FunctionExpContext;

            DoubleSpreadsheetVisitor visitor = new DoubleSpreadsheetVisitor();
            double result = visitor.VisitFunctionExp(context);

            Assert.Equal(result, double.Parse("2"));
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            string input = "log(10 + A1 * 35 + (5.4 - 7.4))";

            ICharStream       inputStream       = CharStreams.fromString(input);
            SpreadsheetLexer  spreadsheetLexer  = new SpreadsheetLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(spreadsheetLexer);
            SpreadsheetParser spreadsheetParser = new SpreadsheetParser(commonTokenStream);

            SpreadsheetParser.ExpressionContext expressionContext = spreadsheetParser.expression();
            DoubleSpreadsheetVisitor            visitor           = new DoubleSpreadsheetVisitor();

            Console.WriteLine(visitor.Visit(expressionContext));
        }
        public void testWrongVisitFunctionExp()
        {
            setup("logga(100)");

            FunctionExpContext context = parser.expression() as FunctionExpContext;

            DoubleSpreadsheetVisitor visitor = new DoubleSpreadsheetVisitor();
            double result = visitor.VisitFunctionExp(context);

            CommonTokenStream ts = (CommonTokenStream)parser.InputStream;

            // this is syntactically correct and would be true even for a good function
            Assert.Equal(SpreadsheetLexer.NAME, ts.Get(0).Type);
            Assert.Equal(null, errorListener.Symbol);
            // we choose to return 0 if we can't the find function with that NAME
            // so that's how we know is wrong
            Assert.Equal(0, result);
        }