Skip to content

Commit b7f7b53

Browse files
committed
adding ValidatingVisitor#passesTypeCheck()
This method handles the possible combinations of the Schema#requires* methods and the nullable flag It is currently only used by ObjectSchema Also removing unused ValidatingVisitor ctor.
1 parent 48cdd3b commit b7f7b53

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.everit.json.schema;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.verifyZeroInteractions;
8+
9+
import org.json.JSONObject;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
public class ValidatingVisitorTest {
14+
15+
private ValidationFailureReporter reporter;
16+
17+
18+
@Before
19+
public void before() {
20+
reporter = mock(ValidationFailureReporter.class);
21+
}
22+
23+
@Test
24+
public void passesTypeCheck_otherType_noRequires() {
25+
ValidatingVisitor subject = new ValidatingVisitor("string", reporter);
26+
assertFalse(subject.passesTypeCheck(JSONObject.class, false, null));
27+
verifyZeroInteractions(reporter);
28+
}
29+
30+
@Test
31+
public void passesTypeCheck_otherType_requires() {
32+
ValidatingVisitor subject = new ValidatingVisitor("string", reporter);
33+
assertFalse(subject.passesTypeCheck(JSONObject.class, true, null));
34+
verify(reporter).failure(JSONObject.class, "string");
35+
}
36+
37+
@Test
38+
public void passesTypeCheck_otherType_nullPermitted_nullObject() {
39+
ValidatingVisitor subject = new ValidatingVisitor(JSONObject.NULL, reporter);
40+
assertFalse(subject.passesTypeCheck(JSONObject.class, true, Boolean.TRUE));
41+
verifyZeroInteractions(reporter);
42+
}
43+
44+
@Test
45+
public void passesTypeCheck_otherType_nullPermitted_nullReference() {
46+
ValidatingVisitor subject = new ValidatingVisitor(null, reporter);
47+
assertFalse(subject.passesTypeCheck(JSONObject.class, true, Boolean.TRUE));
48+
verifyZeroInteractions(reporter);
49+
}
50+
51+
@Test
52+
public void passesTypeCheck_nullPermitted_nonNullValue() {
53+
ValidatingVisitor subject = new ValidatingVisitor("string", reporter);
54+
assertFalse(subject.passesTypeCheck(JSONObject.class, true, Boolean.TRUE));
55+
verify(reporter).failure(JSONObject.class, "string");
56+
}
57+
58+
@Test
59+
public void passesTypeCheck_requiresType_nullableIsNull() {
60+
ValidatingVisitor subject = new ValidatingVisitor(null, reporter);
61+
assertFalse(subject.passesTypeCheck(JSONObject.class, true, null));
62+
verify(reporter).failure(JSONObject.class, null);
63+
}
64+
65+
@Test
66+
public void passesTypeCheck_sameType() {
67+
ValidatingVisitor subject = new ValidatingVisitor("string", reporter);
68+
assertTrue(subject.passesTypeCheck(String.class, true, Boolean.TRUE));
69+
verifyZeroInteractions(reporter);
70+
}
71+
}

0 commit comments

Comments
 (0)