Skip to content

Commit

Permalink
Use Parser.dateExpr for date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Entea committed Sep 7, 2016
1 parent e45b772 commit 1e87401
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
3 changes: 3 additions & 0 deletions base/src/main/scala/co/uproot/abandon/Ast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class InputFileNotFoundError(fileName:String) extends InputError("File not found
class ConstraintError(msg: String) extends RuntimeException(msg)

object Date {
val MIN: Date = Date(0, 1, 1)
val MAX: Date = Date(9999, 1, 1)

val yearMultiplier = 10000
val monthMultiplier = 100

Expand Down
15 changes: 7 additions & 8 deletions base/src/main/scala/co/uproot/abandon/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ object SettingsHelper {
}

def makeDateConstraints(config: Config): DateConstraint = {
val format = DateTimeFormatter.ISO_LOCAL_DATE
val from = AbandonParser.dateExpr(ParserHelper.scanner(config.getString("from"))).map(Some(_)).getOrElse(None)
val to = AbandonParser.dateExpr(ParserHelper.scanner(config.getString("to"))).map(Some(_)).getOrElse(None)

val from = Try(LocalDate.parse(config.getString("from"), format)).map(Some(_)).getOrElse(None)
val to = Try(LocalDate.parse(config.getString("to"), format)).map(Some(_)).getOrElse(None)
DateConstraint(from, to)
}

Expand Down Expand Up @@ -217,14 +216,14 @@ case class NegativeConstraint(val accName: String) extends Constraint with SignC
val signStr = "negative"
}

case class DateConstraint(dateFrom: Option[LocalDate], dateTo: Option[LocalDate]) extends Constraint {
case class DateConstraint(dateFrom: Option[Date], dateTo: Option[Date]) extends Constraint {
override def check(appState: AppState): Boolean = {
appState.accState.posts.find(post => {
val from = dateFrom.getOrElse(LocalDate.MIN)
val to = dateTo.getOrElse(LocalDate.MAX)
val date = LocalDate.of(post.date.year, post.date.month, post.date.day)
val from = dateFrom.getOrElse(Date.MIN)
val to = dateTo.getOrElse(Date.MAX)
val date = post.date

from.isAfter(date) || to.isBefore(date)
DateOrdering.compare(from, date) > 0 || DateOrdering.compare(to, date) < 0
}).foreach(post => {
throw new ConstraintError(
s"${post.name} is not in date range of " +
Expand Down
6 changes: 4 additions & 2 deletions base/src/main/scala/co/uproot/abandon/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ object AbandonParser extends StandardTokenParsers with PackratParsers {
private lazy val conditionExpr = (numericExpr ~ comparisonExpr ~ numericExpr) ^^ { case (e1 ~ op ~ e2) => ConditionExpr(e1, op, e2)}
private lazy val comparisonExpr: PackratParser[String] = ((">" | "<" | "=") ~ "=" ^^ {case (o1 ~ o2) => o1 + o2}) | ">" | "<"

private lazy val compactTxFrag = (("." ~> (dateFrag | isoDateFrag) ~ accountName ~ numericExpr ~ eolComment) ^^ {
private lazy val compactTxFrag = (("." ~> dateExpr ~ accountName ~ numericExpr ~ eolComment) ^^ {
case date ~ accountName ~ amount ~ optComment =>
Transaction(date, List(Post(accountName, Option(amount), optComment)), None, None, Nil)
})

private lazy val txFrag = (((dateFrag | isoDateFrag) ~ (annotation?) ~ (payee?)) <~ eol) ~ (eolComment*) ~ (post+) ^^ {
private lazy val txFrag = ((dateExpr ~ (annotation?) ~ (payee?)) <~ eol) ~ (eolComment*) ~ (post+) ^^ {
case date ~ annotationOpt ~ optPayee ~ optComment ~ posts =>
val annotationStrOpt = annotationOpt.map(_.mkString(""))
Transaction(date, posts, annotationStrOpt, optPayee, optComment.flatten)
Expand All @@ -230,6 +230,8 @@ object AbandonParser extends StandardTokenParsers with PackratParsers {
case name ~ amount ~ commentOpt => Post(name, amount, commentOpt)
}

lazy val dateExpr = dateFrag | isoDateFrag

lazy val isoDateFrag = ((((integer <~ "-") ~ integer) <~ "-") ~ integer) ^? ({
case y ~ m ~ d if (isValidDate(y, m, d)) =>
Date(y, m, d)
Expand Down
18 changes: 9 additions & 9 deletions base/src/test/scala/co/uproot/abandon/DateConstraintTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@ class DateConstraintTest extends FlatSpec with Matchers with BeforeAndAfterEach
var appState: AppState = null

override def beforeEach() {
setupMocks
setupMocks()
}

it should "return true on valid posts" in {
val constraint = new DateConstraint(
Some(LocalDate.of(2013, Month.JANUARY, 1)),
Some(LocalDate.of(2013, Month.DECEMBER, 31))
Some(Date(2013, 1, 1)),
Some(Date(2013, 12, 31))
)

constraint.check(appState) should be(true)
}

it should "throw exception on end date violation" in {
val constraint = new DateConstraint(
Some(LocalDate.of(2013, Month.JANUARY, 1)),
Some(LocalDate.of(2013, Month.NOVEMBER, 1))
Some(Date(2013, 1, 1)),
Some(Date(2013, 11, 1))
)
an [ConstraintError] should be thrownBy constraint.check(appState)

val constraintWithoutFrom = new DateConstraint(None, Some(LocalDate.of(2013, Month.NOVEMBER, 1)))
val constraintWithoutFrom = new DateConstraint(None, Some(Date(2013, 11, 1)))
an [ConstraintError] should be thrownBy constraintWithoutFrom.check(appState)
}

it should "throw exception on start date violation" in {
val constraint = new DateConstraint(
Some(LocalDate.of(2013, Month.JUNE, 1)),
Some(LocalDate.of(2013, Month.NOVEMBER, 1))
Some(Date(2013, 6, 1)),
Some(Date(2013, 11, 1))
)
an [ConstraintError] should be thrownBy constraint.check(appState)

val constraintWithoutTo = new DateConstraint(Some(LocalDate.of(2013, Month.NOVEMBER, 1)), None)
val constraintWithoutTo = new DateConstraint(Some(Date(2013, 11, 1)), None)
an [ConstraintError] should be thrownBy constraintWithoutTo.check(appState)
}

Expand Down

0 comments on commit 1e87401

Please sign in to comment.