|
1 |
| -{-# LANGUAGE CPP #-} |
2 |
| -{-# LANGUAGE DeriveGeneric #-} |
3 |
| -{-# LANGUAGE PackageImports #-} |
4 |
| -{-# LANGUAGE RecordWildCards #-} |
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE DeriveGeneric #-} |
| 3 | +{-# LANGUAGE PackageImports #-} |
| 4 | +{-# LANGUAGE RecordWildCards #-} |
5 | 5 | {-# LANGUAGE ScopedTypeVariables #-}
|
6 | 6 | {-# OPTIONS_GHC -fno-warn-orphans #-}
|
7 | 7 | module Data.Swagger.Schema.ValidationSpec where
|
8 | 8 |
|
9 |
| -import Control.Applicative |
10 |
| -import Control.Lens ((&), (.~), (?~)) |
11 |
| -import Data.Aeson |
12 |
| -import Data.Aeson.Types |
13 |
| -import Data.Int |
14 |
| -import Data.IntMap (IntMap) |
15 |
| -import Data.Hashable (Hashable) |
16 |
| -import "unordered-containers" Data.HashSet (HashSet) |
| 9 | +import Control.Applicative |
| 10 | +import Control.Lens ((&), (.~), (?~)) |
| 11 | +import Data.Aeson |
| 12 | +import Data.Aeson.Types |
| 13 | +import Data.Hashable (Hashable) |
| 14 | +import Data.HashMap.Strict (HashMap) |
| 15 | +import qualified Data.HashMap.Strict as HashMap |
| 16 | +import "unordered-containers" Data.HashSet (HashSet) |
17 | 17 | import qualified "unordered-containers" Data.HashSet as HashSet
|
18 |
| -import Data.HashMap.Strict (HashMap) |
19 |
| -import qualified Data.HashMap.Strict as HashMap |
20 |
| -import Data.List.NonEmpty.Compat (NonEmpty(..), nonEmpty) |
21 |
| -import Data.Map (Map, fromList) |
22 |
| -import Data.Monoid (mempty) |
23 |
| -import Data.Proxy |
24 |
| -import Data.Time |
25 |
| -import qualified Data.Text as T |
26 |
| -import qualified Data.Text.Lazy as TL |
27 |
| -import Data.Version (Version) |
28 |
| -import Data.Set (Set) |
29 |
| -import Data.Word |
30 |
| -import GHC.Generics |
31 |
| - |
32 |
| -import Data.Swagger |
33 |
| -import Data.Swagger.Declare |
34 |
| - |
35 |
| -import Test.Hspec |
36 |
| -import Test.Hspec.QuickCheck |
37 |
| -import Test.QuickCheck |
38 |
| -import Test.QuickCheck.Instances () |
| 18 | +import Data.Int |
| 19 | +import Data.IntMap (IntMap) |
| 20 | +import Data.List.NonEmpty.Compat (NonEmpty (..), nonEmpty) |
| 21 | +import Data.Map (Map, fromList) |
| 22 | +import Data.Monoid (mempty) |
| 23 | +import Data.Proxy |
| 24 | +import Data.Set (Set) |
| 25 | +import qualified Data.Text as T |
| 26 | +import qualified Data.Text.Lazy as TL |
| 27 | +import Data.Time |
| 28 | +import Data.Version (Version) |
| 29 | +import Data.Word |
| 30 | +import GHC.Generics |
| 31 | + |
| 32 | +import Data.Swagger |
| 33 | +import Data.Swagger.Declare |
| 34 | + |
| 35 | +import Test.Hspec |
| 36 | +import Test.Hspec.QuickCheck |
| 37 | +import Test.QuickCheck |
| 38 | +import Test.QuickCheck.Instances () |
39 | 39 |
|
40 | 40 | shouldValidate :: (ToJSON a, ToSchema a) => Proxy a -> a -> Bool
|
41 | 41 | shouldValidate _ x = validateToJSON x == []
|
@@ -83,6 +83,7 @@ spec = do
|
83 | 83 | prop "(HashMap String Int)" $ shouldValidate (Proxy :: Proxy (HashMap String Int))
|
84 | 84 | prop "(HashMap T.Text Int)" $ shouldValidate (Proxy :: Proxy (HashMap T.Text Int))
|
85 | 85 | prop "(HashMap TL.Text Bool)" $ shouldValidate (Proxy :: Proxy (HashMap TL.Text Bool))
|
| 86 | + prop "Object" $ shouldValidate (Proxy :: Proxy Object) |
86 | 87 | prop "(Int, String, Double)" $ shouldValidate (Proxy :: Proxy (Int, String, Double))
|
87 | 88 | prop "(Int, String, Double, [Int])" $ shouldValidate (Proxy :: Proxy (Int, String, Double, [Int]))
|
88 | 89 | prop "(Int, String, Double, [Int], Int)" $ shouldValidate (Proxy :: Proxy (Int, String, Double, [Int], Int))
|
@@ -139,9 +140,9 @@ instance Arbitrary Color where
|
139 | 140 | arbitrary = arbitraryBoundedEnum
|
140 | 141 |
|
141 | 142 | invalidColorToJSON :: Color -> Value
|
142 |
| -invalidColorToJSON Red = toJSON "red" |
143 |
| -invalidColorToJSON Green = toJSON "green" |
144 |
| -invalidColorToJSON Blue = toJSON "blue" |
| 143 | +invalidColorToJSON Red = toJSON "red" |
| 144 | +invalidColorToJSON Green = toJSON "green" |
| 145 | +invalidColorToJSON Blue = toJSON "blue" |
145 | 146 |
|
146 | 147 | -- ========================================================================
|
147 | 148 | -- Paint (record with bounded enum property)
|
@@ -261,3 +262,17 @@ instance Arbitrary FreeForm where
|
261 | 262 | instance Eq ZonedTime where
|
262 | 263 | ZonedTime t (TimeZone x _ _) == ZonedTime t' (TimeZone y _ _) = t == t' && x == y
|
263 | 264 |
|
| 265 | +-- ======================================================================== |
| 266 | +-- Arbitrary instance for Data.Aeson.Value |
| 267 | +-- ======================================================================== |
| 268 | + |
| 269 | +instance Arbitrary Value where |
| 270 | + -- Weights are almost random |
| 271 | + -- Uniform oneof tends not to build complex objects cause of recursive call. |
| 272 | + arbitrary = resize 4 $ frequency |
| 273 | + [ (3, Object <$> arbitrary) |
| 274 | + , (3, Array <$> arbitrary) |
| 275 | + , (3, String <$> arbitrary) |
| 276 | + , (3, Number <$> arbitrary) |
| 277 | + , (3, Bool <$> arbitrary) |
| 278 | + , (1, return Null) ] |
0 commit comments