move test helpers to ‘Util’ module

So we can reuse them to test other modules.
This commit is contained in:
mrkkrp 2015-08-22 02:46:13 +06:00
parent 85be098854
commit aa4189f4bc
2 changed files with 26 additions and 13 deletions

View File

@ -332,16 +332,3 @@ prop_user_state n m = runParser p 0 "" "" === Right (n + m)
prop_user_backtrack :: Integer -> Integer -> Property
prop_user_backtrack n m = runParser p 0 "" "" === Right n
where p = setState n >> lookAhead (setState m >> eof) >> getState
-- Helpers
infix 4 /=\
(/=\) :: (Eq a, Show a) => Parser a -> a -> Property
p /=\ x = simpleParse p "" === Right x
abcRow :: Int -> Int -> Int -> String
abcRow a b c = replicate a 'a' ++ replicate b 'b' ++ replicate c 'c'
abcRow' :: Bool -> Bool -> Bool -> String
abcRow' a b c = abcRow (fromEnum a) (fromEnum b) (fromEnum c)

View File

@ -32,6 +32,9 @@ module Util
, simpleParse
, checkChar
, checkString
, (/=\)
, abcRow
, abcRow'
, posErr
, uneCh
, uneStr
@ -102,6 +105,29 @@ checkString p a' l s' = checkParser p (w a' 0 s') s'
| otherwise = posErr 0 s' [uneStr (take i' s'), exSpec l]
where i' = succ i
infix 4 /=\
-- | @p /=\\ x@ runs parser @p@ on empty input and compares its result
-- (which should be successful) with @x@. Succeeds when the result is equal
-- to @x@, prints counterexample on failure.
(/=\) :: (Eq a, Show a) => Parser a -> a -> Property
p /=\ x = simpleParse p "" === Right x
-- | @abcRow a b c@ generates string consisting of character “a” repeated
-- @a@ times, character “b” repeated @b@ times, and finally character “c”
-- repeated @c@ times.
abcRow :: Int -> Int -> Int -> String
abcRow a b c = replicate a 'a' ++ replicate b 'b' ++ replicate c 'c'
-- | @abcRow' a b c@ generates string that includes character “a” if @a@ is
-- 'True', then optionally character “b” if @b@ is 'True', then character
-- “c” if @c@ is 'True'.
abcRow' :: Bool -> Bool -> Bool -> String
abcRow' a b c = abcRow (fromEnum a) (fromEnum b) (fromEnum c)
-- | @posErr pos s ms@ is an easy way to model result of parser that
-- fails. @pos@ is how many tokens (characters) has been consumed before
-- failure. @s@ is input of the parser. @ms@ is a list, collection of