MCPcopy Index your code
hub / github.com/python/cpython / test_custom_struct_new

Method test_custom_struct_new

Lib/test/test_struct.py:897–916  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

895 self.assertEqual(my_struct.pack(12345), b'\x30\x39')
896
897 def test_custom_struct_new(self):
898 # New way, no warnings:
899 class MyStruct(struct.Struct):
900 def __new__(cls, *args, **kwargs):
901 return super().__new__(cls, '>h')
902
903 for format in '>h', '<h', 42, '$', '\u20ac', '\udc00', b'\xa4':
904 with self.subTest(format=format):
905 my_struct = MyStruct(format)
906 self.assertEqual(my_struct.format, '>h')
907 self.assertEqual(my_struct.pack(12345), b'\x30\x39')
908 my_struct = MyStruct(format='<h')
909 self.assertEqual(my_struct.format, '>h')
910 self.assertEqual(my_struct.pack(12345), b'\x30\x39')
911 my_struct = MyStruct()
912 self.assertEqual(my_struct.format, '>h')
913 self.assertEqual(my_struct.pack(12345), b'\x30\x39')
914 my_struct = MyStruct('<h', 42)
915 self.assertEqual(my_struct.format, '>h')
916 self.assertEqual(my_struct.pack(12345), b'\x30\x39')
917
918 def test_custom_struct_new_and_init(self):
919 # New way, no warnings:

Callers

nothing calls this directly

Calls 4

MyStructClass · 0.85
subTestMethod · 0.45
assertEqualMethod · 0.45
packMethod · 0.45

Tested by

no test coverage detected