def test_bytes(self): for errtype in ( hprof.error.HprofError, hprof.error.FormatError, hprof.error.UnexpectedEof, hprof.error.UnhandledError, ): for indata, insize in indatas(): with self.subTest((errtype, indata)): with patch('hprof._parsing._parse', side_effect=errtype('boo')): with self.assertRaises(errtype, msg='boo'): hprof.parse(indata)
def test_parse(self, mock): for j in (0, 1): for i, (indata, insize) in enumerate(indatas()): progress = MagicMock() if j else None mock.reset_mock() with self.subTest(indata, progresscb=progress): out = hprof.parse(indata, progress) self.assertEqual(mock.call_count, 1) (hf, arg, cb), kwarg = mock.call_args self.assertFalse(kwarg) self.assertIsInstance(arg, memoryview) self.assertEqual(arg, b'Hello World!\n') self.assertIs(cb, progress) self.assertIs(hf, out) if progress: if i < 2: self.assertEqual(progress.call_count, 0) else: self.assertEqual(progress.call_count, 3) self.assertEqual(progress.call_args_list[0][0], ('extracting', 0, insize)) self.assertEqual(progress.call_args_list[0][1], {}) self.assertEqual( progress.call_args_list[1][0], ('extracting', insize - 1, insize)) self.assertEqual(progress.call_args_list[1][1], {}) self.assertEqual(progress.call_args_list[2][0], ('extracting', insize, insize)) self.assertEqual(progress.call_args_list[2][1], {}) with hf as tmp: self.assertIs(tmp, hf) self.assertEqual(arg[0], ord('H')) with self.assertRaisesRegex(ValueError, 'released'): arg[0]
def test_string_error(self, mock): with self.assertRaises(TypeError): hprof.parse('Hello String!')
def test_text_file_error(self, mock): with open('testdata/helloworld.txt') as f: with self.assertRaises(TypeError): hprof.parse(f)