Skip to content

Commit f208db0

Browse files
committed
Merge pull request skyscreamer#9 from hertzsprung/enum-enhance
Enhance JSONCompareMode enum
2 parents 541de43 + 9a76ca3 commit f208db0

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

src/main/java/org/skyscreamer/jsonassert/JSONCompareMode.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* <tr><th>STRICT_ORDER</th><th>yes</th><th>yes</th></tr>
1313
* </table>
1414
*
15-
* <p>If extensbility not allowed, then all of the expected values must match in what's being tested,
15+
* <p>If extensibility not allowed, then all of the expected values must match in what's being tested,
1616
* but any additional fields will cause the test to fail. When extensibility is allowed, all values
1717
* must still match. For example, if you're expecting:</p>
1818
*
@@ -45,7 +45,7 @@ public enum JSONCompareMode {
4545
*/
4646
NON_EXTENSIBLE(false, false),
4747
/**
48-
* Strict order checking. Not extensible, but strict array ordering.
48+
* Strict order checking. Extensible, and strict array ordering.
4949
*/
5050
STRICT_ORDER(true, true);
5151

@@ -72,4 +72,30 @@ public boolean isExtensible() {
7272
public boolean hasStrictOrder() {
7373
return _strictOrder;
7474
}
75+
76+
/**
77+
* Get the equivalent {@code JSONCompareMode} with or without strict ordering.
78+
*
79+
* @return the equivalent {@code JSONCompareMode}
80+
*/
81+
public JSONCompareMode withStrictOrdering(boolean strictOrdering) {
82+
if (strictOrdering) {
83+
return isExtensible() ? STRICT_ORDER : STRICT;
84+
} else {
85+
return isExtensible() ? LENIENT : NON_EXTENSIBLE;
86+
}
87+
}
88+
89+
/**
90+
* Get the equivalent {@code JSONCompareMode} with or without extensibility.
91+
*
92+
* @return the equivalent {@code JSONCompareMode}
93+
*/
94+
public JSONCompareMode withExtensible(boolean extensible) {
95+
if (extensible) {
96+
return hasStrictOrder() ? STRICT_ORDER : LENIENT;
97+
} else {
98+
return hasStrictOrder() ? STRICT : NON_EXTENSIBLE;
99+
}
100+
}
75101
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.skyscreamer.jsonassert;
2+
3+
import static junit.framework.Assert.assertEquals;
4+
import static junit.framework.Assert.assertFalse;
5+
import static junit.framework.Assert.assertTrue;
6+
import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT;
7+
import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE;
8+
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;
9+
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT_ORDER;
10+
11+
import org.junit.Test;
12+
13+
/**
14+
* Unit tests for {@link JSONCompareMode}
15+
*/
16+
public class JSONCompareModeTest {
17+
@Test
18+
public void testWithStrictOrdering() {
19+
assertTrue(LENIENT.withStrictOrdering(true).hasStrictOrder());
20+
assertTrue(LENIENT.withStrictOrdering(true).isExtensible());
21+
assertTrue(NON_EXTENSIBLE.withStrictOrdering(true).hasStrictOrder());
22+
assertFalse(NON_EXTENSIBLE.withStrictOrdering(true).isExtensible());
23+
24+
assertEquals(STRICT.withStrictOrdering(true), STRICT);
25+
assertEquals(STRICT_ORDER.withStrictOrdering(true), STRICT_ORDER);
26+
}
27+
28+
@Test
29+
public void testWithoutStrictOrdering() {
30+
assertFalse(STRICT_ORDER.withStrictOrdering(false).hasStrictOrder());
31+
assertTrue(STRICT_ORDER.withStrictOrdering(false).isExtensible());
32+
assertFalse(STRICT.withStrictOrdering(false).hasStrictOrder());
33+
assertFalse(STRICT.withStrictOrdering(false).isExtensible());
34+
35+
assertEquals(LENIENT.withStrictOrdering(false), LENIENT);
36+
assertEquals(NON_EXTENSIBLE.withStrictOrdering(false), NON_EXTENSIBLE);
37+
}
38+
39+
@Test
40+
public void testWithExtensibility() {
41+
assertTrue(NON_EXTENSIBLE.withExtensible(true).isExtensible());
42+
assertFalse(NON_EXTENSIBLE.withExtensible(true).hasStrictOrder());
43+
assertTrue(STRICT.withExtensible(true).isExtensible());
44+
assertTrue(STRICT.withExtensible(true).hasStrictOrder());
45+
46+
assertEquals(LENIENT.withExtensible(true), LENIENT);
47+
assertEquals(STRICT_ORDER.withExtensible(true), STRICT_ORDER);
48+
}
49+
50+
@Test
51+
public void testWithoutExtensibility() {
52+
assertFalse(STRICT_ORDER.withExtensible(false).isExtensible());
53+
assertTrue(STRICT_ORDER.withExtensible(false).hasStrictOrder());
54+
assertFalse(LENIENT.withExtensible(false).isExtensible());
55+
assertFalse(LENIENT.withExtensible(false).hasStrictOrder());
56+
57+
assertEquals(STRICT.withExtensible(false), STRICT);
58+
assertEquals(NON_EXTENSIBLE.withExtensible(false), NON_EXTENSIBLE);
59+
}
60+
}

0 commit comments

Comments
 (0)