@@ -4,55 +4,67 @@ import Control.Lens ((&), (.~), (^.))
44import Data.List.NonEmpty (nonEmpty )
55import Data.ProtoLens (Message (messageName ))
66import Data.Proxy (Proxy (Proxy ))
7+ import GHC.Stack (HasCallStack )
78import Hedgehog qualified as H
89import Hedgehog.Gen qualified as H
910import Proto.Compiler (CompilerError , CompilerInput , CompilerOutput , CompilerOutput'CompilerOutput (CompilerOutput'CompilerError , CompilerOutput'CompilerResult ), CompilerResult )
1011import Proto.Compiler_Fields (maybe'compilerOutput , modules , typeDefs )
1112import Test.LambdaBuffers.Compiler.Gen.Utils (pick )
1213import 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