]> Cypherpunks repositories - keks.git/commitdiff
Split strings testing
authorSergey Matveev <stargrave@stargrave.org>
Sat, 30 Nov 2024 18:01:35 +0000 (21:01 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sat, 30 Nov 2024 19:38:56 +0000 (22:38 +0300)
Binary and Unicode strings are two distinct data types.
However, of course, sharing big amount of code.

pyac/tests/test_str.py

index ae1d8fc6fc9dcd6ced3e4a9f6eceff85d9681c600eb1c67e43217e65021912cd..2b808554b5142b6f5b13beab6f6799f5c8e30f171b343dbf92b0c3bfaedcc4e8 100644 (file)
@@ -25,16 +25,16 @@ invalid_utf8_3byte_s = integers(min_value=0, max_value=(1 << 10) - 1).map(
 )
 
 
-class TestString(TestCase):
+class TestStr(TestCase):
     @given(text(max_size=60))
-    def test_encode_utf8(self, test_str: str) -> None:
+    def test_encode(self, test_str: str) -> None:
         if(len(test_str.encode("utf-8")) > 60):
             return
         encoded = dumps(test_str)
         tag = (TagStr | TagUTF8 | len(test_str.encode("utf-8"))).to_bytes(1, "big")
         self.assertSequenceEqual(encoded, tag + test_str.encode("utf-8"))
 
-    def test_long_utf8(self) -> None:
+    def test_long(self) -> None:
         long_strings = ["a" * 62, "a" * 318, "a" * 65853]
         for long_string in long_strings:
             encoded = dumps(long_string)
@@ -42,7 +42,35 @@ class TestString(TestCase):
             self.assertSequenceEqual(decoded, long_string)
             self.assertSequenceEqual(tail, b"")
 
-    def test_encode_non_utf8(self) -> None:
+    def test_throws_when_null_byte_in_utf(self) -> None:
+        with self.assertRaises(DecodeError) as err:
+            result, tail = loads(b"\xc5he\x00\x01\x02")
+        self.assertEqual(str(err.exception), "null byte in UTF-8")
+
+    @given(invalid_utf8_2byte_s)
+    def test_throws_when_invalid_utf_2bytes(self, invalid_utf: bytes) -> None:
+        with self.assertRaises(DecodeError) as err:
+            encoded = dumps("hello")[:-1] + invalid_utf
+            result, tail = loads(encoded)
+        self.assertEqual(str(err.exception), "invalid UTF-8")
+
+    @given(invalid_utf8_3byte_s)
+    def test_throws_when_invalid_utf_3bytes(self, invalid_utf: bytes) -> None:
+        with self.assertRaises(DecodeError) as err:
+            encoded = dumps("hello")[:-2] + invalid_utf
+            result, tail = loads(encoded)
+        self.assertEqual(str(err.exception), "invalid UTF-8")
+
+    def test_throws_when_not_enough_data_for_length(self) -> None:
+        long_string = "a" * 318
+        encoded = dumps(long_string)[:2]
+        with self.assertRaises(NotEnoughData) as err:
+            loads(encoded)
+        self.assertEqual(err.exception.n, 3)
+
+
+class TestBin(TestCase):
+    def test_encode(self) -> None:
         bs = b"\x00\x01\x02"
         encoded = dumps(bs)
         self.assertSequenceEqual(encoded, b"\x83\x00\x01\x02")
@@ -50,7 +78,7 @@ class TestString(TestCase):
         self.assertSequenceEqual(decoded, b"\x00\x01\x02")
         self.assertSequenceEqual(tail, b"")
 
-    def test_long_non_utf8(self) -> None:
+    def test_long(self) -> None:
         long_bss = [b"\x01" * 62, b"\x01" * 318, b"\x01" * 65853]
         for long_bs in long_bss:
             encoded = dumps(long_bs)
@@ -65,27 +93,8 @@ class TestString(TestCase):
         self.assertEqual(err.exception.n, 6)
 
     def test_throws_when_not_enough_data_for_length(self) -> None:
-        long_string = "a" * 318
+        long_string = b"a" * 318
         encoded = dumps(long_string)[:2]
         with self.assertRaises(NotEnoughData) as err:
             loads(encoded)
         self.assertEqual(err.exception.n, 3)
-
-    def test_throws_when_null_byte_in_utf(self) -> None:
-        with self.assertRaises(DecodeError) as err:
-            result, tail = loads(b"\xc5he\x00\x01\x02")
-        self.assertEqual(str(err.exception), "null byte in UTF-8")
-
-    @given(invalid_utf8_2byte_s)
-    def test_throws_when_invalid_utf_2bytes(self, invalid_utf: bytes) -> None:
-        with self.assertRaises(DecodeError) as err:
-            encoded = dumps("hello")[:-1] + invalid_utf
-            result, tail = loads(encoded)
-        self.assertEqual(str(err.exception), "invalid UTF-8")
-
-    @given(invalid_utf8_3byte_s)
-    def test_throws_when_invalid_utf_3bytes(self, invalid_utf: bytes) -> None:
-        with self.assertRaises(DecodeError) as err:
-            encoded = dumps("hello")[:-2] + invalid_utf
-            result, tail = loads(encoded)
-        self.assertEqual(str(err.exception), "invalid UTF-8")