From 86bc0bd10ce9eeecbed51f8892eff68cb5d083d5 Mon Sep 17 00:00:00 2001 From: Osborne Yaw Sapaty Date: Thu, 15 Jun 2023 09:53:02 +0000 Subject: [PATCH] added missing tests for localdate and code fix for decodeLocalDate --- src/Morphir/Value/Native.elm | 28 ++++++++++++---------- tests/Morphir/SDK/LocalDateTests.elm | 35 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/Morphir/Value/Native.elm b/src/Morphir/Value/Native.elm index d5767e58d..2bee3bf3b 100644 --- a/src/Morphir/Value/Native.elm +++ b/src/Morphir/Value/Native.elm @@ -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) + ) {-| -} diff --git a/tests/Morphir/SDK/LocalDateTests.elm b/tests/Morphir/SDK/LocalDateTests.elm index 220c37fd0..7d5e7c65b 100644 --- a/tests/Morphir/SDK/LocalDateTests.elm +++ b/tests/Morphir/SDK/LocalDateTests.elm @@ -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 ]