Skip to content

Commit

Permalink
Additional validation (travisbrown#301)
Browse files Browse the repository at this point in the history
* Additional validation

* Work around another Gen[Instant] issue
  • Loading branch information
travisbrown authored Jul 28, 2021
1 parent 49ffe03 commit c818f34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
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

0 comments on commit c818f34

Please sign in to comment.