diff options
author | Anthon van der Neut <anthon@mnt.org> | 2018-09-26 22:35:23 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2018-09-26 22:35:23 +0200 |
commit | 946e383b47a227032e92d432c85568caed8065e2 (patch) | |
tree | 52f9cb806bc6984a29e3e060d3b412245688c983 /_test | |
parent | 07e7eb667bbd75b4c127f4f418e8e35c72fb12fe (diff) | |
download | ruamel.yaml-946e383b47a227032e92d432c85568caed8065e2.tar.gz |
fix issue #214 part two: use __qualname__ for tag:yaml.org,2002:python/object/apply
this definately needs some test
*When this change indeed resolves your problem, please **Close** this issue*.
*(You can do so using the WorkFlow pull-down (close to the top right of this page))*
Diffstat (limited to '_test')
-rw-r--r-- | _test/test_float.py | 88 | ||||
-rw-r--r-- | _test/test_int.py | 100 | ||||
-rw-r--r-- | _test/test_issues.py | 3 | ||||
-rw-r--r-- | _test/test_z_data.py | 201 |
4 files changed, 176 insertions, 216 deletions
diff --git a/_test/test_float.py b/_test/test_float.py index 1f104f3..c996efd 100644 --- a/_test/test_float.py +++ b/_test/test_float.py @@ -55,99 +55,11 @@ class TestFloat: for d in data: assert -0.00001 < d < 0.00001 - def test_round_trip_zeros_1(self): - # not sure if this should be supported, but it is - data = round_trip("""\ - - 00.0 - - +00.0 - - -00.0 - """) - print(data) - for d in data: - assert -0.00001 < d < 0.00001 - def Xtest_round_trip_non_exp_trailing_dot(self): data = round_trip("""\ """) print(data) - def test_round_trip_exp_00(self): - data = round_trip("""\ - - 42e56 - - 42E56 - - 42.0E56 - - +42.0e56 - - 42.0E+056 - - +42.00e+056 - """) - print(data) - for d in data: - assert 41.99e56 < d < 42.01e56 - - def test_round_trip_exp_00f(self): - data = round_trip("""\ - - 42.E56 - """) - print(data) - for d in data: - assert 41.99e56 < d < 42.01e56 - - def test_round_trip_exp_01(self): - data = round_trip("""\ - - -42e56 - - -42E56 - - -42.0e56 - - -42.0E+056 - """) - print(data) - for d in data: - assert -41.99e56 > d > -42.01e56 - - def test_round_trip_exp_02(self): - data = round_trip("""\ - - 42e-56 - - 42E-56 - - 42.0E-56 - - +42.0e-56 - - 42.0E-056 - - +42.0e-056 - """) - print(data) - for d in data: - assert 41.99e-56 < d < 42.01e-56 - - def test_round_trip_exp_03(self): - data = round_trip("""\ - - -42e-56 - - -42E-56 - - -42.0e-56 - - -42.0E-056 - """) - print(data) - for d in data: - assert -41.99e-56 > d > -42.01e-56 - - def test_round_trip_exp_04(self): - round_trip("""\ - - 1.2e+34 - - 1.23e+034 - - 1.230e+34 - - 1.023e+34 - - -1.023e+34 - - 250e6 - """) - - def test_round_trip_exp_05(self): - data = round_trip("""\ - - 3.0517578123e-56 - - 3.0517578123E-56 - - 3.0517578123e-056 - - 3.0517578123E-056 - """) - print(data) - for d in data: - assert 3.0517578122e-56 < d < 3.0517578124e-56 - def test_yaml_1_1_no_dot(self): from ruamel.yaml.error import MantissaNoDotYAML1_1Warning diff --git a/_test/test_int.py b/_test/test_int.py index b995f59..4e7de6b 100644 --- a/_test/test_int.py +++ b/_test/test_int.py @@ -4,21 +4,12 @@ from __future__ import print_function, absolute_import, division, unicode_litera import pytest # NOQA -from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump +from roundtrip import dedent, round_trip_load, round_trip_dump # http://yaml.org/type/int.html is where underscores in integers are defined class TestBinHexOct: - def test_round_trip_hex_oct(self): - round_trip("""\ - - 42 - - 0b101010 - - 0x2a - - 0x2A - - 0o52 - """) - def test_calculate(self): # make sure type, leading zero(s) and underscore are preserved s = dedent("""\ @@ -28,89 +19,16 @@ class TestBinHexOct: - 0x2A - 0o00_52 """) - x = round_trip_load(s) - for idx, elem in enumerate(x): - # x[idx] = type(elem)(elem - 21) + d = round_trip_load(s) + for idx, elem in enumerate(d): elem -= 21 - x[idx] = elem - for idx, elem in enumerate(x): - # x[idx] = type(elem)(2 * elem) + d[idx] = elem + for idx, elem in enumerate(d): elem *= 2 - x[idx] = elem - for idx, elem in enumerate(x): + d[idx] = elem + for idx, elem in enumerate(d): t = elem elem **= 2 elem //= t - x[idx] = elem - assert round_trip_dump(x) == s - - # if a scalar int has one or more leading zeros, it is assumed that the width - # of the int is significant, as padding with a zero doesn't make much sense - # please note that none of this should work on YAML 1.1 as it collides with - # the old octal representation. - - def test_leading_zero_hex_oct_bin(self): - round_trip("""\ - - 0b0101010 - - 0b00101010 - - 0x02a - - 0x002a - - 0x02A - - 0x002A - - 0o052 - - 0o0052 - """) - - def test_leading_zero_int(self): - round_trip("""\ - - 042 - - 0042 - """) - - def test_leading_zero_YAML_1_1(self): - d = round_trip_load("""\ - %YAML 1.1 - --- - - 042 - - 0o42 - """) - assert d[0] == 0o42 - assert d[1] == '0o42' - - def test_underscore(self): - round_trip("""\ - - 0b10000_10010010 - - 0b0_0000_1001_0010 - - 0x2_87_57_b2_ - - 0x0287_57B2 - - 0x_0_2_8_7_5_7_B_2 - - 0o2416_53662 - - 42_42_ - """) - - def test_leading_underscore(self): - d = round_trip_load("""\ - - 0x_2_8_7_5_7_B_2 - - _42_42_ - - 42_42_ - """) - assert d[0] == 42424242 - assert d[1] == '_42_42_' - assert d[2] == 4242 - - def test_big(self): - # bitbucket issue 144 reported by ccatterina - d = round_trip_load("""\ - - 2_147_483_647 - - 9_223_372_036_854_775_808 - """) - assert d[0] == 2147483647 - assert d[1] == 9223372036854775808 - - -class TestIntIssues: - def test_issue_218_single_plus_sign_is_not_int(self): - d = round_trip_load("""\ - +: 1 - """) - assert d == {'+': 1} + d[idx] = elem + assert round_trip_dump(d) == s diff --git a/_test/test_issues.py b/_test/test_issues.py index 4b94b08..5ee5125 100644 --- a/_test/test_issues.py +++ b/_test/test_issues.py @@ -508,11 +508,10 @@ class TestIssues: """ d = YAML().round_trip_all(inp) # NOQA - - @pytest.mark.xfail(strict=True, reason='faulty initialisation', raises=AttributeError) def test_issue_242(self): from ruamel.yaml.comments import CommentedMap d0 = CommentedMap([('a', 'b')]) + assert d0['a'] == 'b' # @pytest.mark.xfail(strict=True, reason='bla bla', raises=AssertionError) # def test_issue_xxx(self): diff --git a/_test/test_z_data.py b/_test/test_z_data.py index 5a142cf..93dc7f0 100644 --- a/_test/test_z_data.py +++ b/_test/test_z_data.py @@ -1,42 +1,173 @@ # coding: utf-8 -from __future__ import print_function +from __future__ import print_function, unicode_literals -import sys -import os import pytest # NOQA -import platform # NOQA - -sys.path.insert(0, os.path.dirname(__file__) + '/lib') import warnings # NOQA - -args = [] - - -def test_data(): - import test_appliance # NOQA - - collections = [] - import test_yaml - - collections.append(test_yaml) - test_appliance.run(collections, args) - - -# @pytest.mark.skipif(not ruamel.yaml.__with_libyaml__, -# reason="no libyaml") - - -def test_data_ext(): - collections = [] - import ruamel.yaml # NOQA - import test_appliance # NOQA - - warnings.simplefilter('ignore', ruamel.yaml.error.UnsafeLoaderWarning) - if ruamel.yaml.__with_libyaml__: - import test_yaml_ext - - collections.append(test_yaml_ext) - test_appliance.run(collections, args) +from ruamel.std.pathlib import Path + +base_path = Path('data') # that is ruamel.yaml.data + + +class YAMLData(object): + yaml_tag = '!YAML' + + def __init__(self, s): + self._s = s + + # fmt: off + special = { + 'SPC': ' ', + 'TAB': '\t', + '---': '---', + '...': '...', + } + # fmt: on + + @property + def value(self): + if hasattr(self, '_p'): + return self._p + assert ' \n' not in self._s + assert '\t\n' not in self._s + self._p = self._s + for k, v in YAMLData.special.items(): + k = '<' + k + '>' + self._p = self._p.replace(k, v) + return self._p + + def test_rewrite(self, s): + assert ' \n' not in s + assert '\t\n' not in s + for k, v in YAMLData.special.items(): + k = '<' + k + '>' + s = s.replace(k, v) + return s + + @classmethod + def from_yaml(cls, constructor, node): + from ruamel.yaml.nodes import MappingNode + + if isinstance(node, MappingNode): + return cls(constructor.construct_mapping(node)) + return cls(node.value) + + +class Output(YAMLData): + yaml_tag = '!Output' + + +class Assert(YAMLData): + yaml_tag = '!Assert' + + @property + def value(self): + from ruamel.yaml.compat import Mapping + + if hasattr(self, '_pa'): + return self._pa + if isinstance(self._s, Mapping): + self._s['lines'] = self.test_rewrite(self._s['lines']) + self._pa = self._s + return self._pa + + +def pytest_generate_tests(metafunc): + from ruamel.yaml import YAML + + yaml = YAML(typ='safe', pure=True) + # yaml = YAML() + yaml.register_class(YAMLData) + yaml.register_class(Output) + yaml.register_class(Assert) + test_yaml = [] + paths = sorted(base_path.glob('*.yaml')) + idlist = [] + for path in paths: + idlist.append(path.stem) + x = yaml.load_all(path) + test_yaml.append([list(x)]) + metafunc.parametrize(['yaml'], test_yaml, ids=idlist, scope='class') + + +class TestYAMLData(object): + def yaml(self, yaml_version=None): + from ruamel.yaml import YAML + + y = YAML() + y.preserve_quotes = True + if yaml_version: + y.version = yaml_version + return y + + def yaml_load(self, value, yaml_version=None): + yaml = self.yaml(yaml_version=yaml_version) + data = yaml.load(value) + return yaml, data + + def run_rt(self, input, output=None, yaml_version=None): + from ruamel.yaml.compat import StringIO + + yaml, data = self.yaml_load(input.value, yaml_version=yaml_version) + buf = StringIO() + yaml.dump(data, buf) + expected = input.value if output is None else output.value + assert buf.getvalue() == expected + + def run_load_assert(self, input, confirm, yaml_version=None): + from ruamel.yaml.compat import Mapping + + d = self.yaml_load(input.value, yaml_version=yaml_version)[1] # NOQA + print('confirm.value', confirm.value, type(confirm.value)) + if isinstance(confirm.value, Mapping): + r = range(confirm.value['range']) + lines = confirm.value['lines'].splitlines() + for idx in r: # NOQA + for line in lines: + line = 'assert ' + line + print(line) + exec(line) + else: + for line in confirm.value.splitlines(): + line = 'assert ' + line + print(line) + exec(line) + + def test_yaml_data(self, yaml): + from ruamel.yaml.compat import Mapping + + idx = 0 + typ = None + yaml_version = None + if isinstance(yaml[0], Mapping): + d = yaml[0] + typ = d.get('type') + yaml_version = d.get('yaml_version') + idx += 1 + data = output = confirm = None + for doc in yaml[idx:]: + if isinstance(doc, Output): + output = doc + elif isinstance(doc, Assert): + confirm = doc + elif isinstance(doc, YAMLData): + data = doc + if typ is None: + if data is not None and output is not None: + typ = 'rt' + elif data is not None and confirm is not None: + typ = 'load_assert' + else: + assert data is not None + typ = 'rt' + print('type:', typ) + print('data:', data.value) + print('output:', output.value if output is not None else output) + if typ == 'rt': + self.run_rt(data, output, yaml_version=yaml_version) + elif typ == 'load_assert': + self.run_load_assert(data, confirm, yaml_version=yaml_version) + else: + assert False |