Skip to content

Commit c42f38b

Browse files
committed
Improved error reporting in tests
1 parent f15f4fa commit c42f38b

File tree

3 files changed

+51
-38
lines changed

3 files changed

+51
-38
lines changed

lambda-buffers-compiler/lambda-buffers-compiler.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ test-suite tests
153153
hs-source-dirs: test
154154
main-is: Test.hs
155155
build-depends:
156-
, containers
157-
, generic-lens
156+
, containers >=0.6
157+
, generic-lens >=2.2
158158
, hedgehog
159159
, lambda-buffers-compiler
160160
, lambda-buffers-compiler-pb >=0.1

lambda-buffers-compiler/test/Test/LambdaBuffers/Compiler.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Test.Tasty.Hedgehog (testProperty)
1616
test :: TestTree
1717
test =
1818
testGroup
19-
"Compiler Proto API tests"
19+
"Compiler API tests"
2020
[ allCorrectCompInpCompile
2121
, allCorrectCompInpCompileAfterBenignMut
2222
]
@@ -40,10 +40,11 @@ allCorrectCompInpCompileAfterBenignMut =
4040
[ Mut.shuffleModules
4141
, Mut.shuffleTyDefs
4242
]
43-
(compInp', _) <- H.forAllWith (const "mutation") (Mut.mutFn mut compInp)
43+
compInp' <- H.forAllWith (const "") (Mut.mutFn mut compInp)
4444
let compOut = runCompiler compInp
4545
compOut' = runCompiler compInp'
4646
compilationOk compOut
4747
compilationOk compOut'
48+
Mut.mutAssert mut compOut'
4849

4950
-- TODO(bladyjoker): Add error producing mutations.

lambda-buffers-compiler/test/Test/LambdaBuffers/Compiler/Gen/Mutation.hs

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,67 @@ import Control.Lens ((&), (.~), (^.))
44
import Data.List.NonEmpty (nonEmpty)
55
import Data.ProtoLens (Message (messageName))
66
import Data.Proxy (Proxy (Proxy))
7+
import GHC.Stack (HasCallStack)
78
import Hedgehog qualified as H
89
import Hedgehog.Gen qualified as H
910
import Proto.Compiler (CompilerError, CompilerInput, CompilerOutput, CompilerOutput'CompilerOutput (CompilerOutput'CompilerError, CompilerOutput'CompilerResult), CompilerResult)
1011
import Proto.Compiler_Fields (maybe'compilerOutput, modules, typeDefs)
1112
import Test.LambdaBuffers.Compiler.Gen.Utils (pick)
1213
import Test.Tasty (TestName)
13-
import Test.Tasty.HUnit (Assertion, HasCallStack, assertFailure)
1414

15-
data Mutation = MkMutation
15+
data Mutation m = MkMutation
1616
{ mutLabel :: TestName
17-
, mutFn :: CompilerInput -> H.Gen (CompilerInput, CompilerOutput -> Assertion)
17+
, mutFn :: CompilerInput -> H.Gen CompilerInput
18+
, mutAssert :: H.MonadTest m => CompilerOutput -> m ()
1819
}
1920

20-
instance Show Mutation where
21-
show :: Mutation -> String
21+
instance Show (Mutation m) where
22+
show :: Mutation m -> String
2223
show = show . mutLabel
2324

2425
-- | Benign mutations
25-
shuffleModules :: Mutation
26-
shuffleModules = MkMutation "Shuffling modules inside of the CompilerInput should not affect compilation" $ \compInp -> do
27-
shuffled <- H.shuffle (compInp ^. modules)
28-
return
29-
( compInp & modules .~ shuffled
30-
, compilerResOrFail (\_ -> return ())
26+
shuffleModules :: Mutation m
27+
shuffleModules =
28+
MkMutation
29+
"Shuffling modules inside of the CompilerInput should not affect compilation"
30+
( \compInp -> do
31+
shuffled <- H.shuffle (compInp ^. modules)
32+
return $ compInp & modules .~ shuffled
3133
)
34+
(compilerResOrFail (const H.success))
3235

33-
shuffleTyDefs :: Mutation
34-
shuffleTyDefs = MkMutation "Shuffling type definitions inside of the Module should not affect compilation" $ \compInp -> do
35-
case nonEmpty $ compInp ^. modules of
36-
Nothing ->
37-
return
38-
( compInp
39-
, compilerResOrFail (\_ -> return ())
40-
)
41-
Just ms -> do
42-
(m, ms') <- pick ms
43-
shuffled <- H.shuffle (m ^. typeDefs)
44-
let m' = m & typeDefs .~ shuffled
45-
return
46-
( compInp & modules .~ m' : ms'
47-
, compilerResOrFail (\_ -> return ())
48-
)
36+
shuffleTyDefs :: Mutation m
37+
shuffleTyDefs =
38+
MkMutation
39+
"Shuffling type definitions inside of the Module should not affect compilation"
40+
( \compInp -> do
41+
case nonEmpty $ compInp ^. modules of
42+
Nothing ->
43+
return compInp
44+
Just ms -> do
45+
(m, ms') <- pick ms
46+
shuffled <- H.shuffle (m ^. typeDefs)
47+
let m' = m & typeDefs .~ shuffled
48+
return $ compInp & modules .~ m' : ms'
49+
)
50+
(compilerResOrFail (const H.success))
4951

5052
-- | Utils
51-
compilerOut :: HasCallStack => (CompilerError -> Assertion) -> (CompilerResult -> Assertion) -> CompilerOutput -> Assertion
52-
compilerOut err res co = case co ^. maybe'compilerOutput of
53-
Nothing -> assertFailure $ "compiler_output field must be set in " <> show (messageName (Proxy @CompilerOutput))
54-
Just (CompilerOutput'CompilerError cerr) -> err cerr
55-
Just (CompilerOutput'CompilerResult cres) -> res cres
53+
compilerOut :: HasCallStack => H.MonadTest m => (CompilerError -> m ()) -> (CompilerResult -> m ()) -> CompilerOutput -> m ()
54+
compilerOut err res co = do
55+
H.annotate "Received CompilerOutput"
56+
H.annotateShow co
57+
case co ^. maybe'compilerOutput of
58+
Nothing -> do
59+
H.annotate $ "compiler_output field must be set in " <> show (messageName (Proxy @CompilerOutput))
60+
H.failure
61+
Just (CompilerOutput'CompilerError cerr) -> err cerr
62+
Just (CompilerOutput'CompilerResult cres) -> res cres
5663

57-
compilerResOrFail :: HasCallStack => (CompilerResult -> Assertion) -> CompilerOutput -> Assertion
58-
compilerResOrFail = compilerOut (\cerr -> assertFailure $ "Expected to succeed but failed with " <> show cerr)
64+
compilerResOrFail :: HasCallStack => H.MonadTest m => (CompilerResult -> m ()) -> CompilerOutput -> m ()
65+
compilerResOrFail =
66+
compilerOut
67+
( \cerr -> do
68+
H.annotateShow cerr
69+
H.failure
70+
)

0 commit comments

Comments
 (0)