Skip to content

Commit

Permalink
added missing tests for localdate and code fix for decodeLocalDate
Browse files Browse the repository at this point in the history
  • Loading branch information
osborneMs committed Jun 15, 2023
1 parent de3a5c5 commit 86bc0bd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/Morphir/Value/Native.elm
Original file line number Diff line number Diff line change
Expand Up @@ -420,18 +420,22 @@ encodeLocalDate localDate =

{-| -}
decodeLocalDate : Decoder LocalDate
decodeLocalDate _ value =
case value of
Value.Apply () (Value.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "from", "i", "s", "o" ] )) (Value.Literal () (StringLiteral str)) ->
case LocalDate.fromISO str of
Just localDate ->
Ok localDate

Nothing ->
Err <| ErrorWhileEvaluatingDerivedType ("Invalid ISO format: " ++ str)

_ ->
Err (ExpectedDerivedType ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "local", "date" ] ) value)
decodeLocalDate e value =
e value
|> Result.andThen
(\v ->
case v of
Value.Apply () (Value.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "from", "i", "s", "o" ] )) (Value.Literal () (StringLiteral str)) ->
case LocalDate.fromISO str of
Just localDate ->
Ok localDate

Nothing ->
Err <| ErrorWhileEvaluatingDerivedType ("Invalid ISO format: " ++ str)

_ ->
Err (ExpectedDerivedType ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "local", "date" ] ) value)
)


{-| -}
Expand Down
35 changes: 35 additions & 0 deletions tests/Morphir/SDK/LocalDateTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,41 @@ mathTests =
Date.fromCalendarDate 2020 Feb 1
|> LocalDate.addYears -1
|> Expect.equal (Date.fromCalendarDate 2019 Feb 1)
, test "fromISO string to localDate" <|
\_ ->
LocalDate.fromISO "2023-06-13"
|> Maybe.withDefault (Date.fromCalendarDate 2019 Feb 1)
|> Expect.equal (Date.fromCalendarDate 2023 Jun 13)
, test "toISOString" <|
\_ ->
Date.fromCalendarDate 2023 Jun 13
|> LocalDate.toISOString
|> Expect.equal "2023-06-13"
, test "fromParts" <|
\_ ->
LocalDate.fromParts 2023 6 9
|> Maybe.withDefault (Date.fromCalendarDate 2019 Feb 1)
|> Expect.equal (Date.fromCalendarDate 2023 Jun 9)
, test "diffInDays" <|
\_ ->
Date.fromCalendarDate 2023 Jun 19
|> LocalDate.diffInDays (Date.fromCalendarDate 2023 Jun 9)
|> Expect.equal 10
, test "diffInWeeks" <|
\_ ->
Date.fromCalendarDate 2023 Jun 19
|> LocalDate.diffInWeeks (Date.fromCalendarDate 2023 Jun 9)
|> Expect.equal 1
, test "diffInMonths" <|
\_ ->
Date.fromCalendarDate 2023 Jun 19
|> LocalDate.diffInMonths (Date.fromCalendarDate 2023 Jun 9)
|> Expect.equal 0
, test "diffInYears" <|
\_ ->
Date.fromCalendarDate 2024 Jun 19
|> LocalDate.diffInYears (Date.fromCalendarDate 2023 Jun 9)
|> Expect.equal 1
]


Expand Down

0 comments on commit 86bc0bd

Please sign in to comment.