Skip to content
This repository has been archived by the owner on Aug 2, 2020. It is now read-only.

Commit

Permalink
Refactor Selftest, add more tests for matchVersionedFilePath.
Browse files Browse the repository at this point in the history
  • Loading branch information
snowleopard committed Feb 16, 2016
1 parent a849c93 commit 8ae1c56
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions src/Rules/Selftest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,42 @@ instance Arbitrary Way where
instance Arbitrary WayUnit where
arbitrary = arbitraryBoundedEnum

test :: Testable a => a -> Action ()
test = liftIO . quickCheck

selftestRules :: Rules ()
selftestRules =
"selftest" ~> do
test $ \(x :: Way) -> read (show x) == x
test $ \n xs ->
let res = chunksOfSize n xs
in concat res == xs && all (\r -> length r == 1 || length (concat r) <= n) res
test $ chunksOfSize 3 ["a","b","c","defg","hi","jk"] == [["a","b","c"],["defg"],["hi"],["jk"]]

test $ matchVersionedFilePath "foo/bar" ".a" "foo/bar.a" == True
test $ matchVersionedFilePath "foo/bar" ".a" "foo\\bar.a" == True
test $ matchVersionedFilePath "foo/bar" "a" "foo/bar.a" == True
test $ matchVersionedFilePath "foo/bar" "" "foo/bar.a" == False
test $ matchVersionedFilePath "foo/bar" "a" "foo/bar-0.1.a" == True
test $ matchVersionedFilePath "foo/bar-" "a" "foo/bar-0.1.a" == True
test $ matchVersionedFilePath "foo/bar/" "a" "foo/bar-0.1.a" == False

-- TODO: add automated tests for matchVersionedFilePath too

test :: Testable a => a -> Action ()
test = liftIO . quickCheck
testWays
testChunksOfSize
testMatchVersionedFilePath

testWays :: Action ()
testWays = do
putBuild $ "==== Read Way, Show Way"
test $ \(x :: Way) -> read (show x) == x

testChunksOfSize :: Action ()
testChunksOfSize = do
putBuild $ "==== chunksOfSize"
test $ chunksOfSize 3 [ "a", "b", "c" , "defg" , "hi" , "jk" ]
== [ ["a", "b", "c"], ["defg"], ["hi"], ["jk"] ]
test $ \n xs ->
let res = chunksOfSize n xs
in concat res == xs && all (\r -> length r == 1 || length (concat r) <= n) res

testMatchVersionedFilePath :: Action ()
testMatchVersionedFilePath = do
putBuild $ "==== matchVersionedFilePath"
test $ matchVersionedFilePath "foo/bar" ".a" "foo/bar.a" == True
test $ matchVersionedFilePath "foo/bar" ".a" "foo\\bar.a" == False
test $ matchVersionedFilePath "foo/bar" "a" "foo/bar.a" == True
test $ matchVersionedFilePath "foo/bar" "" "foo/bar.a" == False
test $ matchVersionedFilePath "foo/bar" "a" "foo/bar-0.1.a" == True
test $ matchVersionedFilePath "foo/bar-" "a" "foo/bar-0.1.a" == True
test $ matchVersionedFilePath "foo/bar/" "a" "foo/bar-0.1.a" == False

test $ \prefix suffix -> forAll versions $ \version ->
matchVersionedFilePath prefix suffix (prefix ++ version ++ suffix)
where
versions = listOf . elements $ '-' : '.' : ['0'..'9']

1 comment on commit 8ae1c56

@snowleopard
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #197.

Please sign in to comment.