[Lxml-checkins] r47820 - in lxml/branch/lxml-1.3/src/lxml: . tests
jholg at codespeak.net
jholg at codespeak.net
Wed Oct 24 14:22:40 CEST 2007
Author: jholg
Date: Wed Oct 24 14:22:40 2007
New Revision: 47820
Modified:
lxml/branch/lxml-1.3/src/lxml/objectify.pyx
lxml/branch/lxml-1.3/src/lxml/tests/test_objectify.py
Log:
Fixed StringElement __cmp__ and __mod__.
Modified: lxml/branch/lxml-1.3/src/lxml/objectify.pyx
==============================================================================
--- lxml/branch/lxml-1.3/src/lxml/objectify.pyx (original)
+++ lxml/branch/lxml-1.3/src/lxml/objectify.pyx Wed Oct 24 14:22:40 2007
@@ -681,13 +681,6 @@
raise TypeError, "invalid types for * operator"
def __mod__(self, other):
- if python.PyTuple_Check(other):
- l = []
- for item in other:
- python.PyList_Append(l, _strValueOf(item))
- other = tuple(l)
- else:
- other = _strValueOf(other)
return _strValueOf(self) % other
cdef class NoneElement(ObjectifiedDataElement):
@@ -770,7 +763,7 @@
if python._isString(obj):
return obj
if isinstance(obj, _Element):
- return textOf((<_Element>obj)._c_node)
+ return textOf((<_Element>obj)._c_node) or ''
if obj is None:
return ''
return str(obj)
Modified: lxml/branch/lxml-1.3/src/lxml/tests/test_objectify.py
==============================================================================
--- lxml/branch/lxml-1.3/src/lxml/tests/test_objectify.py (original)
+++ lxml/branch/lxml-1.3/src/lxml/tests/test_objectify.py Wed Oct 24 14:22:40 2007
@@ -748,7 +748,7 @@
def test_type_str_cmp(self):
XML = self.XML
- root = XML(u'<root><b>test</b><b>taste</b></root>')
+ root = XML(u'<root><b>test</b><b>taste</b><b></b><b/></root>')
self.assertFalse(root.b[0] < root.b[1])
self.assertFalse(root.b[0] <= root.b[1])
self.assertFalse(root.b[0] == root.b[1])
@@ -762,10 +762,18 @@
self.assert_(root.b[0] > 5)
self.assert_(5 < root.b[0])
+ self.assertEquals("", root.b[2])
+ self.assertEquals(root.b[2], "")
+ self.assertEquals("", root.b[3])
+ self.assertEquals(root.b[3], "")
+ self.assertEquals(root.b[2], root.b[3])
+
root.b = "test"
self.assert_(root.b)
root.b = ""
self.assertFalse(root.b)
+ self.assertEquals(root.b, "")
+ self.assertEquals("", root.b)
def test_type_int_cmp(self):
XML = self.XML
@@ -788,6 +796,8 @@
root.b = 0
self.assertFalse(root.b)
+ # float + long share the NumberElement implementation with int
+
def test_type_bool_cmp(self):
XML = self.XML
root = XML(u'<root><b>false</b><b>true</b></root>')
@@ -812,6 +822,45 @@
root.b = False
self.assertFalse(root.b)
+ def test_type_none_cmp(self):
+ XML = self.XML
+ root = XML(u"""
+ <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <b xsi:nil="true"></b><b xsi:nil="true"/>
+ </root>""")
+ self.assert_(root.b[0] == root.b[1])
+ self.assertFalse(root.b[0])
+ self.assertEquals(root.b[0], None)
+ self.assertEquals(None, root.b[0])
+
+ for comparison in ["abc", 5, 7.3, True, [], ()]:
+ none = root.b[1]
+ self.assert_(none < comparison, "%s (%s) should be < %s" %
+ (none, type(none), comparison) )
+ self.assert_(comparison > none, "%s should be > %s (%s)" %
+ (comparison, none, type(none)))
+
+ def test_type_str_mod(self):
+ s = "%d %f %s %r"
+ el = objectify.DataElement(s)
+ values = (1, 7.0, "abcd", None)
+ self.assertEquals(s % values, el % values)
+
+ s = "%d"
+ el = objectify.DataElement(s)
+ val = 5
+ self.assertEquals(s % val, el % val)
+
+ s = "%d %s"
+ el = objectify.DataElement(s)
+ val = 5
+ self.assertRaises(TypeError, el.__mod__, val)
+
+ s = ""
+ el = objectify.DataElement(s)
+ val = 5
+ self.assertRaises(TypeError, el.__mod__, val)
+
def test_pytype_annotation(self):
XML = self.XML
root = XML(u'''\
More information about the lxml-checkins
mailing list