[4069] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
| 3 |
|
---|
| 4 | import static org.junit.Assert.*
|
---|
| 5 |
|
---|
| 6 | import org.junit.*
|
---|
[7081] | 7 | import org.openstreetmap.josm.JOSMFixture
|
---|
[4069] | 8 | import org.openstreetmap.josm.data.coor.LatLon
|
---|
| 9 | import org.openstreetmap.josm.data.osm.DataSet
|
---|
| 10 | import org.openstreetmap.josm.data.osm.Node
|
---|
| 11 | import org.openstreetmap.josm.data.osm.Relation
|
---|
| 12 | import org.openstreetmap.josm.data.osm.RelationMember
|
---|
| 13 | import org.openstreetmap.josm.gui.mappaint.Environment
|
---|
| 14 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context
|
---|
| 15 |
|
---|
| 16 | class KeyConditionTest {
|
---|
| 17 |
|
---|
| 18 | def shouldFail = new GroovyTestCase().&shouldFail
|
---|
[7081] | 19 |
|
---|
[4069] | 20 | def DataSet ds;
|
---|
[7081] | 21 |
|
---|
[4069] | 22 | @BeforeClass
|
---|
| 23 | public static void createJOSMFixture(){
|
---|
| 24 | JOSMFixture.createUnitTestFixture().init()
|
---|
| 25 | }
|
---|
[7081] | 26 |
|
---|
[4069] | 27 | @Before
|
---|
| 28 | public void setUp() {
|
---|
| 29 | ds = new DataSet()
|
---|
| 30 | }
|
---|
[7081] | 31 |
|
---|
[4069] | 32 | def relation(id) {
|
---|
| 33 | def r = new Relation(id,1)
|
---|
| 34 | ds.addPrimitive(r)
|
---|
| 35 | return r
|
---|
| 36 | }
|
---|
[7081] | 37 |
|
---|
[4069] | 38 | def node(id) {
|
---|
| 39 | def n = new Node(id,1)
|
---|
| 40 | n.setCoor(new LatLon(0,0))
|
---|
| 41 | ds.addPrimitive(n)
|
---|
| 42 | return n
|
---|
| 43 | }
|
---|
[7081] | 44 |
|
---|
[4069] | 45 | @Test
|
---|
| 46 | public void create() {
|
---|
[7081] | 47 |
|
---|
[4069] | 48 | // ["a label"]
|
---|
[7081] | 49 | Condition c = Condition.createKeyCondition("a key", false, Condition.KeyMatchType.FALSE, Context.PRIMITIVE)
|
---|
[4069] | 50 | // ["a label"?]
|
---|
[7081] | 51 | c = Condition.createKeyCondition("a key", false, Condition.KeyMatchType.TRUE, Context.PRIMITIVE)
|
---|
[4069] | 52 | // [!"a label"]
|
---|
[7081] | 53 | c = Condition.createKeyCondition("a key", true, Condition.KeyMatchType.FALSE, Context.PRIMITIVE)
|
---|
[4069] | 54 | // [!"a label"?]
|
---|
[7081] | 55 | c = Condition.createKeyCondition("a key", true, Condition.KeyMatchType.TRUE, Context.PRIMITIVE)
|
---|
| 56 |
|
---|
[4069] | 57 | // ["a label"]
|
---|
[7081] | 58 | c = Condition.createKeyCondition("a key", false, null, Context.LINK)
|
---|
[4069] | 59 | // [!"a label"]
|
---|
[7081] | 60 | c = Condition.createKeyCondition("a key", true, null, Context.LINK)
|
---|
| 61 |
|
---|
[4069] | 62 | shouldFail(MapCSSException) {
|
---|
| 63 | // ["a label"?]
|
---|
[7081] | 64 | c = Condition.createKeyCondition("a key", false, Condition.KeyMatchType.TRUE, Context.LINK)
|
---|
[4069] | 65 | }
|
---|
[7081] | 66 |
|
---|
[4069] | 67 | shouldFail(MapCSSException) {
|
---|
| 68 | // [!"a label"?]
|
---|
[7081] | 69 | c = Condition.createKeyCondition("a key", true, Condition.KeyMatchType.TRUE, Context.LINK)
|
---|
[4069] | 70 | }
|
---|
| 71 | }
|
---|
[7081] | 72 |
|
---|
[4069] | 73 | @Test
|
---|
| 74 | public void applies_1() {
|
---|
| 75 | Relation r = relation(1)
|
---|
| 76 | Node n = node(1)
|
---|
| 77 | r.addMember(new RelationMember("my_role", n))
|
---|
[7081] | 78 |
|
---|
[8415] | 79 | Environment e = new Environment(n).withParent(r).withIndex(0, r.membersCount).withLinkContext()
|
---|
[7081] | 80 |
|
---|
| 81 | Condition cond = Condition.createKeyCondition("my_role", false, null, Context.LINK)
|
---|
| 82 | assert cond.applies(e)
|
---|
| 83 |
|
---|
| 84 | cond = Condition.createKeyCondition("my_role", true, null, Context.LINK)
|
---|
[4069] | 85 | assert !cond.applies(e)
|
---|
| 86 | }
|
---|
[7081] | 87 |
|
---|
[4069] | 88 | @Test
|
---|
| 89 | public void applies_2() {
|
---|
| 90 | Relation r = relation(1)
|
---|
| 91 | Node n = node(1)
|
---|
| 92 | r.addMember(new RelationMember("my_role", n))
|
---|
[7081] | 93 |
|
---|
[8415] | 94 | Environment e = new Environment(n).withParent(r).withIndex(0, r.membersCount).withLinkContext()
|
---|
[7081] | 95 |
|
---|
| 96 | Condition cond = Condition.createKeyCondition("another_role", false, null, Context.LINK)
|
---|
[4069] | 97 | assert !cond.applies(e)
|
---|
[7081] | 98 |
|
---|
| 99 | cond = Condition.createKeyCondition("another_role", true, null, Context.LINK)
|
---|
[4069] | 100 | assert cond.applies(e)
|
---|
[7081] | 101 | }
|
---|
[4069] | 102 | }
|
---|