module Test.Ganeti.THH
( testTHH
) where
import Test.QuickCheck
import Text.JSON
import Ganeti.THH
import Ganeti.PartialParams
import Test.Ganeti.PartialParams
import Test.Ganeti.TestHelper
import Test.Ganeti.TestCommon
$(buildObject "TestObj" "tobj"
[ optionalField $ simpleField "a" [t| Int |]
, optionalNullSerField $ simpleField "b" [t| Int |]
])
$(genArbitrary ''TestObj)
prop_OptFields :: TestObj -> Property
prop_OptFields to =
let a_member = case tobjA to of
Nothing -> []
Just x -> [("a", showJSON x)]
b_member = [("b", case tobjB to of
Nothing -> JSNull
Just x -> showJSON x)]
in showJSON to ==? makeObj (a_member ++ b_member)
prop_TestObj_serialization :: TestObj -> Property
prop_TestObj_serialization = testArraySerialisation
prop_TestObj_deserialisationFail :: Property
prop_TestObj_deserialisationFail =
forAll ((arbitrary :: Gen [(String, Int)])
`suchThat` any (flip notElem ["a", "b"] . fst))
$ testDeserialisationFail (TestObj Nothing Nothing) . encJSDict
$(buildObject "UnitObj" "uobj" [])
$(genArbitrary ''UnitObj)
prop_UnitObj_serialization :: UnitObj -> Property
prop_UnitObj_serialization = testArraySerialisation
prop_UnitObj_deserialisationFail :: Property
prop_UnitObj_deserialisationFail =
forAll ((arbitrary :: Gen [(String, Int)]) `suchThat` (not . null))
$ testDeserialisationFail UnitObj . encJSDict
$(buildParam "Test" "tparam"
[ simpleField "c" [t| Int |]
, simpleField "d" [t| String |]
])
$(genArbitrary ''FilledTestParams)
$(genArbitrary ''PartialTestParams)
prop_fillWithPartialParams :: Property
prop_fillWithPartialParams =
let partial = PartialTestParams (Just 4) Nothing
filled = FilledTestParams 2 "42"
expected = FilledTestParams 4 "42"
in fillParams filled partial ==? expected
prop_fillPartialLaw1 :: FilledTestParams -> PartialTestParams -> Property
prop_fillPartialLaw1 = testFillParamsLaw1
prop_toParams :: Property
prop_toParams =
let filled = FilledTestParams 2 "42"
expected = FilledTestParams 4 "42"
in toPartial (FilledTestParams 2 "42") ==?
PartialTestParams (Just 2) (Just "42")
prop_fillPartialLaw2 :: FilledTestParams -> FilledTestParams -> Property
prop_fillPartialLaw2 = testToParamsLaw2
prop_fillPartialLaw3 :: FilledTestParams -> Property
prop_fillPartialLaw3 = testToFilledLaw3
prop_fillPartialMonoidLaw1 :: FilledTestParams -> Property
prop_fillPartialMonoidLaw1 = testToFilledMonoidLaw1
prop_fillPartialMonoidLaw2
:: FilledTestParams -> PartialTestParams -> PartialTestParams -> Property
prop_fillPartialMonoidLaw2 = testToFilledMonoidLaw2
testSuite "THH"
[ 'prop_OptFields
, 'prop_TestObj_serialization
, 'prop_TestObj_deserialisationFail
, 'prop_UnitObj_serialization
, 'prop_UnitObj_deserialisationFail
, 'prop_fillWithPartialParams
, 'prop_fillPartialLaw1
, 'prop_toParams
, 'prop_fillPartialLaw2
, 'prop_fillPartialLaw3
, 'prop_fillPartialMonoidLaw1
, 'prop_fillPartialMonoidLaw2
]