Skip to content

Commit

Permalink
assert iterator return str
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Sep 6, 2024
1 parent aeba5a7 commit 1d7adda
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def test_reverse_readline(self):
"""
with open(os.path.join(TEST_DIR, "3000_lines.txt"), encoding="utf-8") as f:
for idx, line in enumerate(reverse_readline(f)):
assert isinstance(line, str)
assert (
int(line) == self.NUMLINES - idx
), f"read_backwards read {line} whereas it should have read {self.NUMLINES - idx}"
Expand All @@ -98,6 +99,7 @@ def test_reverse_readline_fake_big(self):
"""
with open(os.path.join(TEST_DIR, "3000_lines.txt"), encoding="utf-8") as f:
for idx, line in enumerate(reverse_readline(f, max_mem=0)):
assert isinstance(line, str)
assert (
int(line) == self.NUMLINES - idx
), f"read_backwards read {line} whereas it should have read {self.NUMLINES - idx}"
Expand All @@ -110,8 +112,9 @@ def test_reverse_readline_bz2(self):
lines = []
with zopen(os.path.join(TEST_DIR, "myfile_bz2.bz2"), "rb") as f:
for line in reverse_readline(f):
lines.append(line.strip())
assert lines[-1].strip() == "HelloWorld."
lines.append(line)
assert lines == ["\n", "\n", "HelloWorld."] # test file has two empty lines
assert all(isinstance(line, str) for line in lines)

def test_empty_file(self):
"""
Expand All @@ -133,7 +136,8 @@ def test_line_ending(self, l_end):

with open("test_file.txt", "r", encoding="utf-8") as file:
for idx, line in enumerate(reverse_readline(file)):
assert line.strip() == contents[len(contents) - idx - 1]
assert line == contents[len(contents) - idx - 1]
assert isinstance(line, str)


class TestReverseReadfile:
Expand All @@ -146,6 +150,7 @@ def test_reverse_readfile(self):
"""
fname = os.path.join(TEST_DIR, "3000_lines.txt")
for idx, line in enumerate(reverse_readfile(fname)):
assert isinstance(line, str)
assert int(line) == self.NUMLINES - idx

def test_reverse_readfile_gz(self):
Expand All @@ -155,6 +160,7 @@ def test_reverse_readfile_gz(self):
"""
fname = os.path.join(TEST_DIR, "3000_lines.txt.gz")
for idx, line in enumerate(reverse_readfile(fname)):
assert isinstance(line, str)
assert int(line) == self.NUMLINES - idx

def test_reverse_readfile_bz2(self):
Expand All @@ -164,6 +170,7 @@ def test_reverse_readfile_bz2(self):
"""
fname = os.path.join(TEST_DIR, "3000_lines.txt.bz2")
for idx, line in enumerate(reverse_readfile(fname)):
assert isinstance(line, str)
assert int(line) == self.NUMLINES - idx

def test_empty_file(self):
Expand All @@ -185,7 +192,8 @@ def test_line_ending(self, l_end):

with open("test_file.txt", "r", encoding="utf-8") as file:
for idx, line in enumerate(reverse_readline(file)):
assert line.strip() == contents[len(contents) - idx - 1]
assert isinstance(line, str)
assert line == contents[len(contents) - idx - 1]


class TestZopen:
Expand Down

0 comments on commit 1d7adda

Please sign in to comment.