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_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_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_recursive_switch_parse(): xml_switch = Switch( type, { float: Float("float"), int: Switch(lambda x: x % 2, { 0: Int("even"), 1: Int("odd") }) }) obj = 3.4 assert xml_switch.parse("<float>3.4</float>") == obj obj = 3 assert xml_switch.parse("<odd>3</odd>") == obj
def test_float_parse(): obj = ValueContainer(4.2) assert Float("test").parse('<test>4.2</test>') == obj
def test_float_build(): obj = 4.2 assert Float("test").build(obj) == '<test>4.2</test>'
def test_switch_same_tag_parse(): xml_switch = Switch(type, {int: Int("int"), float: Float("int")}) obj = 7.7 assert xml_switch.parse("<int>7.7</int>") == obj
def test_switch_build(): xml_switch = Switch(type, {int: Int("int"), float: Float("float")}) obj = 7 assert xml_switch.build(obj) == "<int>7</int>"
def test_switch_no_match_parse(): xml_switch = Switch(type, {int: Int("int"), float: Float("float")}) with pytest.raises(SwitchNoMatchError): xml_switch.parse("<string>7</string>") with pytest.raises(SwitchNoMatchError): xml_switch.parse("<int>a</int>")
def test_switch_no_match_build(): xml_switch = Switch(type, {int: Int("int"), float: Float("float")}) obj = "7" with pytest.raises(SwitchNoMatchError): xml_switch.build(obj)