File: Gen.hs

package info (click to toggle)
haskell-http-media 0.8.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 188 kB
  • sloc: haskell: 1,572; makefile: 5
file content (72 lines) | stat: -rw-r--r-- 2,522 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
-- | Contains definitions for generating 'ByteString's.
module Network.HTTP.Media.Gen
  ( genToken,
    genByteStringFrom,
    genCIByteStringFrom,
    genByteString,
    genCIByteString,
    genDiffWith,
    genDiffByteString,
    genDiffCIByteString,
    padString,
  )
where

import Control.Monad (join, liftM2)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.CaseInsensitive (CI, original)
import qualified Data.CaseInsensitive as CI
import Data.Monoid ((<>))
import qualified Network.HTTP.Media.Utils as Utils
import Test.QuickCheck.Gen (Gen)
import qualified Test.QuickCheck.Gen as Gen
import Prelude hiding ((<>))

-- | Generates a valid header token.
genToken :: Gen (CI ByteString)
genToken = genCIByteStringFrom Utils.tokenChars

-- | Produces a non-empty ByteString of random characters from the given set.
genByteStringFrom :: String -> Gen ByteString
genByteStringFrom = fmap BS.pack . Gen.listOf1 . Gen.elements

genCIByteStringFrom :: String -> Gen (CI ByteString)
genCIByteStringFrom = fmap CI.mk . genByteStringFrom

-- | Produces a non-empty ByteString of random alphanumeric characters with a
-- non-numeric head.
genByteString :: Gen ByteString
genByteString =
  fmap BS.pack . (:)
    <$> Gen.elements alpha
    <*> Gen.scale (max 0 . pred) (Gen.listOf (Gen.elements alphaNum))
  where
    alpha = ['a' .. 'z'] ++ ['A' .. 'Z']
    alphaNum = alpha ++ ['0' .. '9']

-- | Produces a non-empty case-insensitive ByteString of random alphanumeric
-- characters with a non-numeric head.
genCIByteString :: Gen (CI ByteString)
genCIByteString = fmap CI.mk genByteString

-- | Produces a non-empty ByteString different to the given one using the
-- given generator.
genDiffWith :: (Eq a) => Gen a -> a -> Gen a
genDiffWith gen = Gen.suchThat gen . (/=)

-- | Produces a non-empty ByteString of random alphanumeric characters that
-- is case-insensitively different to the given one.
genDiffByteString :: ByteString -> Gen ByteString
genDiffByteString = fmap original . genDiffCIByteString . CI.mk

-- | Produces a non-empty case-insensitive ByteString of random alphanumeric
-- characters that is different to the given one.
genDiffCIByteString :: CI ByteString -> Gen (CI ByteString)
genDiffCIByteString = genDiffWith genCIByteString

-- | Pad a 'ByteString' with a random amount of tab and space characters.
padString :: ByteString -> Gen ByteString
padString c = join (liftM2 pad) (BS.pack <$> Gen.listOf (Gen.elements " \t"))
  where
    pad a b = a <> c <> b