on master help sign in
Add test from SYB
565302cCsongor Kiss
generic-lens.cabal+17
@@ -100,6 +100,23 @@ test-suite generic-lens-test
100 build-depends: dump-core
100 build-depends: dump-core
101 ghc-options: -fplugin=DumpCore -fplugin-opt DumpCore:core-html
101 ghc-options: -fplugin=DumpCore -fplugin-opt DumpCore:core-html
102
102
103+test-suite generic-lens-syb-tree
104+ type: exitcode-stdio-1.0
105+ hs-source-dirs: test/syb
106+ main-is: Tree.hs
107+
108+ build-depends: base >= 4.9 && <= 5.0
109+ , generic-lens
110+ , lens
111+ , profunctors
112+ , HUnit
113+
114+ default-language: Haskell2010
115+ ghc-options: -Wall
116+ if flag(dump-core)
117+ build-depends: dump-core
118+ ghc-options: -fplugin=DumpCore -fplugin-opt DumpCore:core-html
119+
103test-suite doctests
120test-suite doctests
104 default-language: Haskell2010
121 default-language: Haskell2010
105 type: exitcode-stdio-1.0
122 type: exitcode-stdio-1.0
test/syb/Tree.hsnew file+53
@@ -0,0 +1,53 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE ScopedTypeVariables #-}
4+{-# LANGUAGE TypeApplications #-}
5+
6+{-
7+Example adapted from SYB
8+========================
9+-}
10+
11+module Main (main) where
12+
13+import Control.Lens.Fold (toListOf)
14+import Control.Monad (void)
15+import Data.Generics.Product
16+import GHC.Generics
17+import Test.HUnit
18+
19+main :: IO ()
20+main = void $ runTestTT tests
21+
22+-- A parameterised datatype for binary trees with data at the leafs
23+data Tree a w = Leaf a
24+ | Fork (Tree a w) (Tree a w)
25+ | WithWeight (Tree a w) w
26+ deriving (Show, Generic)
27+
28+-- A typical tree
29+mytree :: Tree Int Int
30+mytree = Fork (WithWeight (Leaf 42) 1)
31+ (WithWeight (Fork (Leaf 88) (Leaf 37)) 2)
32+
33+-- Print everything like an Int in mytree
34+-- In fact, we show two attempts:
35+-- 1. print really just everything like an Int
36+-- 2. print everything wrapped with Leaf
37+-- So (1.) confuses leafs and weights whereas (2.) does not.
38+tests :: Test
39+tests = show ( toListOf (typesDeep @Int) mytree
40+ , toListOf (param @1) mytree
41+ ) ~=? output
42+
43+-- original code from SYB:
44+--tests = show ( listify (\(_::Int) -> True) mytree
45+-- , everything (++) ([] `mkQ` fromLeaf) mytree
46+-- ) ~=? output
47+-- where
48+-- fromLeaf :: Tree Int Int -> [Int]
49+-- fromLeaf (Leaf x) = [x]
50+-- fromLeaf _ = []
51+
52+output :: String
53+output = "([42,1,88,37,2],[42,88,37])"