1 19 20 package org.netbeans.lib.lexer.test.simple; 21 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.EnumSet ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import junit.framework.TestCase; 28 import org.netbeans.api.lexer.Language; 29 import org.netbeans.api.lexer.TokenId; 30 import org.netbeans.lib.lexer.test.LexerTestUtilities; 31 32 33 38 public class SimpleLanguageTest extends TestCase { 39 40 private static final int IDS_SIZE = 18; 41 42 public SimpleLanguageTest(String testName) { 43 super(testName); 44 } 45 46 protected void setUp() throws java.lang.Exception { 47 } 48 49 protected void tearDown() throws java.lang.Exception { 50 } 51 52 public void testTokenIds() { 53 Language<SimpleTokenId> language = SimpleTokenId.language(); 55 Set ids = language.tokenIds(); 56 assertEquals("Invalid ids.size()", IDS_SIZE, ids.size()); 57 58 TokenId[] idArray = { 59 SimpleTokenId.IDENTIFIER, 60 SimpleTokenId.PLUS, 61 SimpleTokenId.MINUS, 62 SimpleTokenId.PLUS_MINUS_PLUS, 63 SimpleTokenId.DIV, 64 SimpleTokenId.STAR, 65 SimpleTokenId.BLOCK_COMMENT, 66 SimpleTokenId.BLOCK_COMMENT_INCOMPLETE, 67 SimpleTokenId.WHITESPACE, 68 SimpleTokenId.LINE_COMMENT, 69 SimpleTokenId.ERROR, 70 SimpleTokenId.PUBLIC, 71 SimpleTokenId.PRIVATE, 72 SimpleTokenId.STATIC, 73 SimpleTokenId.JAVADOC_COMMENT, 74 SimpleTokenId.JAVADOC_COMMENT_INCOMPLETE, 75 SimpleTokenId.STRING_LITERAL, 76 SimpleTokenId.STRING_LITERAL_INCOMPLETE 77 78 }; 79 80 Collection testIds = Arrays.asList(idArray); 82 LexerTestUtilities.assertCollectionsEqual("Ids do not match with test ones", 83 ids, testIds); 84 85 int ind = 0; 87 int lastOrdinal = -1; 88 for (Iterator it = ids.iterator(); it.hasNext();) { 89 TokenId id = (TokenId) it.next(); 90 if (id.ordinal() == lastOrdinal) { 91 fail("Duplicate ordinal for " + id); 92 } 93 if (id.ordinal() <= lastOrdinal) { 94 fail("Token ids not sorted by ordinal: " + id); 95 } 96 lastOrdinal = id.ordinal(); 97 } 98 99 try { 100 language.validTokenId("invalid-name"); 101 fail("Error: exception not thrown"); 102 } catch (IllegalArgumentException e) { 103 } 105 106 try { 107 language.validTokenId(-1); 108 fail("Error: exception not thrown"); 109 } catch (IndexOutOfBoundsException e) { 110 } 112 113 try { 114 language.validTokenId(20); 115 fail("Error: exception not thrown"); 116 } catch (IndexOutOfBoundsException e) { 117 } 119 120 assertEquals(17, language.maxOrdinal()); 121 122 Set cats = language.tokenCategories(); 124 Collection testCats = Arrays.asList(new String [] { 125 "operator", "test-category", "whitespace", 126 "incomplete", "error", "comment", "keyword", "string" 127 }); 128 LexerTestUtilities.assertCollectionsEqual("Invalid token categories", 129 cats, testCats); 130 131 LexerTestUtilities.assertCollectionsEqual(language.tokenCategoryMembers("operator"), 132 Arrays.asList(new TokenId[] { 133 SimpleTokenId.PLUS, 134 SimpleTokenId.MINUS, 135 SimpleTokenId.PLUS_MINUS_PLUS, 136 SimpleTokenId.STAR, 137 SimpleTokenId.DIV, 138 }) 139 ); 140 141 LexerTestUtilities.assertCollectionsEqual(language.tokenCategoryMembers("test-category"), 142 Arrays.asList(new TokenId[] { 143 SimpleTokenId.PLUS, 144 SimpleTokenId.MINUS, 145 SimpleTokenId.IDENTIFIER, 146 }) 147 ); 148 149 LexerTestUtilities.assertCollectionsEqual(language.tokenCategoryMembers("whitespace"), 150 Arrays.asList(new TokenId[] { 151 SimpleTokenId.WHITESPACE, 152 }) 153 ); 154 155 LexerTestUtilities.assertCollectionsEqual(language.tokenCategoryMembers("error"), 156 Arrays.asList(new TokenId[] { 157 SimpleTokenId.ERROR, 158 SimpleTokenId.BLOCK_COMMENT_INCOMPLETE, 159 SimpleTokenId.JAVADOC_COMMENT_INCOMPLETE, 160 SimpleTokenId.STRING_LITERAL_INCOMPLETE, 161 }) 162 ); 163 164 LexerTestUtilities.assertCollectionsEqual(language.tokenCategoryMembers("comment"), 165 Arrays.asList(new TokenId[] { 166 SimpleTokenId.LINE_COMMENT, 167 SimpleTokenId.BLOCK_COMMENT, 168 SimpleTokenId.JAVADOC_COMMENT, 169 SimpleTokenId.BLOCK_COMMENT_INCOMPLETE, 170 SimpleTokenId.JAVADOC_COMMENT_INCOMPLETE 171 }) 172 ); 173 174 LexerTestUtilities.assertCollectionsEqual(language.tokenCategories(SimpleTokenId.IDENTIFIER), 175 Arrays.asList(new String [] { 176 "test-category", 177 }) 178 ); 179 180 LexerTestUtilities.assertCollectionsEqual(language.tokenCategories(SimpleTokenId.PLUS), 181 Arrays.asList(new String [] { 182 "test-category", 183 "operator", 184 }) 185 ); 186 187 LexerTestUtilities.assertCollectionsEqual(language.tokenCategories(SimpleTokenId.BLOCK_COMMENT_INCOMPLETE), 188 Arrays.asList(new String [] { 189 "error", 190 "incomplete", 191 "comment", 192 }) 193 ); 194 195 LexerTestUtilities.assertCollectionsEqual("Invalid language.indexedIds()", 197 language.merge( 198 EnumSet.of(SimpleTokenId.IDENTIFIER), 199 language.merge(language.tokenCategoryMembers("comment"), 200 language.tokenCategoryMembers("error")) 201 ), 202 Arrays.asList(new TokenId[] { 203 SimpleTokenId.LINE_COMMENT, 204 SimpleTokenId.BLOCK_COMMENT, 205 SimpleTokenId.JAVADOC_COMMENT, 206 SimpleTokenId.BLOCK_COMMENT_INCOMPLETE, 207 SimpleTokenId.JAVADOC_COMMENT_INCOMPLETE, 208 SimpleTokenId.STRING_LITERAL_INCOMPLETE, 209 SimpleTokenId.ERROR, 210 SimpleTokenId.IDENTIFIER, 211 }) 212 ); 213 214 } 215 216 } 217 | Popular Tags |