|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""The ``tree`` dumper writes a :class:`complex` number instead of raising. |
| 3 | +
|
| 4 | +GitHub issue #1266: :meth:`dictdumper.tree.Tree._append_number` hands every |
| 5 | +number to :func:`math.isnan`, which raises ``TypeError: must be real number, not |
| 6 | +complex`` for the :class:`complex` values :class:`~dictdumper.tree.Tree` routes |
| 7 | +there. :func:`~pcapkit.dumpkit.common.make_dumper` now writes those itself, in |
| 8 | +the writer's own ``-> <number>`` style. ``json`` and ``plist`` are untouched: |
| 9 | +they already write a :class:`complex` as the string ``(1+2j)``. |
| 10 | +
|
| 11 | +:mod:`pcapkit` is imported inside each test, after |
| 12 | +:func:`~tests._support.reimport_once_per_class`, not at module load, so that the |
| 13 | +dumper is built from the import that is live for this class. |
| 14 | +
|
| 15 | +""" |
| 16 | + |
| 17 | +import json |
| 18 | +import os |
| 19 | +import plistlib |
| 20 | +import tempfile |
| 21 | +import unittest |
| 22 | + |
| 23 | +from tests._support import reimport_once_per_class |
| 24 | + |
| 25 | +#: Name passed to every dumper call. |
| 26 | +NAME = 'x' |
| 27 | + |
| 28 | + |
| 29 | +class TestTreeComplexNumbers(unittest.TestCase): |
| 30 | + """Pin how each writer renders a :class:`complex` value.""" |
| 31 | + |
| 32 | + def setUp(self) -> None: |
| 33 | + reimport_once_per_class(self) |
| 34 | + tmp = tempfile.TemporaryDirectory() |
| 35 | + self.addCleanup(tmp.cleanup) |
| 36 | + self.tmp = tmp.name |
| 37 | + |
| 38 | + def dump(self, writer: str, value: object) -> str: |
| 39 | + """Write ``value`` through ``make_dumper(dictdumper.<writer>)``.""" |
| 40 | + import importlib |
| 41 | + |
| 42 | + dictdumper = importlib.import_module('dictdumper') |
| 43 | + common = importlib.import_module('pcapkit.dumpkit.common') |
| 44 | + |
| 45 | + path = os.path.join(self.tmp, f'{writer}.out') |
| 46 | + dumper = common.make_dumper(getattr(dictdumper, writer))(path) |
| 47 | + dumper(value, name=NAME) |
| 48 | + with open(path, encoding='utf-8') as file: |
| 49 | + return file.read() |
| 50 | + |
| 51 | + def test_tree_writes_top_level_complex(self) -> None: |
| 52 | + self.assertEqual(self.dump('Tree', {'c': 1 + 2j}), |
| 53 | + f'{NAME}\n |-- c -> (1+2j)\n') |
| 54 | + |
| 55 | + def test_tree_writes_nested_complex(self) -> None: |
| 56 | + text = self.dump('Tree', {'outer': {'inner': -0.5 - 3j}}) |
| 57 | + self.assertIn('|-- inner -> (-0.5-3j)', text) |
| 58 | + |
| 59 | + def test_tree_writes_complex_in_list(self) -> None: |
| 60 | + text = self.dump('Tree', {'items': [1, 2j, 1.5]}) |
| 61 | + self.assertIn('-> 2j', text) |
| 62 | + self.assertIn('-> 1', text) |
| 63 | + self.assertIn('-> 1.5', text) |
| 64 | + |
| 65 | + def test_tree_spells_non_finite_parts_as_for_float(self) -> None: |
| 66 | + text = self.dump('Tree', {'c': complex(float('inf'), float('nan')), |
| 67 | + 'f': float('inf')}) |
| 68 | + self.assertIn('|-- c -> (Infinity+NaNj)', text) |
| 69 | + self.assertIn('|-- f -> Infinity', text) |
| 70 | + |
| 71 | + def test_tree_writes_real_numbers_as_upstream(self) -> None: |
| 72 | + self.assertEqual(self.dump('Tree', {'i': 1, 'f': 1.5, 'n': float('nan')}), |
| 73 | + f'{NAME}\n |-- i -> 1\n |-- f -> 1.5\n |-- n -> NaN\n') |
| 74 | + |
| 75 | + def test_json_and_plist_keep_complex_as_string(self) -> None: |
| 76 | + value = {'c': 1 + 2j, 'outer': {'inner': 2j}, 'items': [2j]} |
| 77 | + expected = {NAME: {'c': '(1+2j)', 'outer': {'inner': '2j'}, 'items': ['2j']}} |
| 78 | + self.assertEqual( |
| 79 | + self.dump('JSON', value), |
| 80 | + '{\n\t"x": {\n\t\t"c": "(1+2j)",\n\t\t"outer": {\n\t\t\t"inner": "2j"\n' |
| 81 | + '\t\t},\n\t\t"items": [ "2j" ]\n\t}\n}', |
| 82 | + ) |
| 83 | + self.assertEqual(json.loads(self.dump('JSON', value)), expected) |
| 84 | + self.assertEqual(plistlib.loads(self.dump('PLIST', value).encode('utf-8')), expected) |
| 85 | + |
| 86 | + def test_only_tree_overrides_append_number(self) -> None: |
| 87 | + import importlib |
| 88 | + |
| 89 | + dictdumper = importlib.import_module('dictdumper') |
| 90 | + common = importlib.import_module('pcapkit.dumpkit.common') |
| 91 | + |
| 92 | + for writer in ('JSON', 'PLIST'): |
| 93 | + with self.subTest(writer=writer): |
| 94 | + upstream = getattr(dictdumper, writer) |
| 95 | + self.assertNotIn('_append_number', vars(common.make_dumper(upstream))) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + unittest.main() |
0 commit comments