Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Feb 25, 2024
1 parent a85ada9 commit 175e647
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 5 deletions.
13 changes: 8 additions & 5 deletions nestedtext/nestedtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,12 +591,12 @@ def as_line(self, kind="value", offset=0):
Specify either “key” or “value” depending on which token is
desired.
offset:
If offset is None, the error pointer is not added to the line.
If offset is an integer, the pointer is moved to the right by
If *offset* is None, the error pointer is not added to the line.
If *offset* is an integer, the pointer is moved to the right by
this many characters. The default is 0.
If offset is a tuple, it must have two values. The first is the
row offset and the second is the column offset. This is useful
for annotating errors in multiline strings.
If *offset* is a tuple, it must have two values. The first is
the row offset and the second is the column offset. This is
useful for annotating errors in multiline strings.
"""
# get the line and the column number of the key or value
if kind == "key":
Expand All @@ -611,6 +611,9 @@ def as_line(self, kind="value", offset=0):
line = self.line
col = self.col

if not line: # this occurs if input is completely empty
return ""

# process the offset
if offset is None:
return line.render()
Expand Down
108 changes: 108 additions & 0 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 inform import Error, Info, render, indent, join, dedent
from quantiphy import Quantity
import subprocess

test_api = Path(__file__).parent / 'official_tests' / 'api'
import sys; sys.path.append(str(test_api))
Expand Down Expand Up @@ -1229,6 +1230,82 @@ def test_load_dialect():
data = nt.loads(document, dialect="i")
assert data == {"[7:0] data":"", "{7:0} bits":""}

# test_empty {{{2
@parametrize(
"top,expected", [(any,None), (dict,{}), (list,[]), (str,"")]
)
def test_empty_blank_lines(top, expected):
# just blank lines
document = ""

keymap = {}
data = nt.loads(document, top=top, keymap=keymap)
assert data == expected
assert keymap[()].as_line(offset=None) == ''

# test_empty_blank_lines {{{2
@parametrize(
"top,expected", [(any,None), (dict,{}), (list,[]), (str,"")]
)
def test_empty_blank_lines(top, expected):
# just blank lines
document = "\n\n\n"

keymap = {}
data = nt.loads(document, top=top, keymap=keymap)
assert data == expected
assert keymap[()].as_line(offset=None) == ' 4 ❬❭'

# test_empty_comments {{{2
@parametrize(
"top,expected", [(any,None), (dict,{}), (list,[]), (str,"")]
)
def test_empty_comments(top, expected):
# just blank lines
document = "#comment 0\n#comment 1\n#comment 2\n# comment3"

keymap = {}
data = nt.loads(document, top=top, keymap=keymap)
assert data == expected
assert keymap[()].as_line(offset=None) == ' 4 ❬# comment3❭'

# test_empty_stdin {{{2
@parametrize(
"top,expected", [(any,None), (dict,{}), (list,[]), (str,"")]
)
def test_empty_stdin(monkeypatch, top, expected):
# just blank lines
document = ""
monkeypatch.setattr('sys.stdin', StringIO(document))

keymap = {}
data = nt.load(sys.stdin, top=top, keymap=keymap, source="❬stdin❭")
assert data == expected
assert keymap[()].as_line(offset=None) == ''

# test_empty_dev_null {{{2
@parametrize(
"top,expected", [(any,None), (dict,{}), (list,[]), (str,"")]
)
def test_empty_dev_null(monkeypatch, top, expected):
# just blank lines
keymap = {}
data = nt.load("/dev/null", top=top, keymap=keymap, source="❬stdin❭")
assert data == expected
assert keymap[()].as_line(offset=None) == ''

# test_empty_fd0 {{{2
def test_empty_fd0(monkeypatch):
# I only seem to get one shot at testing 0 as a file descriptor

# load stdin, which will be empty
monkeypatch.setattr('sys.stdin', StringIO(''))
keymap = {}
data = nt.load(0, keymap=keymap)
assert data == {}
assert keymap[()].as_line(offset=None) == ''


# Test dump {{{1
# test_dump_success_cases {{{2
@parametrize_dump_api
Expand Down Expand Up @@ -1899,4 +1976,35 @@ def test_dump_dialect():
list:
""", strip_nl="b")

# Test round-trip {{{1
# test_file_descriptors {{{2
def test_file_descriptors(tmp_path):
# program that writes out a simple NT document to stdout
write_data = dedent("""
import nestedtext
data = {"dict": {}, "list": [], "str": "", "mls":"\\n"}
nestedtext.dump(data, 1)
""")
writer = tmp_path / "writer.py"
# program that reads a simple NT document from stderr
read_data = dedent("""
import nestedtext
data = nestedtext.load(0)
assert data == {"dict": {}, "list": [], "str": "", "mls":"\\n"}
""")
reader = tmp_path / "reader.py"

writer.write_text(write_data)
reader.write_text(read_data)

results = subprocess.run(
f"python {writer!s} | python {reader!s}",
shell = True,
capture_output = True,
env = {"COVERAGE_PROCESS_START":""},
)
assert results.stdout == b""
assert results.stderr == b""
assert results.returncode == 0

# vim: fdm=marker

0 comments on commit 175e647

Please sign in to comment.