Statistics
| Branch: | Tag: | Revision:

root / htest / Test / Ganeti / THH.hs @ 61899e64

History | View | Annotate | Download (2.1 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Unittests for our template-haskell generated code.
4

    
5
-}
6

    
7
{-
8

    
9
Copyright (C) 2012 Google Inc.
10

    
11
This program is free software; you can redistribute it and/or modify
12
it under the terms of the GNU General Public License as published by
13
the Free Software Foundation; either version 2 of the License, or
14
(at your option) any later version.
15

    
16
This program is distributed in the hope that it will be useful, but
17
WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
General Public License for more details.
20

    
21
You should have received a copy of the GNU General Public License
22
along with this program; if not, write to the Free Software
23
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
02110-1301, USA.
25

    
26
-}
27

    
28
module Test.Ganeti.THH
29
  ( testTHH
30
  ) where
31

    
32
import Test.QuickCheck
33

    
34
import Text.JSON
35

    
36
import Ganeti.THH
37
import Ganeti.JSON
38

    
39
import Test.Ganeti.TestHelper
40
import Test.Ganeti.TestCommon
41

    
42
{-# ANN module "HLint: ignore Use camelCase" #-}
43

    
44
-- * Custom types
45

    
46
-- | Type used to test optional field implementation. Equivalent to
47
-- @data TestObj = TestObj { tobjA :: Maybe Int, tobjB :: Maybe Int
48
-- }@.
49
$(buildObject "TestObj" "tobj"
50
  [ optionalField $ simpleField "a" [t| Int |]
51
  , optionalNullSerField $ simpleField "b" [t| Int |]
52
  ])
53

    
54
-- | Arbitrary instance for 'TestObj'.
55
$(genArbitrary ''TestObj)
56

    
57
-- | Tests that serialising an (arbitrary) 'TestObj' instance is
58
-- correct: fully optional fields are represented in the resulting
59
-- dictionary only when non-null, optional-but-required fields are
60
-- always represented (with either null or an actual value).
61
prop_OptFields :: TestObj -> Property
62
prop_OptFields to =
63
  let a_member = case tobjA to of
64
                   Nothing -> []
65
                   Just x -> [("a", showJSON x)]
66
      b_member = [("b", case tobjB to of
67
                          Nothing -> JSNull
68
                          Just x -> showJSON x)]
69
  in showJSON to ==? makeObj (a_member ++ b_member)
70

    
71

    
72
testSuite "THH"
73
            [ 'prop_OptFields
74
            ]