Statistics
| Branch: | Tag: | Revision:

root / test / hs / Test / Ganeti / JSON.hs @ 896cc964

History | View | Annotate | Download (2.7 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 Data.List
32
import Test.QuickCheck
33

    
34
import qualified Text.JSON as J
35

    
36
import Test.Ganeti.TestHelper
37
import Test.Ganeti.TestCommon
38

    
39
import qualified Ganeti.BasicTypes as BasicTypes
40
import qualified Ganeti.JSON as JSON
41

    
42
prop_toArray :: [Int] -> Property
43
prop_toArray intarr =
44
  let arr = map J.showJSON intarr in
45
  case JSON.toArray (J.JSArray arr) of
46
    BasicTypes.Ok arr' -> arr ==? arr'
47
    BasicTypes.Bad err -> failTest $ "Failed to parse array: " ++ err
48

    
49
prop_toArrayFail :: Int -> String -> Bool -> Property
50
prop_toArrayFail i s b =
51
  -- poor man's instance Arbitrary JSValue
52
  forAll (elements [J.showJSON i, J.showJSON s, J.showJSON b]) $ \item ->
53
  case JSON.toArray item::BasicTypes.Result [J.JSValue] of
54
    BasicTypes.Bad _ -> passTest
55
    BasicTypes.Ok result -> failTest $ "Unexpected parse, got " ++ show result
56

    
57
arrayMaybeToJson :: (J.JSON a) => [Maybe a] -> String -> JSON.JSRecord
58
arrayMaybeToJson xs k = [(k, J.JSArray $ map sh xs)]
59
  where
60
    sh x = case x of
61
      Just v -> J.showJSON v
62
      Nothing -> J.JSNull
63

    
64
prop_arrayMaybeFromObj :: String -> [Maybe Int] -> String -> Property
65
prop_arrayMaybeFromObj t xs k =
66
  case JSON.tryArrayMaybeFromObj t (arrayMaybeToJson xs k) k of
67
    BasicTypes.Ok xs' -> xs' ==? xs
68
    BasicTypes.Bad e -> failTest $ "Parsing failing, got: " ++ show e
69

    
70
prop_arrayMaybeFromObjFail :: String -> String -> Property
71
prop_arrayMaybeFromObjFail t k =
72
  case JSON.tryArrayMaybeFromObj t [] k of
73
    BasicTypes.Ok r -> fail $
74
                       "Unexpected result, got: " ++ show (r::[Maybe Int])
75
    BasicTypes.Bad e -> conjoin [ Data.List.isInfixOf t e ==? True
76
                                , Data.List.isInfixOf k e ==? True
77
                                ]
78

    
79
testSuite "JSON"
80
          [ 'prop_toArray
81
          , 'prop_toArrayFail
82
          , 'prop_arrayMaybeFromObj
83
          , 'prop_arrayMaybeFromObjFail
84
          ]