#!/usr/bin/env python3 # pylint: disable=wrong-import-position """Lots of unit tests for the new readline0.py.""" from __future__ import with_statement import os import unittest import readline0 class Tests(unittest.TestCase): # pylint: disable=R0904 # R0904: We need lots of public methods - we provide lots of tests """Unit tests for readline0.""" @classmethod def do_a_test(cls, byte_string, expected_list, blocksize): """Run one test with 3 parameters.""" with open('file.txt', 'wb') as file_: file_.write(byte_string) with open('file.txt', 'rb') as file_: actual_list = [] for line in readline0.readline0(file_=file_, blocksize=blocksize): actual_list.append(line) assert expected_list == actual_list, "\nExpected list: %s\nActual list: %s\n" % (expected_list, actual_list) os.unlink('file.txt') def test1_3_5_final_null(self): """1 3 5 final null.""" self.do_a_test(b'ab\0de\0gh\0ij\0kl\0', [b'ab', b'de', b'gh', b'ij', b'kl'], 1) def test1_3_5_no_final_null(self): """1 3 5 no final null.""" self.do_a_test(b'ab\0de\0gh\0ij\0kl', [b'ab', b'de', b'gh', b'ij', b'kl'], 1) def test2_3_5_final_null(self): """2 3 5 final null.""" self.do_a_test(b'ab\0de\0gh\0ij\0kl\0', [b'ab', b'de', b'gh', b'ij', b'kl'], 2) def test2_3_5_no_final_null(self): """2 3 5 no final null.""" self.do_a_test(b'ab\0de\0gh\0ij\0kl', [b'ab', b'de', b'gh', b'ij', b'kl'], 2) def test2_3_6_final_null(self): """2 3 6 final null.""" self.do_a_test(b'ab\0de\0gh\0ij\0kl\0mn\0', [b'ab', b'de', b'gh', b'ij', b'kl', b'mn'], 2) def test2_3_6_no_final_null(self): """2 3 6 no final null.""" self.do_a_test(b'ab\0de\0gh\0ij\0kl\0mn', [b'ab', b'de', b'gh', b'ij', b'kl', b'mn'], 2) def test2_4_6_final_null(self): """2 4 5 final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno\0', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 2) def test2_4_6_no_final_null(self): """2 4 5 no final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 2) def test3_4_6_final_null(self): """3 4 6 final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno\0', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 3) def test3_4_6_no_final_null(self): """3 4 6 no final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 3) def test4_4_6_final_null(self): """4 4 6 final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno\0', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 4) def test4_4_6_no_final_null(self): """4 4 6 no final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 4) def test5_4_6_final_null(self): """5 4 6 final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno\0', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 5) def test5_4_6_no_final_null(self): """5 4 6 no final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 5) def test8_4_6_final_null(self): """8 4 6 final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno\0', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 8) def test8_4_6_no_final_null(self): """8 4 6 no final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 8) def test9_4_6_final_null(self): """9 4 6 final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno\0', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 9) def test9_4_6_no_final_null(self): """9 4 6 no final null.""" self.do_a_test(b'abc\0def\0ghi\0ijk\0klm\0mno', [b'abc', b'def', b'ghi', b'ijk', b'klm', b'mno'], 9) unittest.main()