Verified Commit 6e9b1d5f authored by Camil Staps's avatar Camil Staps 🚀

Add a PropertyTestGenerator for lists (see clean-test-properties#8)

parent 54643f5c
Pipeline #18173 passed with stage
in 2 minutes and 51 seconds
......@@ -107,12 +107,21 @@ instance docDescription ParamDoc
:: PropertyVarInstantiation = PropertyVarInstantiation !(!String, !Type)
/**
* A test generator generates values of some type. The first argument of the
* constructor is the function type of the generator, for instance
* `[(k,v)] -> {{Map}} k v`. The second argument is the implementation, which
* should assume the generator is called `gen` (e.g.: `gen elems = ...`).
* A test generator generates values of some type. There are different ways to
* write test generators:
*
* - `PTG_Function`: The first argument is the function type of the generator,
* for instance `[(k,v)] -> {{Map}} k v`. This receives arguments of type
* `[(k,v)]` and transforms them into generated values of type `Map k v`. The
* second argument is the Clean implementation, which should assume the
* generator is called `gen` (e.g.: `gen elems = ...`).
* - `PTG_List`: The first argument is the type for which values are to be
* generated (e.g.: `Int`). The second argument is a Clean expression for a
* list of that type (e.g.: `[0..10]`).
*/
:: PropertyTestGenerator = PropertyTestGenerator !Type !String
:: PropertyTestGenerator
= PTG_Function !Type !String
| PTG_List !Type !String
derive gDefault FunctionDoc, Property, PropertyVarInstantiation, PropertyTestGenerator
......
......@@ -22,7 +22,7 @@ import Data.List
import Data.Maybe
import Data.Tuple
from Text import <+,
class Text(join,split,trim,rtrim,replaceSubString,endsWith),
class Text(join,split,trim,rtrim,replaceSubString,endsWith,startsWith),
instance Text String, instance Text [Char]
import Text.Language
import Text.Parsers.Simple.ParserCombinators
......@@ -216,11 +216,18 @@ docBlockToDoc{|PropertyVarInstantiation|} (Left [s]) = case split "=" s of
_ -> Left (UnknownError "property var instantiation could not be parsed")
docBlockToDoc{|PropertyVarInstantiation|} _ = abort "error in docBlockToDoc{|PropertyVarInstantiation|}\n"
docBlockToDoc{|PropertyTestGenerator|} (Left [s]) = case parseType (fromString sig) of
Just t -> Right (PropertyTestGenerator t (trimMultiLine imp), [])
Nothing -> Left (UnknownError "type could not be parsed")
docBlockToDoc{|PropertyTestGenerator|} (Left [s])
| startsWith "list: " sig = case parseType [c \\ c <-: sig & i <- [0..] | i > 4] of
Just t -> Right (PTG_List t imp, [])
Nothing -> error
| otherwise = case parseType (fromString sig) of
Just t -> Right (PTG_Function t imp, [])
Nothing -> error
where
[sig:imp] = split "\n" s
sig = trim sig`
imp = trimMultiLine imp`
[sig`:imp`] = split "\n" s
error = Left (UnknownError "test generator could not be parsed")
docBlockToDoc{|PropertyTestGenerator|} _ = abort "error in docBlockToDoc{|PropertyTestGenerator|}\n"
derive docBlockToDoc ModuleDoc, FunctionDoc, ClassMemberDoc, ConstructorDoc,
......@@ -288,7 +295,11 @@ docToDocBlock{|Property|} True (ForAll name args impl) = Left
docToDocBlock{|Property|} _ _ = abort "error in docToDocBlock{|Property|}\n"
docToDocBlock{|PropertyVarInstantiation|} True (PropertyVarInstantiation (a,t)) = Left [a +++ " = " <+ t]
docToDocBlock{|PropertyVarInstantiation|} _ _ = abort "error in docToDocBlock{|PropertyVarInstantiation|}\n"
docToDocBlock{|PropertyTestGenerator|} True (PropertyTestGenerator t impl) = Left [t <+ "\n" +++ impl]
docToDocBlock{|PropertyTestGenerator|} True ptg = Left [t <+ "\n" +++ imp]
where
(t,imp) = case ptg of
PTG_Function t imp -> (t,imp)
PTG_List t imp -> (t,imp)
docToDocBlock{|PropertyTestGenerator|} _ _ = abort "error in docToDocBlock{|PropertyTestGenerator|}\n"
derive docToDocBlock ModuleDoc, FunctionDoc, ClassMemberDoc, ClassDoc,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment