1 /*
   2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.scenario.effect.compiler.parser;
  27 
  28 import com.sun.scenario.effect.compiler.JSLParser;
  29 import com.sun.scenario.effect.compiler.model.Type;
  30 import com.sun.scenario.effect.compiler.tree.Expr;
  31 import com.sun.scenario.effect.compiler.tree.LiteralExpr;
  32 import com.sun.scenario.effect.compiler.tree.VariableExpr;
  33 import static com.sun.scenario.effect.compiler.parser.Expressions.SIMPLE_EXPRESSION;
  34 import org.antlr.runtime.RecognitionException;
  35 import org.junit.Before;
  36 import org.junit.Test;
  37 import static org.junit.Assert.assertEquals;
  38 import static org.junit.Assert.assertTrue;
  39 
  40 public class PrimaryExprTest extends ParserBase {
  41 
  42     private String primary;
  43 
  44     @Before
  45     public void setUp() {
  46         this.primary = primary();
  47     }
  48 
  49     @Test
  50     public void variable() throws Exception {
  51         Expr tree = parseTreeFor("foo");
  52         assertTrue(tree instanceof VariableExpr);
  53         assertEquals(((VariableExpr)tree).getVariable().getName(), "foo");
  54     }
  55 
  56     @Test
  57     public void intLiteral() throws Exception {
  58         Expr tree = parseTreeFor("123");
  59         assertTrue(tree instanceof LiteralExpr);
  60         assertEquals(((LiteralExpr)tree).getValue(), new Integer(123));
  61     }
  62 
  63     @Test
  64     public void floatLiteral() throws Exception {
  65         Expr tree = parseTreeFor("1.234");
  66         assertTrue(tree instanceof LiteralExpr);
  67         assertEquals(((LiteralExpr)tree).getValue(), new Float(1.234));
  68     }
  69 
  70     @Test
  71     public void boolLiteralT() throws Exception {
  72         Expr tree = parseTreeFor("true");
  73         assertTrue(tree instanceof LiteralExpr);
  74         assertEquals(((LiteralExpr)tree).getValue(), Boolean.TRUE);
  75     }
  76 
  77     @Test
  78     public void boolLiteralF() throws Exception {
  79         Expr tree = parseTreeFor("false");
  80         assertTrue(tree instanceof LiteralExpr);
  81         assertEquals(((LiteralExpr)tree).getValue(), Boolean.FALSE);
  82     }
  83 
  84     @Test
  85     public void bracketted() throws Exception {
  86         Expr tree = parseTreeFor("(" + primary + ")");
  87     }
  88 
  89     @Test(expected = RecognitionException.class)
  90     public void notAPrimaryExpression() throws Exception {
  91         parseTreeFor("!(@&#");
  92     }
  93 
  94     private Expr parseTreeFor(String text) throws RecognitionException {
  95         JSLParser parser = parserOver(text);
  96         parser.getSymbolTable().declareVariable("foo", Type.INT, null);
  97         return parser.primary_expression();
  98     }
  99 
 100     protected String primary() {
 101         return SIMPLE_EXPRESSION;
 102     }
 103 }