示例#1
0
 def test_many_lines(self):
     numbers = range(1000)
     filename = 'newlines_at_end.txt'
     contents = "\n".join("line {}".format(i) for i in numbers)
     with patch_open(contents, filename):
         for i, line in zip(reversed(numbers), last_lines(filename)):
             self.assertEqual(line.rstrip(), "line {}".format(i))
 def test_iterator(self):
     filename = 'iterator_test'
     contents = dedent("""
         This is the start
         Of a file
         This is the end
     """).strip()
     with patch_open(contents, filename):
         lines = last_lines(filename)
         self.assertEqual(next(lines), "This is the end")
     self.assertEqual(next(lines), "Of a file\n")
     self.assertEqual(next(lines), "This is the start\n")
     with self.assertRaises(StopIteration):
         next(lines)
     self.assertEqual(list(lines), [])
 def test_lazy_reading(self):
     contents = "hi\n" * 10000
     filename = 'lazy reading'
     with patch_open(contents, filename, max_read=DEFAULT_BUFFER_SIZE) as fake_open:
         lines = last_lines(filename)
         self.assertEqual(len(fake_open.file.reads), 0, 'no reads yet')
         for i in range(10000):
             if i == 100:
                 self.assertLess(
                     len(fake_open.file.reads),
                     3,
                     'should really only be 1 read at this point',
                 )
             self.assertEqual(next(lines), "hi\n")
         self.assertEqual(next(lines, None), None)
     self.assertLess(
         len(fake_open.file.reads),
         10,
         'whole file should be less than 10 reads',
     )
 def test_unicode_characters(self):
     contents = (
         '\U0001f496 \U0001f601\U0001f31f \u26c4\u274c \u274c \xa9\n'
         '\u2728\u274c \U0001f31f \U0001f496 \U0001f496 \U0001f496 \u2728\n'
         '\u26c4 \U0001f496 \u2728\U0001f496 \u26c4\U0001f601 \U0001f31f\n'
         '\u2728 \U0001f601 \U0001f496 \u26c4 \u274c \u2728 \U0001f31f\n'
         '\U0001f496 \U0001f31f \u2728 \U0001f496 \U0001f601 \u26c4\n'
         '\U0001f496 \xc0 \u26c4 \u26c4 \u274c \U0001f31f \u2728\n'
         '\U0001f31f \U0001f31f \u2728 \U0001f601 \U0001f496 \U0001f31f\n'
         '\U0001f601 \U0001f601 \U0001f31f \u26c4 \U0001f31f \u26c4\n'
         '\U0001f496 \U0001f31f \U0001f31f \u274c \U0001f31f \U0001f31f\n'
         '\U0001f601\u274c\u26c4\u2728\U0001f601\u26c4\u274c\n') * 100
     file_lines = contents.splitlines()
     filename = 'lazy reading'
     with patch_open(contents, filename, max_read=DEFAULT_BUFFER_SIZE):
         lines = last_lines(filename)
         i = 0
         for actual, expected in zip(lines, reversed(file_lines)):
             i += 1
             self.assertEqual(actual.rstrip('\n'), expected)
 def test_newlines_at_end(self):
     contents = "text!\n\n\n\n\n"
     filename = 'newlines_at_end.txt'
     reversed_lines = ["\n", "\n", "\n", "\n", "text!\n"]
     with patch_open(contents, filename):
         self.assertEqual(list(last_lines(filename)), reversed_lines)
 def test_two_newlines_with_one_at_the_end(self):
     contents = "two lines\nof text\n"
     filename = 'hello.txt'
     reversed_lines = ["of text\n", "two lines\n"]
     with patch_open(contents, filename):
         self.assertEqual(list(last_lines(filename)), reversed_lines)
 def test_one_newline_in_middle(self):
     contents = "two lines\nof text"
     filename = 'my file.txt'
     reversed_lines = ["of text", "two lines\n"]
     with patch_open(contents, filename):
         self.assertEqual(list(last_lines(filename)), reversed_lines)
 def test_one_newline_at_the_end(self):
     contents = "a single line of text\n"
     filename = 'file2.txt'
     with patch_open(contents, filename):
         self.assertEqual(list(last_lines(filename)), [contents])
 def test_no_newlines(self):
     contents = "a single line of text"
     filename = 'file1.txt'
     with patch_open(contents, filename):
         self.assertEqual(list(last_lines(filename)), [contents])