(self, pack_into)
| 435 | (b'cd01',)) |
| 436 | |
| 437 | def _test_pack_into(self, pack_into): |
| 438 | test_string = b'Reykjavik rocks, eow!' |
| 439 | writable_buf = memoryview(array.array('b', b' '*100)) |
| 440 | |
| 441 | # Test without offset |
| 442 | pack_into(writable_buf, 0, test_string) |
| 443 | from_buf = writable_buf.tobytes()[:len(test_string)] |
| 444 | self.assertEqual(from_buf, test_string) |
| 445 | |
| 446 | # Test with offset. |
| 447 | pack_into(writable_buf, 10, test_string) |
| 448 | from_buf = writable_buf.tobytes()[:len(test_string)+10] |
| 449 | self.assertEqual(from_buf, test_string[:10] + test_string) |
| 450 | |
| 451 | # Test with negative offset. |
| 452 | pack_into(writable_buf, -30, test_string) |
| 453 | from_buf = writable_buf.tobytes()[-30:-30+len(test_string)] |
| 454 | self.assertEqual(from_buf, test_string) |
| 455 | |
| 456 | # Go beyond boundaries. |
| 457 | small_buf = array.array('b', b' '*10) |
| 458 | with self.assertRaises((ValueError, struct.error)): |
| 459 | pack_into(small_buf, 0, test_string) |
| 460 | with self.assertRaises((ValueError, struct.error)): |
| 461 | pack_into(writable_buf, 90, test_string) |
| 462 | with self.assertRaises((ValueError, struct.error)): |
| 463 | pack_into(writable_buf, -10, test_string) |
| 464 | with self.assertRaises((ValueError, struct.error)): |
| 465 | pack_into(writable_buf, 150, test_string) |
| 466 | with self.assertRaises((ValueError, struct.error)): |
| 467 | pack_into(writable_buf, -150, test_string) |
| 468 | |
| 469 | # Test invalid buffer. |
| 470 | self.assertRaises(TypeError, pack_into, b' '*100, 0, test_string) |
| 471 | self.assertRaises(TypeError, pack_into, ' '*100, 0, test_string) |
| 472 | self.assertRaises(TypeError, pack_into, [0]*100, 0, test_string) |
| 473 | self.assertRaises(TypeError, pack_into, None, 0, test_string) |
| 474 | self.assertRaises(TypeError, pack_into, writable_buf[::2], 0, test_string) |
| 475 | self.assertRaises(TypeError, pack_into, writable_buf[::-1], 0, test_string) |
| 476 | |
| 477 | # Test bogus offset (issue bpo-3694) |
| 478 | with self.assertRaises(TypeError): |
| 479 | pack_into(writable_buf, None, test_string) |
| 480 | with self.assertRaises(TypeError): |
| 481 | pack_into(writable_buf, 0.0, test_string) |
| 482 | with self.assertRaises((IndexError, OverflowError)): |
| 483 | pack_into(writable_buf, 2**1000, test_string) |
| 484 | with self.assertRaises((IndexError, OverflowError)): |
| 485 | pack_into(writable_buf, -2**1000, test_string) |
| 486 | |
| 487 | @unittest.expectedFailure # TODO: RUSTPYTHON; BufferError: non-contiguous buffer is not a bytes-like object |
| 488 | def test_pack_into(self): |
no test coverage detected