Ejemplo n.º 1
0
def test_numerics():
    top = XMLStruct('<top><a>10</a><b>5</b><c>0</c></top>')
    a = top.a
    b = top.b

    assert a
    assert not top.c

    assert a > b
    assert a >= b
    assert b < a
    assert b <= a
    assert a + b == 15
    assert a + 2 == 12
    assert 2 + a == 12
    assert a - b == 5
    assert a - 1 == 9
    assert 1 - a == -9
    assert a * b == 50
    assert a * 3 == 30
    assert 3 * a == 30
    assert a / b == 10 / 5, 10 / 5
    assert a / 4 == 10 / 4, 10 / 4
    assert 4 / a == 4 / 10, 4 / 10
    assert a / 4. == 2.5, 10 / 4.
    assert 4. / a == 0.4, 4. / 10
    assert a // b == 2
    assert a // 3 == 3
    assert 23 // a == 2
    assert a**b == 100000
    assert a**3 == 1000, a**3
    assert 2**a == 1024
    assert abs(a) == 10
    assert -a == -10
    assert a % b == 0
    assert a % 3 == 1
    assert 23 % a == 3
    assert a >> b == 0
    assert a >> 1 == 5
    assert 33333 >> a == 32
    assert a << b == 320
    assert a << 1 == 20
    assert 1 << a == 1024
    assert ~a == -11

    assert a.__float__() / 3 == 10. / 3

    assert float(a) / 3 == float(10) / 3
    assert int(a) == int(10)
    assert oct(a) == oct(10)
    assert hex(a) == hex(10)

    assert range(a) == range(10)

    assert a.bit_length() == 4
Ejemplo n.º 2
0
def test_set_content():
    xml1 = XMLStruct('<top><child>hello</child><num>10</num></top>')
    xml2 = XMLStruct('<top><child>there</child><num>12</num></top>')
    assert xml1 != xml2
    xml2.child = "hello"
    xml2.num = 10
    assert xml1 == xml2
    assert '<child>hello</child>' in xml2.dumps()
    assert '<num>10</num>' in xml2.dumps()
Ejemplo n.º 3
0
def test_same_obj():
    top = XMLStruct(
        '<top><a><a1>a1text</a1></a><a><a1>a1text2</a1></a><b><b1>b1text</b1></b><b><b1>b1text2</b1></b></top>'
    )
    aa = list(top)
    assert id(aa[0]) == id(top.a)
    assert id(aa[0]) == id(top("a"))
    assert id(aa[2]) == id(top.b)
    assert id(aa[2]) == id(top("b"))
    bb = list(top.a)
    assert id(bb[0]) == id(top.a.a1)
Ejemplo n.º 4
0
def test_set_attr():
    xml1 = XMLStruct('<top><child name="child1">hello</child></top>')
    xml2 = XMLStruct('<top><child name="child2">hello</child></top>')
    assert xml1 != xml2
    xml2.child["name"] = "child1"
    assert xml1 == xml2
    xml2.child["foo"] = "bar"
    assert '<child foo="bar" name="child1">' in xml2.dumps()
Ejemplo n.º 5
0
def test_equality():
    top1 = XMLStruct(_mydir + '/plant_catalog.xml')
    top2 = XMLStruct(_mydir + '/plant_catalog.xml')
    assert top1 == top2
    assert not (top1 != top2)
    xml1 = XMLStruct('<top><child name="child1"><val>100 </val></child></top>')
    xml2 = XMLStruct('<top><child name="child1"><val>0x64</val></child></top>')
    xml3 = XMLStruct('<top><child name="child1"><val>0x65</val></child></top>')
    xml4 = XMLStruct('<top><child name="child2"><val>0x64</val></child></top>')
    xml5 = XMLStruct('<top></top>')
    xml6 = XMLStruct('''<top><child name="child1"><val>0x64</val></child>
                     <child name="child1"><val>0x64</val></child></top>''')
    assert xml1 == xml2 and xml2 == xml1
    assert xml3 != xml2 and xml2 != xml3
    assert xml4 != xml2 and xml2 != xml4
    assert xml5 != xml2 and xml2 != xml5
    assert xml6 != xml2 and xml2 != xml6

    assert not (xml1 == None)
    assert not (None == xml1)
    assert xml1 != None
    assert None != xml1
Ejemplo n.º 6
0
def test_str_ops():
    top = XMLStruct('<top><c>cqc</c><d>dqd</d><e></e><f>   a s d   </f></top>')

    c = top.c
    d = top.d

    assert c
    assert not top.e

    assert c + d == "cqcdqd"
    assert c + "aaa" == "cqcaaa"
    assert "aaa" + c == "aaacqc"

    assert c.startswith("cq")
    assert d.endswith("qd")

    assert c.replace('c', 'w') == "wqw"

    assert "qd" in d

    assert d in ["assdf", "dqd"]

    assert len(d) == 3

    assert d.upper() == "DQD"
    assert d.lower() == "dqd"

    f = top.f
    assert f.rstrip() == "   a s d"
    assert d.rstrip("d") == "dq"
    assert f.lstrip() == "a s d   "
    assert d.lstrip("d") == "qd"
    assert f.strip() == "a s d"
    assert d.strip("d") == "q"
    assert f.split() == ["a", "s", "d"]
    assert f.split(" ") == ['', '', '', 'a', 's', 'd', '', '', '']
Ejemplo n.º 7
0
def test_is_int():
    top = XMLStruct(
        '<top><num>10</num><s>hello</s><num>20</num><s>world</s></top>')
    assert top.num._is_int()
    assert not top.s._is_int()
Ejemplo n.º 8
0
def test_hash():
    top = XMLStruct('<top><child>hello</child></top>')
    a = {top.child: 123}
    assert a['hello'] == 123
Ejemplo n.º 9
0
def test_text():
    top = XMLStruct(xml1)
    assert top.child.text == "hello"
Ejemplo n.º 10
0
def test_first_item():
    top = XMLStruct(xml2)
    assert top.messages.message['name'] == 'msg1'
Ejemplo n.º 11
0
def test_file():
    top = XMLStruct(_mydir + '/plant_catalog.xml')
    assert len(top) == 36
Ejemplo n.º 12
0
def test_longint():
    xml1 = XMLStruct('<top><child>hello</child><num>0xFFFFFFFF</num></top>')
    assert xml1.num == 0xFFFFFFFF
Ejemplo n.º 13
0
def test_find():
    top = XMLStruct(xml2)
    msg2 = top.messages("message", name="msg2")
    assert msg2['name'] == 'msg2'
Ejemplo n.º 14
0
def test_attr_as_int():
    top = XMLStruct(xml1)
    assert top.child["id"] == 0xe2
    assert top.child.id == 0xe2
    assert repr(top.child.id) == repr(0xe2)
Ejemplo n.º 15
0
def test_dumps2():
    xml1 = XMLStruct('<top><child name="child1">hello</child></top>')
    assert xml1.dumps() == """<?xml version="1.0" encoding="UTF-8"?>
Ejemplo n.º 16
0
def test_dict_by_tag():
    top = XMLStruct(_mydir + '/plant_catalog.xml')
    name2plant = top.as_dict('COMMON')
    assert len(name2plant) == 36
    assert name2plant["Dutchman's-Breeches"].BOTANICAL == 'Dicentra cucullaria'
Ejemplo n.º 17
0
def test_dict_by_attr():
    top = XMLStruct(xml2)
    msg = top.messages.message
    name2field = msg.as_dict('name')
    assert len(name2field) == 3
    assert name2field['field3'].description == 'Field #3'
Ejemplo n.º 18
0
def test_empty_tag():
    top = XMLStruct(xml1e)
    assert top.num == ""
    assert top.child == ""
Ejemplo n.º 19
0
def test_iter():
    top = XMLStruct(xml2)
    msg_names = [m['name'] for m in top.messages]
    assert msg_names == ['msg1', 'msg2']
    descriptions = [f.description for f in top.messages[1]]
    assert descriptions == ['Feld #1', 'Feld #2', 'Feld #3']