module UtilTest where import Prelude () import BasicPrelude import Test.Tasty.HUnit import qualified Network.Protocol.XMPP as XMPP import qualified Data.XML.Types as XML import TestInstances () import Util -- Because Arbitrary for XML.Element can be slow exampleElement :: XML.Element exampleElement = XML.Element (s"{example.com}x") [] [] prop_jidEscapeRoundtrip :: Text -> Bool prop_jidEscapeRoundtrip txt = unescapeJid (escapeJid txt) == txt prop_sRoundtrip :: String -> Bool prop_sRoundtrip str = textToString (s str) == str prop_iqReply :: XMPP.IQ -> Bool prop_iqReply iq = XMPP.iqType reply == XMPP.IQResult && XMPP.iqFrom reply == XMPP.iqTo iq && XMPP.iqTo reply == XMPP.iqFrom iq && XMPP.iqPayload reply == Just exampleElement where reply = iqReply (Just exampleElement) iq prop_iqError :: XMPP.IQ -> Bool prop_iqError iq = XMPP.iqType err == XMPP.IQError && XMPP.iqFrom err == XMPP.iqTo iq && XMPP.iqTo err == XMPP.iqFrom iq && XMPP.iqPayload err == Just exampleElement where err = iqError exampleElement iq prop_getBody :: Text -> Bool prop_getBody bodyTxt = getBody message == Just bodyTxt where message = (XMPP.emptyMessage XMPP.MessageNormal) { XMPP.messagePayloads = [ exampleElement, XML.Element (s"{jabber:component:accept}body") [] [ XML.NodeContent $ XML.ContentText mempty, XML.NodeContent $ XML.ContentText bodyTxt ] ] } prop_getSubject :: Text -> Bool prop_getSubject subjectTxt = getSubject message == Just subjectTxt where message = (XMPP.emptyMessage XMPP.MessageNormal) { XMPP.messagePayloads = [ exampleElement, XML.Element (s"{jabber:component:accept}subject") [] [ XML.NodeContent $ XML.ContentText mempty, XML.NodeContent $ XML.ContentText subjectTxt ] ] } unit_childFound :: IO () unit_childFound = child (s"{findme.example.com}x") message @?= Just findme where message = (XMPP.emptyMessage XMPP.MessageNormal) { XMPP.messagePayloads = [exampleElement, findme, exampleElement] } findme = XML.Element (s"{findme.example.com}x") [] [] unit_childNotFound :: IO () unit_childNotFound = child (s"{notfindme.example.com}x") message @?= Nothing where message = (XMPP.emptyMessage XMPP.MessageNormal) { XMPP.messagePayloads = [exampleElement, findme, exampleElement] } findme = XML.Element (s"{findme.example.com}x") [] [] unit_errorChildFound :: IO () unit_errorChildFound = errorChild message @?= Just err where message = (XMPP.emptyMessage XMPP.MessageNormal) { XMPP.messagePayloads = [exampleElement, err, exampleElement] } err = errorPayload "cancel" "not-found" (s"Not here") [] unit_errorChildNotFound :: IO () unit_errorChildNotFound = errorChild message @?= Nothing where message = (XMPP.emptyMessage XMPP.MessageNormal) { XMPP.messagePayloads = [exampleElement, exampleElement] }