Skip to content

Commit e840934

Browse files
committed
Precautionally resize also gen_su_ab
1 parent affa1b2 commit e840934

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/Data/API/Tools/QuickCheck.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,19 @@ distinguishedElements (x:xs) = ((True, x) : map ((,) False) xs)
194194
-- | Generate an 'Arbitrary' instance for a union:
195195
--
196196
-- > instance Arbitrary Foo where
197-
-- > arbitrary = oneOf [ fmap Bar arbitrary, fmap Baz arbitrary ]
197+
-- > arbitrary = sized $ \ x -> oneOf [ fmap Bar (resize (x `div` 2) arbitrary)
198+
-- > , fmap Baz (resize (x `div` 2) arbitrary) ]
198199

199200
gen_su_ab :: Tool (APINode, SpecUnion)
200201
gen_su_ab = mkTool $ \ ts (an, su) -> mkArbitraryInstance ts (nodeRepT an) (bdy an su) (shrinkUnion an su)
201202
where
202203
bdy an su | null (suFields su) = nodeConE an
203-
| otherwise = [e| oneof $(listE alts) |]
204-
where
205-
alts = [ [e| fmap $(nodeAltConE an k) arbitrary |]
206-
| (k, _) <- suFields su ]
204+
| otherwise = do
205+
x <- newName "x"
206+
let alts = [ [e| fmap $(nodeAltConE an k) (QC.resize ($(varE x) `div` 2) arbitrary) |]
207+
| (k, _) <- suFields su ]
208+
appE (varE 'QC.sized) $ lamE [varP x] $
209+
varE 'oneof `appE` listE alts
207210

208211
-- For a union, we shrink the individual wrappers.
209212
shrinkUnion :: APINode -> SpecUnion -> ExpQ

0 commit comments

Comments
 (0)