-
Notifications
You must be signed in to change notification settings - Fork 2
/
QuantifierTest.java
84 lines (74 loc) · 3.45 KB
/
QuantifierTest.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
class QuantifierTest extends BaseTest {
private static final Language language = Language.JAVA;
private static final Map<Quantifier, String> symbols = Map.of(
Quantifier.ONE, "",
Quantifier.ONE_OR_MORE, "+",
Quantifier.ZERO_OR_ONE, "?",
Quantifier.ZERO_OR_MORE, "*"
);
private static final class QuantifierArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return symbols.entrySet().stream()
.map(entry -> {
Quantifier quantifier = entry.getKey();
String symbol = entry.getValue();
String pattern = "((_)" + symbol + " @capture)";
Query query = Query.getFor(language, pattern);
List<Capture> captures = query.getCaptures();
Capture capture = captures.stream().findFirst().orElseThrow();
return Arguments.of(quantifier, capture, query);
});
}
}
@ParameterizedTest(name = "[{index}] {0}")
@ArgumentsSource(QuantifierArgumentsProvider.class)
void testGetQuantifiers(Quantifier expected, Capture capture, Query query) {
List<Quantifier> quantifiers = capture.getQuantifiers();
Assertions.assertNotNull(quantifiers);
Assertions.assertFalse(quantifiers.isEmpty());
Assertions.assertEquals(1, quantifiers.size());
Quantifier actual = quantifiers.stream()
.findFirst()
.orElseGet(Assertions::fail);
Assertions.assertEquals(expected, actual);
query.close();
}
@Test
void testGetQuantifier() {
@Cleanup Query query = Query.builder()
.language(language)
.pattern("((_) @capture)")
.pattern("(identifier)")
.build();
List<Capture> captures = query.getCaptures();
List<Pattern> patterns = query.getPatterns();
Capture capture = captures.stream().findFirst().orElseThrow();
Pattern pattern = patterns.stream().skip(1).findFirst().orElseThrow();
Quantifier quantifier = capture.getQuantifier(pattern);
Assertions.assertEquals(Quantifier.ZERO, quantifier);
}
@Test
void testGetQuantifierThrows() {
@Cleanup Query original = Query.getFor(language, "((_) @capture)");
@Cleanup Query copy = Query.getFor(language, "((_) @capture)");
List<Capture> captures = original.getCaptures();
Capture capture = captures.stream().findFirst().orElseThrow();
List<Pattern> patterns = copy.getPatterns();
Pattern pattern = patterns.stream().findFirst().orElseThrow();
Assertions.assertThrows(NullPointerException.class, () -> capture.getQuantifier(null));
Assertions.assertThrows(IllegalArgumentException.class, () -> capture.getQuantifier(pattern));
}
}