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