| 1 |
{-# LANGUAGE TemplateHaskell, FunctionalDependencies #-} |
| 2 |
{-# OPTIONS -fno-warn-unused-binds #-} |
| 3 |
|
| 4 |
{-| Unittests for our template-haskell generated code. |
| 5 |
|
| 6 |
-} |
| 7 |
|
| 8 |
{- |
| 9 |
|
| 10 |
Copyright (C) 2012 Google Inc. |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
Redistribution and use in source and binary forms, with or without |
| 14 |
modification, are permitted provided that the following conditions are |
| 15 |
met: |
| 16 |
|
| 17 |
1. Redistributions of source code must retain the above copyright notice, |
| 18 |
this list of conditions and the following disclaimer. |
| 19 |
|
| 20 |
2. Redistributions in binary form must reproduce the above copyright |
| 21 |
notice, this list of conditions and the following disclaimer in the |
| 22 |
documentation and/or other materials provided with the distribution. |
| 23 |
|
| 24 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 25 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 26 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 27 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 28 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 29 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 30 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 31 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
|
| 36 |
-} |
| 37 |
|
| 38 |
module Test.Ganeti.THH |
| 39 |
( testTHH |
| 40 |
) where |
| 41 |
|
| 42 |
import Test.QuickCheck |
| 43 |
|
| 44 |
import Text.JSON |
| 45 |
|
| 46 |
import Ganeti.THH |
| 47 |
import Ganeti.PartialParams |
| 48 |
|
| 49 |
import Test.Ganeti.PartialParams |
| 50 |
import Test.Ganeti.TestHelper |
| 51 |
import Test.Ganeti.TestCommon |
| 52 |
|
| 53 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
| 54 |
|
| 55 |
-- * Custom types |
| 56 |
|
| 57 |
-- | Type used to test optional field implementation. Equivalent to |
| 58 |
-- @data TestObj = TestObj { tobjA :: Maybe Int, tobjB :: Maybe Int |
| 59 |
-- }@. |
| 60 |
$(buildObject "TestObj" "tobj" |
| 61 |
[ optionalField $ simpleField "a" [t| Int |] |
| 62 |
, optionalNullSerField $ simpleField "b" [t| Int |] |
| 63 |
]) |
| 64 |
|
| 65 |
-- | Arbitrary instance for 'TestObj'. |
| 66 |
$(genArbitrary ''TestObj) |
| 67 |
|
| 68 |
-- | Tests that serialising an (arbitrary) 'TestObj' instance is |
| 69 |
-- correct: fully optional fields are represented in the resulting |
| 70 |
-- dictionary only when non-null, optional-but-required fields are |
| 71 |
-- always represented (with either null or an actual value). |
| 72 |
prop_OptFields :: TestObj -> Property |
| 73 |
prop_OptFields to = |
| 74 |
let a_member = case tobjA to of |
| 75 |
Nothing -> [] |
| 76 |
Just x -> [("a", showJSON x)] |
| 77 |
b_member = [("b", case tobjB to of |
| 78 |
Nothing -> JSNull |
| 79 |
Just x -> showJSON x)] |
| 80 |
in showJSON to ==? makeObj (a_member ++ b_member) |
| 81 |
|
| 82 |
-- | Test serialization of TestObj. |
| 83 |
prop_TestObj_serialization :: TestObj -> Property |
| 84 |
prop_TestObj_serialization = testArraySerialisation |
| 85 |
|
| 86 |
-- | Test that all superfluous keys will fail to parse. |
| 87 |
prop_TestObj_deserialisationFail :: Property |
| 88 |
prop_TestObj_deserialisationFail = |
| 89 |
forAll ((arbitrary :: Gen [(String, Int)]) |
| 90 |
`suchThat` any (flip notElem ["a", "b"] . fst)) |
| 91 |
$ testDeserialisationFail (TestObj Nothing Nothing) . encJSDict |
| 92 |
|
| 93 |
-- | A unit-like data type. |
| 94 |
$(buildObject "UnitObj" "uobj" []) |
| 95 |
|
| 96 |
$(genArbitrary ''UnitObj) |
| 97 |
|
| 98 |
-- | Test serialization of UnitObj. |
| 99 |
prop_UnitObj_serialization :: UnitObj -> Property |
| 100 |
prop_UnitObj_serialization = testArraySerialisation |
| 101 |
|
| 102 |
-- | Test that all superfluous keys will fail to parse. |
| 103 |
prop_UnitObj_deserialisationFail :: Property |
| 104 |
prop_UnitObj_deserialisationFail = |
| 105 |
forAll ((arbitrary :: Gen [(String, Int)]) `suchThat` (not . null)) |
| 106 |
$ testDeserialisationFail UnitObj . encJSDict |
| 107 |
|
| 108 |
$(buildParam "Test" "tparam" |
| 109 |
[ simpleField "c" [t| Int |] |
| 110 |
, simpleField "d" [t| String |] |
| 111 |
]) |
| 112 |
|
| 113 |
$(genArbitrary ''FilledTestParams) |
| 114 |
$(genArbitrary ''PartialTestParams) |
| 115 |
|
| 116 |
-- | Tests that filling partial parameters works as expected. |
| 117 |
prop_fillWithPartialParams :: Property |
| 118 |
prop_fillWithPartialParams = |
| 119 |
let partial = PartialTestParams (Just 4) Nothing |
| 120 |
filled = FilledTestParams 2 "42" |
| 121 |
expected = FilledTestParams 4 "42" |
| 122 |
in fillParams filled partial ==? expected |
| 123 |
|
| 124 |
-- | Tests that filling partial parameters satisfies the law. |
| 125 |
prop_fillPartialLaw1 :: FilledTestParams -> PartialTestParams -> Property |
| 126 |
prop_fillPartialLaw1 = testFillParamsLaw1 |
| 127 |
|
| 128 |
-- | Tests that filling partial parameters works as expected. |
| 129 |
prop_toParams :: Property |
| 130 |
prop_toParams = |
| 131 |
let filled = FilledTestParams 2 "42" |
| 132 |
expected = FilledTestParams 4 "42" |
| 133 |
in toPartial (FilledTestParams 2 "42") ==? |
| 134 |
PartialTestParams (Just 2) (Just "42") |
| 135 |
|
| 136 |
-- | Tests that filling partial parameters satisfies the law. |
| 137 |
prop_fillPartialLaw2 :: FilledTestParams -> FilledTestParams -> Property |
| 138 |
prop_fillPartialLaw2 = testToParamsLaw2 |
| 139 |
|
| 140 |
-- | Tests that filling partial parameters satisfies the law. |
| 141 |
prop_fillPartialLaw3 :: FilledTestParams -> Property |
| 142 |
prop_fillPartialLaw3 = testToFilledLaw3 |
| 143 |
|
| 144 |
-- | Tests that the monoid action laws are satisfied. |
| 145 |
prop_fillPartialMonoidLaw1 :: FilledTestParams -> Property |
| 146 |
prop_fillPartialMonoidLaw1 = testToFilledMonoidLaw1 |
| 147 |
|
| 148 |
-- | Tests that the monoid action laws are satisfied. |
| 149 |
prop_fillPartialMonoidLaw2 |
| 150 |
:: FilledTestParams -> PartialTestParams -> PartialTestParams -> Property |
| 151 |
prop_fillPartialMonoidLaw2 = testToFilledMonoidLaw2 |
| 152 |
|
| 153 |
testSuite "THH" |
| 154 |
[ 'prop_OptFields |
| 155 |
, 'prop_TestObj_serialization |
| 156 |
, 'prop_TestObj_deserialisationFail |
| 157 |
, 'prop_UnitObj_serialization |
| 158 |
, 'prop_UnitObj_deserialisationFail |
| 159 |
, 'prop_fillWithPartialParams |
| 160 |
, 'prop_fillPartialLaw1 |
| 161 |
, 'prop_toParams |
| 162 |
, 'prop_fillPartialLaw2 |
| 163 |
, 'prop_fillPartialLaw3 |
| 164 |
, 'prop_fillPartialMonoidLaw1 |
| 165 |
, 'prop_fillPartialMonoidLaw2 |
| 166 |
] |