/* * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package test.javafx.css; import com.sun.javafx.css.parser.Token; import com.sun.javafx.css.parser.TokenShim; import java.io.CharArrayReader; import java.io.Reader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import javafx.css.CssLexerShim; import static org.junit.Assert.*; import static org.junit.Assert.assertEquals; import org.junit.Test; public class CssLexerTest { public CssLexerTest() { } private void checkTokens(List resultTokens, TokenShim... expectedTokens) throws org.junit.ComparisonFailure { if (expectedTokens.length != resultTokens.size()) { throw new org.junit.ComparisonFailure( "lengths do not match", Arrays.toString(expectedTokens), resultTokens.toString() ); } for (int n = 0; n getTokens(String string) { Reader reader = new CharArrayReader(string.toCharArray()); final CssLexerShim lexer = new CssLexerShim(); lexer.setReader(reader); final List tokens = new ArrayList(); TokenShim token = null; do { token = lexer.nextToken(); tokens.add(token); } while (token.getType() != Token.EOF); return Collections.unmodifiableList(tokens); } private void lexDigitsWithUnits(String units, int type) throws org.junit.ComparisonFailure { checkTokens(getTokens("123"+units), new TokenShim(type, "123"+units), TokenShim.EOF_TOKEN); checkTokens(getTokens("123.45"+units), new TokenShim(type, "123.45"+units), TokenShim.EOF_TOKEN); checkTokens(getTokens(".45"+units), new TokenShim(type, ".45"+units), TokenShim.EOF_TOKEN); checkTokens(getTokens("-123"+units), new TokenShim(type, "-123"+units), TokenShim.EOF_TOKEN); checkTokens(getTokens("-.45"+units), new TokenShim(type, "-.45"+units), TokenShim.EOF_TOKEN); checkTokens(getTokens("+123"+units), new TokenShim(type, "+123"+units), TokenShim.EOF_TOKEN); checkTokens(getTokens("+.45"+units), new TokenShim(type, "+.45"+units), TokenShim.EOF_TOKEN); } @Test public void testLexValidDigits() { lexDigitsWithUnits("", CssLexerShim.NUMBER); } @Test public void testLexValidDigitsWithCM() { lexDigitsWithUnits("cm", CssLexerShim.CM); // case should be ignored lexDigitsWithUnits("cM", CssLexerShim.CM); } @Test public void testLexValidDigitsWithDEG() { lexDigitsWithUnits("deg", CssLexerShim.DEG); // case should be ignored lexDigitsWithUnits("dEg", CssLexerShim.DEG); } @Test public void testLexValidDigitsWithEM() { lexDigitsWithUnits("em", CssLexerShim.EMS); // case should be ignored lexDigitsWithUnits("Em", CssLexerShim.EMS); } @Test public void testLexValidDigitsWithEX() { lexDigitsWithUnits("ex", CssLexerShim.EXS); // case should be ignored lexDigitsWithUnits("Ex", CssLexerShim.EXS); } @Test public void testLexValidDigitsWithGRAD() { lexDigitsWithUnits("grad", CssLexerShim.GRAD); // case should be ignored lexDigitsWithUnits("gRad", CssLexerShim.GRAD); } @Test public void testLexValidDigitsWithIN() { lexDigitsWithUnits("in", CssLexerShim.IN); // case should be ignored lexDigitsWithUnits("In", CssLexerShim.IN); } @Test public void testLexValidDigitsWithMM() { lexDigitsWithUnits("mm", CssLexerShim.MM); // case should be ignored lexDigitsWithUnits("mM", CssLexerShim.MM); } @Test public void testLexValidDigitsWithPC() { lexDigitsWithUnits("pc", CssLexerShim.PC); // case should be ignored lexDigitsWithUnits("Pc", CssLexerShim.PC); } @Test public void testLexValidDigitsWithPT() { lexDigitsWithUnits("pt", CssLexerShim.PT); // case should be ignored lexDigitsWithUnits("PT", CssLexerShim.PT); } @Test public void testLexValidDigitsWithPX() { lexDigitsWithUnits("px", CssLexerShim.PX); // case should be ignored lexDigitsWithUnits("Px", CssLexerShim.PX); } @Test public void testLexValidDigitsWithRAD() { lexDigitsWithUnits("rad", CssLexerShim.RAD); // case should be ignored lexDigitsWithUnits("RaD", CssLexerShim.RAD); } @Test public void testLexValidDigitsWithTURN() { lexDigitsWithUnits("turn", CssLexerShim.TURN); // case should be ignored lexDigitsWithUnits("TurN", CssLexerShim.TURN); } @Test public void testLexValidDigitsWithS() { lexDigitsWithUnits("s", CssLexerShim.SECONDS); // case should be ignored lexDigitsWithUnits("S", CssLexerShim.SECONDS); } @Test public void testLexValidDigitsWithMS() { lexDigitsWithUnits("ms", CssLexerShim.MS); // case should be ignored lexDigitsWithUnits("mS", CssLexerShim.MS); } @Test public void testLexValidDigitsWithPCT() { lexDigitsWithUnits("%", CssLexerShim.PERCENTAGE); } @Test public void testLexValidDigitsWithBadUnits() { lexDigitsWithUnits("xyzzy", Token.INVALID); } @Test public void textLexValidDigitsValidDigits() { checkTokens( getTokens("foo: 10pt; bar: 20%;"), new TokenShim(CssLexerShim.IDENT, "foo"), new TokenShim(CssLexerShim.COLON, ":"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.PT, "10pt"), new TokenShim(CssLexerShim.SEMI, ";"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.IDENT, "bar"), new TokenShim(CssLexerShim.COLON, ":"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.PERCENTAGE, "20%"), new TokenShim(CssLexerShim.SEMI, ";"), TokenShim.EOF_TOKEN ); } @Test public void textLexInvalidDigitsValidDigits() { checkTokens( getTokens("foo: 10pz; bar: 20%;"), new TokenShim(CssLexerShim.IDENT, "foo"), new TokenShim(CssLexerShim.COLON, ":"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(Token.INVALID, "10pz"), new TokenShim(CssLexerShim.SEMI, ";"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.IDENT, "bar"), new TokenShim(CssLexerShim.COLON, ":"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.PERCENTAGE, "20%"), new TokenShim(CssLexerShim.SEMI, ";"), TokenShim.EOF_TOKEN ); } @Test public void textLexValidDigitsBangImportant() { checkTokens( getTokens("foo: 10pt !important;"), new TokenShim(CssLexerShim.IDENT, "foo"), new TokenShim(CssLexerShim.COLON, ":"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.PT, "10pt"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.IMPORTANT_SYM, "!important"), new TokenShim(CssLexerShim.SEMI, ";"), TokenShim.EOF_TOKEN ); } @Test public void textLexInvalidDigitsBangImportant() { checkTokens( getTokens("foo: 10pz !important;"), new TokenShim(CssLexerShim.IDENT, "foo"), new TokenShim(CssLexerShim.COLON, ":"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(Token.INVALID, "10pz"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.IMPORTANT_SYM, "!important"), new TokenShim(CssLexerShim.SEMI, ";"), TokenShim.EOF_TOKEN ); } @Test public void textLexValidDigitsInSequence() { checkTokens( getTokens("-1 0px 1pt .5em;"), new TokenShim(CssLexerShim.NUMBER, "-1"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.PX, "0px"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.PT, "1pt"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.EMS, ".5em"), new TokenShim(CssLexerShim.SEMI, ";"), TokenShim.EOF_TOKEN ); } @Test public void textLexInvalidDigitsInSequence() { checkTokens( getTokens("-1 0px 1pz .5em;"), new TokenShim(CssLexerShim.NUMBER, "-1"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.PX, "0px"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(Token.INVALID, "1pz"), new TokenShim(CssLexerShim.WS, " "), new TokenShim(CssLexerShim.EMS, ".5em"), new TokenShim(CssLexerShim.SEMI, ";"), TokenShim.EOF_TOKEN ); } @Test public void testTokenOffset() { String str = "a: b;"; // [?][0] = line // [?][1] = offset TokenShim[] expected = { new TokenShim(CssLexerShim.IDENT, "a", 1, 0), new TokenShim(CssLexerShim.COLON, ":", 1, 1), new TokenShim(CssLexerShim.WS, " ", 1, 2), new TokenShim(CssLexerShim.IDENT, "b", 1, 3), new TokenShim(CssLexerShim.SEMI, ";", 1, 4), TokenShim.EOF_TOKEN }; List tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n tlist = getTokens(str); checkTokens(tlist, expected); for(int n=0; n