Statistics
| Branch: | Tag: | Revision:

root / test / hs / Test / Ganeti / THH.hs @ a59d5fa1

History | View | Annotate | Download (2 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

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

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

    
43
-- * Custom types
44

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

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

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

    
70

    
71
testSuite "THH"
72
            [ 'prop_OptFields
73
            ]