1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
{-# LANGUAGE DeriveDataTypeable #-}
module GShow (tests) where
{-
The generic show example from the 2nd boilerplate paper.
(There were some typos in the ICFP 2004 paper.)
Also check out Data.Generics.Text.
-}
import Test.Tasty.HUnit
import Data.Generics hiding (gshow)
import Prelude hiding (showString)
gshow :: Data a => a -> String
gshow = gshow_help `extQ` showString
gshow_help :: Data a => a -> String
gshow_help t
= "("
++ showConstr (toConstr t)
++ concat (intersperse " " (gmapQ gshow t))
++ ")"
showString :: String -> String
showString s = "\"" ++ concat (map escape s) ++ "\""
where
escape '\n' = "\\n"
escape other_char = [other_char]
gshowList :: Data b => [b] -> String
gshowList xs
= "[" ++ concat (intersperse "," (map gshow xs)) ++ "]"
gshow' :: Data a => a -> String
gshow' = gshow_help `ext1Q` gshowList
`extQ` showString
intersperse :: a -> [a] -> [a]
intersperse _ [] = []
intersperse x [e] = [e]
intersperse x (e:es) = (e:(x:intersperse x es))
tests = ( gshow' "foo"
, gshow' [True,False]
) @=? output
output = ("\"foo\"","[(True),(False)]")
|