Skip to content

Commit 7883dcb

Browse files
committed
Added coverage information
1 parent c42f38b commit 7883dcb

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ test-suite tests
172172
Test.KindCheck
173173
Test.KindCheck.Errors
174174
Test.LambdaBuffers.Compiler
175+
Test.LambdaBuffers.Compiler.Coverage
175176
Test.LambdaBuffers.Compiler.Gen
176177
Test.LambdaBuffers.Compiler.Gen.Mutation
177178
Test.LambdaBuffers.Compiler.Gen.Utils

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Hedgehog.Gen qualified as H
77
import LambdaBuffers.Compiler (runCompiler)
88
import Proto.Compiler (CompilerOutput)
99
import Proto.Compiler_Fields (compilerResult)
10+
import Test.LambdaBuffers.Compiler.Coverage (coverage)
1011
import Test.LambdaBuffers.Compiler.Gen (genCompilerInput)
1112
import Test.LambdaBuffers.Compiler.Gen.Mutation qualified as Mut
1213
import Test.Tasty (TestTree, testGroup)
@@ -25,7 +26,14 @@ compilationOk :: H.MonadTest m => CompilerOutput -> m ()
2526
compilationOk compOut = compOut H.=== (defMessage & compilerResult .~ defMessage)
2627

2728
allCorrectCompInpCompile :: HasCallStack => TestTree
28-
allCorrectCompInpCompile = testProperty "All correct CompilerInputs must compile" (H.property $ H.forAll genCompilerInput >>= compilationOk . runCompiler)
29+
allCorrectCompInpCompile =
30+
testProperty
31+
"All correct CompilerInputs must compile"
32+
( H.property $ do
33+
compInp <- H.forAll genCompilerInput
34+
coverage compInp
35+
compilationOk . runCompiler $ compInp
36+
)
2937

3038
allCorrectCompInpCompileAfterBenignMut :: HasCallStack => TestTree
3139
allCorrectCompInpCompileAfterBenignMut =
@@ -34,6 +42,7 @@ allCorrectCompInpCompileAfterBenignMut =
3442
$ H.property
3543
$ do
3644
compInp <- H.forAll genCompilerInput
45+
coverage compInp
3746
mut <-
3847
H.forAll $
3948
H.element
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Test.LambdaBuffers.Compiler.Coverage (coverage) where
2+
3+
import Control.Lens ((^.))
4+
import Hedgehog (MonadTest, collect)
5+
import Proto.Compiler (CompilerInput)
6+
import Proto.Compiler_Fields (modules, typeDefs)
7+
8+
-- TODO(bladyjoker): Add stats on TyDef per Module, TyArgs per TyDef etc...
9+
coverage :: MonadTest m => CompilerInput -> m ()
10+
coverage compInp = do
11+
let nModules = length $ compInp ^. modules
12+
nTyDefs = length $ [td | m <- compInp ^. modules, td <- m ^. typeDefs]
13+
collect ("number of modules" :: String, nModules)
14+
collect ("number of type definitions" :: String, nTyDefs)

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import GHC.Enum qualified as Int
2020
import Hedgehog qualified as H
2121
import Hedgehog.Gen qualified as H
2222
import Hedgehog.Range qualified as H
23+
import Hedgehog.Range qualified as HR
2324
import Proto.Compiler (ClassName, CompilerInput, ConstrName, Kind, Kind'KindRef (Kind'KIND_REF_TYPE), Module, ModuleName, ModuleNamePart, SourceInfo, Sum, Sum'Constructor, Ty, TyAbs, TyArg, TyBody, TyDef, TyName, VarName)
2425
import Proto.Compiler_Fields (argKind, argName, column, constrName, constructors, fields, file, foreignTyRef, kindArrow, kindRef, left, localTyRef, moduleName, modules, name, ntuple, parts, posFrom, posTo, right, row, sourceInfo, tyAbs, tyApp, tyArgs, tyBody, tyFunc, tyName, tyRef, tyVar, typeDefs, varName)
2526
import Proto.Compiler_Fields qualified as P
2627
import Test.LambdaBuffers.Compiler.Gen.Utils (distribute, indexBy)
27-
import Test.Tasty.Hedgehog ()
2828

2929
-- | Upper bound on various generators
3030
limit :: Int
@@ -37,7 +37,7 @@ genAlphaNum = H.alphaNum
3737
genUpperCamelCase :: H.Gen Text
3838
genUpperCamelCase = do
3939
h <- H.upper
40-
t <- H.list (H.linear 1 limit) genAlphaNum
40+
t <- H.list (HR.constant 1 limit) genAlphaNum
4141
return $ Text.pack $ h : t
4242

4343
genModuleNamePart :: H.Gen ModuleNamePart
@@ -47,7 +47,7 @@ genModuleNamePart = do
4747

4848
genModuleName :: H.Gen ModuleName
4949
genModuleName = do
50-
ps <- H.list (H.linear 1 limit) genModuleNamePart
50+
ps <- H.list (HR.constant 1 limit) genModuleNamePart
5151
return $ defMessage & parts .~ ps
5252

5353
genTyName :: H.Gen TyName
@@ -68,7 +68,7 @@ genConstrName = do
6868
genVarName :: H.Gen VarName
6969
genVarName = do
7070
h <- H.lower
71-
t <- H.list (H.linear 1 4) H.lower
71+
t <- H.list (HR.constant 1 4) H.lower
7272
return $ defMessage & name .~ Text.pack (h : t)
7373

7474
starKind :: Kind
@@ -147,7 +147,7 @@ genTyApp kind tydefs args =
147147

148148
genConstructor :: TyDefs -> Set TyArg -> ConstrName -> H.Gen Sum'Constructor
149149
genConstructor tydefs args cn = do
150-
tys <- H.list (H.linear 0 limit) (genTy starKind tydefs args)
150+
tys <- H.list (HR.constant 0 limit) (genTy starKind tydefs args)
151151
return $
152152
defMessage
153153
& constrName .~ cn
@@ -174,7 +174,7 @@ genTyAbs tydefs ctorNs = do
174174
vns <-
175175
if tydefs == mempty
176176
then return mempty
177-
else H.set (H.linear 0 limit) genVarName
177+
else H.set (HR.constant 0 limit) genVarName
178178
args <- for (Set.toList vns) genTyArg
179179
body <- genTyBody tydefs (Set.fromList args) ctorNs
180180
return $
@@ -194,8 +194,8 @@ genTyDef tydefs tyn ctors = do
194194

195195
genModule :: Map ModuleName Module -> ModuleName -> H.Gen Module
196196
genModule availableMods mn = do
197-
tyNs <- NESet.fromList <$> H.nonEmpty (H.linear 0 limit) genTyName
198-
ctorNs <- H.set (H.linear (length tyNs) (length tyNs * limit)) genConstrName
197+
tyNs <- NESet.fromList <$> H.nonEmpty (HR.constant 0 limit) genTyName
198+
ctorNs <- H.set (HR.constant (length tyNs) (length tyNs * limit)) genConstrName
199199
tyNsWithCtorNs <- Map.map NESet.fromList <$> distribute (toList ctorNs) (NESet.toSet tyNs)
200200
let foreignTyDefs = collectTyDefs availableMods
201201
tydefs <-
@@ -220,7 +220,7 @@ genModule availableMods mn = do
220220

221221
genCompilerInput :: H.Gen CompilerInput
222222
genCompilerInput = do
223-
mns <- H.set (H.linear 0 limit) genModuleName
223+
mns <- H.set (HR.constant 0 limit) genModuleName
224224
ms <-
225225
foldM
226226
( \availableMods mn -> do
@@ -234,8 +234,8 @@ genCompilerInput = do
234234
-- | Utils
235235
withSourceInfo :: HasField a "sourceInfo" SourceInfo => a -> H.Gen a
236236
withSourceInfo msg = do
237-
f <- Text.pack <$> H.list (H.linear 1 10) H.unicodeAll
238-
i <- H.int (H.linear 0 Int.maxBound)
237+
f <- Text.pack <$> H.list (HR.constant 1 10) H.unicodeAll
238+
i <- H.int (HR.constant 0 Int.maxBound)
239239
let pos =
240240
defMessage
241241
& row .~ fromIntegral i

0 commit comments

Comments
 (0)