def test_structured_if_then_else_parse(): def predicate(obj): return type(obj) == int xml_struct = Struct("test", IfThenElse(predicate, Int("int"), String("String")), String("description")) obj = Container(int=3, description="What") assert xml_struct.parse( "<test><int>3</int><description>What</description></test>") == obj
def test_recursive_struct_parse(): xml_struct = Struct("testa", Struct("testb", Int("testint"), String("string"))) obj = Container(testb={"testint": 3, "string": "night"}) assert xml_struct.parse( '<testa><testb><testint>3</testint><string>night</string></testb></testa>' ) == obj
def test_switch_default_parse(): xml_switch = Switch(type, { int: Int("int"), float: Float("float") }, String("string")) obj = "7" assert xml_switch.parse("<string>7</string>") == obj
def test_switch_default_build(): xml_switch = Switch(type, { int: Int("int"), float: Float("float") }, String("string")) obj = "7" assert xml_switch.build(obj) == "<string>7</string>"
def test_if_then_else_parse(): def predicate(obj): return type(obj) == int xml_elif = IfThenElse(predicate, Int("int"), String("String")) obj = 7 assert xml_elif.parse("<int>7</int>") == obj
def test_structured_switch_parse(): xml_struct = Struct("test", Switch(type, { int: Int("int"), float: Float("float") }), String("description")) obj = Container(int=3, description="What") assert xml_struct.parse( "<test><int>3</int><description>What</description></test>") == obj
def test_struct_build(): xml_struct = Struct("test", Int("testint"), String("teststring")) obj = Container({ "testint": 3, "teststring": "night" }, xml_attrib={"attr": "attrv"}) assert xml_struct.build( obj ) == '<test attr="attrv"><testint>3</testint><teststring>night</teststring></test>'
def test_overriding_attributes(): xml_struct = Struct( "testa", Struct("testb", Int("testint", {"attr": "attra"}), String("string", {"attr": "attrv"}))) obj = Container( testb={ "testint": ValueContainer(3, xml_attrib={"attr": "attrb"}), "string": "night" }) assert xml_struct.build( obj ) == '<testa><testb><testint attr="attrb">3</testint><string attr="attrv">night</string></testb></testa>'
def test_empty_string_parse(): obj = ValueContainer("") assert String("brooo").parse('<brooo></brooo>') == obj assert String("brooo").parse('<brooo />') == obj
def test_empty_string_build(): obj = "" assert String("brooo").build(obj) in ('<brooo></brooo>', "<brooo />")
def test_string_parse(): obj = ValueContainer("or") assert String("ts").parse('<ts>or</ts>') == obj
def test_string_build(): obj = "or" assert String("ts").build(obj) == '<ts>or</ts>'
def test_same_tags_subelements_parse(): xml_struct = OrderedStruct("test", String("string"), String("string")) obj = OrderedPairContainer(("string", "3"), ("string", "4")) assert xml_struct.parse( '<test><string>3</string><string>4</string></test>') == obj
def test_switch_several_match_parse(): xml_switch = Switch(type, {int: Int("int"), str: String("int")}) with pytest.raises(SwitchSeveralMatchError): xml_switch.parse("<int>7</int>")