-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve exceptions rethrown by unwrapAnnotatedHUnitFailure (#202)
closes #201
- Loading branch information
1 parent
5b40191
commit 3b99f3a
Showing
5 changed files
with
259 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
freckle-app/tests/Freckle/App/Test/Hspec/AnnotatedExceptionSpec.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
module Freckle.App.Test.Hspec.AnnotatedExceptionSpec | ||
( spec | ||
) where | ||
|
||
import Freckle.App.Prelude | ||
|
||
import Data.Annotation (toAnnotation) | ||
import Data.List (intercalate) | ||
import Freckle.App.Exception (AnnotatedException (..)) | ||
import Freckle.App.Test.Hspec.AnnotatedException (annotateHUnitFailure) | ||
import GHC.Exts (fromList) | ||
import GHC.Stack (CallStack, SrcLoc (..)) | ||
import Test.HUnit.Lang (FailureReason (..), HUnitFailure (..)) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "annotateHUnitFailure" $ do | ||
describe "does nothing if there are no annotations" $ do | ||
it "when the failure is Reason" $ | ||
let e = HUnitFailure Nothing (Reason "x") | ||
in annotateHUnitFailure (AnnotatedException [] e) `shouldBe` e | ||
|
||
it "when the failure is ExpectedButGot with no message" $ | ||
let e = HUnitFailure Nothing (ExpectedButGot Nothing "a" "b") | ||
in annotateHUnitFailure (AnnotatedException [] e) `shouldBe` e | ||
|
||
it "when the failure is ExpectedButGot with a message" $ | ||
let e = HUnitFailure Nothing (ExpectedButGot (Just "x") "a" "b") | ||
in annotateHUnitFailure (AnnotatedException [] e) `shouldBe` e | ||
|
||
describe "can show an annotation" $ do | ||
it "when the failure is Reason" $ | ||
annotateHUnitFailure | ||
AnnotatedException | ||
{ annotations = [toAnnotation @Int 56] | ||
, exception = HUnitFailure Nothing (Reason "x") | ||
} | ||
`shouldBe` HUnitFailure | ||
Nothing | ||
( Reason . intercalate "\n" $ | ||
[ "x" | ||
, "" | ||
, "Annotations:" | ||
, "\t * Annotation @Int 56" | ||
] | ||
) | ||
|
||
it "when the failure is ExpectedButGot with no message" $ do | ||
annotateHUnitFailure | ||
AnnotatedException | ||
{ annotations = [toAnnotation @Int 56] | ||
, exception = HUnitFailure Nothing (ExpectedButGot Nothing "a" "b") | ||
} | ||
`shouldBe` HUnitFailure | ||
Nothing | ||
( ExpectedButGot | ||
( Just . intercalate "\n" $ | ||
[ "Annotations:" | ||
, "\t * Annotation @Int 56" | ||
] | ||
) | ||
"a" | ||
"b" | ||
) | ||
|
||
it "when the failure is ExpectedButGot with a message" $ | ||
annotateHUnitFailure | ||
AnnotatedException | ||
{ annotations = [toAnnotation @Int 56] | ||
, exception = HUnitFailure Nothing (ExpectedButGot (Just "x") "a" "b") | ||
} | ||
`shouldBe` HUnitFailure | ||
Nothing | ||
( ExpectedButGot | ||
( Just . intercalate "\n" $ | ||
[ "x" | ||
, "" | ||
, "Annotations:" | ||
, "\t * Annotation @Int 56" | ||
] | ||
) | ||
"a" | ||
"b" | ||
) | ||
|
||
it "can show a stack trace" $ | ||
annotateHUnitFailure | ||
AnnotatedException | ||
{ annotations = | ||
[ toAnnotation @CallStack $ | ||
fromList | ||
[ | ||
( "abc" | ||
, SrcLoc | ||
{ srcLocPackage = "thepackage" | ||
, srcLocModule = "Foo" | ||
, srcLocFile = "src/Foo.hs" | ||
, srcLocStartLine = 7 | ||
, srcLocStartCol = 50 | ||
, srcLocEndLine = 8 | ||
, srcLocEndCol = 23 | ||
} | ||
) | ||
] | ||
] | ||
, exception = HUnitFailure Nothing (Reason "x") | ||
} | ||
`shouldBe` HUnitFailure | ||
Nothing | ||
( Reason . intercalate "\n" $ | ||
[ "x" | ||
, "" | ||
, "CallStack (from HasCallStack):" | ||
, " abc, called at src/Foo.hs:7:50 in thepackage:Foo" | ||
] | ||
) | ||
|
||
it "can show both an annotation and a stack trace" $ | ||
annotateHUnitFailure | ||
AnnotatedException | ||
{ annotations = | ||
[ toAnnotation @Text "Visibility is poor" | ||
, toAnnotation @CallStack $ | ||
fromList | ||
[ | ||
( "abc" | ||
, SrcLoc | ||
{ srcLocPackage = "thepackage" | ||
, srcLocModule = "Foo" | ||
, srcLocFile = "src/Foo.hs" | ||
, srcLocStartLine = 7 | ||
, srcLocStartCol = 50 | ||
, srcLocEndLine = 8 | ||
, srcLocEndCol = 23 | ||
} | ||
) | ||
] | ||
] | ||
, exception = HUnitFailure Nothing (Reason "x") | ||
} | ||
`shouldBe` HUnitFailure | ||
Nothing | ||
( Reason . intercalate "\n" $ | ||
[ "x" | ||
, "" | ||
, "Annotations:" | ||
, "\t * Annotation @Text \"Visibility is poor\"" | ||
, "" | ||
, "CallStack (from HasCallStack):" | ||
, " abc, called at src/Foo.hs:7:50 in thepackage:Foo" | ||
] | ||
) |