Skip to content

Commit

Permalink
Test of lexing foreign js function
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Mar 15, 2024
1 parent fbbb153 commit 43ef037
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertNotEquals;
Expand Down Expand Up @@ -37,7 +38,40 @@ public void testSimpleMain() {
assertNextToken(seq, "42", "constant.character.numeric");
}

@Test
public void testForeignJavaScriptFunction() {
var code = """
foreign js dbg = '''
debugger;
""";
var hier = org.netbeans.api.lexer.TokenHierarchy.create(code, ensoLanguage);
assertNotNull(hier);
var seq = hier.tokenSequence();
assertEquals(4, seq.tokenCount());
assertNextToken(seq, "foreign js dbg = ", null);
assertNextToken(seq, "'''", "string.quoted.triple.begin");
assertNextToken(seq, "\n", null);
assertNextToken(seq, " debugger;\n", null);
assertFalse("EOF", seq.moveNext());
}

private static void assertNextToken(TokenSequence<?> seq, String expectedText, String expectedCategory) {
try {
assertNextTokenImpl(seq, expectedText, expectedCategory);
} catch (AssertionError err) {
var sb = new StringBuilder();
sb.append(err.getMessage()).append("\n");
sb.append("Remaining tokens:\n");
seq.movePrevious();
while (seq.moveNext()) {
final Object categories = seq.token().getProperty("categories");
sb.append(seq.token().text()).append(" categories: ").append(categories).append("\n");
}
throw new AssertionError(sb.toString(), err);
}
}

private static void assertNextTokenImpl(TokenSequence<?> seq, String expectedText, String expectedCategory) {
assertTrue("Can move next", seq.moveNext());
var tok = seq.token();
assertNotNull("Token found", tok);
Expand All @@ -46,6 +80,7 @@ private static void assertNextToken(TokenSequence<?> seq, String expectedText, S
}
if (expectedCategory != null) {
var categories = (List<String>) tok.getProperty("categories");
assertNotNull("Categories found", categories);
var at = categories.indexOf(expectedCategory);
assertNotEquals("Category " + expectedCategory + " found in " + categories, -1, at);
}
Expand Down

0 comments on commit 43ef037

Please sign in to comment.