/
hunit.hs
279 lines (231 loc) · 9.1 KB
/
hunit.hs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
{-# LANGUAGE TemplateHaskell #-}
-----------------------------------------------------------------------------
-- |
-- Module : Main (hunit)
-- Copyright : (C) 2012-14 Edward Kmett
-- License : BSD-style (see the file LICENSE)
-- Maintainer : Edward Kmett <ekmett@gmail.com>
-- Stability : provisional
-- Portability : portable
--
-- This module provides a simple hunit test suite for lens.
--
-- The code attempts to enumerate common use cases rather than give an example
-- of each available lens function. The tests here merely scratch the surface
-- of what is possible using the lens package; there are a great many use cases
-- (and lens functions) that aren't covered.
-----------------------------------------------------------------------------
module Main where
import Control.Lens
import Control.Monad.State
import Data.Char
import Data.List as List
import Data.Monoid
import Data.Map as Map
import Test.Framework.Providers.HUnit
import Test.Framework.TH
import Test.Framework
import Test.HUnit hiding (test)
data Point =
Point
{ _x :: Int -- ^ X coordinate
, _y :: Int -- ^ Y coordinate
} deriving (Show, Eq, Ord)
makeLenses ''Point
data Box =
Box
{ _low :: Point -- ^ The lowest used coordinates.
, _high :: Point -- ^ The highest used coordinates.
} deriving (Show, Eq)
makeLenses ''Box
data Polygon =
Polygon
{ _points :: [ Point ]
, _labels :: Map Point String
, _box :: Box
} deriving (Show, Eq)
makeLenses ''Polygon
data Shape = SBox Box | SPolygon Polygon | SCircle Point Int | SVoid
makePrisms ''Shape
origin =
Point { _x = 0, _y = 0 }
vectorFrom fromPoint toPoint =
Point
{ _x = toPoint^.x - fromPoint^.x
, _y = toPoint^.y - fromPoint^.y
}
trig =
Polygon
{ _points = [ Point { _x = 0, _y = 0 }
, Point { _x = 4, _y = 7 }
, Point { _x = 8, _y = 0 } ]
, _labels = fromList [ (Point { _x = 0, _y = 0 }, "Origin")
, (Point { _x = 4, _y = 7 }, "Peak") ]
, _box = Box { _low = Point { _x = 0, _y = 0 }
, _high = Point { _x = 8, _y = 7 } }
}
case_read_record_field =
(trig^.box.high.y)
@?= 7
case_read_state_record_field =
runState test trig @?= (7, trig)
where
test = use $ box.high.y
case_read_record_field_and_apply_function =
(trig^.points.to last.to (vectorFrom origin).x)
@?= 8
case_read_state_record_field_and_apply_function =
runState test trig @?= (8, trig)
where test = use $ points.to last.to (vectorFrom origin).x
case_write_record_field =
(trig & box.high.y .~ 6)
@?= trig { _box = (trig & _box)
{ _high = (trig & _box & _high)
{ _y = 6 } } }
case_write_state_record_field = do
let trig' = trig { _box = (trig & _box)
{ _high = (trig & _box & _high)
{ _y = 6 } } }
runState test trig @?= ((), trig')
where
test = box.high.y .= 6
case_write_record_field_and_access_new_value =
(trig & box.high.y <.~ 6)
@?= (6, trig { _box = (trig & _box)
{ _high = (trig & _box & _high)
{ _y = 6 } } })
case_write_state_record_field_and_access_new_value = do
let trig' = trig { _box = (trig & _box)
{ _high = (trig & _box & _high)
{ _y = 6 } } }
runState test trig @?= (6, trig')
where
test = box.high.y <.= 6
case_write_record_field_and_access_old_value =
(trig & box.high.y <<.~ 6)
@?= (7, trig { _box = (trig & _box)
{ _high = (trig & _box & _high)
{ _y = 6 } } })
case_write_state_record_field_and_access_old_value = do
let trig' = trig { _box = (trig & _box)
{ _high = (trig & _box & _high)
{ _y = 6 } } }
runState test trig @?= (7, trig')
where
test = box.high.y <<.= 6
case_modify_record_field =
(trig & box.low.y %~ (+ 2))
@?= trig { _box = (trig & _box)
{ _low = (trig & _box & _low)
{ _y = ((trig & _box & _low & _y) + 2) } } }
case_modify_state_record_field = do
let trig' = trig { _box = (trig & _box)
{ _low = (trig & _box & _low)
{ _y = ((trig & _box & _low & _y) + 2) } } }
runState test trig @?= ((), trig')
where
test = box.low.y %= (+ 2)
case_modify_record_field_and_access_new_value =
(trig & box.low.y <%~ (+ 2))
@?= (2, trig { _box = (trig & _box)
{ _low = (trig & _box & _low)
{ _y = ((trig & _box & _low & _y) + 2) } } })
case_modify_state_record_field_and_access_new_value = do
let trig' = trig { _box = (trig & _box)
{ _low = (trig & _box & _low)
{ _y = ((trig & _box & _low & _y) + 2) } } }
runState test trig @?= (2, trig')
where
test = box.low.y <%= (+ 2)
case_modify_record_field_and_access_old_value =
(trig & box.low.y <<%~ (+ 2))
@?= (0, trig { _box = (trig & _box)
{ _low = (trig & _box & _low)
{ _y = ((trig & _box & _low & _y) + 2) } } })
case_modify_state_record_field_and_access_old_value = do
let trig' = trig { _box = (trig & _box)
{ _low = (trig & _box & _low)
{ _y = ((trig & _box & _low & _y) + 2) } } }
runState test trig @?= (0, trig')
where
test = box.low.y <<%= (+ 2)
case_modify_record_field_and_access_side_result = do
runState test trig @?= (8, trig')
where
test = box.high %%= modifyAndCompute
modifyAndCompute point =
(point ^. x, point & y +~ 2)
trig' = trig { _box = (trig & _box)
{ _high = (trig & _box & _high)
{ _y = ((trig & _box & _high & _y) + 2) } } }
case_increment_record_field =
(trig & box.low.y +~ 1) -- And similarly for -~ *~ //~ ^~ ^^~ **~ ||~ &&~
@?= trig { _box = (trig & _box)
{ _low = (trig & _box & _low)
{ _y = ((trig & _box & _low & _y) + 1) } } }
case_increment_state_record_field =
runState test trig @?= ((), trig')
where
test = box.low.y += 1
trig' = trig { _box = (trig & _box)
{ _low = (trig & _box & _low)
{ _y = ((trig & _box & _low & _y) + 1) } } }
case_append_to_record_field =
(trig & points <>~ [ origin ])
@?= trig { _points = (trig & _points) <> [ origin ] }
case_append_to_state_record_field = do
runState test trig @?= ((), trig')
where
test = points <>= [ origin ]
trig' = trig { _points = (trig & _points) <> [ origin ] }
case_append_to_record_field_and_access_new_value =
(trig & points <<>~ [ origin ])
@?= (_points trig <> [ origin ], trig { _points = (trig & _points) <> [ origin ] })
case_append_to_state_record_field_and_access_new_value = do
runState test trig @?= (_points trig <> [ origin ], trig')
where
test = points <<>= [ origin ]
trig' = trig { _points = (trig & _points) <> [ origin ] }
case_append_to_record_field_and_access_old_value =
(trig & points <<%~ (<>[origin]))
@?= (_points trig, trig { _points = (trig & _points) <> [ origin ] })
case_append_to_state_record_field_and_access_old_value = do
runState test trig @?= (_points trig, trig')
where
test = points <<%= (<>[origin])
trig' = trig { _points = (trig & _points) <> [ origin ] }
case_read_maybe_map_entry = trig^.labels.at origin @?= Just "Origin"
case_read_maybe_state_map_entry =
runState test trig @?= (Just "Origin", trig)
where test = use $ labels.at origin
case_read_map_entry = trig^.labels.ix origin @?= "Origin"
case_read_state_map_entry = runState test trig @?= ("Origin", trig)
where test = use $ labels.ix origin
case_modify_map_entry =
(trig & labels.ix origin %~ List.map toUpper)
@?= trig { _labels = fromList [ (Point { _x = 0, _y = 0 }, "ORIGIN")
, (Point { _x = 4, _y = 7 }, "Peak") ] }
case_insert_maybe_map_entry =
(trig & labels.at (Point { _x = 8, _y = 0 }) .~ Just "Right")
@?= trig { _labels = fromList [ (Point { _x = 0, _y = 0 }, "Origin")
, (Point { _x = 4, _y = 7 }, "Peak")
, (Point { _x = 8, _y = 0 }, "Right") ] }
case_delete_maybe_map_entry =
(trig & labels.at origin .~ Nothing)
@?= trig { _labels = fromList [ (Point { _x = 4, _y = 7 }, "Peak") ] }
case_read_list_entry =
(trig ^? points.element 0)
@?= Just origin
case_write_list_entry =
(trig & points.element 0 .~ Point { _x = 2, _y = 0 })
@?= trig { _points = [ Point { _x = 2, _y = 0 }
, Point { _x = 4, _y = 7 }
, Point { _x = 8, _y = 0 } ] }
case_write_through_list_entry =
(trig & points.element 0 . x .~ 2)
@?= trig { _points = [ Point { _x = 2, _y = 0 }
, Point { _x = 4, _y = 7 }
, Point { _x = 8, _y = 0 } ] }
main :: IO ()
main = defaultMain [$testGroupGenerator]