Skip to content

Commit c66a958

Browse files
committed
fix json tests
1 parent eeab190 commit c66a958

4 files changed

Lines changed: 23 additions & 11 deletions

File tree

json/src/main/java/alpine/json/JsonReader.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ Element read(String input) throws ParsingException {
2020
this.characters = input.toCharArray();
2121
this.position = 0;
2222
this.skipWhitespace();
23-
return this.readElement();
23+
var element = this.readElement();
24+
this.skipWhitespace();
25+
26+
if (this.position < this.characters.length) {
27+
throw new ParsingException(this.input, "Unexpected trailing content!", this.position);
28+
}
29+
30+
return element;
2431
}
2532

2633
private Element readElement() throws ParsingException {

json/src/main/java/alpine/json/ObjectElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public boolean hasValue(Element value) {
181181
return this.elements.containsValue(value);
182182
}
183183

184-
public boolean hasString(String value) {
184+
public boolean hasValue(String value) {
185185
return this.hasValue(Element.string(value));
186186
}
187187

json/src/test/java/alpine/json/ExternalTests.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import java.util.Objects;
1313
import java.util.stream.Stream;
1414

15+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
16+
import static org.junit.jupiter.api.Assertions.fail;
17+
1518
final class ExternalTests {
1619
@SuppressWarnings("resource")
1720
static Stream<Arguments> arguments() {
@@ -42,14 +45,15 @@ static Stream<Arguments> arguments() {
4245
@ParameterizedTest(name = "{0}")
4346
@MethodSource("arguments")
4447
void test(String name, Behavior behavior, String string) {
45-
try {
46-
Json.read(string);
47-
} catch (ParsingException e) {
48-
if (behavior == Behavior.SHOULD_PASS) {
49-
throw new AssertionError(name + " should have passed!", e);
50-
}
51-
} catch (StackOverflowError ignored) {
48+
if (behavior == Behavior.SHOULD_PASS) {
49+
assertDoesNotThrow(() -> Json.read(string));
50+
} else if (behavior == Behavior.SHOULD_FAIL) {
51+
try {
52+
Json.read(string);
53+
fail("Expected ParsingException or StackOverflowError");
54+
} catch (ParsingException | StackOverflowError ignored) {
5255

56+
}
5357
}
5458
}
5559

json/src/test/java/alpine/json/JsonTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ void testInvalid() {
1818
"test",
1919
"{", "}", "[", "]",
2020
"\"", "\"\0\"",
21-
"123abc", "[123e]", "0.", ".0"
21+
"123abc", "[123e]", "0.", ".0",
22+
"[],", "{}abc"
2223
}) {
2324
assertThrows(ParsingException.class, () -> Json.read(string));
2425
}
@@ -65,7 +66,7 @@ void testObjectAccess() {
6566

6667
@Test
6768
void testObjectSetting() {
68-
object().set("key", "value").expect("key", StringElement.class);
69+
assertDoesNotThrow(() -> object().set("key", "value").expect("key", StringElement.class));
6970
assertThrows(AssertionError.class, () -> object().set("key", "value").expect("key", NullElement.class));
7071
}
7172

0 commit comments

Comments
 (0)