checking exported ledger types to have Arbitrary instances
Pre-submit checklist:
- Branch
- [x] Tests are provided (if possible)
- [x] Commit sequence broadly makes sense
- [x] Key commits have useful messages
- [ ] Changelog fragments have been written (if appropriate)
- [ ] Relevant tickets are mentioned in commit messages
- [ ] Formatting, PNG optimization, etc. are updated
- PR
- [x] (For external contributions) Corresponding issue exists and is linked in the description #6210
- [ ] Targeting master unless this is a cherry-pick backport
- [x] Self-reviewed the diff
- [ ] Useful pull request description
- [ ] Reviewer requested
To create a Tasty test suite that will allow you to use Haskell Program Coverage (HPC) to find all types exported by a Haskell module, you can follow these steps. We'll write a simple example module, create a test suite using Tasty, and then run HPC to analyze the coverage.
Step 1: Create a Sample Module
First, let's create a sample Haskell module that exports some types and functions. Save this as MyModule.hs.
-- MyModule.hs
module MyModule (MyType(..), myFunction, anotherFunction) where
-- A simple data type
data MyType = MyConstructor1 Int | MyConstructor2 String deriving (Show)
-- A function that uses MyType
myFunction :: MyType -> String
myFunction (MyConstructor1 n) = "Number: " ++ show n
myFunction (MyConstructor2 s) = "String: " ++ s
-- Another function that does not use MyType
anotherFunction :: Int -> Int
anotherFunction x = x + 1
Step 2: Create a Test Suite
Next, create a test suite using Tasty. Save this as MyModuleTest.hs.
-- MyModuleTest.hs
{-# LANGUAGE OverloadedStrings #-}
import Test.Tasty
import Test.Tasty.HUnit
import MyModule
main :: IO ()
main = defaultMain tests
tests :: TestTree
tests = testGroup "MyModule Tests"
[ testCase "Test MyConstructor1" $
myFunction (MyConstructor1 42) @?= "Number: 42"
, testCase "Test MyConstructor2" $
myFunction (MyConstructor2 "Hello") @?= "String: Hello"
, testCase "Test anotherFunction" $
anotherFunction 5 @?= 6
]
Step 3: Compile with Coverage
Now, compile your module and test suite with the -fhpc flag:
ghc -fhpc -o MyModuleTest MyModule.hs MyModuleTest.hs
Step 4: Run the Tests
Execute the test suite:
./MyModuleTest
This will run your tests and generate a .tix file containing the coverage information.
Step 5: Generate Coverage Reports
Now, generate a coverage report using HPC:
hpc report MyModuleTest
You can also generate markup files to visualize the coverage in your source code:
hpc markup MyModuleTest
This will create HTML files that you can open in a web browser to see which parts of your code were executed during the tests.
Step 6: Analyze the Coverage
By examining the coverage report and the markup files, you can identify which types and functions from MyModule were tested. Specifically, look for:
-
Function Coverage: Check if
myFunctionandanotherFunctionwere executed. -
Data Constructor Coverage: Ensure that both constructors of
MyTypewere tested.
Conclusion
This setup allows you to use Tasty and HPC to test your Haskell code while also gathering coverage information to see which types and functions are being exercised by your tests. This is a powerful way to ensure that your code is well-tested and that all exported types are covered.
- [x] assert existence of an instance for a type
- [ ] add parser to list exported types
The code I wrote uses ghc --show-iface command which if I run in shell tells me that Magic Number is mismatched.
How to read code coverage report?
cabal test plutus-ledger-api-test --enable-coverage
- https://stackoverflow.com/questions/54960594/how-can-i-generate-html-code-coverage-reports-with-new-cabal
- https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/hpc.html