File: Arbitrary.hs

package info (click to toggle)
haskell-os-string 2.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 332 kB
  • sloc: haskell: 3,283; makefile: 3
file content (69 lines) | stat: -rw-r--r-- 2,025 bytes parent folder | download | duplicates (9)
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
{-# OPTIONS_GHC -Wno-orphans #-}

module Arbitrary where

import Data.Char
import Data.Maybe
import System.OsString
import System.OsString.Internal.Types
import qualified System.OsString.Posix as Posix
import qualified System.OsString.Windows as Windows
import Data.ByteString ( ByteString )
import qualified Data.ByteString as ByteString
import Test.QuickCheck


instance Arbitrary OsString where
  arbitrary = fmap fromJust $ encodeUtf <$> listOf filepathChar

instance Arbitrary PosixString where
  arbitrary = fmap fromJust $ Posix.encodeUtf <$> listOf filepathChar

instance Arbitrary WindowsString where
  arbitrary = fmap fromJust $ Windows.encodeUtf <$> listOf filepathChar


newtype NonNullString = NonNullString { nonNullString :: String }
  deriving Show

instance Arbitrary NonNullString where
  arbitrary = NonNullString <$> listOf filepathChar

filepathChar :: Gen Char
filepathChar = arbitraryUnicodeChar `suchThat` (\c -> not (isNull c) && isValidUnicode c)
 where
  isNull = (== '\NUL')
  isValidUnicode c = case generalCategory c of
      Surrogate -> False
      NotAssigned -> False
      _ -> True


newtype NonNullAsciiString = NonNullAsciiString { nonNullAsciiString :: String }
  deriving Show

instance Arbitrary NonNullAsciiString where
  arbitrary = NonNullAsciiString <$> listOf filepathAsciiChar

filepathAsciiChar :: Gen Char
filepathAsciiChar = arbitraryASCIIChar `suchThat` (\c -> not (isNull c))
 where
  isNull = (== '\NUL')

newtype NonNullSurrogateString = NonNullSurrogateString { nonNullSurrogateString :: String }
  deriving Show

instance Arbitrary NonNullSurrogateString where
  arbitrary = NonNullSurrogateString <$> listOf filepathWithSurrogates

filepathWithSurrogates :: Gen Char
filepathWithSurrogates =
  frequency
    [(3, arbitraryASCIIChar),
     (1, arbitraryUnicodeChar),
     (1, arbitraryBoundedEnum)
    ]


instance Arbitrary ByteString where arbitrary = ByteString.pack <$> arbitrary
instance CoArbitrary ByteString where coarbitrary = coarbitrary . ByteString.unpack