Skip to content

Commit

Permalink
Refine behavior on unusual cases. Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Jul 8, 2021
1 parent e1e404c commit 32c858e
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 34 deletions.
41 changes: 18 additions & 23 deletions nestedtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,20 +485,18 @@ def __init__(self, line=None, col=None, key_line=None, key_col=None):
self.key_col = key_col

def __repr__(self):
from inform import indent
from collection import Collection
lines = [f"{self.__class__.__name__}("]
for each in ['line', 'col', 'key_line', 'key_col']:
val = getattr(self, each)
if val is not None:
lines.append(indent(f"{each} = {val},"))
if self.children:
lines.append(indent("children = ("))
for k, v in Collection(self.children).items():
lines.append(indent(f"{k}: {v!r}", stops=2))
lines.append(indent('), ('))
lines[-1] = indent('),')
return '\n'.join(lines) + '\n)'
components = []
components.append(f"lineno={self.line.lineno}")
components.append(f"colno={self.col}")
key_line = self.key_line
if key_line is None:
key_line = self.line
components.append(f"key_lineno={key_line.lineno}")
key_col = self.key_col
if key_col is None:
key_col = self.col
components.append(f"key_colno={key_col}")
return f"{self.__class__.__name__}({', '.join(components)})"

# as_tuple() {{{3
def as_tuple(self, kind='value'):
Expand Down Expand Up @@ -537,7 +535,7 @@ def as_line(self, kind='value'):
Specify either 'key' or 'value' depending on which token is
desired.
"""
if kind == 'key' and self.key_line:
if kind == 'key':
line = self.key_line
col = self.key_col
if line is None:
Expand Down Expand Up @@ -719,11 +717,11 @@ def get_values(self):
return self.values, self.keymap

# render {{{3
def render(self, index):
def render(self, index): # pragma: no cover
return f"«{self.text}»\n {index*' '}▲"

# __repr__ {{{3
def __repr__(self):
def __repr__(self): # pragma: no cover
name = self.__class__.__name__
return f"{name}({self.text!r})"

Expand Down Expand Up @@ -1249,15 +1247,15 @@ def convert(self, obj):
def render_key(self, key):
key = self.convert(key)
if self.is_a_scalar(key):
key = convert_returns(str(key))
key = str(key)
if not self.is_a_str(key) and callable(self.default):
key = self.default(key)
if not self.is_a_str(key):
raise NestedTextError(
template = 'keys must be strings.',
culprit = key
) from None
return key
return convert_returns(key)

# render_dict_item {{{3
def render_dict_item(self, key, value, level):
Expand Down Expand Up @@ -1318,9 +1316,6 @@ def render_inline_list(self, obj):
def render_inline_scalar(self, obj, exclude):
obj = self.convert(obj)
if self.is_a_str(obj):
stripped = obj.strip()
if len(obj) > len(stripped):
raise NotSuitableForInline from None
value = obj
elif self.is_a_scalar(obj):
value = '' if obj is None else str(obj)
Expand All @@ -1339,7 +1334,7 @@ def render_inline_scalar(self, obj, exclude):

if exclude & set(value):
raise NotSuitableForInline from None
if value.strip(' ') != value:
if value.strip() != value:
raise NotSuitableForInline from None
return value

Expand Down
171 changes: 160 additions & 11 deletions tests/test_nestedtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from io import StringIO
from textwrap import dedent
from inform import Error, Info, render, indent
from quantiphy import Quantity

test_api = Path(__file__).parent / 'official_tests' / 'api'
import sys; sys.path.append(str(test_api))
Expand Down Expand Up @@ -780,14 +781,28 @@ def fix_key(key):
keymap = {}
addresses = nt.loads(document, keymap=keymap)

doc_lines = document.splitlines()

for case in cases:
given, expected = case.split('→')
keys = tuple(fix_key(n) for n in given.split())
expected = tuple(int(n) for n in expected.split())
location = keymap[keys]
assert location.as_tuple() == (expected[2], expected[3]), keys
assert location.as_tuple('value') == (expected[2], expected[3]), keys
assert location.as_tuple('key') == (expected[0], expected[1]), keys
key_lineno, key_colno, lineno, colno = expected
assert location.as_tuple() == (lineno, colno), keys
assert location.as_tuple('value') == (lineno, colno), keys
assert location.as_tuple('key') == (key_lineno, key_colno), keys

assert location.line.render() == f'{lineno:>4} «{doc_lines[lineno]}»'
rendered = f"{lineno:>4} «{doc_lines[lineno]}»\n {colno*' '}▲"
assert location.line.render(colno) == rendered
assert location.as_line() == rendered
assert location.as_line('value') == rendered
rendered = f"{key_lineno:>4} «{doc_lines[key_lineno]}»\n {key_colno*' '}▲"
assert location.as_line('key') == rendered
assert str(location.line) == doc_lines[lineno]
assert repr(location.line) == f'Line({lineno}: «{doc_lines[lineno]}»)'
assert repr(location) == f"Location(lineno={lineno}, colno={colno}, key_lineno={key_lineno}, key_colno={key_colno})"


# Test dump {{{1
Expand Down Expand Up @@ -819,18 +834,82 @@ def test_dump_error_cases(dump, data_in, culprit, message, tmp_path):
# test_dump_default {{{2
@parametrize_dump_api
def test_dump_default(dump, tmp_path):
data = dict(none=None, true=True, false=False, empty_dict={}, empty_list=[])
data = dict(
none = None,
true = True,
false = False,
empty_dict = {},
empty_list = [],
zero = 0
)

expected = dedent('''\
none:
true: True
false: False
empty_dict:
{}
empty_list:
[]
zero: 0
''').strip()
assert dump(data, tmp_path) == expected
assert dump(data, tmp_path, default=repr) == expected

with pytest.raises(nt.NestedTextError):
data = dict(none = None)
dump(data, tmp_path, default='strict')

with pytest.raises(nt.NestedTextError):
data = {None: 'none'}
dump(data, tmp_path, default='strict')

with pytest.raises(nt.NestedTextError):
data = dict(true = True)
dump(data, tmp_path, default='strict')

with pytest.raises(nt.NestedTextError):
data = {True: 'true'}
dump(data, tmp_path, default='strict')

with pytest.raises(nt.NestedTextError):
data = dict(false = False)
dump(data, tmp_path, default='strict')

with pytest.raises(nt.NestedTextError):
data = {False: 'false'}
dump(data, tmp_path, default='strict')

with pytest.raises(nt.NestedTextError):
data = dict(zero = 0)
dump(data, tmp_path, default='strict')

with pytest.raises(nt.NestedTextError):
data = {0: 'zero'}
dump(data, tmp_path, default='strict')

data = {
None: 'none',
True: 'true',
False: 'false',
3: 'three',
}

assert dump(data, tmp_path) == dedent('''\
none:
true: True
false: False
empty_dict:
{}
empty_list:
[]
None: none
True: true
False: false
3: three
''').strip()

assert dump(data, tmp_path, default=repr) == dedent('''\
None: none
True: true
False: false
3: three
''').strip()


# test_dump_sort_keys {{{2
@parametrize_dump_api
def test_dump_sort_keys(dump, tmp_path):
Expand Down Expand Up @@ -975,6 +1054,12 @@ def __nestedtext_converter__(self):
result = dump(y, tmp_path, converters=converters, width=99)
expected = '{date: 1969-07-20}'
assert result == expected
result = dump(y, tmp_path, default=str)
expected = 'date: 1969-07-20T00:00:00+00:00'
assert result == expected
result = dump(y, tmp_path, default=str, width=99)
expected = 'date: 1969-07-20T00:00:00+00:00'
assert result == expected

# arrow object as key
y = {given: 'moon landing'}
Expand All @@ -984,6 +1069,45 @@ def __nestedtext_converter__(self):
result = dump(y, tmp_path, converters=converters, width=99)
expected = '{1969-07-20: moon landing}'
assert result == expected
result = dump(y, tmp_path, default=str)
expected = '1969-07-20T00:00:00+00:00: moon landing'
assert result == expected
result = dump(y, tmp_path, default=str, width=99)
expected = '1969-07-20T00:00:00+00:00: moon landing'
assert result == expected

# converting quantity object
converters = {Quantity: lambda q: q.render(prec=8)}

# arrow object as value
y = {'c': Quantity('c')}
result = dump(y, tmp_path, converters=converters)
expected = 'c: 299.792458 Mm/s'
assert result == expected
result = dump(y, tmp_path, converters=converters, width=99)
expected = '{c: 299.792458 Mm/s}'
assert result == expected
result = dump(y, tmp_path, default=str)
expected = 'c: 299.79 Mm/s'
assert result == expected
result = dump(y, tmp_path, default=str, width=99)
expected = '{c: 299.79 Mm/s}'
assert result == expected

# arrow object as key
y = {Quantity('c'): 'c'}
result = dump(y, tmp_path, converters=converters)
expected = '299.792458 Mm/s: c'
assert result == expected
result = dump(y, tmp_path, converters=converters, width=99)
expected = '{299.792458 Mm/s: c}'
assert result == expected
result = dump(y, tmp_path, default=str)
expected = '299.79 Mm/s: c'
assert result == expected
result = dump(y, tmp_path, default=str, width=99)
expected = '{299.79 Mm/s: c}'
assert result == expected

# integer object as key
y = {0: 'zero', 'one': 'one', 'four': 'four'}
Expand Down Expand Up @@ -1198,6 +1322,31 @@ def test_dump_converters_err(dump, tmp_path, data, culprit, kwargs):
""").strip(),
dict(inline_level=0, width=20)
),
({' a': 'A'}, ': a\n > A', dict(width=80)),
({'a ': 'A'}, ': a \n > A', dict(width=80)),
({'\ta': 'A'}, ': \ta\n > A', dict(width=80)),
({'a\t': 'A'}, ': a\t\n > A', dict(width=80)),
({'a,b': 'A'}, 'a,b: A', dict(width=80)),
({'a:b': 'A'}, 'a:b: A', dict(width=80)),
({'a[b': 'A'}, 'a[b: A', dict(width=80)),
({'a]b': 'A'}, 'a]b: A', dict(width=80)),
({'a{b': 'A'}, 'a{b: A', dict(width=80)),
({'a}b': 'A'}, 'a}b: A', dict(width=80)),
({'a\nb': 'A'}, ': a\n: b\n > A', dict(width=80)),
({'a\rb': 'A'}, ': a\n: b\n > A', dict(width=80)),
({'A': ' a'}, 'A: a', dict(width=80)),
({'A': 'a '}, 'A: a ', dict(width=80)),
({'A': '\ta'}, 'A: \ta', dict(width=80)),
({'A': 'a\t'}, 'A: a\t', dict(width=80)),
({'A': 'a,b'}, 'A: a,b', dict(width=80)),
({'A': 'a:b'}, 'A: a:b', dict(width=80)),
({'A': 'a[b'}, 'A: a[b', dict(width=80)),
({'A': 'a]b'}, 'A: a]b', dict(width=80)),
({'A': 'a{b'}, 'A: a{b', dict(width=80)),
({'A': 'a}b'}, 'A: a}b', dict(width=80)),
({'A': 'a\nb'}, 'A:\n > a\n > b', dict(width=80)),
({'A': 'a\rb'}, 'A:\n > a\n > b', dict(width=80)),
]
)
def test_dump_width(dump, tmp_path, given, expected, kwargs):
Expand Down

0 comments on commit 32c858e

Please sign in to comment.