-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for isValidUtf8ByteString and isValidUtf8ByteArray
- Loading branch information
Showing
3 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{-# LANGUAGE CPP #-} | ||
module Tests.Properties.Validate (testValidate) where | ||
|
||
import Data.Array.Byte (ByteArray) | ||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString as B | ||
import Data.ByteString.Short (toShort) | ||
import Data.Either (isRight) | ||
import Data.Text.Encoding (decodeUtf8', encodeUtf8) | ||
import Data.Text.Internal.Validate (isValidUtf8ByteString, isValidUtf8ByteArray) | ||
import Test.Tasty (TestTree, testGroup) | ||
import Test.Tasty.QuickCheck ((===), Gen, Property, | ||
testProperty, arbitrary, forAllShrink, oneof, shrink) | ||
import Tests.QuickCheckUtils () | ||
#if MIN_VERSION_bytestring(0,12,0) | ||
import Data.ByteString.Short (unShortByteString) | ||
#else | ||
import Data.ByteString.Short (ShortByteString(SBS)) | ||
import Data.Array.Byte (ByteArray(ByteArray)) | ||
|
||
unShortByteString :: ShortByteString -> ByteArray | ||
unShortByteString (SBS ba) = ByteArray ba | ||
#endif | ||
|
||
testValidate :: TestTree | ||
testValidate = testGroup "validate" | ||
[ testProperty "bytestring" $ forAllShrink genByteString shrink $ \bs -> | ||
isValidUtf8ByteString bs === isRight (decodeUtf8' bs) | ||
-- We have all we need to shrink here but I'm too lazy to do that now. | ||
, testProperty "bytearray" $ forAllByteArray $ \ba off len bs -> | ||
isValidUtf8ByteArray ba off len === isRight (decodeUtf8' bs) | ||
] | ||
|
||
genByteString :: Gen ByteString | ||
genByteString = oneof | ||
[ arbitrary | ||
, encodeUtf8 <$> arbitrary | ||
] | ||
|
||
-- | We want to test 'isValidUtf8ByteArray' with various offsets, so we insert a random | ||
-- prefix and remember its length. | ||
forAllByteArray :: (ByteArray -> Int -> Int -> ByteString -> Property) -> Property | ||
forAllByteArray prop = | ||
forAllShrink genByteString shrink $ \mainSlice -> | ||
forAllShrink arbitrary shrink $ \prefix -> | ||
let bs2ba = unShortByteString . toShort in | ||
prop (bs2ba (prefix `B.append` mainSlice)) (B.length prefix) (B.length mainSlice) mainSlice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters