summaryrefslogtreecommitdiff
path: root/src/lxml/tests
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2021-08-12 16:58:41 +0200
committerStefan Behnel <stefan_ml@behnel.de>2021-08-12 16:59:26 +0200
commit83e6c031994d553b74991501c6cd85e3517fadd8 (patch)
treef455881a911562a009f0de2f4dcc783d181af8aa /src/lxml/tests
parente23a807e816373e9eae9d45b5cecdd85ed2fa76a (diff)
downloadpython-lxml-xml_int_float_parsing.tar.gz
Implement a dedicated int/float parser for XML (schema) values in lxml.objectify.xml_int_float_parsing
This disables support for "_" in numbers, which are allowed by Python but not by XMLSchema. Wee keep a few additional literals, such as "+NaN", simply because they shouldn't hurt. See https://mail.python.org/archives/list/lxml@python.org/thread/6F7VIDKWZTJ6LB6VOX6IJNNWICYHFPNR/
Diffstat (limited to 'src/lxml/tests')
-rw-r--r--src/lxml/tests/test_objectify.py69
1 files changed, 62 insertions, 7 deletions
diff --git a/src/lxml/tests/test_objectify.py b/src/lxml/tests/test_objectify.py
index a12ae7e1..178ba256 100644
--- a/src/lxml/tests/test_objectify.py
+++ b/src/lxml/tests/test_objectify.py
@@ -6,7 +6,9 @@ Tests specific to the lxml.objectify API
from __future__ import absolute_import
-import unittest, operator
+import operator
+import random
+import unittest
from .common_imports import (
etree, HelperTestCase, fileInTestDir, doctest, make_doctest, _bytes, _str, BytesIO
@@ -2641,6 +2643,9 @@ class ObjectifyTestCase(HelperTestCase):
<l>4294967296</l>
<l>-4294967296</l>
<f>1.1</f>
+ <f>.1</f>
+ <f>.1E23</f>
+ <f>.1E-23</f>
<b>true</b>
<b>false</b>
<s>Strange things happen, where strings collide</s>
@@ -2649,6 +2654,11 @@ class ObjectifyTestCase(HelperTestCase):
<s>t</s>
<s>f</s>
<s></s>
+ <s>12_34</s>
+ <s>1.2_34</s>
+ <s>34E</s>
+ <s>.E</s>
+ <s>.</s>
<s>None</s>
<n xsi:nil="true" />
</root>
@@ -2656,20 +2666,65 @@ class ObjectifyTestCase(HelperTestCase):
root = XML(xml)
for i in root.i:
- self.assertTrue(isinstance(i, objectify.IntElement))
+ self.assertTrue(isinstance(i, objectify.IntElement), (i.text, type(i)))
for l in root.l:
- self.assertTrue(isinstance(l, objectify.IntElement))
+ self.assertTrue(isinstance(l, objectify.IntElement), (l.text, type(l)))
for f in root.f:
- self.assertTrue(isinstance(f, objectify.FloatElement))
+ self.assertTrue(isinstance(f, objectify.FloatElement), (f.text, type(f)))
for b in root.b:
- self.assertTrue(isinstance(b, objectify.BoolElement))
+ self.assertTrue(isinstance(b, objectify.BoolElement), (b.text, type(b)))
self.assertEqual(True, root.b[0])
self.assertEqual(False, root.b[1])
for s in root.s:
- self.assertTrue(isinstance(s, objectify.StringElement))
- self.assertTrue(isinstance(root.n, objectify.NoneElement))
+ self.assertTrue(isinstance(s, objectify.StringElement), (s.text, type(s)))
+ self.assertTrue(isinstance(root.n, objectify.NoneElement), root.n)
self.assertEqual(None, root.n)
+ def test_standard_lookup_fuzz(self):
+ SPACES = ('',) * 10 + ('\t', 'x', '\n', '\r\n', u'\xA0', u'\x0A', u'\u200A', u'\u200B')
+ DIGITS = ('', '0', '1', '11', '21', '345678', '9'*20)
+
+ def space(_choice=random.choice):
+ return _choice(SPACES)
+
+ fuzz = [
+ '<t>%s</t>\n' % (space() + sign + digits + point + fraction + exp + exp_sign + exp_digits + special + space())
+ for sign in ('', '+', '-')
+ for digits in DIGITS
+ for point in ('', '.')
+ for fraction in DIGITS
+ for exp in ('', 'E')
+ for exp_sign in ('', '+', '-')
+ for exp_digits in DIGITS
+ for special in ('', 'INF', 'inf', 'NaN', 'nan', 'an', 'na', 'ana', 'nf')
+ ]
+
+ root = self.XML(_bytes('''\
+ <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ ''' + ''.join(fuzz) + '''
+ </root>
+ '''))
+
+ test_count = 0
+ for el in root.iterchildren():
+ text = el.text
+ expected_type = objectify.ObjectifiedElement
+ if text:
+ try:
+ int(text)
+ expected_type = objectify.IntElement
+ except ValueError:
+ try:
+ float(text)
+ expected_type = objectify.FloatElement
+ except ValueError:
+ expected_type = objectify.StringElement
+
+ self.assertTrue(isinstance(el, expected_type), (text, expected_type, type(el)))
+ test_count += 1
+ self.assertEqual(len(fuzz), test_count)
+
+
def test_suite():
suite = unittest.TestSuite()
suite.addTests([unittest.makeSuite(ObjectifyTestCase)])