Cover our utility functons with unit tests
This commit is contained in:
parent
a56355b1f9
commit
a102423515
3 changed files with 36 additions and 1 deletions
|
|
@ -67,6 +67,7 @@
|
||||||
nixfmt
|
nixfmt
|
||||||
hpack
|
hpack
|
||||||
hlint
|
hlint
|
||||||
|
haskellPackages.doctest
|
||||||
];
|
];
|
||||||
inputsFrom = builtins.attrValues self.packages.${system};
|
inputsFrom = builtins.attrValues self.packages.${system};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,30 @@
|
||||||
-- | Internal module in order to facilitate testability.
|
-- | Internal module in order to facilitate testability.
|
||||||
module HCat.Internal where
|
module HCat.Internal where
|
||||||
|
|
||||||
|
-- | @parseArgs@ takes a list of strings and returns a single FilePath if there was exactly one element.
|
||||||
|
--
|
||||||
|
-- >>> parseArgs ["foo"]
|
||||||
|
-- Right "foo"
|
||||||
parseArgs :: [String] -> Either String FilePath
|
parseArgs :: [String] -> Either String FilePath
|
||||||
parseArgs args = case args of
|
parseArgs args = case args of
|
||||||
[] -> Left "No filename given!"
|
[] -> Left "No filename given!"
|
||||||
[arg] -> Right arg
|
[arg] -> Right arg
|
||||||
_ -> Left "Only a single file is supported"
|
_ -> Left "Only a single file is supported"
|
||||||
|
|
||||||
|
-- | @chunksOf n@ splits a list into chunks of at most @n@ items.
|
||||||
|
--
|
||||||
|
-- >>> chunksOf 3 "abcdefgh"
|
||||||
|
-- ["abc","def","gh"]
|
||||||
|
--
|
||||||
|
-- >>> chunksOf 0 "abcdefgh"
|
||||||
|
-- []
|
||||||
|
--
|
||||||
|
-- >>> chunksOf (-1) "abcdefgh"
|
||||||
|
-- []
|
||||||
|
chunksOf :: Int -> [a] -> [[a]]
|
||||||
|
chunksOf _ [] = []
|
||||||
|
chunksOf n xs@(_ : _)
|
||||||
|
| n <= 0 = []
|
||||||
|
| otherwise =
|
||||||
|
let (chunk, rest) = splitAt n xs
|
||||||
|
in chunk : chunksOf n rest
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,11 @@
|
||||||
|
|
||||||
module HCatSpec (spec) where
|
module HCatSpec (spec) where
|
||||||
|
|
||||||
import HCat.Internal (parseArgs)
|
import HCat.Internal (chunksOf, parseArgs)
|
||||||
|
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
|
import Test.Hspec.QuickCheck (prop)
|
||||||
|
import Test.QuickCheck (Positive (getPositive))
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = do
|
spec = do
|
||||||
|
|
@ -13,3 +15,13 @@ spec = do
|
||||||
parseArgs ["foo"] `shouldBe` Right "foo"
|
parseArgs ["foo"] `shouldBe` Right "foo"
|
||||||
parseArgs [] `shouldBe` Left "No filename given!"
|
parseArgs [] `shouldBe` Left "No filename given!"
|
||||||
parseArgs ["foo", "bar"] `shouldBe` Left "Only a single file is supported"
|
parseArgs ["foo", "bar"] `shouldBe` Left "Only a single file is supported"
|
||||||
|
describe "chunksOf" $ do
|
||||||
|
prop "each chunk contains at most N items" $ \n xs ->
|
||||||
|
let chunkLengths = length <$> chunksOf n (xs :: [Int])
|
||||||
|
in case chunkLengths of
|
||||||
|
[] -> pure ()
|
||||||
|
ns -> maximum ns `shouldSatisfy` (<= n)
|
||||||
|
|
||||||
|
prop "the sum of all lengths is equal to the length of the input" $ \n xs ->
|
||||||
|
let chunkLengths = length <$> chunksOf (getPositive n) (xs :: [Int])
|
||||||
|
in sum chunkLengths `shouldBe` length xs
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue