[serde-hjson] Apply generic JSON read test to Hjson reader

This commit is contained in:
2025-08-03 20:47:10 +02:00
parent 7ce3aaac06
commit 7fec263af0
3 changed files with 37 additions and 14 deletions

View File

@@ -5,6 +5,8 @@ plugins {
dependencies { dependencies {
api(project(":tweed5-serde-api")) api(project(":tweed5-serde-api"))
testImplementation(project(":serde-json-test-utils"))
} }
tasks.shadowJar { tasks.shadowJar {

View File

@@ -75,7 +75,7 @@ public class HjsonReader implements TweedDataReader {
if (lexerToken.type() == HjsonLexerToken.Type.BRACE_CLOSE) { if (lexerToken.type() == HjsonLexerToken.Type.BRACE_CLOSE) {
contexts.pop(); contexts.pop();
state = State.AFTER_VALUE; state = State.AFTER_VALUE;
return TweedDataTokens.getMapEnd(); return wrapValueLikeTokenContextAppropriate(TweedDataTokens.getMapEnd());
} else if (lexerToken.type() == HjsonLexerToken.Type.LINE_FEED || lexerToken.type() == HjsonLexerToken.Type.COMMA) { } else if (lexerToken.type() == HjsonLexerToken.Type.LINE_FEED || lexerToken.type() == HjsonLexerToken.Type.COMMA) {
state = State.BEFORE_OBJECT_KEY; state = State.BEFORE_OBJECT_KEY;
} else { } else {
@@ -90,7 +90,7 @@ public class HjsonReader implements TweedDataReader {
if (lexerToken.type() == HjsonLexerToken.Type.BRACE_CLOSE) { if (lexerToken.type() == HjsonLexerToken.Type.BRACE_CLOSE) {
contexts.pop(); contexts.pop();
state = State.AFTER_VALUE; state = State.AFTER_VALUE;
return TweedDataTokens.getMapEnd(); return wrapValueLikeTokenContextAppropriate(TweedDataTokens.getMapEnd());
} else if (lexerToken.type() == HjsonLexerToken.Type.QUOTELESS_STRING || lexerToken.type() == HjsonLexerToken.Type.JSON_STRING) { } else if (lexerToken.type() == HjsonLexerToken.Type.QUOTELESS_STRING || lexerToken.type() == HjsonLexerToken.Type.JSON_STRING) {
state = State.AFTER_OBJECT_KEY; state = State.AFTER_OBJECT_KEY;
return TweedDataTokens.asMapEntryKey(createStringToken(lexerToken)); return TweedDataTokens.asMapEntryKey(createStringToken(lexerToken));
@@ -108,7 +108,7 @@ public class HjsonReader implements TweedDataReader {
if (lexerToken.type() == HjsonLexerToken.Type.BRACKET_CLOSE) { if (lexerToken.type() == HjsonLexerToken.Type.BRACKET_CLOSE) {
contexts.pop(); contexts.pop();
state = State.AFTER_VALUE; state = State.AFTER_VALUE;
return TweedDataTokens.getListEnd(); return wrapValueLikeTokenContextAppropriate(TweedDataTokens.getListEnd());
} else if (lexerToken.type() == HjsonLexerToken.Type.COMMA || lexerToken.type() == HjsonLexerToken.Type.LINE_FEED) { } else if (lexerToken.type() == HjsonLexerToken.Type.COMMA || lexerToken.type() == HjsonLexerToken.Type.LINE_FEED) {
state = State.BEFORE_VALUE; state = State.BEFORE_VALUE;
} else { } else {
@@ -124,7 +124,7 @@ public class HjsonReader implements TweedDataReader {
eatGeneralLexerToken(); eatGeneralLexerToken();
contexts.pop(); contexts.pop();
state = State.AFTER_VALUE; state = State.AFTER_VALUE;
return TweedDataTokens.getListEnd(); return wrapValueLikeTokenContextAppropriate(TweedDataTokens.getListEnd());
} }
return TweedDataTokens.asListValue(nextValueToken()); return TweedDataTokens.asListValue(nextValueToken());
} }
@@ -138,27 +138,31 @@ public class HjsonReader implements TweedDataReader {
switch (lexerToken.type()) { switch (lexerToken.type()) {
case NULL: case NULL:
state = State.AFTER_VALUE; state = State.AFTER_VALUE;
return TweedDataTokens.getNull(); return wrapValueLikeTokenContextAppropriate(TweedDataTokens.getNull());
case TRUE: case TRUE:
case FALSE: case FALSE:
state = State.AFTER_VALUE; state = State.AFTER_VALUE;
return createBooleanToken(lexerToken); return wrapValueLikeTokenContextAppropriate(createBooleanToken(lexerToken));
case NUMBER: case NUMBER:
state = State.AFTER_VALUE; state = State.AFTER_VALUE;
return createNumberToken(lexerToken); return wrapValueLikeTokenContextAppropriate(createNumberToken(lexerToken));
case QUOTELESS_STRING: case QUOTELESS_STRING:
case JSON_STRING: case JSON_STRING:
case MULTILINE_STRING: case MULTILINE_STRING:
state = State.AFTER_VALUE; state = State.AFTER_VALUE;
return createStringToken(lexerToken); return wrapValueLikeTokenContextAppropriate(createStringToken(lexerToken));
case BRACKET_OPEN: case BRACKET_OPEN: {
state = State.BEFORE_VALUE; state = State.BEFORE_VALUE;
TweedDataToken token = wrapValueLikeTokenContextAppropriate(TweedDataTokens.getListStart());
contexts.push(Context.LIST); contexts.push(Context.LIST);
return TweedDataTokens.getListStart(); return token;
case BRACE_OPEN: }
case BRACE_OPEN: {
state = State.BEFORE_OBJECT_KEY; state = State.BEFORE_OBJECT_KEY;
TweedDataToken token = wrapValueLikeTokenContextAppropriate(TweedDataTokens.getMapStart());
contexts.push(Context.OBJECT); contexts.push(Context.OBJECT);
return TweedDataTokens.getMapStart(); return token;
}
default: default:
throw createIllegalTokenException( throw createIllegalTokenException(
lexerToken, lexerToken,
@@ -625,6 +629,16 @@ public class HjsonReader implements TweedDataReader {
return lexer.nextInnerObjectToken(); return lexer.nextInnerObjectToken();
} }
private TweedDataToken wrapValueLikeTokenContextAppropriate(TweedDataToken token) {
if (currentContext() == Context.LIST) {
return TweedDataTokens.asListValue(token);
} else if (currentContext() == Context.OBJECT) {
return TweedDataTokens.asMapEntryValue(token);
} else {
return token;
}
}
private Context currentContext() { private Context currentContext() {
assert contexts.peek() != null; assert contexts.peek() != null;
return contexts.peek(); return contexts.peek();

View File

@@ -1,7 +1,9 @@
package de.siphalor.tweed5.data.hjson; package de.siphalor.tweed5.data.hjson;
import de.siphalor.tweed5.dataapi.api.TweedDataReadException; import de.siphalor.tweed5.dataapi.api.TweedDataReadException;
import de.siphalor.tweed5.dataapi.api.TweedDataReader;
import de.siphalor.tweed5.dataapi.api.TweedDataToken; import de.siphalor.tweed5.dataapi.api.TweedDataToken;
import de.siphalor.tweed5.testutils.serde.json.JsonReaderTest;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
@@ -10,9 +12,14 @@ import java.io.StringReader;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class HjsonReaderTest { class HjsonReaderTest implements JsonReaderTest {
private static final double DOUBLE_PRECISION = 0.000000001D; private static final double DOUBLE_PRECISION = 0.000000001D;
@Override
public TweedDataReader createJsonReader(String text) {
return new HjsonReader(new HjsonLexer(new StringReader(text)));
}
@ParameterizedTest @ParameterizedTest
@CsvSource({ @CsvSource({
"127,127", "127,127",