Statistics
| Branch: | Tag: | Revision:

root / test / hs / Test / Ganeti / JSON.hs @ 93f1e606

History | View | Annotate | Download (3.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 a) => Arbitrary (JSON.MaybeForJSON a) where
45
  arbitrary = liftM JSON.MaybeForJSON arbitrary
46

    
47
instance Arbitrary JSON.TimeAsDoubleJSON where
48
  arbitrary = liftM JSON.TimeAsDoubleJSON arbitrary
49

    
50
prop_toArray :: [Int] -> Property
51
prop_toArray intarr =
52
  let arr = map J.showJSON intarr in
53
  case JSON.toArray (J.JSArray arr) of
54
    BasicTypes.Ok arr' -> arr ==? arr'
55
    BasicTypes.Bad err -> failTest $ "Failed to parse array: " ++ err
56

    
57
prop_toArrayFail :: Int -> String -> Bool -> Property
58
prop_toArrayFail i s b =
59
  -- poor man's instance Arbitrary JSValue
60
  forAll (elements [J.showJSON i, J.showJSON s, J.showJSON b]) $ \item ->
61
  case JSON.toArray item::BasicTypes.Result [J.JSValue] of
62
    BasicTypes.Bad _ -> passTest
63
    BasicTypes.Ok result -> failTest $ "Unexpected parse, got " ++ show result
64

    
65
arrayMaybeToJson :: (J.JSON a) => [Maybe a] -> String -> JSON.JSRecord
66
arrayMaybeToJson xs k = [(k, J.JSArray $ map sh xs)]
67
  where
68
    sh x = case x of
69
      Just v -> J.showJSON v
70
      Nothing -> J.JSNull
71

    
72
prop_arrayMaybeFromObj :: String -> [Maybe Int] -> String -> Property
73
prop_arrayMaybeFromObj t xs k =
74
  case JSON.tryArrayMaybeFromObj t (arrayMaybeToJson xs k) k of
75
    BasicTypes.Ok xs' -> xs' ==? xs
76
    BasicTypes.Bad e -> failTest $ "Parsing failing, got: " ++ show e
77

    
78
prop_arrayMaybeFromObjFail :: String -> String -> Property
79
prop_arrayMaybeFromObjFail t k =
80
  case JSON.tryArrayMaybeFromObj t [] k of
81
    BasicTypes.Ok r -> fail $
82
                       "Unexpected result, got: " ++ show (r::[Maybe Int])
83
    BasicTypes.Bad e -> conjoin [ Data.List.isInfixOf t e ==? True
84
                                , Data.List.isInfixOf k e ==? True
85
                                ]
86

    
87
prop_MaybeForJSON_serialisation :: JSON.MaybeForJSON String -> Property
88
prop_MaybeForJSON_serialisation = testSerialisation
89

    
90
prop_TimeAsDoubleJSON_serialisation :: JSON.TimeAsDoubleJSON -> Property
91
prop_TimeAsDoubleJSON_serialisation = testSerialisation
92

    
93
testSuite "JSON"
94
          [ 'prop_toArray
95
          , 'prop_toArrayFail
96
          , 'prop_arrayMaybeFromObj
97
          , 'prop_arrayMaybeFromObjFail
98
          , 'prop_MaybeForJSON_serialisation
99
          , 'prop_TimeAsDoubleJSON_serialisation
100
          ]