|
| 1 | +/* |
| 2 | + * Copyright 2014 Daniel Bechler |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package de.danielbechler.diff.inclusion |
| 18 | + |
| 19 | +import de.danielbechler.diff.ObjectDifferBuilder |
| 20 | +import de.danielbechler.diff.node.DiffNode |
| 21 | +import de.danielbechler.diff.path.NodePath |
| 22 | +import spock.lang.Specification |
| 23 | + |
| 24 | +class ExclusiveInclusionIT extends Specification { |
| 25 | + |
| 26 | + def objectDifferBuilder = ObjectDifferBuilder.startBuilding() |
| 27 | + .filtering() |
| 28 | + .returnNodesWithState(DiffNode.State.IGNORED).and() |
| 29 | + |
| 30 | + def 'inclusion via type based property does NOT exclude nodes that are not explicitly included'() { |
| 31 | + given: |
| 32 | + def base = new A(b: new B(foo: 'base', bar: 'base'), c: new C(baz: 'base')) |
| 33 | + def working = new A(b: new B(foo: 'working', bar: 'base'), c: new C(baz: 'working')) |
| 34 | + |
| 35 | + when: |
| 36 | + def node = objectDifferBuilder |
| 37 | + .inclusion() |
| 38 | + .include().propertyNameOfType(B, 'foo').and() |
| 39 | + .build() |
| 40 | + .compare(working, base) |
| 41 | + |
| 42 | + then: |
| 43 | + node.getChild('b').getChild('foo').isChanged() |
| 44 | + node.getChild('b').getChild('bar').isIgnored() |
| 45 | + node.getChild('c').getChild('baz').isChanged() |
| 46 | + } |
| 47 | + |
| 48 | + class A { |
| 49 | + B b |
| 50 | + C c |
| 51 | + } |
| 52 | + |
| 53 | + class B { |
| 54 | + String foo |
| 55 | + String bar |
| 56 | + } |
| 57 | + |
| 58 | + class C { |
| 59 | + String baz |
| 60 | + } |
| 61 | + |
| 62 | + def 'inclusion via property name does exclude nodes that are not explicitly included'() { |
| 63 | + given: |
| 64 | + def base = new A(b: new B(foo: 'base', bar: 'base'), c: new C(baz: 'base')) |
| 65 | + def working = new A(b: new B(foo: 'working', bar: 'working'), c: new C(baz: 'working')) |
| 66 | + |
| 67 | + when: |
| 68 | + def node = objectDifferBuilder |
| 69 | + .inclusion() |
| 70 | + .include().propertyName('b').and() |
| 71 | + .build() |
| 72 | + .compare(working, base) |
| 73 | + |
| 74 | + then: |
| 75 | + node.getChild('b').hasChanges() |
| 76 | + node.getChild('b').getChild('foo').isChanged() |
| 77 | + node.getChild('b').getChild('bar').isChanged() |
| 78 | + node.getChild('c').isIgnored() |
| 79 | + } |
| 80 | + |
| 81 | + def 'inclusion via node path does exclude nodes that are not explicitly included'() { |
| 82 | + given: |
| 83 | + def base = new A(b: new B(foo: 'base', bar: 'base'), c: new C(baz: 'base')) |
| 84 | + def working = new A(b: new B(foo: 'working', bar: 'working'), c: new C(baz: 'working')) |
| 85 | + |
| 86 | + when: |
| 87 | + def node = objectDifferBuilder |
| 88 | + .inclusion() |
| 89 | + .include().node(NodePath.with('b')).and() |
| 90 | + .build() |
| 91 | + .compare(working, base) |
| 92 | + |
| 93 | + then: |
| 94 | + node.getChild('b').hasChanges() |
| 95 | + node.getChild('b').getChild('foo').isChanged() |
| 96 | + node.getChild('b').getChild('bar').isChanged() |
| 97 | + node.getChild('c').isIgnored() |
| 98 | + } |
| 99 | + |
| 100 | + def 'inclusion via type does exclude nodes that are not explicitly included - even its children'() { |
| 101 | + given: |
| 102 | + def base = new A(b: new B(foo: 'base', bar: 'base'), c: new C(baz: 'base')) |
| 103 | + def working = new A(b: new B(foo: 'working', bar: 'working'), c: new C(baz: 'working')) |
| 104 | + |
| 105 | + when: 'the property with type B is explicitly included' |
| 106 | + def node = objectDifferBuilder |
| 107 | + .inclusion() |
| 108 | + .include().type(B).and() |
| 109 | + .build() |
| 110 | + .compare(working, base) |
| 111 | + |
| 112 | + then: 'the included type should not be ignored' |
| 113 | + node.getChild('b').isIgnored() == false |
| 114 | + |
| 115 | + and: 'its sibling with a different type schould be ignored' |
| 116 | + node.getChild('c').isIgnored() |
| 117 | + |
| 118 | + and: 'the children are also ignored, even though the parent is included' |
| 119 | + node.getChild('b').getChild('foo').isIgnored() |
| 120 | + node.getChild('b').getChild('bar').isIgnored() |
| 121 | + } |
| 122 | + |
| 123 | + def 'inclusion via category does exclude nodes that are not explicitly included'() { |
| 124 | + given: |
| 125 | + def base = new A(b: new B(foo: 'base', bar: 'base'), c: new C(baz: 'base')) |
| 126 | + def working = new A(b: new B(foo: 'working', bar: 'working'), c: new C(baz: 'working')) |
| 127 | + |
| 128 | + when: |
| 129 | + def node = objectDifferBuilder |
| 130 | + .categories().ofNode(NodePath.with('b')).toBe('included-category').and() |
| 131 | + .inclusion() |
| 132 | + .include().category('included-category').and() |
| 133 | + .build() |
| 134 | + .compare(working, base) |
| 135 | + |
| 136 | + then: |
| 137 | + node.getChild('b').hasChanges() |
| 138 | + node.getChild('b').getChild('foo').isChanged() |
| 139 | + node.getChild('b').getChild('bar').isChanged() |
| 140 | + node.getChild('c').isIgnored() |
| 141 | + } |
| 142 | +} |
0 commit comments