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 java.util.List; 29 import com.sun.scenario.effect.compiler.JSLParser; 30 import com.sun.scenario.effect.compiler.model.Function; 31 import com.sun.scenario.effect.compiler.model.Param; 32 import com.sun.scenario.effect.compiler.model.Qualifier; 33 import com.sun.scenario.effect.compiler.model.Type; 34 import com.sun.scenario.effect.compiler.model.Variable; 35 import com.sun.scenario.effect.compiler.tree.ExtDecl; 36 import com.sun.scenario.effect.compiler.tree.FuncDef; 37 import com.sun.scenario.effect.compiler.tree.VarDecl; 38 import org.antlr.runtime.RecognitionException; 39 import org.junit.Test; 40 41 import static org.junit.Assert.*; 42 43 public class ExternalDeclarationTest extends ParserBase { 44 45 @Test 46 public void declaration() throws Exception { 47 ExtDecl tree = parseTreeFor("param float4 foo;").get(0); 48 assertTrue(tree instanceof VarDecl); 49 VarDecl d = (VarDecl)tree; 50 Variable var = d.getVariable(); 51 assertNotNull(var); 52 assertEquals(var.getQualifier(), Qualifier.PARAM); 53 assertEquals(var.getType(), Type.FLOAT4); 54 assertEquals(var.getName(), "foo"); 55 assertNull(d.getInit()); 56 } 57 58 @Test 59 public void multiDeclaration() throws Exception { 60 List<ExtDecl> decls = parseTreeFor("param float4 foo, bar;"); 61 assertEquals(decls.size(), 2); 62 ExtDecl tree; 63 tree = decls.get(0); 64 assertTrue(tree instanceof VarDecl); 65 VarDecl d = (VarDecl)tree; 66 Variable var = d.getVariable(); 67 assertNotNull(var); 68 assertEquals(var.getQualifier(), Qualifier.PARAM); 69 assertEquals(var.getType(), Type.FLOAT4); 70 assertEquals(var.getName(), "foo"); 71 assertNull(d.getInit()); 72 tree = decls.get(1); 73 assertTrue(tree instanceof VarDecl); 74 d = (VarDecl)tree; 75 var = d.getVariable(); 76 assertNotNull(var); 77 assertEquals(var.getQualifier(), Qualifier.PARAM); 78 assertEquals(var.getType(), Type.FLOAT4); 79 assertEquals(var.getName(), "bar"); 80 assertNull(d.getInit()); 81 } 82 83 @Test 84 public void funcDefNoParam() throws Exception { 85 ExtDecl tree = parseTreeFor("void test() { int i = 3; }").get(0); 86 assertTrue(tree instanceof FuncDef); 87 FuncDef d = (FuncDef)tree; 88 Function func = d.getFunction(); 89 assertNotNull(func); 90 assertEquals(func.getReturnType(), Type.VOID); 91 assertEquals(func.getName(), "test"); 92 List<Param> params = func.getParams(); 93 assertNotNull(params); 94 assertEquals(params.size(), 0); 95 assertNotNull(d.getStmt()); 96 } 97 98 @Test 99 public void funcDefOneParam() throws Exception { 100 ExtDecl tree = parseTreeFor("void test(float3 foo) { int i = 3; }").get(0); 101 assertTrue(tree instanceof FuncDef); 102 FuncDef d = (FuncDef)tree; 103 Function func = d.getFunction(); 104 assertNotNull(func); 105 assertEquals(func.getReturnType(), Type.VOID); 106 assertEquals(func.getName(), "test"); 107 List<Param> params = func.getParams(); 108 assertNotNull(params); 109 assertEquals(params.size(), 1); 110 assertNotNull(d.getStmt()); 111 } 112 113 @Test 114 public void funcDefTwoParam() throws Exception { 115 ExtDecl tree = parseTreeFor("void test(float3 foo, float3 bar) { int i = 3; }").get(0); 116 assertTrue(tree instanceof FuncDef); 117 FuncDef d = (FuncDef)tree; 118 Function func = d.getFunction(); 119 assertNotNull(func); 120 assertEquals(func.getReturnType(), Type.VOID); 121 assertEquals(func.getName(), "test"); 122 List<Param> params = func.getParams(); 123 assertNotNull(params); 124 assertEquals(params.size(), 2); 125 assertNotNull(d.getStmt()); 126 } 127 128 @Test(expected = RecognitionException.class) 129 public void notAnExtDecl() throws Exception { 130 parseTreeFor("foo = 4"); 131 } 132 133 private List<ExtDecl> parseTreeFor(String text) throws RecognitionException { 134 JSLParser parser = parserOver(text); 135 return parser.external_declaration(); 136 } 137 }