Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve QC instances #86

Merged
merged 4 commits into from
Dec 11, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion tests/QC/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,30 @@ repackBS_ = go . cycle
| otherwise = let (h,t) = B.splitAt b s
in h : go bs t
go _ _ = error "unpossible"


newtype Unicode = Unicode {fromUnicode :: Char}

valid :: Unicode -> Bool
valid (Unicode c) = c < '\55296' || '\57343' < c

instance Arbitrary Unicode where
arbitrary = fmap Unicode (oneof [arbitrary, arbitraryBoundedEnum]) `suchThat` valid
shrink = filter valid . map (Unicode . toEnum) . shrink . fromEnum . fromUnicode

packUnicode :: [Unicode] -> T.Text
packUnicode = T.pack . map fromUnicode

unpackUnicode :: T.Text -> [Unicode]
unpackUnicode = map Unicode . T.unpack

instance Arbitrary T.Text where
arbitrary = T.pack <$> arbitrary
arbitrary = packUnicode <$> arbitrary
shrink = map packUnicode . shrink . unpackUnicode

instance Arbitrary TL.Text where
arbitrary = repackT <$> arbitrary <*> arbitrary
shrink = map TL.fromChunks . shrink . TL.toChunks

repackT :: Repack -> T.Text -> TL.Text
repackT (NonEmpty bs) =
Expand Down