git.iko.soy/typeable/attoparsec
on master help sign in
Merge pull request #86 from SeanRBurton/improve_tests

Improve QC instances

commita6913c1d53a67ae37e5864ec3803787a4066e9af authorBryan O'Sullivan <bos@serpentine.com> dateThu 11 Dec 2014, 11:18 (12 years ago) parentbc39852a773af4
1 file changed+19−1
tests/QC/Common.hs+19−1
tests/QC/Common.hs+19−1
@@ -54,12 +54,30 @@ repackBS_ = go . cycle
5454 | otherwise = let (h,t) = B.splitAt b s
5555 in h : go bs t
5656 go _ _ = error "unpossible"
57+
58+
59+newtype Unicode = Unicode {fromUnicode :: Char}
60+
61+valid :: Unicode -> Bool
62+valid (Unicode c) = c < '\55296' || '\57343' < c
63+
64+instance Arbitrary Unicode where
65+ arbitrary = fmap Unicode (oneof [arbitrary, arbitraryBoundedEnum]) `suchThat` valid
66+ shrink = filter valid . map (Unicode . toEnum) . shrink . fromEnum . fromUnicode
67+
68+packUnicode :: [Unicode] -> T.Text
69+packUnicode = T.pack . map fromUnicode
70+
71+unpackUnicode :: T.Text -> [Unicode]
72+unpackUnicode = map Unicode . T.unpack
5773
5874instance Arbitrary T.Text where
59− arbitrary = T.pack <$> arbitrary
75+ arbitrary = packUnicode <$> arbitrary
76+ shrink = map packUnicode . shrink . unpackUnicode
6077
6178instance Arbitrary TL.Text where
6279 arbitrary = repackT <$> arbitrary <*> arbitrary
80+ shrink = map TL.fromChunks . shrink . TL.toChunks
6381
6482repackT :: Repack -> T.Text -> TL.Text
6583repackT (NonEmpty bs) =