Skip to content

Commit

Permalink
refactored test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
soenkehahn committed Aug 20, 2015
1 parent 105556f commit 92c4b1a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
39 changes: 13 additions & 26 deletions test/GraphSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import Graph
import Helper
import Ast

getDeadNames :: IO [String]
getDeadNames = do
ast <- eitherToError $ parse ["Foo.hs"]
let graph = usedTopLevelNames ast
roots <- eitherToError $ return $
findExports ast [mkModuleName "Foo"]
return $ fmap showName $ deadNames graph roots

spec :: Spec
spec = do
describe "deadNames" $ do
Expand All @@ -19,12 +27,7 @@ spec = do
foo = ()
bar = ()
|] $ do
-- fixme: refactor this:
Right ast <- parse ["Foo.hs"]
let graph = usedTopLevelNames ast
Right roots = findExports ast [mkModuleName "Foo"]
fmap showName (deadNames graph roots)
`shouldBe` ["Foo.bar"]
getDeadNames `shouldReturn` ["Foo.bar"]

it "allows to specify multiple roots" $ do
withFoo [i|
Expand All @@ -35,11 +38,7 @@ spec = do
bar = ()
baz = ()
|] $ do
Right ast <- parse ["Foo.hs"]
let graph = usedTopLevelNames ast
Right roots = findExports ast [mkModuleName "Foo"]
fmap showName (deadNames graph roots)
`shouldBe` ["Foo.baz"]
getDeadNames `shouldReturn` ["Foo.baz"]

it "detects usage of names in instance methods" $ do
withFoo [i|
Expand All @@ -49,11 +48,7 @@ spec = do
show A = foo
foo = "foo"
|] $ do
Right ast <- parse ["Foo.hs"]
let graph = usedTopLevelNames ast
Right roots = findExports ast [mkModuleName "Foo"]
fmap showName (deadNames graph roots)
`shouldBe` []
getDeadNames `shouldReturn` []

it "returns dead names in topological order" $ do
withFoo [i|
Expand All @@ -62,11 +57,7 @@ spec = do
a = b
c = ()
|] $ do
Right ast <- parse ["Foo.hs"]
let graph = usedTopLevelNames ast
Right roots = findExports ast [mkModuleName "Foo"]
fmap showName (deadNames graph roots)
`shouldBe` (words "Foo.a Foo.b Foo.c")
getDeadNames `shouldReturn` (words "Foo.a Foo.b Foo.c")

it "finds used names in default implementations of methods in class declarations" $ do
withFoo [i|
Expand All @@ -76,8 +67,4 @@ spec = do
a _ = foo
foo = "foo"
|] $ do
Right ast <- parse ["Foo.hs"]
let graph = usedTopLevelNames ast
Right roots = findExports ast [mkModuleName "Foo"]
fmap showName (deadNames graph roots)
`shouldBe` []
getDeadNames `shouldReturn` []
4 changes: 4 additions & 0 deletions test/Helper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ swallowExceptions = handle (\ (_ :: SomeException) -> return ())

usageGraph :: Graph a -> [(a, [a])]
usageGraph = _usageGraph

eitherToError :: IO (Either String a) -> IO a
eitherToError action = do
either die return =<< action

0 comments on commit 92c4b1a

Please sign in to comment.