コード例 #1
0
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>"
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
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
コード例 #5
0
def test_float_parse():
    obj = ValueContainer(4.2)
    assert Float("test").parse('<test>4.2</test>') == obj
コード例 #6
0
def test_float_build():
    obj = 4.2
    assert Float("test").build(obj) == '<test>4.2</test>'
コード例 #7
0
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
コード例 #8
0
def test_switch_build():
    xml_switch = Switch(type, {int: Int("int"), float: Float("float")})
    obj = 7
    assert xml_switch.build(obj) == "<int>7</int>"
コード例 #9
0
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>")
コード例 #10
0
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)