Skip to content

Commit 44ec2ce

Browse files
Michael Snoymanhvr
Michael Snoyman
authored andcommitted
Extend tutf8_err testcases to cover ab90c65
This also also makes the testsuite compatible w/ QC 2.10 and consequently closes #211 and #212
1 parent 63208c5 commit 44ec2ce

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

tests/Tests/Properties.hs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,38 @@ data Badness = Solo | Leading | Trailing
123123
instance Arbitrary Badness where
124124
arbitrary = elements [Solo, Leading, Trailing]
125125

126-
t_utf8_err :: Badness -> DecodeErr -> Property
127-
t_utf8_err bad de = do
126+
t_utf8_err :: Badness -> Maybe DecodeErr -> Property
127+
t_utf8_err bad mde = do
128128
let gen = case bad of
129129
Solo -> genInvalidUTF8
130130
Leading -> B.append <$> genInvalidUTF8 <*> genUTF8
131131
Trailing -> B.append <$> genUTF8 <*> genInvalidUTF8
132132
genUTF8 = E.encodeUtf8 <$> genUnicode
133-
forAll gen $ \bs -> MkProperty $ do
134-
onErr <- genDecodeErr de
135-
unProperty . monadicIO $ do
136-
l <- run $ let len = T.length (E.decodeUtf8With onErr bs)
137-
in (len `seq` return (Right len)) `Exception.catch`
138-
(\(e::UnicodeException) -> return (Left e))
139-
assert $ case l of
140-
Left err -> length (show err) >= 0
141-
Right _ -> de /= Strict
133+
forAll gen $ \bs -> MkProperty $
134+
case mde of
135+
-- generate an invalid character
136+
Nothing -> do
137+
c <- choose ('\x10000', maxBound)
138+
let onErr _ _ = Just c
139+
unProperty . monadicIO $ do
140+
l <- run $ let len = T.length (E.decodeUtf8With onErr bs)
141+
in (len `seq` return (Right len)) `Exception.catch`
142+
(\(e::Exception.SomeException) -> return (Left e))
143+
assert $ case l of
144+
Left err ->
145+
"non-BMP replacement characters not supported" `T.isInfixOf` T.pack (show err)
146+
Right _ -> False
147+
148+
-- generate a valid onErr
149+
Just de -> do
150+
onErr <- genDecodeErr de
151+
unProperty . monadicIO $ do
152+
l <- run $ let len = T.length (E.decodeUtf8With onErr bs)
153+
in (len `seq` return (Right len)) `Exception.catch`
154+
(\(e::UnicodeException) -> return (Left e))
155+
assert $ case l of
156+
Left err -> length (show err) >= 0
157+
Right _ -> de /= Strict
142158

143159
t_utf8_err' :: B.ByteString -> Property
144160
t_utf8_err' bs = monadicIO . assert $ case E.decodeUtf8' bs of
@@ -204,9 +220,10 @@ t_decode_with_error4' =
204220
case E.streamDecodeUtf8With (\_ _ -> Just 'x') (B.pack [0xC2, 97, 97, 97]) of
205221
E.Some x _ _ -> x === "xaaa"
206222

207-
t_infix_concat bs1 text bs2 rep =
223+
t_infix_concat bs1 text bs2 =
224+
forAll (genDecodeErr Replace) $ \onErr ->
208225
text `T.isInfixOf`
209-
E.decodeUtf8With (\_ _ -> rep) (B.concat [bs1, E.encodeUtf8 text, bs2])
226+
E.decodeUtf8With onErr (B.concat [bs1, E.encodeUtf8 text, bs2])
210227

211228
s_Eq s = (s==) `eq` ((S.streamList s==) . S.streamList)
212229
where _types = s :: String

tests/Tests/QuickCheckUtils.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ genDecodeErr :: DecodeErr -> Gen T.OnDecodeError
210210
genDecodeErr Lenient = return T.lenientDecode
211211
genDecodeErr Ignore = return T.ignore
212212
genDecodeErr Strict = return T.strictDecode
213-
genDecodeErr Replace = arbitrary
213+
genDecodeErr Replace = (\c _ _ -> c) <$> frequency
214+
[ (1, return Nothing)
215+
, (50, Just <$> choose ('\x1', '\xffff'))
216+
]
214217

215218
instance Arbitrary DecodeErr where
216219
arbitrary = elements [Lenient, Ignore, Strict, Replace]

text.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ test-suite tests
246246

247247
build-depends:
248248
HUnit >= 1.2,
249-
QuickCheck >= 2.7 && < 2.10,
249+
QuickCheck >= 2.7 && < 2.11,
250250
array,
251251
base,
252252
binary,

0 commit comments

Comments
 (0)