root / test / hs / Test / Ganeti / JSON.hs @ dde8b625
History | View | Annotate | Download (3 kB)
1 |
{-# LANGUAGE TemplateHaskell #-} |
---|---|
2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
3 |
|
4 |
{-| Unittests for ganeti-htools. |
5 |
|
6 |
-} |
7 |
|
8 |
{- |
9 |
|
10 |
Copyright (C) 2009, 2010, 2011, 2012 Google Inc. |
11 |
|
12 |
This program is free software; you can redistribute it and/or modify |
13 |
it under the terms of the GNU General Public License as published by |
14 |
the Free Software Foundation; either version 2 of the License, or |
15 |
(at your option) any later version. |
16 |
|
17 |
This program is distributed in the hope that it will be useful, but |
18 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 |
General Public License for more details. |
21 |
|
22 |
You should have received a copy of the GNU General Public License |
23 |
along with this program; if not, write to the Free Software |
24 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
25 |
02110-1301, USA. |
26 |
|
27 |
-} |
28 |
|
29 |
module Test.Ganeti.JSON (testJSON) where |
30 |
|
31 |
import Control.Monad |
32 |
import Data.List |
33 |
import Test.QuickCheck |
34 |
|
35 |
import qualified Text.JSON as J |
36 |
|
37 |
import Test.Ganeti.TestHelper |
38 |
import Test.Ganeti.TestCommon |
39 |
import Test.Ganeti.Types () |
40 |
|
41 |
import qualified Ganeti.BasicTypes as BasicTypes |
42 |
import qualified Ganeti.JSON as JSON |
43 |
|
44 |
instance Arbitrary JSON.TimeAsDoubleJSON where |
45 |
arbitrary = liftM JSON.TimeAsDoubleJSON arbitrary |
46 |
|
47 |
prop_toArray :: [Int] -> Property |
48 |
prop_toArray intarr = |
49 |
let arr = map J.showJSON intarr in |
50 |
case JSON.toArray (J.JSArray arr) of |
51 |
BasicTypes.Ok arr' -> arr ==? arr' |
52 |
BasicTypes.Bad err -> failTest $ "Failed to parse array: " ++ err |
53 |
|
54 |
prop_toArrayFail :: Int -> String -> Bool -> Property |
55 |
prop_toArrayFail i s b = |
56 |
-- poor man's instance Arbitrary JSValue |
57 |
forAll (elements [J.showJSON i, J.showJSON s, J.showJSON b]) $ \item -> |
58 |
case JSON.toArray item::BasicTypes.Result [J.JSValue] of |
59 |
BasicTypes.Bad _ -> passTest |
60 |
BasicTypes.Ok result -> failTest $ "Unexpected parse, got " ++ show result |
61 |
|
62 |
arrayMaybeToJson :: (J.JSON a) => [Maybe a] -> String -> JSON.JSRecord |
63 |
arrayMaybeToJson xs k = [(k, J.JSArray $ map sh xs)] |
64 |
where |
65 |
sh x = case x of |
66 |
Just v -> J.showJSON v |
67 |
Nothing -> J.JSNull |
68 |
|
69 |
prop_arrayMaybeFromObj :: String -> [Maybe Int] -> String -> Property |
70 |
prop_arrayMaybeFromObj t xs k = |
71 |
case JSON.tryArrayMaybeFromObj t (arrayMaybeToJson xs k) k of |
72 |
BasicTypes.Ok xs' -> xs' ==? xs |
73 |
BasicTypes.Bad e -> failTest $ "Parsing failing, got: " ++ show e |
74 |
|
75 |
prop_arrayMaybeFromObjFail :: String -> String -> Property |
76 |
prop_arrayMaybeFromObjFail t k = |
77 |
case JSON.tryArrayMaybeFromObj t [] k of |
78 |
BasicTypes.Ok r -> fail $ |
79 |
"Unexpected result, got: " ++ show (r::[Maybe Int]) |
80 |
BasicTypes.Bad e -> conjoin [ Data.List.isInfixOf t e ==? True |
81 |
, Data.List.isInfixOf k e ==? True |
82 |
] |
83 |
|
84 |
prop_TimeAsDoubleJSON_serialisation :: JSON.TimeAsDoubleJSON -> Property |
85 |
prop_TimeAsDoubleJSON_serialisation = testSerialisation |
86 |
|
87 |
testSuite "JSON" |
88 |
[ 'prop_toArray |
89 |
, 'prop_toArrayFail |
90 |
, 'prop_arrayMaybeFromObj |
91 |
, 'prop_arrayMaybeFromObjFail |
92 |
, 'prop_TimeAsDoubleJSON_serialisation |
93 |
] |