Skip to content

Commit

Permalink
migrated tests to junit; improve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ingon committed Feb 26, 2020
1 parent c177527 commit e0825ef
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 279 deletions.
59 changes: 32 additions & 27 deletions src/main/java/dev/ingon/json/zero/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public static void parse(char[] source, ContentHandler handler) throws ParseExce
}

private static int element(char[] source, ContentHandler handler, int begin) throws ParseException {
int end = whitespaceNoEnd(source, begin);
int end = whitespace(source, begin);
if (end >= source.length) {
throw new ParseException(end, "expected value, but got EOF");
}

end = value(source, handler, end);
if (end == -1) {
Expand Down Expand Up @@ -63,7 +66,7 @@ private static int value(char[] source, ContentHandler handler, int begin) throw
case '9':
return numberValue(source, handler, begin, begin);
default:
throw new ParseException(begin, "unexpected character: " + ch);
throw new ParseException(begin, "expected value, but got: " + ch);
}
}

Expand All @@ -72,7 +75,10 @@ private static int objectValue(char[] source, ContentHandler handler, int begin)
return -1;
}

int end = whitespaceNoEnd(source, begin + 1);
int end = whitespace(source, begin + 1);
if (end >= source.length) {
throw new ParseException(end, "expected object key or '}', but got EOF");
}

if (source[end] == '}') {
if (! handler.endObject()) {
Expand All @@ -90,7 +96,10 @@ private static int objectValue(char[] source, ContentHandler handler, int begin)
return -1;
}

end = whitespaceNoEnd(source, end);
end = whitespace(source, end);
if (end >= source.length) {
throw new ParseException(end, "expected ':', but got EOF");
}
if (source[end] != ':') {
throw new ParseException(end, "expected ':', but got: " + source[end]);
}
Expand All @@ -111,7 +120,10 @@ private static int objectValue(char[] source, ContentHandler handler, int begin)

char ch = source[end];
if (ch == ',') {
end = whitespaceNoEnd(source, end + 1);
end = whitespace(source, end + 1);
if (end >= source.length) {
throw new ParseException(end, "expected object entry, but got EOF");
}
continue;
} else if (ch == '}') {
if (! handler.endObject()) {
Expand All @@ -129,7 +141,10 @@ private static int arrayValue(char[] source, ContentHandler handler, int begin)
return -1;
}

int end = whitespaceNoEnd(source, begin + 1);
int end = whitespace(source, begin + 1);
if (end >= source.length) {
throw new ParseException(end, "expected value or ']', but got EOF");
}

if (source[end] == ']') {
if (! handler.endArray()) {
Expand Down Expand Up @@ -209,7 +224,7 @@ private static int stringValue(char[] source, ContentHandler handler, int begin,
continue;
case 'u':
if (contentEnd + 4 >= source.length) {
throw new ParseException(contentEnd, "expect unicode escape, but got EOF");
throw new ParseException(source.length, "expect unicode escape, but got EOF");
}

if (! isHex(source[++contentEnd])) {
Expand All @@ -226,7 +241,7 @@ private static int stringValue(char[] source, ContentHandler handler, int begin,
}
continue;
default:
throw new ParseException(contentEnd, "expected an escape sequence");
throw new ParseException(contentEnd, "expected escape sequence char, but got: " + ch);
}
} else if (ch < ' ') {
throw new ParseException(contentEnd, "invalid character in string");
Expand All @@ -238,7 +253,7 @@ private static int stringValue(char[] source, ContentHandler handler, int begin,

private static int nullValue(char[] source, ContentHandler handler, int begin) throws ParseException {
if (begin + 3 >= source.length) {
throw new ParseException(begin, "expected null, but found EOF");
throw new ParseException(source.length, "expected null, but found EOF");
}

if (source[begin + 1] != 'u') {
Expand All @@ -260,7 +275,7 @@ private static int nullValue(char[] source, ContentHandler handler, int begin) t

private static int trueValue(char[] source, ContentHandler handler, int begin) throws ParseException {
if (begin + 3 >= source.length) {
throw new ParseException(begin, "expected true, but found EOF");
throw new ParseException(source.length, "expected true, but found EOF");
}

if (source[begin + 1] != 'r') {
Expand All @@ -282,7 +297,7 @@ private static int trueValue(char[] source, ContentHandler handler, int begin) t

private static int falseValue(char[] source, ContentHandler handler, int begin) throws ParseException {
if (begin + 4 >= source.length) {
throw new ParseException(begin, "expected null, but found EOF");
throw new ParseException(source.length, "expected false, but found EOF");
}

if (source[begin + 1] != 'a') {
Expand Down Expand Up @@ -317,7 +332,7 @@ private static int numberNegativeValue(char[] source, ContentHandler handler, in
} else if (ch >= '1' && ch <= '9') {
return numberValue(source, handler, begin, end);
} else {
throw new ParseException(end, "expected [0-9] but got: " + ch);
throw new ParseException(end, "expected number, but got: " + ch);
}
}

Expand Down Expand Up @@ -389,9 +404,9 @@ private static int numberFraction(char[] source, ContentHandler handler, int beg

if (!hadDigit) {
if (end >= source.length) {
throw new ParseException(end, "expected [0-9], but got EOF");
throw new ParseException(end, "expected fraction digits, but got EOF");
}
throw new ParseException(end, "expected [0-9], but got: " + source[end]);
throw new ParseException(end, "expected fraction digits, but got: " + source[end]);
}

if (end >= source.length) {
Expand All @@ -415,7 +430,7 @@ private static int numberFraction(char[] source, ContentHandler handler, int beg
private static int numberExponent(char[] source, ContentHandler handler, int begin, int position) throws ParseException {
int end = position + 1;
if (end >= source.length) {
throw new ParseException(end, "expected exponent, but got EOF");
throw new ParseException(end, "expected exponent digits, but got EOF");
}

if (source[end] == '+' || source[end] == '-') {
Expand All @@ -434,9 +449,9 @@ private static int numberExponent(char[] source, ContentHandler handler, int beg

if (!hadDigit) {
if (end >= source.length) {
throw new ParseException(end, "expected [0-9], but got EOF");
throw new ParseException(end, "expected exponent digits, but got EOF");
}
throw new ParseException(end, "expected [0-9], but got: " + source[end]);
throw new ParseException(end, "expected exponent digits, but got: " + source[end]);
}

if (! handler.doubleValue(source, begin, end)) {
Expand All @@ -455,16 +470,6 @@ private static int whitespace(char[] source, int begin) {
}
return position;
}

private static int whitespaceNoEnd(char[] source, int begin) throws ParseException {
for (int position = begin; position < source.length; position++) {
char ch = source[position];
if (! (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t')) {
return position;
}
}
throw new ParseException(begin, "unexpected EOF");
}

private static boolean isHex(char ch) {
return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/dev/ingon/json/zero/Examples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.ingon.json.zero;

import org.junit.Test;

import dev.ingon.json.zero.hl.JsonMapHandler;
import dev.ingon.json.zero.hl.JsonParser;
import dev.ingon.json.zero.hl.JsonStringHandler;

public class Examples {
@Test
public void testHello() throws Exception {
Parser.parse("{\"hello\": \"world\"}".toCharArray(), new DefaultContentHandler() {
@Override
public boolean beginObjectEntry(char[] source, int begin, int end, int escapeCount) throws ParseException {
System.out.print(readString(source, begin, end, escapeCount));
return true;
}

@Override
public boolean stringValue(char[] source, int begin, int end, int escapeCount) throws ParseException {
System.out.print(" ");
System.out.println(readString(source, begin, end, escapeCount));
return false;
}
});
}

@Test
public void testHelloHL() throws Exception {
var result = JsonParser.parse("{\"hello\": \"world\"}".toCharArray(),
new JsonMapHandler<String>(new JsonStringHandler()));
for (var e : result.entrySet()) {
System.out.format("%s %s", e.getKey(), e.getValue());
}
}
}
26 changes: 0 additions & 26 deletions src/test/java/dev/ingon/json/zero/ParserBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,6 @@
import dev.ingon.json.zero.hl.JsonStringHandler;

public class ParserBenchmark {
@Test
public void testHelloLow() throws Exception {
Parser.parse("{\"hello\": \"world\"}".toCharArray(), new DefaultContentHandler() {
@Override
public boolean beginObjectEntry(char[] source, int begin, int end, int escapeCount) throws ParseException {
System.out.print(readString(source, begin, end, escapeCount));
return true;
}

@Override
public boolean stringValue(char[] source, int begin, int end, int escapeCount) throws ParseException {
System.out.print(" ");
System.out.println(readString(source, begin, end, escapeCount));
return false;
}
});
}

@Test
public void testHelloHigh() throws Exception {
var result = JsonParser.parse("{\"hello\": \"world\"}".toCharArray(),
new JsonMapHandler<String>(new JsonStringHandler()));
for (var e : result.entrySet()) {
System.out.format("%s %s", e.getKey(), e.getValue());
}
}

public static void main(String[] args) {
int sz = 1;
Expand Down
117 changes: 0 additions & 117 deletions src/test/java/dev/ingon/json/zero/ParserFailTest.java

This file was deleted.

Loading

0 comments on commit e0825ef

Please sign in to comment.