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

Commit

Permalink
Document and test encode/decodeModule.
Browse files Browse the repository at this point in the history
See #197, #210.
  • Loading branch information
snowleopard committed Feb 20, 2016
1 parent 903ab6c commit 5e32c91
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,16 @@ versionToInt s = major * 1000 + minor * 10 + patch

-- | Given a module name extract the directory and file name, e.g.:
--
-- > decodeModule "Data.Functor.Identity" = ("Data/Functor/", "Identity")
-- > decodeModule "Data.Functor.Identity" == ("Data/Functor/", "Identity")
-- > decodeModule "Prelude" == ("./", "Prelude")
decodeModule :: String -> (FilePath, String)
decodeModule = splitFileName . replaceEq '.' '/'

-- | Given the directory and file name find the corresponding module name, e.g.:
--
-- > encodeModule "Data/Functor/" "Identity.hs" = "Data.Functor.Identity"
-- > encodeModule "Data/Functor/" "Identity.hs" == "Data.Functor.Identity"
-- > encodeModule "./" "Prelude" == "Prelude"
-- > uncurry encodeModule (decodeModule name) == name
encodeModule :: FilePath -> String -> String
encodeModule dir file = replaceEq '/' '.' $ dir -/- takeBaseName file

Expand Down
14 changes: 14 additions & 0 deletions src/Rules/Selftest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ selftestRules =
testWays
testChunksOfSize
testMatchVersionedFilePath
testModuleNames

testWays :: Action ()
testWays = do
Expand Down Expand Up @@ -54,3 +55,16 @@ testMatchVersionedFilePath = do
matchVersionedFilePath prefix suffix (prefix ++ version ++ suffix)
where
versions = listOf . elements $ '-' : '.' : ['0'..'9']

testModuleNames :: Action ()
testModuleNames = do
putBuild $ "==== Encode/decode module name"
test $ encodeModule "Data/Functor/" "Identity.hs" == "Data.Functor.Identity"
test $ encodeModule "./" "Prelude" == "Prelude"

test $ decodeModule "Data.Functor.Identity" == ("Data/Functor/", "Identity")
test $ decodeModule "Prelude" == ("./", "Prelude")

test $ forAll names $ \n -> uncurry encodeModule (decodeModule n) == n
where
names = intercalate "." <$> listOf1 (listOf1 $ elements "abcABC123_'")

0 comments on commit 5e32c91

Please sign in to comment.