finnsson / test-generator Goto Github PK
View Code? Open in Web Editor NEWAutomagically (using Template Haskell) generates the Haskell-code you need when using HUnit
Home Page: http://finnsson.github.com/test-generator/
Automagically (using Template Haskell) generates the Haskell-code you need when using HUnit
Home Page: http://finnsson.github.com/test-generator/
I use test-framework-th for http://ajhc.metasepi.org/.
But it does not support running with thread.
Could you support defaultMainWithOpts to use RunnerOptions?
Best regards,
Kiwamu Okabe
Such that if I have
test_someTests :: [Test]
test_someTests = [ testCase ... ]
tests = $testGroupGenerator
The generator will pick it up as
tests = testGroup "SomeModule" [ testGroup "someTests" test_someTests, ... ]
test-framework
already supports nested groups like this, and I'd like to use it to make some generated test cases show more detail in the output. It also makes it possible to run individual tests with greater control, and also parallelizes better.
$(defaultMainGenerator) is not seeing the arbitary instance of Results.
Yet $quickCheckAll, from quickchek, works for these, so must be possible
Example TypeSynonymInstances Code:
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE OverlappingInstances #-}
module Main where
import ClassyPrelude
import qualified Data.Map as Map
import Test.HUnit
import Data.Number.Fixed
import Test.QuickCheck hiding (Result)
import Test.QuickCheck.All
import Test.Framework.TH
import Test.Framework
import Test.Framework.Providers.QuickCheck2
type Result = (Int,Int)
instance Arbitrary Result where
arbitrary = elements resultElements
resultMax = 3
resultElements :: [Result]
resultElements = [(h,a)| h <- [0..resultMax], a <- [0..resultMax]]
type Results = Map Result Rational
instance Monoid Results where
mempty = empty
mappend = Map.unionWith (+)
instance Arbitrary Results where
arbitrary = do
rs <-listOf $ elements resultElements
ws <-listOf $ elements [1, 1.5 .. 2]
return $ Map.fromListWith (+) $ zip rs ws
main = do
runTests1
runTests2
return ()
prop_resultsAdd :: Results -> Results -> Bool
prop_resultsAdd rs1 rs2 = mappend rs1 rs2 == mappend rs2 rs1
runTests1 = $quickCheckAll
runTests2 = $(defaultMainGenerator)
Error: No instance for (Arbitrary (Map Result Rational))
Example GeneralizedNewtypeDeriving Code:
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Main2 where
import ClassyPrelude
import qualified Data.Map as Map
import Test.HUnit
import Data.Number.Fixed
import Test.QuickCheck hiding (Result)
import Test.QuickCheck.All
import Test.Framework.TH
import Test.Framework
import Test.Framework.Providers.QuickCheck2
newtype Result = Result (Int,Int) deriving (Show,Eq,Ord)
instance Arbitrary Result where
arbitrary = elements resultElements
resultMax = 3
resultElements :: [Result]
resultElements = [Result (h,a)| h <- [0..resultMax], a <- [0..resultMax]]
newtype Results = Results (Map Result Rational) deriving (Eq,Ord, Show)
instance Monoid Results where
mempty = Results empty
mappend (Results r1)(Results r2) = Results $ Map.unionWith (+) r1 r2
instance Arbitrary Results where
arbitrary = do
rs <-listOf $ elements resultElements
ws <-listOf $ elements [1, 1.5 .. 2]
return . Results . Map.fromListWith (+) $ zip rs ws
main = do
runTests1
runTests2
return ()
runTests1 = $quickCheckAll
runTests2 = $(defaultMainGenerator)
prop_resultsAdd :: Results -> Results -> Bool
prop_resultsAdd rs1 rs2 = mappend rs1 rs2 == mappend rs2 rs1
Error: No instance for (Arbitrary Results)
neither the github readme nor the hackage page contain a runnable program - they should import Test.Framework.TH
If I create a test file with an error in it and then add a property, I get an error like this:
test/Properties.hs:14:10:
Not in scope: ‘prop_analyzeReturnsSameForEquals’
In the splice: $defaultMainGenerator
If I remove the property, I get this error:
test/Properties.hs:32:20:
Expecting one more argument to ‘TestValues’
The first argument of ‘Arbitrary’ should have kind ‘*’,
but ‘TestValues’ has kind ‘* -> *’
In the instance declaration for ‘Arbitrary TestValues’
So, $defaultMainGenerator
suppresses the error messages that are really causing the problem. I've figured out that you can resolve this problem by placing $defaultMainGenerator
at the bottom of the file.
Would it be possible to figure out a way to prevent $defaultMainGenerator
from causing this problem, or should the documentation be updated to recommend declaring main
at the bottom of the file? (I could supply a PR for the second, the first is rather beyond my ability RN.)
A log file of the failure is available here: http://hackage.haskell.org/packages/archive/test-framework-th/0.2.0/logs/failure/ghc-7.0
This works:
{-# LANGUAGE TemplateHaskell #-}
module ResponseTest where
import Test.Framework.TH
import Test.Framework.Providers.QuickCheck2
main = $(defaultMainGenerator)
prop_reverse xs = reverse (reverse xs) == xs
where types = xs::[Int]
But the following does not:
{-# LANGUAGE TemplateHaskell #-}
module ResponseTest where
import Test.Framework.TH
import Test.Framework.Providers.QuickCheck2
main = $(defaultMainGenerator)
prop_reverse xs = reverse (reverse xs) == xs
where types = xs::[Int]
{-
prop_foo = foo
-}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.