diff --git a/Tests/images/dummy.container b/Tests/images/dummy.container new file mode 100644 index 000000000..83e7a3560 Binary files /dev/null and b/Tests/images/dummy.container differ diff --git a/Tests/test_file_container.py b/Tests/test_file_container.py index 56d53fc61..55228be0c 100644 --- a/Tests/test_file_container.py +++ b/Tests/test_file_container.py @@ -3,6 +3,8 @@ from helper import unittest, PillowTestCase, hopper from PIL import Image from PIL import ContainerIO +TEST_FILE = "Tests/images/dummy.container" + class TestFileContainer(PillowTestCase): @@ -16,6 +18,112 @@ class TestFileContainer(PillowTestCase): self.assertEqual(container.isatty(), 0) + def test_seek_mode_0(self): + # Arrange + mode = 0 + with open(TEST_FILE) as fh: + container = ContainerIO.ContainerIO(fh, 22, 100) + + # Act + container.seek(33, mode) + container.seek(33, mode) + + # Assert + self.assertEqual(container.tell(), 33) + + def test_seek_mode_1(self): + # Arrange + mode = 1 + with open(TEST_FILE) as fh: + container = ContainerIO.ContainerIO(fh, 22, 100) + + # Act + container.seek(33, mode) + container.seek(33, mode) + + # Assert + self.assertEqual(container.tell(), 66) + + def test_seek_mode_2(self): + # Arrange + mode = 2 + with open(TEST_FILE) as fh: + container = ContainerIO.ContainerIO(fh, 22, 100) + + # Act + container.seek(33, mode) + container.seek(33, mode) + + # Assert + self.assertEqual(container.tell(), 100) + + def test_read_n0(self): + # Arrange + with open(TEST_FILE) as fh: + container = ContainerIO.ContainerIO(fh, 22, 100) + + # Act + container.seek(81) + data = container.read() + + # Assert + self.assertEqual(data, "7\nThis is line 8\n") + + def test_read_n(self): + # Arrange + with open(TEST_FILE) as fh: + container = ContainerIO.ContainerIO(fh, 22, 100) + + # Act + container.seek(81) + data = container.read(3) + + # Assert + self.assertEqual(data, "7\nT") + + def test_read_eof(self): + # Arrange + with open(TEST_FILE) as fh: + container = ContainerIO.ContainerIO(fh, 22, 100) + + # Act + container.seek(100) + data = container.read() + + # Assert + self.assertEqual(data, "") + + def test_readline(self): + # Arrange + with open(TEST_FILE) as fh: + container = ContainerIO.ContainerIO(fh, 0, 120) + + # Act + data = container.readline() + + # Assert + self.assertEqual(data, "This is line 1\n") + + def test_readlines(self): + # Arrange + expected = ["This is line 1\n", + "This is line 2\n", + "This is line 3\n", + "This is line 4\n", + "This is line 5\n", + "This is line 6\n", + "This is line 7\n", + "This is line 8\n"] + with open(TEST_FILE) as fh: + container = ContainerIO.ContainerIO(fh, 0, 120) + + # Act + data = container.readlines() + + # Assert + + self.assertEqual(data, expected) + if __name__ == '__main__': unittest.main()