forked from seart-group/java-tree-sitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternTest.java
66 lines (61 loc) · 2.51 KB
/
PatternTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ch.usi.si.seart.treesitter;
import lombok.Cleanup;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.List;
class PatternTest extends BaseTest {
@ParameterizedTest(name = "[{index}] {0}")
@ValueSource(strings = {
"(line_comment)(block_comment)",
"(line_comment) (block_comment)",
"(line_comment ) (block_comment )",
"(line_comment ) ( block_comment)",
"( line_comment) (block_comment )",
"( line_comment ) ( block_comment )",
"(line_comment) (block_comment)",
"(line_comment )( block_comment)",
" (line_comment) (block_comment) ",
})
void test(String string) {
List<String> parts = List.of("(line_comment)", "(block_comment)");
@Cleanup Query query = Query.builder()
.language(Language.JAVA)
.pattern(string)
.build();
Assertions.assertEquals(String.join(" ", parts), query.getPattern());
List<Pattern> patterns = query.getPatterns();
Assertions.assertFalse(patterns.isEmpty());
Assertions.assertEquals(2, patterns.size());
for (int i = 0; i < patterns.size(); i++) {
Pattern pattern = patterns.get(i);
Assertions.assertTrue(pattern.getStartOffset() >= 0);
Assertions.assertTrue(pattern.getStartOffset() < pattern.getEndOffset());
String actual = pattern.getValue();
String expected = parts.get(i);
Assertions.assertEquals(expected, actual);
}
}
@Test
void testDisable() {
@Cleanup Parser parser = Parser.getFor(Language.PYTHON);
@Cleanup Tree tree = parser.parse("pass");
@Cleanup Query query = Query.builder()
.language(Language.PYTHON)
.pattern("(module) @capture")
.build();
Node root = tree.getRootNode();
Pattern pattern = query.getPatterns().stream()
.findFirst()
.orElseGet(Assertions::fail);
Assertions.assertTrue(pattern.isEnabled());
pattern.disable();
Assertions.assertFalse(pattern.isEnabled());
@Cleanup QueryCursor cursor = root.walk(query);
for (QueryMatch ignored: cursor) Assertions.fail();
Assertions.assertFalse(pattern.isEnabled());
pattern.disable();
Assertions.assertFalse(pattern.isEnabled());
}
}