Add matching and entry point code

This commit is contained in:
2017-11-12 10:00:26 +03:00
parent cd33a99b84
commit 3244ce81f3
6 changed files with 92 additions and 15 deletions

View File

@ -1,5 +1,6 @@
module FieldSpec (main, spec) where
import Foreign.Marshal.Utils (fromBool)
import Test.Hspec
import Constraint
import Field
@ -21,18 +22,18 @@ spec = do
-- Field validation
describe "Field can be created from" $ do
describe "Field Range can be created from" $ do
it "asterisk" $
parseField "*" (Constraint 0 0) `shouldBe` Just (Field All Every)
parseFieldRange "*" (Constraint 0 0) `shouldBe` Just All
it "number" $
parseField "10" (Constraint 0 10) `shouldBe` Just (Field (Range 10 10) Every)
parseFieldRange "10" (Constraint 0 10) `shouldBe` Just (Range 10 10)
it "range" $
parseField "10-20" (Constraint 0 59) `shouldBe` Just (Field (Range 10 20) Every)
parseFieldRange "10-20" (Constraint 0 59) `shouldBe` Just (Range 10 20)
it "sequence" $
parseField "1,2,3" (Constraint 0 59) `shouldBe` Just (Field (Sequence [1, 2, 3]) Every)
parseFieldRange "1,2,3" (Constraint 0 59) `shouldBe` Just (Sequence [1, 2, 3])
-- Field Step validation
@ -46,3 +47,38 @@ spec = do
describe "Step cant'b created from" $ do
it "word" $
parseFieldStep "hello" `shouldBe` Nothing
-- Field validation
describe "Field can be created from" $ do
it "asterisk" $
parseField "*" (Constraint 0 59) `shouldBe` Just (Field All Every)
it "asterisk with step" $ do
parseField "*/5" (Constraint 0 59) `shouldBe` Just (Field All (Step 5))
it "number with step" $ do
parseField "10/5" (Constraint 0 59) `shouldBe` Just (Field (Range 10 10) (Step 5))
it "range with step" $ do
parseField "0-59/5" (Constraint 0 59) `shouldBe` Just (Field (Range 0 59) (Step 5))
it "sequence with step" $ do
parseField "1,3,4/5" (Constraint 0 59) `shouldBe` Just (Field (Sequence [1, 3, 4]) (Step 5))
-- Field match
describe "Field can match" $ do
it "number" $
count (Field All Every) [0..59] `shouldBe` 60
it "range" $
count (Field (Range 10 20) Every) [0..59] `shouldBe` 11
it "range" $
count (Field All (Step 10)) [0..59] `shouldBe` 6
count :: Field -> [Int] -> Int
count field values = sum $ map m values
where m = fromBool . matchField field

View File

@ -40,6 +40,6 @@ spec = do
countMatches :: String -> [DateTime] -> Int
countMatches p xs = sum $ map (f p) xs
where
f p d = case match p d of
f x d = case match x d of
True -> 1
False -> 0