Add some modules and tests

This commit is contained in:
2017-11-11 20:51:58 +03:00
parent e57e75a905
commit 67aa89a27c
13 changed files with 247 additions and 77 deletions

20
test/ConstraintSpec.hs Normal file
View File

@ -0,0 +1,20 @@
module ConstraintSpec (main, spec) where
import Test.Hspec
import Constraint
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
describe "Constraint" $ do
it "can be created from number" $
makeRangeFromNumber 10 `shouldBe` Constraint 10 10
it "validate number" $
10 `inRange` (Constraint 0 10) `shouldBe` True
it "validate number" $
10 `inRange` (Constraint 15 20) `shouldBe` False

31
test/FieldSpec.hs Normal file
View File

@ -0,0 +1,31 @@
module FieldSpec (main, spec) where
import Test.Hspec
import Constraint
import Field
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
describe "Number" $ do
it "can be parsed from string" $
parseNumber "10" (Constraint 0 10) `shouldBe` Just 10
it "can't be parsed from string" $
parseNumber "10and10" (Constraint 0 10) `shouldBe` Nothing
it "fails constraints" $
parseNumber "10" (Constraint 0 5) `shouldBe` Nothing
describe "Field" $ do
it "can be created from asterisk" $
parseField "*" (Constraint 0 0) `shouldBe` Just (Field All Every)
it "can be created from number" $
parseField "10" (Constraint 0 10) `shouldBe` Just (Field (Range 10 10) Every)
it "can be created from range" $
parseField "10-20" (Constraint 0 59) `shouldBe` Just (Field (Range 10 20) Every)

23
test/HelperSpec.hs Normal file
View File

@ -0,0 +1,23 @@
module HelperSpec (main, spec) where
import Test.Hspec
import Helper
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
describe "Splitting" $ do
it "can process empty string" $
wordsWhen (== '-') "" `shouldBe` []
it "can process only one word" $
wordsWhen (== '-') "10" `shouldBe` ["10"]
it "can separated by '-'" $
wordsWhen (== '-') "10-20" `shouldBe` ["10", "20"]
it "can be separated by ','" $
wordsWhen (== ',') "10,20,30" `shouldBe` ["10", "20", "30"]

45
test/PatternSpec.hs Normal file
View File

@ -0,0 +1,45 @@
module PatternSpec (main, spec) where
import Test.Hspec
import Data.Dates
import Pattern
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
describe "Cron pattern" $ do
it "createParts" $
length (createParts "* * * * * *") `shouldBe` 6
it "matches fixed time" $
let
pattern = "* * * * * *"
date = DateTime 2017 10 11 0 0 0
in
match pattern date `shouldBe` True
it "matches all minutes" $
let
pattern = "* * * * * *"
dates = [DateTime 2017 10 11 0 i 0 | i <- [0..59]]
in
countMatches pattern dates `shouldBe` 60
it "matches exactly moment" $
let
date = (DateTime 2017 10 11 0 0 0)
pattern = "0 0 11 10 * 2017"
in
match pattern date `shouldBe` True
countMatches :: String -> [DateTime] -> Int
countMatches p xs = sum $ map (f p) xs
where
f p d = case match p d of
True -> 1
False -> 0

View File

@ -1,42 +1 @@
import Test.Hspec
import Test.QuickCheck
import Control.Exception (evaluate)
import Data.Dates
import Lib
main :: IO ()
main = hspec $ do
describe "Cron pattern" $ do
it "createParts" $
length (createParts "* * * * * *") `shouldBe` 6
it "matches fixed time" $
let
pattern = "* * * * * *"
date = DateTime 2017 10 11 0 0 0
in
match pattern date `shouldBe` (True :: Bool)
it "matches all minutes" $
let
pattern = "* * * * * *"
dates = [DateTime 2017 10 11 0 i 0 | i <- [0..59]]
in
countMatches pattern dates `shouldBe` (60 :: Int)
it "matches exactly moment" $
let
date = (DateTime 2017 10 11 0 0 0)
pattern = "0 0 11 10 * 2017"
in
match pattern date `shouldBe` (True :: Bool)
countMatches :: String -> [DateTime] -> Int
countMatches p xs = sum $ map (f p) xs
where
f p d = case match p d of
True -> 1
False -> 0
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}