1 package io.github.magwas.inez.query.tests;
2
3 import static org.junit.jupiter.api.Assertions.assertThrows;
4
5 import java.util.Set;
6 import java.util.stream.Stream;
7
8 import org.antlr.v4.runtime.misc.ParseCancellationException;
9 import org.junit.jupiter.api.DisplayName;
10 import org.junit.jupiter.api.Test;
11 import org.mockito.InjectMocks;
12
13 import io.github.magwas.inez.Bridi;
14 import io.github.magwas.inez.parse.ParserConstants;
15 import io.github.magwas.inez.query.QueryProcessorService;
16 import io.github.magwas.konveyor.runtime.LogUtil;
17 import io.github.magwas.konveyor.testing.TestBase;
18 import io.github.magwas.konveyor.testing.TestUtil;
19
20 class QueryProcessorTest extends TestBase implements QueryProcessorTestData {
21
22 @InjectMocks
23 QueryProcessorService queryProcessor;
24
25 @Test
26 @DisplayName("for a nonexisting sumti returns the empty list")
27 void test() {
28 Stream<Bridi> actual = queryProcessor.apply(NONEXISTING_INPUT);
29 TestUtil.assertStreamEquals(Set.of(), actual);
30 }
31
32 @Test
33 @DisplayName("for an existing sumti returns the sumti")
34 void test_1() {
35 TestUtil.assertStreamEquals(Set.of(THING), queryProcessor.apply(THING_REPR));
36 }
37
38 @Test
39 @DisplayName("if more sumtis exist with the representation, return them all")
40 void test_2() {
41 TestUtil.assertStreamEquals(Set.of(GO, GO2), queryProcessor.apply(GO_REPR));
42 }
43
44 @Test
45 @DisplayName("for a reference, return the referenced bridi")
46 void test_3() {
47 LogUtil.addDebuggedClass(QueryProcessorService.class);
48 TestUtil.assertStreamEquals(Set.of(GO), queryProcessor.apply(GO1_REFERENCE));
49 }
50
51 @Test
52 @DisplayName("for a reference to a nonexisting bridi, return an empty list")
53 void test_5() {
54 TestUtil.assertStreamEquals(Set.of(), queryProcessor.apply(NONEXISTENT_REFERENCE));
55 }
56
57 @Test
58 @DisplayName("for a bridi with the matching representation returns it")
59 void test_4() {
60 TestUtil.assertStreamEquals(Set.of(SUMTI_IS_A_THING), queryProcessor.apply(SUMTI_IS_A_THING_REPR));
61 }
62
63 @Test
64 @DisplayName("for a bridi where the representation does not match, but the selbri and sumties do, finds it")
65 void test_6() {
66 TestUtil.assertStreamEquals(Set.of(TAUTOLOGY), queryProcessor.apply(TAUTOLOGY_GENERATED_REPR));
67 }
68
69 @Test
70 @DisplayName("'$?' returns a list of the any sumti")
71 void test_7() {
72 TestUtil.assertStreamEquals(Set.of(ANY), queryProcessor.apply(ParserConstants.QUERY_BRIDI_ID));
73 }
74
75 @Test
76 @DisplayName("for a bridi where one of the sumties is '$?', the matching bridies return")
77 void test1() {
78 TestUtil.assertStreamEquals(SIMPLE_QUERY_OUTPUT, queryProcessor.apply(SIMPLE_INPUT));
79 }
80
81 @Test
82 @DisplayName("for a bridi where all of the sumties is '$?', a ParseCancellationException is thrown")
83 void test1_1() {
84 assertThrows(
85 ParseCancellationException.class,
86 () -> queryProcessor.apply(ALL_ANY_INPUT).toArray());
87 }
88
89 @Test
90 @DisplayName("if no stored bridi matches the query with '$?', and empty list is returned")
91 void test2() {
92 TestUtil.assertStreamEquals(Set.of(), queryProcessor.apply(NONMATCHING_INPUT));
93 }
94
95 @Test
96 @DisplayName("query works with the '$?' being deep down in the query")
97 void test3() {
98 Stream<Bridi> actual = queryProcessor.apply(RECURSIVE_INPUT);
99 TestUtil.assertStreamEquals(Set.of(SUMTI_IS_A_THING_IS_A_THING, TAUTOLOGY_IS_A_THING), actual);
100 }
101 }