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.BinaryExpr; 31 import com.sun.scenario.effect.compiler.tree.DoWhileStmt; 32 import com.sun.scenario.effect.compiler.tree.ExprStmt; 33 import com.sun.scenario.effect.compiler.tree.ForStmt; 34 import com.sun.scenario.effect.compiler.tree.Stmt; 35 import com.sun.scenario.effect.compiler.tree.WhileStmt; 36 import org.antlr.runtime.RecognitionException; 37 import org.junit.Test; 38 39 import static org.junit.Assert.*; 40 41 public class IterationStatementTest extends ParserBase { 42 43 @Test 44 public void whileStmt() throws Exception { 45 Stmt tree = parseTreeFor("while (i >= 3) j += 4;"); 46 assertTrue(tree instanceof WhileStmt); 47 WhileStmt s = (WhileStmt)tree; 48 assertTrue(s.getCondition() instanceof BinaryExpr); 49 assertTrue(s.getStmt() instanceof ExprStmt); 50 } 51 52 @Test 53 public void doWhileStmt() throws Exception { 54 Stmt tree = parseTreeFor("do j += 4; while (i >= 3);"); 55 assertTrue(tree instanceof DoWhileStmt); 56 DoWhileStmt s = (DoWhileStmt)tree; 57 assertTrue(s.getStmt() instanceof ExprStmt); 58 assertTrue(s.getExpr() instanceof BinaryExpr); 59 } 60 61 @Test 62 public void forStmt() throws Exception { 63 Stmt tree = parseTreeFor("for (i = 0; i < 5; i += 2) j += 4;"); 64 assertTrue(tree instanceof ForStmt); 65 ForStmt s = (ForStmt)tree; 66 assertTrue(s.getInit() instanceof ExprStmt); 67 assertTrue(s.getCondition() instanceof BinaryExpr); 68 assertTrue(s.getExpr() instanceof BinaryExpr); 69 assertTrue(s.getStmt() instanceof ExprStmt); 70 } 71 72 @Test 73 public void forStmtNoCondition() throws Exception { 74 Stmt tree = parseTreeFor("for (i = 0; ; i += 2) j += 4;"); 75 assertTrue(tree instanceof ForStmt); 76 ForStmt s = (ForStmt)tree; 77 assertTrue(s.getInit() instanceof ExprStmt); 78 assertNull(s.getCondition()); 79 assertTrue(s.getExpr() instanceof BinaryExpr); 80 assertTrue(s.getStmt() instanceof ExprStmt); 81 } 82 83 @Test 84 public void forStmtNoIncrement() throws Exception { 85 Stmt tree = parseTreeFor("for (i = 0; i < 5; ) j += 4;"); 86 assertTrue(tree instanceof ForStmt); 87 ForStmt s = (ForStmt)tree; 88 assertTrue(s.getInit() instanceof ExprStmt); 89 assertTrue(s.getCondition() instanceof BinaryExpr); 90 assertNull(s.getExpr()); 91 assertTrue(s.getStmt() instanceof ExprStmt); 92 } 93 94 @Test 95 public void forStmtNoConditionOrIncrement() throws Exception { 96 Stmt tree = parseTreeFor("for (i = 0; ; ) j += 4;"); 97 assertTrue(tree instanceof ForStmt); 98 ForStmt s = (ForStmt)tree; 99 assertTrue(s.getInit() instanceof ExprStmt); 100 assertNull(s.getCondition()); 101 assertNull(s.getExpr()); 102 assertTrue(s.getStmt() instanceof ExprStmt); 103 } 104 105 @Test(expected = RecognitionException.class) 106 public void notAnIterationStmt() throws Exception { 107 parseTreeFor("return;"); 108 } 109 110 private Stmt parseTreeFor(String text) throws RecognitionException { 111 JSLParser parser = parserOver(text); 112 parser.getSymbolTable().declareVariable("i", Type.INT, null); 113 parser.getSymbolTable().declareVariable("j", Type.INT, null); 114 return parser.iteration_statement(); 115 } 116 }