tracks/haskell/exercises/luhn/test/Tests.hs in trackler-2.1.0.11 vs tracks/haskell/exercises/luhn/test/Tests.hs in trackler-2.1.0.12

- old
+ new

@@ -1,79 +1,77 @@ -{-# OPTIONS_GHC -fno-warn-type-defaults #-} +{-# LANGUAGE RecordWildCards #-} +import Data.Foldable (for_) import Test.Hspec (Spec, describe, it, shouldBe) import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) -import Luhn (addends, checkDigit, checksum, create, isValid) +import Luhn (isValid) main :: IO () main = hspecWith defaultConfig {configFastFail = True} specs specs :: Spec -specs = describe "luhn" $ do - describe "standard tests" $ do +specs = describe "valid" $ for_ cases test + where + test Case{..} = it description $ isValid input `shouldBe` expected - it "check digit" $ - checkDigit 34567 `shouldBe` 7 +data Case = Case { description :: String + , input :: String + , expected :: Bool + } - it "check digit with input ending in zero" $ - checkDigit 91370 `shouldBe` 0 - - it "check addends" $ - addends 12121 `shouldBe` [1, 4, 1, 4, 1] - - it "check too large addends" $ - addends 8631 `shouldBe` [7, 6, 6, 1] - - -- The reference test cases expect the checksum function to return - -- the simple sum of the transformed digits, not their `mod 10` sum. - -- In this track, we insist on the `mod 10`. :) - - it "checksum" $ - checksum 4913 `shouldBe` 2 -- The reference test expects 22. - - it "checksum of larger number" $ - checksum 201773 `shouldBe` 1 -- The reference test expects 21. - - it "check invalid number" $ - isValid 738 `shouldBe` False - - it "check valid number" $ - isValid 8739567 `shouldBe` True - - it "create valid number" $ - create 123 `shouldBe` 1230 - - it "create larger valid number" $ - create 873956 `shouldBe` 8739567 - - it "create even larger valid number" $ - create 837263756 `shouldBe` 8372637564 - - describe "track-specific tests" $ do - - -- This track has some tests that were not included in the - -- reference test cases from `exercism/x-common/leap.json`. - - it "checksum 1111" $ - checksum 1111 `shouldBe` 6 - - it "checksum 8763" $ - checksum 8763 `shouldBe` 0 - - it "checksum 8739567" $ - checksum 8739567 `shouldBe` 0 - - it "checksum 2323200577663554" $ - checksum 2323200577663554 `shouldBe` 0 - - it "isValid 1111" $ - isValid 1111 `shouldBe` False - - it "isValid 8763" $ - isValid 8763 `shouldBe` True - - it "isValid 2323200577663554" $ - isValid 2323200577663554 `shouldBe` True - - it "create 232320057766355" $ - create 232320057766355 `shouldBe` 2323200577663554 +cases :: [Case] +cases = [ Case { description = "single digit strings can not be valid" + , input = "1" + , expected = False + } + , Case { description = "A single zero is invalid" + , input = "0" + , expected = False + } + , Case { description = "a simple valid SIN that remains valid if reversed" + , input = "059" + , expected = True + } + , Case { description = "a simple valid SIN that becomes invalid if reversed" + , input = "59" + , expected = True + } + , Case { description = "a valid Canadian SIN" + , input = "055 444 285" + , expected = True + } + , Case { description = "invalid Canadian SIN" + , input = "055 444 286" + , expected = False + } + , Case { description = "invalid credit card" + , input = "8273 1232 7352 0569" + , expected = False + } +-- This track is not testing these cases, since we would rather focus on the algorithm, +-- and because it seems strange to be unable to distinguish between well-formed invalid input and malformed input. +-- , Case { description = "valid strings with a non-digit included become invalid" +-- , input = "055a 444 285" +-- , expected = False +-- } +-- , Case { description = "valid strings with punctuation included become invalid" +-- , input = "055-444-285" +-- , expected = False +-- } +-- , Case { description = "valid strings with symbols included become invalid" +-- , input = "055£ 444$ 285" +-- , expected = False +-- } + , Case { description = "single zero with space is invalid" + , input = " 0" + , expected = False + } + , Case { description = "more than a single zero is valid" + , input = "0000 0" + , expected = True + } + , Case { description = "input digit 9 is correctly converted to output digit 9" + , input = "091" + , expected = True + } + ]