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_optional_recursive_parse_none(): xml_struct = Struct("test", Int("testint"), Optional(Int("optestint"))) obj = Container({ "testint": 3, "optestint": None }, xml_attrib={"attr": "attrv"}) assert xml_struct.parse( '<test attr="attrv"><testint>3</testint></test>') == 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_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_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_optional_recursive_build_none(): xml_struct = Struct("test", Int("testint"), Optional(Int("optestint"))) obj = Container({"testint": 3}, xml_attrib={"attr": "attrv"}) assert xml_struct.build( obj) == '<test attr="attrv"><testint>3</testint></test>'