1 /* 2 * Copyright (c) 2024, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import jdk.incubator.code.extern.impl.Scanner; 25 import jdk.incubator.code.extern.impl.Tokens; 26 import org.junit.jupiter.api.Assertions; 27 import org.junit.jupiter.params.ParameterizedTest; 28 import org.junit.jupiter.params.provider.MethodSource; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 33 import static jdk.incubator.code.extern.impl.Tokens.TokenKind.*; 34 35 /* 36 * @test 37 * @modules jdk.incubator.code/jdk.incubator.code.extern.impl 38 * @run junit TestScanner 39 */ 40 41 public class TestScanner { 42 43 static Object[][] data() { 44 return new Object[][] { 45 {"java.lang.Integer", List.of(IDENTIFIER, DOT, IDENTIFIER, DOT, IDENTIFIER)}, 46 {"java.lang.Integer", List.of("java", DOT, "lang", DOT, "Integer")}, 47 {"a<a<a, a>,a<a, a>>", List.of("a", LT, 48 "a", LT, "a", COMMA, "a", GT, 49 COMMA, 50 "a", LT, "a", COMMA, "a", GT, 51 GT)}, 52 {"_->(){}[],.=><?:;+-&^@", List.of( 53 UNDERSCORE, 54 ARROW, 55 LPAREN, 56 RPAREN, 57 LBRACE, 58 RBRACE, 59 LBRACKET, 60 RBRACKET, 61 COMMA, 62 DOT, 63 EQ, 64 GT, 65 LT, 66 QUES, 67 COLON, 68 SEMI, 69 PLUS, 70 SUB, 71 AMP, 72 CARET, 73 MONKEYS_AT 74 )}, 75 {"%1 %a %_1", List.of(VALUE_IDENTIFIER, VALUE_IDENTIFIER, VALUE_IDENTIFIER)}, 76 {"%1 %a %_1", List.of("%1", "%a", "%_1")}, 77 {"\"abc\\n\"", List.of(STRINGLITERAL)}, 78 {"\"abc \\b \\f \\n \\r \\t \\' \\\" \\\\\"", List.of("abc \b \f \n \r \t \' \" \\")}, 79 }; 80 } 81 82 @ParameterizedTest 83 @MethodSource("data") 84 public void test(String content, List<Object> expectedTokens) { 85 Scanner.Factory factory = Scanner.factory(); 86 87 Scanner s = factory.newScanner(content); 88 s.nextToken(); 89 List<Tokens.Token> actualTokens = new ArrayList<>(); 90 while (s.token().kind != EOF) { 91 actualTokens.add(s.token()); 92 s.nextToken(); 93 } 94 95 Assertions.assertEquals(expectedTokens.size(), actualTokens.size()); 96 for (int i = 0; i < expectedTokens.size(); i++) { 97 Object e = expectedTokens.get(i); 98 Tokens.Token a = actualTokens.get(i); 99 if (e instanceof Tokens.TokenKind t) { 100 Assertions.assertEquals(t, a.kind); 101 } else if (e instanceof String v) { 102 String as = switch (a.kind.tag) { 103 case NAMED -> a.name(); 104 case STRING, NUMERIC -> a.stringVal(); 105 case DEFAULT -> a.kind.name; 106 }; 107 Assertions.assertEquals(v, as); 108 } else { 109 assert false; 110 } 111 } 112 } 113 }