From cd33a99b8444ce621b8098ea255604c323089351 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 12 Nov 2017 07:35:36 +0300 Subject: [PATCH] Add field step tests --- src/Field.hs | 5 +++++ test/FieldSpec.hs | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Field.hs b/src/Field.hs index ce31d89..2737046 100644 --- a/src/Field.hs +++ b/src/Field.hs @@ -71,3 +71,8 @@ parseSequence text constraint numbers = map read pieces allInRange = all (\x -> x `inRange` constraint) numbers isValid = length pieces >= 2 && isAllNumbers && allInRange + +parseFieldStep :: String -> Maybe Step +parseFieldStep "" = Just Every +parseFieldStep text | isNumber text = Just (Step (read text)) +parseFieldStep _ = Nothing diff --git a/test/FieldSpec.hs b/test/FieldSpec.hs index 6c1dcf1..3cb1736 100644 --- a/test/FieldSpec.hs +++ b/test/FieldSpec.hs @@ -19,6 +19,8 @@ spec = do it "fails constraints" $ parseNumber "10" (Constraint 0 5) `shouldBe` Nothing + -- Field validation + describe "Field can be created from" $ do it "asterisk" $ parseField "*" (Constraint 0 0) `shouldBe` Just (Field All Every) @@ -31,3 +33,16 @@ spec = do it "sequence" $ parseField "1,2,3" (Constraint 0 59) `shouldBe` Just (Field (Sequence [1, 2, 3]) Every) + + -- Field Step validation + + describe "Step can be created from" $ do + it "empty string" $ + parseFieldStep "" `shouldBe` Just Every + + it "number" $ + parseFieldStep "5" `shouldBe` Just (Step 5) + + describe "Step cant'b created from" $ do + it "word" $ + parseFieldStep "hello" `shouldBe` Nothing