Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 1 | {-# OPTIONS_GHC -fno-warn-orphans #-} |
| 2 | |
| 3 | module Thrift.Arbitraries where |
| 4 | |
| 5 | import Data.Bits() |
| 6 | |
| 7 | import Test.QuickCheck.Arbitrary |
| 8 | |
| 9 | import Control.Applicative ((<$>)) |
| 10 | import Data.Map (Map) |
| 11 | import qualified Data.Map as Map |
| 12 | import qualified Data.Set as Set |
| 13 | import qualified Data.Vector as Vector |
| 14 | import qualified Data.Text.Lazy as Text |
| 15 | import qualified Data.HashSet as HSet |
| 16 | import qualified Data.HashMap.Strict as HMap |
| 17 | import Data.Hashable (Hashable) |
| 18 | |
| 19 | import Data.ByteString.Lazy (ByteString) |
| 20 | import qualified Data.ByteString.Lazy as BS |
| 21 | |
| 22 | -- String has an Arbitrary instance already |
| 23 | -- Bool has an Arbitrary instance already |
| 24 | -- A Thrift 'list' is a Vector. |
| 25 | |
| 26 | instance Arbitrary ByteString where |
| 27 | arbitrary = BS.pack . filter (/= 0) <$> arbitrary |
| 28 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 29 | instance (Arbitrary k) => Arbitrary (Vector.Vector k) where |
| 30 | arbitrary = Vector.fromList <$> arbitrary |
| 31 | |
| 32 | instance Arbitrary Text.Text where |
| 33 | arbitrary = Text.pack . filter (/= '\0') <$> arbitrary |
| 34 | |
| 35 | instance (Eq k, Hashable k, Arbitrary k) => Arbitrary (HSet.HashSet k) where |
| 36 | arbitrary = HSet.fromList <$> arbitrary |
| 37 | |
| 38 | instance (Eq k, Hashable k, Arbitrary k, Arbitrary v) => |
| 39 | Arbitrary (HMap.HashMap k v) where |
| 40 | arbitrary = HMap.fromList <$> arbitrary |
| 41 | |
| 42 | {- |
| 43 | To handle Thrift 'enum' we would ideally use something like: |
| 44 | |
| 45 | instance (Enum a, Bounded a) => Arbitrary a |
| 46 | where arbitrary = elements (enumFromTo minBound maxBound) |
| 47 | |
| 48 | Unfortunately this doesn't play nicely with the type system. |
| 49 | Instead we'll generate an arbitrary instance along with the code. |
| 50 | -} |
| 51 | |
| 52 | {- |
| 53 | There might be some way to introspect on the Haskell structure of a |
| 54 | Thrift 'struct' or 'exception' but generating the code directly is simpler. |
| 55 | -} |