@@ -19,11 +19,9 @@ package de.danielbechler.diff.access
1919import spock.lang.Specification
2020import spock.lang.Unroll
2121
22- /**
23- * @author Daniel Bechler
24- */
2522class InstancesSpec extends Specification {
26- def " getType() throws IllegalArgumentException if base and working have incompatible types" () {
23+
24+ def " getType: throws IllegalArgumentException if base and working have incompatible types" () {
2725 setup :
2826 def instances = new Instances (RootAccessor . instance, working, base, null )
2927
@@ -41,7 +39,7 @@ class InstancesSpec extends Specification {
4139 }
4240
4341 @Unroll
44- def " getType() returns '#resultTypeName' for base of type '#baseClassName' and working of type '#workingClassName'" () {
42+ def " getType: returns '#resultTypeName' for base of type '#baseClassName' and working of type '#workingClassName'" () {
4543 setup :
4644 def instances = Instances . of(working, base)
4745
@@ -50,6 +48,7 @@ class InstancesSpec extends Specification {
5048
5149 where :
5250 base | working || resultType
51+ new ArrayList () | new ArrayList () || ArrayList . class
5352 new ArrayList () | new LinkedHashSet () || Collection . class
5453 new HashMap () | new TreeMap () || Map . class
5554
@@ -59,7 +58,7 @@ class InstancesSpec extends Specification {
5958 }
6059
6160 @Unroll
62- def " getType() returns type of TypeAwareAccessor when it is primitive '#type'" () {
61+ def " getType: returns type of TypeAwareAccessor when it is primitive '#type'" () {
6362 given :
6463 def typeAwareAccessor = Mock (TypeAwareAccessor );
6564 def instances = Instances . of(typeAwareAccessor, 0 , 0L );
@@ -74,21 +73,21 @@ class InstancesSpec extends Specification {
7473 type << [long , int , float , boolean , char , byte , short ]
7574 }
7675
77- def " getType() returns most specific shared type for non-primitive objects (except for Map and Collection)" () {
76+ def "getType: returns most specific shared type for non-primitive objects (except for Map and Collection )" () {
7877 given:
7978 def instances = Instances.of(propertyAccessor, working, base);
8079
8180 expect:
8281 instances.getType() == resultType
8382
8483 where:
85- propertyAccessor | working | base | resultType
84+ propertyAccessor | working | base | resultType
8685 RootAccessor.instance | BigInteger.ONE | BigDecimal.ONE | Number
8786 RootAccessor.instance | new LineNumberReader(new StringReader(" " )) | new BufferedReader(new StringReader(" " )) | BufferedReader
8887 RootAccessor.instance | new StringReader(" " ) | new BufferedReader(new StringReader(" " )) | Reader
8988 }
9089
91- def " getType() uses type of TypeAwareAccessor if no shared type for non-primitive objects could be determined" () {
90+ def " getType : uses type of TypeAwareAccessor if no shared type for non- primitive objects could be determined" () {
9291 given:
9392 def accessor = Mock(TypeAwareAccessor)
9493 def instances = Instances.of(accessor, new StringBuilder(), " " );
@@ -99,4 +98,166 @@ class InstancesSpec extends Specification {
9998 expect:
10099 instances.getType() == CharSequence
101100 }
101+
102+ def 'areNull: returns true when base and working are null'() {
103+ given:
104+ Instances instances = new Instances(RootAccessor.instance, null, null, null);
105+ expect:
106+ instances.areNull()
107+ }
108+
109+ def 'areNull: returns false when base is not null'() {
110+ given:
111+ Instances instances = new Instances(RootAccessor.instance, null, '', null);
112+ expect:
113+ !instances.areNull()
114+ }
115+
116+ def 'areNull: returns false when working is not null'() {
117+ given:
118+ Instances instances = new Instances(RootAccessor.instance, '', null, null);
119+ expect:
120+ !instances.areNull()
121+ }
122+
123+ @Unroll
124+ def 'isPrimitiveType: returns true for primitive type (#type)'() {
125+ given:
126+ def typeAwareAccessor = Stub TypeAwareAccessor
127+ typeAwareAccessor.type >> type
128+ and:
129+ def instances = new Instances(typeAwareAccessor, null, null, null)
130+ expect:
131+ instances.isPrimitiveType()
132+ where:
133+ type << [boolean, short, int, char, long, double, float, byte]
134+ }
135+
136+ @Unroll
137+ def 'isPrimitiveType: returns returns false for primitive wrapper type (#type)'() {
138+ given:
139+ def typeAwareAccessor = Stub TypeAwareAccessor
140+ typeAwareAccessor.type >> type
141+ and:
142+ def instances = new Instances(typeAwareAccessor, null, null, null)
143+ expect:
144+ !instances.isPrimitiveType()
145+ where:
146+ type << [Boolean, Short, Integer, Character, Long, Double, Float, Byte]
147+ }
148+
149+ @Unroll
150+ def 'isPrimitiveType: returns returns false for complex type'() {
151+ given:
152+ def typeAwareAccessor = Stub TypeAwareAccessor
153+ typeAwareAccessor.type >> type
154+ and:
155+ def instances = new Instances(typeAwareAccessor, null, null, null)
156+ expect:
157+ !instances.isPrimitiveType()
158+ where:
159+ type << [String, Object, Date]
160+ }
161+
162+ def 'getFresh: returns zero for numeric types without custom initialization'() {
163+ given:
164+ def typeAwareAccessor = Stub TypeAwareAccessor
165+ typeAwareAccessor.type >> type
166+ and:
167+ def instances = new Instances(typeAwareAccessor, null, null, null)
168+ expect:
169+ instances.getFresh() == 0
170+ where:
171+ type << [short, int, char, long, double, float, byte]
172+ }
173+
174+ def 'getFresh: returns false for primitive boolean type without custom initialization'() {
175+ given:
176+ def typeAwareAccessor = Stub TypeAwareAccessor
177+ typeAwareAccessor.type >> boolean
178+ and:
179+ def instances = new Instances(typeAwareAccessor, null, null, null)
180+ expect:
181+ instances.getFresh() == false
182+ }
183+
184+ def 'getFresh: returns fresh value from constructor for non-primitive types'() {
185+ given:
186+ def typeAwareAccessor = Stub TypeAwareAccessor
187+ typeAwareAccessor.type >> Object
188+ and:
189+ def instances = new Instances(typeAwareAccessor, null, null, 'foo')
190+ expect:
191+ instances.getFresh() == 'foo'
192+ }
193+
194+ @Unroll
195+ def 'getFresh: returns null for primitive wrapper types (#type)'() {
196+ given:
197+ def typeAwareAccessor = Stub TypeAwareAccessor
198+ typeAwareAccessor.type >> type
199+ and:
200+ def instances = Instances.of(typeAwareAccessor, null, null)
201+ expect:
202+ instances.getFresh() == null
203+ where:
204+ type << [Boolean, Short, Integer, Character, Long, Double, Float, Byte]
205+ }
206+
207+ def 'areSame: should return true when working and base are the same object'() {
208+ given:
209+ def object = new Object()
210+ def instances = new Instances(RootAccessor.instance, object, object, null)
211+ expect:
212+ instances.areSame()
213+ }
214+
215+ def 'areSame: should return false when working and base are not the same object'() {
216+ given:
217+ def instances = new Instances(RootAccessor.instance, new Object(), new Object(), null)
218+ expect:
219+ !instances.areSame()
220+ }
221+
222+ @Unroll('hasBeenAdded: should return #result when [working: #working, base: #base, fresh: #fresh]')
223+ def 'hasBeenAdded'() {
224+ given:
225+ def typeAwareAccessor = Stub TypeAwareAccessor
226+ typeAwareAccessor.type >> typeFromAccessor
227+ and:
228+ def instances = new Instances(typeAwareAccessor, working, base, fresh)
229+ expect:
230+ instances.hasBeenAdded() == result
231+ where:
232+ working | base | fresh | typeFromAccessor || result
233+ null | null | null | null || false
234+ 'foo' | null | null | null || true
235+ 'foo' | 'foo' | null | null || false
236+ null | 'foo' | null | null || false
237+ 2 | 0 | 0 | int || true
238+ 2 | 1 | 1 | int || true
239+ 1 | 1 | 1 | int || false
240+ 2 | 0 | 1 | int || false
241+ }
242+
243+ @Unroll('hasBeenRemoved: should return #result when [working: #working, base: #base, fresh: #fresh]')
244+ def 'hasBeenRemoved'() {
245+ given:
246+ def typeAwareAccessor = Stub TypeAwareAccessor
247+ typeAwareAccessor.type >> typeFromAccessor
248+ and:
249+ def instances = Instances.of(typeAwareAccessor, working, base, fresh)
250+ expect:
251+ instances.hasBeenRemoved() == result
252+ where:
253+ working | base | fresh | typeFromAccessor || result
254+ null | null | null | null || false
255+ 'foo' | null | null | null || false
256+ 'foo' | 'foo' | null | null || false
257+ null | 'foo' | null | null || true
258+ 0 | 0 | 0 | int || false
259+ 1 | 1 | 1 | int || false
260+ 0 | 1 | 1 | int || false
261+ 1 | 2 | 1 | int || true
262+ }
102263}
0 commit comments