Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional validation #301

Merged
merged 2 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ static final Expr.Parsed makeTimeLiteral(Token token, Token timeZone) {
boolean positive = timeZone.image.charAt(0) == '+';
int tzHour = Integer.parseInt(timeZone.image.substring(1, 3));
int tzMinute = Integer.parseInt(timeZone.image.substring(4, 6));

if (tzHour > 23 || tzMinute > 59) {
throw new ParsingFailure("Invalid temporal literal");
}

int seconds = tzHour * 60 + tzMinute;
value = positive ? seconds : -seconds;
} else {
Expand Down Expand Up @@ -212,6 +217,11 @@ static final Expr.Parsed makeDateTimeLiteral(Token token, Token timeZone) {
boolean positive = timeZone.image.charAt(0) == '+';
int tzHour = Integer.parseInt(timeZone.image.substring(1, 3));
int tzMinute = Integer.parseInt(timeZone.image.substring(4, 6));

if (tzHour > 23 || tzMinute > 59) {
throw new ParsingFailure("Invalid temporal literal");
}

int seconds = tzHour * 60 + tzMinute;
value = positive ? seconds : -seconds;
} else {
Expand Down
23 changes: 14 additions & 9 deletions tests/src/test/scala/org/dhallj/tests/MiscSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,21 @@ class MiscSuite extends ScalaCheckSuite {
val parts = asString.split("-")

// We need a reasonable four-digit year.
val cleaned = if (parts(0).isEmpty) {
// If it's negative we just make something up.
"2021" + "-" + parts.drop(2).mkString("-")
} else if (parts(0).size != 4) {
// If it's not four digits we just make something up.
"1999" + "-" + parts.drop(1).mkString("-")
} else {
asString
}
val cleaned = (
if (parts(0).isEmpty) {
// If it's negative we just make something up.
"2021" + "-" + parts.drop(2).mkString("-")
} else if (parts(0).size != 4) {
// If it's not four digits we just make something up.
"1999" + "-" + parts.drop(1).mkString("-")
} else {
asString
}
// The ScalaCheck instance produces instants that can't be parsed.
).replaceAll("-02-29", "-02-28")

val expected = Instant.parse(cleaned)

val result = Decode.decode(DhallParser.parse(cleaned).getEncodedBytes).toString

// Keep it low-tech.
Expand Down