{-# OPTIONS -fno-warn-missing-signatures #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE EmptyDataDecls , OverloadedStrings , ScopedTypeVariables , TemplateHaskell , TypeFamilies #-} module Main (main) where import Test.Tasty import GHC.Generics (Generic) import Test.Tasty.HUnit import Test.Tasty.TH import Text.XML.HXT.Arrow.Pickle import PickleUtils import Generics.XmlPickler data SingleCons = SingleCons deriving (Show, Eq, Generic) instance XmlPickler SingleCons where xpickle = gxpickle bidir :: (Show a, Eq a, XmlPickler a) => String -> a -> IO () bidir s d = do eq s (toXML d) eq (Right d) (eitherFromXML s) eq (Right d) (encDec d) encDec :: XmlPickler a => a -> Either String a encDec = eitherFromXML . toXML eq :: (Show a, Eq a) => a -> a -> Assertion eq = (@=?) case_constructorWithoutFields = bidir "" SingleCons data Record = Record { recordField :: Int } deriving (Show, Eq, Generic) instance XmlPickler Record where xpickle = gxpickle case_record = bidir "1" Record { recordField = 1 } data RecordTwoFields = D { d1 :: Int, d2 :: String } deriving (Show, Eq, Generic) instance XmlPickler RecordTwoFields where xpickle = gxpickle case_recordWithFields = bidir "1aap" D {d1 = 1, d2 = "aap"} data E = E Int deriving (Show, Eq, Generic) instance XmlPickler E where xpickle = gxpickle case_constructorOneField = bidir "1" (E 1) data F = F Int String deriving (Show, Eq, Generic) instance XmlPickler F where xpickle = gxpickle --case_constructorWithFields = do -- bidir "1aap" (F 1 "aap") data G = G1 Int | G2 String deriving (Show, Eq, Generic) instance XmlPickler G where xpickle = gxpickle case_sumConstructorsWithField = do bidir "1" (G1 1) bidir "aap" (G2 "aap") data H = H1 { h1 :: Int } | H2 { h2 :: String } deriving (Show, Eq, Generic) instance XmlPickler H where xpickle = gxpickle case_sumRecord = do bidir "

1

" H1 { h1 = 1 } bidir "

aap

" H2 { h2 = "aap" } data J = J1 { j1 :: Int, j2 :: String } | J2 deriving (Show, Eq, Generic) instance XmlPickler J where xpickle = gxpickle case_sumRecordConstructorWithoutFields = do bidir "1aap" J1 {j1 = 1, j2 = "aap"} bidir "" J2 data L = L1 | L2 Int String deriving (Show, Eq, Generic) instance XmlPickler L where xpickle = gxpickle case_sumConstructorWithoutFieldsConstructorWithFields = bidir "" L1 -- bidir "1aap" (L2 1 "aap") data M = M1 | M2 Int M deriving (Show, Eq, Generic) instance XmlPickler M where xpickle = gxpickle case_sumConstructorWithoutFieldsConstructorWithRecursiveField = do let a = M1 let b = M2 1 M1 let c = M2 1 (M2 2 M1) bidir "" a bidir "1" b bidir "12" c data N = N1 | N2 { n1 :: Int, n2 :: N } deriving (Show, Eq, Generic) instance XmlPickler N where xpickle = gxpickle case_sum_constructorWithoutFields_record = do bidir "" N1 bidir "1" N2 { n1 = 1, n2 = N1 } bidir "12" N2 { n1 = 1, n2 = N2 { n1 = 2, n2 = N1 } } data O = O { _o :: [Int] } deriving (Show, Eq, Generic) instance XmlPickler O where xpickle = gxpickle --case_recordListField = -- bidir "123" O {o = [1,2,3]} data P = P [Int] deriving (Show, Eq, Generic) instance XmlPickler P where xpickle = gxpickle --case_constructorListField = -- bidir "

123

" (P [1,2,3]) data Q = Q Int Int Int deriving (Show, Eq, Generic) instance XmlPickler Q where xpickle = gxpickle --case_ConstructorSameTypedFields = -- bidir "123" (Q 1 2 3) data T = T { r1 :: Maybe Int } deriving (Show, Eq, Generic) instance XmlPickler T where xpickle = gxpickle case_RecordMaybeField = do bidir "" T { r1 = Nothing } bidir "1" T { r1 = Just 1 } data V = V1 | V2 | V3 deriving (Show, Eq, Generic) instance XmlPickler V where xpickle = gxpickle case_constructorsWithoutFields = do bidir "" V1 bidir "" V2 data W = W { underscore1_ :: Int, _underscore2 :: Int } deriving (Show, Eq, Generic) instance XmlPickler W where xpickle = gxpickle case_recordWithUnderscoredFields = bidir "12" W {underscore1_ = 1, _underscore2 = 2} data Stat = StatA | StatB (Maybe Prog) deriving (Eq, Show, Generic) data Prog = Prog { aff :: Int } deriving (Eq, Show, Generic) instance XmlPickler Stat where xpickle = gxpickle instance XmlPickler Prog where xpickle = gxpickle case_stat = do let a = StatB (Just Prog { aff = 1 }) bidir "1" a let b = StatB Nothing bidir "" b data X = X (Maybe Int) Int deriving (Eq, Show, Generic) instance XmlPickler X where xpickle = gxpickle --case_constructorWithMaybeField = do -- let a = X (Just 1) 2 -- bidir "12" a -- -- let b = X Nothing 2 -- bidir "2" b -- -- eq (Left "when expecting a Int, encountered Boolean instead" :: Either String X) -- (eitherFromXML "[true,2]") data X1 = X1 { x1a :: Maybe Int, x1b :: Int } deriving (Eq, Show, Generic) instance XmlPickler X1 where xpickle = gxpickle case_recordWithMaybeField = do let a = X1 { x1a = Just 1, x1b = 2 } bidir "12" a let b = X1 Nothing 2 bidir "2" b eq (Nothing :: Maybe X1) (maybeFromXML "{\"x1a\":true,\"x1b\":2}") data X2 = X2 { x2 :: Maybe Int } deriving (Eq, Show, Generic) instance XmlPickler X2 where xpickle = gxpickle case_recordWithOnlyOneMaybeField = bidir "1" X2 { x2 = Just 1 } tests :: TestTree tests = $testGroupGenerator main :: IO () main = defaultMain $ testGroup "regular-xmlpickler" [tests]