2021-08-19 23:09:35 +00:00
|
|
|
module Homework.Ch01.CreditCardsSpec where
|
2021-08-12 22:56:01 +00:00
|
|
|
|
2021-08-19 23:09:35 +00:00
|
|
|
import Homework.Ch01.CreditCards
|
2021-08-12 22:56:01 +00:00
|
|
|
import Test.Hspec
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = describe "Credit Card Validation" $ do
|
|
|
|
describe "toDigits" $ do
|
|
|
|
context "n is less than 1" $ do
|
|
|
|
it "returns an empty list" $ do
|
|
|
|
toDigits 0 `shouldBe` []
|
|
|
|
|
|
|
|
context "n contains a single digit" $ do
|
|
|
|
it "creates a list with one digit" $ do
|
|
|
|
toDigits 9 `shouldBe` [9]
|
|
|
|
|
|
|
|
context "n contains more than one digit" $ do
|
|
|
|
it "creates a list of digits" $ do
|
|
|
|
toDigits 12345 `shouldBe` [1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
describe "toDigitsRev" $ do
|
|
|
|
context "n is less than 1" $ do
|
|
|
|
it "returns an empty list" $ do
|
|
|
|
toDigitsRev 0 `shouldBe` []
|
|
|
|
|
|
|
|
context "n contains a single digit" $ do
|
|
|
|
it "creates a list with one digit" $ do
|
|
|
|
toDigitsRev 9 `shouldBe` [9]
|
|
|
|
|
|
|
|
context "n contains more than one digit" $ do
|
|
|
|
it "creates a list of the digits in reverse order" $ do
|
|
|
|
toDigitsRev 12345 `shouldBe` [5, 4, 3, 2, 1]
|
|
|
|
|
|
|
|
describe "doubleEveryOther" $ do
|
|
|
|
context "list length is odd" $ do
|
|
|
|
it "does not double the last digit" $ do
|
|
|
|
doubleEveryOther [1, 2, 3, 4, 5] `shouldBe` [1, 4, 3, 8, 5]
|
|
|
|
|
|
|
|
context "list length is even" $ do
|
|
|
|
it "doubles every other digit" $ do
|
|
|
|
doubleEveryOther [1, 2, 3, 4, 5, 6] `shouldBe` [1, 4, 3, 8, 5, 12]
|
|
|
|
|
|
|
|
describe "sumDigits" $ do
|
|
|
|
it "splits doubled digits into single digits and sums all single digits together" $ do
|
|
|
|
sumDigits [16, 7, 12, 5] `shouldBe` 22
|
|
|
|
|
2021-08-13 20:03:19 +00:00
|
|
|
context "validators" $ do
|
|
|
|
let validators =
|
|
|
|
[ ("validateWithParens", validateWithParens),
|
|
|
|
("validateWithDollars", validateWithDollars),
|
|
|
|
("validateWithCompose", validateWithCompose)
|
|
|
|
]
|
|
|
|
|
|
|
|
accept num = (num, True)
|
|
|
|
reject num = (num, False)
|
|
|
|
labelExpectation num expectIsValid
|
|
|
|
| expectIsValid = "accepts " ++ show num
|
|
|
|
| otherwise = "rejects " ++ show num
|
|
|
|
|
|
|
|
nums =
|
|
|
|
[ accept 5105105105105100,
|
|
|
|
accept 2223577120017656,
|
|
|
|
accept 371449635398431,
|
|
|
|
accept 6011000990139424,
|
|
|
|
accept 30569309025904,
|
|
|
|
accept 3566002020360505,
|
|
|
|
reject 5105105105105101,
|
|
|
|
reject 5420933878724339,
|
|
|
|
reject 5506923616306249,
|
|
|
|
reject 3999292939485618
|
|
|
|
]
|
|
|
|
|
|
|
|
runExpectation validate' num expectedResult =
|
|
|
|
it (labelExpectation num expectedResult) $ do
|
|
|
|
validate' num `shouldBe` expectedResult
|
|
|
|
|
|
|
|
runValidator label validate' =
|
|
|
|
describe label $ do
|
|
|
|
sequence_ (uncurry (runExpectation validate') <$> nums)
|
|
|
|
|
|
|
|
sequence_ (uncurry runValidator <$> validators)
|