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 junit.framework.Assert;
  30 import junit.framework.AssertionFailedError;
  31 import org.antlr.runtime.RecognitionException;
  32 import org.junit.Test;
  33 
  34 import static org.junit.Assert.*;
  35 
  36 public class FieldSelectTest extends ParserBase {
  37 
  38     @Test
  39     public void rgba() throws Exception {
  40         String tree = parseTreeFor(".rgba");
  41         assertEquals(tree, ".rgba");
  42     }
  43 
  44     @Test
  45     public void rgb() throws Exception {
  46         String tree = parseTreeFor(".rgb");
  47         assertEquals(tree, ".rgb");
  48     }
  49 
  50     @Test
  51     public void rg() throws Exception {
  52         String tree = parseTreeFor(".rg");
  53         assertEquals(tree, ".rg");
  54     }
  55 
  56     @Test
  57     public void r() throws Exception {
  58         String tree = parseTreeFor(".r");
  59         assertEquals(tree, ".r");
  60     }
  61 
  62     @Test
  63     public void aaaa() throws Exception {
  64         String tree = parseTreeFor(".aaaa");
  65         assertEquals(tree, ".aaaa");
  66     }
  67 
  68     @Test
  69     public void abgr() throws Exception {
  70         String tree = parseTreeFor(".abgr");
  71         assertEquals(tree, ".abgr");
  72     }
  73 
  74     @Test
  75     public void xyzw() throws Exception {
  76         String tree = parseTreeFor(".xyzw");
  77         assertEquals(tree, ".xyzw");
  78     }
  79 
  80     @Test
  81     public void xyz() throws Exception {
  82         String tree = parseTreeFor(".xyz");
  83         assertEquals(tree, ".xyz");
  84     }
  85 
  86     @Test
  87     public void xy() throws Exception {
  88         String tree = parseTreeFor(".xy");
  89         assertEquals(tree, ".xy");
  90     }
  91 
  92     @Test
  93     public void x() throws Exception {
  94         String tree = parseTreeFor(".x");
  95         assertEquals(tree, ".x");
  96     }
  97 
  98     @Test
  99     public void zzz() throws Exception {
 100         String tree = parseTreeFor(".zzz");
 101         assertEquals(tree, ".zzz");
 102     }
 103 
 104     @Test
 105     public void wzyz() throws Exception {
 106         String tree = parseTreeFor(".wzyx");
 107         assertEquals(tree, ".wzyx");
 108     }
 109 
 110     @Test(expected = RecognitionException.class)
 111     public void notAFieldSelection1() throws Exception {
 112         parseTreeFor("qpz");
 113     }
 114 
 115     @Test(expected = AssertionFailedError.class)
 116     public void notAFieldSelection2() throws Exception {
 117         parseTreeFor(".xqpz", true);
 118     }
 119 
 120     @Test(expected = AssertionFailedError.class)
 121     public void tooManyVals() throws Exception {
 122         parseTreeFor(".xyzwx", true);
 123     }
 124 
 125     @Test(expected = AssertionFailedError.class)
 126     public void mixedVals() throws Exception {
 127         parseTreeFor(".xyba", true);
 128     }
 129 
 130     private String parseTreeFor(String text) throws RecognitionException {
 131         return parseTreeFor(text, false);
 132     }
 133 
 134     private String parseTreeFor(String text, boolean expectEx) throws RecognitionException {
 135         JSLParser parser = parserOver(text);
 136         String ret = parser.field_selection();
 137         // TODO: there's probably a better way to check for trailing (invalid) characters
 138         boolean sawException = false;
 139         try {
 140             parser.field_selection();
 141         } catch (Exception e) {
 142             sawException = true;
 143         }
 144         if (sawException == expectEx) {
 145             Assert.fail(expectEx ? "Expecting EOF" : "Not expecting EOF");
 146         }
 147         return ret;
 148     }
 149 }