1{-# OPTIONS -fglasgow-exts #-}
2
3module GShow2 (tests) where
4
5{-
6
7This test exercices GENERIC show for the infamous company datatypes. The
8output of the program should be some representation of the infamous
9"genCom" company.
10
11-}
12
13import Test.HUnit
14
15import Data.Generics
16import CompanyDatatypes
17
18tests = gshow genCom ~=? output
19
20{-
21
22Here is another exercise:
23The following function gshow' is a completely generic variation on gshow.
24It would print strings as follows:
25
26*Main> gshow' "abc"
27"((:) ('a') ((:) ('b') ((:) ('c') ([]))))"
28
29The original gshow does a better job because it is customised for strings:
30
31*Main> gshow "foo"
32"\"foo\""
33
34In fact, this is what Haskell's normal show would also do:
35
36*Main> show "foo"
37"\"foo\""
38
39-}
40
41gshow' :: Data a => a -> String
42gshow' t =     "("
43            ++ showConstr (toConstr t)
44            ++ concat (gmapQ ((++) " " . gshow') t)
45            ++ ")"
46
47output = "(C ((:) (D \"Research\" (E (P \"Laemmel\" \"Amsterdam\") (S (8000.0))) ((:) (PU (E (P \"Joost\" \"Amsterdam\") (S (1000.0)))) ((:) (PU (E (P \"Marlow\" \"Cambridge\") (S (2000.0)))) ([])))) ((:) (D \"Strategy\" (E (P \"Blair\" \"London\") (S (100000.0))) ([])) ([]))))"
48