Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 33.9k
Closed
Labels
testsTests in the Lib/test dirTests in the Lib/test dirtopic-XMLtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Right now one test in test_xml_etree contains two helpers called check_mapping and check_string:
cpython/Lib/test/test_xml_etree.py
Lines 206 to 241 in 8dd2766
| defcheck_string(string): | |
| len(string) | |
| forcharinstring: | |
| self.assertEqual(len(char), 1, | |
| msg="expected one-character string, got %r"%char) | |
| new_string=string+"" | |
| new_string=string+" " | |
| string[:0] | |
| defcheck_mapping(mapping): | |
| len(mapping) | |
| keys=mapping.keys() | |
| items=mapping.items() | |
| forkeyinkeys: | |
| item=mapping[key] | |
| mapping["key"] ="value" | |
| self.assertEqual(mapping["key"], "value", | |
| msg="expected value string, got %r"%mapping["key"]) | |
| defcheck_element(element): | |
| self.assertTrue(ET.iselement(element), msg="not an element") | |
| direlem=dir(element) | |
| forattrin'tag', 'attrib', 'text', 'tail': | |
| self.assertTrue(hasattr(element, attr), | |
| msg='no %s member'%attr) | |
| self.assertIn(attr, direlem, | |
| msg='no %s visible by dir'%attr) | |
| check_string(element.tag) | |
| check_mapping(element.attrib) | |
| ifelement.textisnotNone: | |
| check_string(element.text) | |
| ifelement.tailisnotNone: | |
| check_string(element.tail) | |
| foreleminelement: | |
| check_element(elem) |
It originates from very old code:
cpython/Lib/test/test_xml_etree.py
Lines 140 to 169 in d9a550b
| defcheck_string(string): | |
| len(string) | |
| forcharinstring: | |
| iflen(char) !=1: | |
| print("expected one-character string, got %r"%char) | |
| new_string=string+"" | |
| new_string=string+" " | |
| string[:0] | |
| defcheck_mapping(mapping): | |
| len(mapping) | |
| keys=mapping.keys() | |
| items=mapping.items() | |
| forkeyinkeys: | |
| item=mapping[key] | |
| mapping["key"] ="value" | |
| ifmapping["key"] !="value": | |
| print("expected value string, got %r"%mapping["key"]) | |
| defcheck_element(element): | |
| ifnotET.iselement(element): | |
| print("not an element") | |
| ifnothasattr(element, "tag"): | |
| print("no tag member") | |
| ifnothasattr(element, "attrib"): | |
| print("no attrib member") | |
| ifnothasattr(element, "text"): | |
| print("no text member") | |
| ifnothasattr(element, "tail"): | |
| print("no tail member") |
It is half-baked with lots of obvious things to be improved.
I think that it is safe just to replace them with:
check_stringtoassertIsInstance(str)check_mappingtoassertIsInstance(dict)
It is more correct, because both Python and C implementations only use str and dict for checked attributes. And in this case we can skip re-inventing tests for mapping and string.
Linked PRs
Metadata
Metadata
Assignees
Labels
testsTests in the Lib/test dirTests in the Lib/test dirtopic-XMLtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error