Skip to content

Commit

Permalink
fix(builtin-function): fix string conversion of years-months-durations
Browse files Browse the repository at this point in the history
* add tests for string conversion of zero-length years-months-duration
* add tests for string conversion of negative years-months-duration
  • Loading branch information
s-frick committed May 9, 2023
1 parent a30a205 commit 38c8e38
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ object ConversionBuiltinFunctions {
.replaceAll("$1$3")
ValString(dateTimeWithOffsetOrZoneId)
}
case List(ValYearMonthDuration(from)) => ValString(from.toString)
case List(duration: ValYearMonthDuration) => ValString(duration.toString)
case List(duration: ValDayTimeDuration) => ValString(duration.toString)
}
)
Expand Down
22 changes: 22 additions & 0 deletions src/main/scala/org/camunda/feel/syntaxtree/Val.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ case class ValDateTime(value: DateTime) extends Val {
}

case class ValYearMonthDuration(value: YearMonthDuration) extends Val {

override def toString: String = {
def makeString(sign: String, year: Long, month: Long): String = {
val y = Option(year).filterNot(_ == 0).map(_ + "Y").getOrElse("")
val m = Option(month).filterNot(_ == 0).map(_ + "M").getOrElse("")

val stringBuilder = new StringBuilder("")
stringBuilder.append(sign).append("P").append(y).append(m)
stringBuilder.toString()
}

val year = value.getYears
val month = value.getMonths % 12

if (year == 0 && month == 0)
"P0Y"
else if (year <= 0 && month <= 0)
makeString("-", -year, -month)
else
makeString("", year, month)
}

override val properties: Map[String, Val] = Map(
"years" -> ValNumber(value.getYears),
"months" -> ValNumber(value.getMonths)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ class BuiltinConversionFunctionsTest
eval(""" string(@"P1DT2H3M4S") """) should be(ValString("P1DT2H3M4S"))
}

it should "convert zero-length years-months-duration" in {

eval(""" string(@"P0Y") """) should be(ValString("P0Y"))
eval(""" string(@"-P0M") """) should be(ValString("P0Y"))
eval(""" string(@"P0Y0M") """) should be(ValString("P0Y"))
}
it should "convert negative years-months-duration" in {

eval(""" string(@"-P1Y") """) should be(ValString("-P1Y"))
eval(""" string(@"-P5M") """) should be(ValString("-P5M"))
eval(""" string(@"-P3Y1M") """) should be(ValString("-P3Y1M"))
}
it should "convert years-months-duration" in {

eval(""" string(@"P1Y") """) should be(ValString("P1Y"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ class DateTimeDurationPropertiesTest

result shouldBe a[ValError]
result.asInstanceOf[ValError].error should startWith(
"No property found with name 'x' of value 'ValYearMonthDuration(P2Y3M)'. Available properties:")
"No property found with name 'x' of value 'P2Y3M'. Available properties:")
}

it should "has properties with @-notation" in {
Expand Down

0 comments on commit 38c8e38

Please sign in to comment.