Skip to content

Commit

Permalink
Make configuration settable from test-config
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Haraldsen committed Jan 28, 2024
1 parent 1ce1c8f commit 7c07945
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
26 changes: 20 additions & 6 deletions src/main/java/com/ethlo/time/ParseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@
* #L%
*/

import java.util.Optional;

public class ParseConfig
{
public static final ParseConfig DEFAULT = new ParseConfig(new char[]{'T', 't', ' '}, new char[]{'.'});
public static final ParseConfig DEFAULT = new ParseConfig(null, null, true);

private final char[] allowedDateTimeSeparators;
private final char[] allowedFractionSeparators;
private final boolean failOnTrailingJunk;

private ParseConfig(char[] allowedDateTimeSeparators, char[] allowedFractionSeparators)
protected ParseConfig(char[] allowedDateTimeSeparators, char[] allowedFractionSeparators, boolean failOnTrailingJunk)
{
this.allowedDateTimeSeparators = allowedDateTimeSeparators;
this.allowedFractionSeparators = allowedFractionSeparators;
this.allowedDateTimeSeparators = Optional.ofNullable(allowedDateTimeSeparators).orElse(new char[]{'T', 't', ' '});
this.allowedFractionSeparators = Optional.ofNullable(allowedFractionSeparators).orElse(new char[]{'.'});
this.failOnTrailingJunk = failOnTrailingJunk;
}

public ParseConfig withDateTimeSeparators(char... allowed)
{
assertChars(allowed);
return new ParseConfig(allowed, allowedFractionSeparators);
return new ParseConfig(allowed, allowedFractionSeparators, failOnTrailingJunk);
}

private void assertChars(char[] allowed)
Expand All @@ -54,7 +58,17 @@ private void assertChars(char[] allowed)
public ParseConfig withFractionSeparators(char... allowed)
{
assertChars(allowed);
return new ParseConfig(allowedDateTimeSeparators, allowed);
return new ParseConfig(allowedDateTimeSeparators, allowed, failOnTrailingJunk);
}

public ParseConfig failOnTrailingJunk(boolean failOnTrailingJunk)
{
return new ParseConfig(allowedDateTimeSeparators, allowedFractionSeparators, failOnTrailingJunk);
}

public boolean isFailOnTrailingJunk()
{
return failOnTrailingJunk;
}

public char[] getDateTimeSeparators()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void testAll(TestParam param)
TemporalAccessor result;
if (param.isLenient())
{
result = ITU.parseLenient(param.getInput());
result = ITU.parseLenient(param.getInput(), param.getConfig() != null ? param.getConfig() : ParseConfig.DEFAULT);
}
else
{
Expand Down
21 changes: 19 additions & 2 deletions src/test/java/com/ethlo/time/TestParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ public class TestParam
private final int errorIndex;
private final Instant expected;
private final String note;
private final ParseConfig config;

@ConstructorProperties(value = {"input", "lenient", "error", "error_index", "expected", "note"})
public TestParam(String input, boolean lenient, String error, Integer errorIndex, String expected, String note)
@ConstructorProperties(value = {"input", "lenient", "error", "error_index", "expected", "note", "config"})
public TestParam(String input, boolean lenient, String error, Integer errorIndex, String expected, String note, SerializableParseConfig config)
{
this.input = input;
this.lenient = lenient;
this.error = error;
this.errorIndex = errorIndex != null ? errorIndex : -1;
this.expected = parseExpected(expected);
this.note = note;
this.config = config != null ? config : ParseConfig.DEFAULT;
}

private Instant parseExpected(String expected)
Expand Down Expand Up @@ -86,6 +88,7 @@ public String toString()
", expected=" + expected + '\'' +
", error='" + error + '\'' +
", errorOffset=" + errorIndex + '\'' +
", config=" + config + '\'' +
", note=" + note +
'}';
}
Expand All @@ -99,4 +102,18 @@ public Instant getExpected()
{
return expected;
}

public ParseConfig getConfig()
{
return config;
}

public static class SerializableParseConfig extends ParseConfig
{
@ConstructorProperties({"allowed_date_separators", "allowed_fraction_separators", "fail_on_trailing_junk"})
protected SerializableParseConfig(final char[] allowedDateTimeSeparators, final char[] allowedFractionSeparators, final boolean failOnTrailingJunk)
{
super(allowedDateTimeSeparators, allowedFractionSeparators, failOnTrailingJunk);
}
}
}
21 changes: 21 additions & 0 deletions src/test/resources/test-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,27 @@
{
"input": "2020-12-31T00:00:00-05:00 x",
"error": "Trailing junk data after position 27: 2020-12-31T00:00:00-05:00 x",
"config": {
"fail_on_trailing_junk": true
},
"lenient": true
},
{
"input": "2020-12-31T00:00:00-05:00 x",
"expected": "1609390800,0",
"config": {
"fail_on_trailing_junk": false
},
"lenient": true
},
{
"input": "2020-12-31|20:22:12,546464-05:00 x",
"expected": "1609464132,546464000",
"config": {
"fail_on_trailing_junk": false,
"allowed_date_separators": "|Tt ",
"allowed_fraction_separators": ","
},
"lenient": true
},
{
Expand Down

0 comments on commit 7c07945

Please sign in to comment.