1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
6 | import static org.junit.jupiter.api.Assertions.fail;
|
---|
7 |
|
---|
8 | import org.junit.jupiter.api.BeforeEach;
|
---|
9 | import org.junit.jupiter.api.Test;
|
---|
10 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
|
---|
18 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyCondition;
|
---|
19 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyMatchType;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 | import org.openstreetmap.josm.tools.Logging;
|
---|
22 |
|
---|
23 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Unit tests of {@link KeyCondition}.
|
---|
27 | */
|
---|
28 | class KeyConditionTest {
|
---|
29 |
|
---|
30 | private DataSet ds;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Setup rule
|
---|
34 | */
|
---|
35 | @RegisterExtension
|
---|
36 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
37 | public JOSMTestRules test = new JOSMTestRules().projection();
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Setup test
|
---|
41 | */
|
---|
42 | @BeforeEach
|
---|
43 | public void setUp() {
|
---|
44 | ds = new DataSet();
|
---|
45 | }
|
---|
46 |
|
---|
47 | Relation relation(int id) {
|
---|
48 | Relation r = new Relation(id, 1);
|
---|
49 | ds.addPrimitive(r);
|
---|
50 | return r;
|
---|
51 | }
|
---|
52 |
|
---|
53 | Node node(int id) {
|
---|
54 | Node n = new Node(id, 1);
|
---|
55 | n.setCoor(LatLon.ZERO);
|
---|
56 | ds.addPrimitive(n);
|
---|
57 | return n;
|
---|
58 | }
|
---|
59 |
|
---|
60 | private static void shouldFail(Runnable r) {
|
---|
61 | try {
|
---|
62 | r.run();
|
---|
63 | fail("should throw exception");
|
---|
64 | } catch (MapCSSException e) {
|
---|
65 | Logging.trace(e);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Test {@link ConditionFactory#createKeyCondition}.
|
---|
71 | */
|
---|
72 | @Test
|
---|
73 | public void create() {
|
---|
74 |
|
---|
75 | // ["a label"]
|
---|
76 | ConditionFactory.createKeyCondition("a key", false, KeyMatchType.FALSE, Context.PRIMITIVE);
|
---|
77 | // ["a label"?]
|
---|
78 | ConditionFactory.createKeyCondition("a key", false, KeyMatchType.TRUE, Context.PRIMITIVE);
|
---|
79 | // [!"a label"]
|
---|
80 | ConditionFactory.createKeyCondition("a key", true, KeyMatchType.FALSE, Context.PRIMITIVE);
|
---|
81 | // [!"a label"?]
|
---|
82 | ConditionFactory.createKeyCondition("a key", true, KeyMatchType.TRUE, Context.PRIMITIVE);
|
---|
83 |
|
---|
84 | // ["a label"]
|
---|
85 | ConditionFactory.createKeyCondition("a key", false, null, Context.LINK);
|
---|
86 | // [!"a label"]
|
---|
87 | ConditionFactory.createKeyCondition("a key", true, null, Context.LINK);
|
---|
88 |
|
---|
89 | // ["a label"?]
|
---|
90 | shouldFail(() ->
|
---|
91 | ConditionFactory.createKeyCondition("a key", false, KeyMatchType.TRUE, Context.LINK)
|
---|
92 | );
|
---|
93 |
|
---|
94 | // [!"a label"?]
|
---|
95 | shouldFail(() ->
|
---|
96 | ConditionFactory.createKeyCondition("a key", true, KeyMatchType.TRUE, Context.LINK)
|
---|
97 | );
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Test
|
---|
101 | public void applies_1() {
|
---|
102 | Relation r = relation(1);
|
---|
103 | Node n = node(1);
|
---|
104 | r.addMember(new RelationMember("my_role", n));
|
---|
105 |
|
---|
106 | Environment e = new Environment(n).withParent(r).withIndex(0, r.getMembersCount()).withLinkContext();
|
---|
107 |
|
---|
108 | Condition cond = ConditionFactory.createKeyCondition("my_role", false, null, Context.LINK);
|
---|
109 | assertTrue(cond.applies(e));
|
---|
110 |
|
---|
111 | cond = ConditionFactory.createKeyCondition("my_role", true, null, Context.LINK);
|
---|
112 | assertFalse(cond.applies(e));
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Test
|
---|
116 | public void applies_2() {
|
---|
117 | Relation r = relation(1);
|
---|
118 | Node n = node(1);
|
---|
119 | r.addMember(new RelationMember("my_role", n));
|
---|
120 |
|
---|
121 | Environment e = new Environment(n).withParent(r).withIndex(0, r.getMembersCount()).withLinkContext();
|
---|
122 |
|
---|
123 | Condition cond = ConditionFactory.createKeyCondition("another_role", false, null, Context.LINK);
|
---|
124 | assertFalse(cond.applies(e));
|
---|
125 |
|
---|
126 | cond = ConditionFactory.createKeyCondition("another_role", true, null, Context.LINK);
|
---|
127 | assertTrue(cond.applies(e));
|
---|
128 | }
|
---|
129 | }
|
---|