def test14string_machine_invalid_values(self): """Test the string_machine with some invalid values.""" def raise_error(_): # skipcq: PY-D0003 raise Exception("Invalid returned as valid.") for c in range(0x20): # ascii control characters state = string_machine(raise_error) self.assertIsNone(state(c)) for c in range(0x80, 0xc0): # some characters after the ascii table state = string_machine(raise_error) self.assertIsNone(state(c))
def test12utf8_machine_started_from_string_machine(self): """Test if the utf8_machine is started from the string_machine.""" def check_value(_data): # skipcq: PY-D0003, PTC-W0049 pass string = b"File pattern: file\x5f<file-nr>.txt" state = string_machine(check_value) utf8_machine_found = False for c in string: state = state(c) if state.__name__ == '_utf8': utf8_machine_found = True self.assertIsNone(state(ord(b'"'))) self.assertFalse(utf8_machine_found) string = b"It is 20\xc2\xb0C" state = string_machine(check_value) utf8_machine_found = False for c in string: state = state(c) if state.__name__ == '_utf8': utf8_machine_found = True self.assertIsNone(state(ord(b'"'))) self.assertTrue(utf8_machine_found) string = b"This is a foreign letter: \xe0\xa0\xab" state = string_machine(check_value) utf8_machine_found = False for c in string: state = state(c) if state.__name__ == '_utf8': utf8_machine_found = True self.assertIsNone(state(ord(b'"'))) self.assertTrue(utf8_machine_found) string = b"This is an egyptian hieroglyph: \xf0\x93\x80\x90" state = string_machine(check_value) utf8_machine_found = False for c in string: state = state(c) if state.__name__ == '_utf8': utf8_machine_found = True self.assertIsNone(state(ord(b'"'))) self.assertTrue(utf8_machine_found)
def test15string_machine_escaped_strings(self): """Test all allowed escape strings in the string_machine.""" def check_value(data): # skipcq: PY-D0003 self.assertEqual(data, compare_strings) escape_strings = b"bf\"\\/" compare_strings = "\b\f\"\\/" state = string_machine(check_value) for c in escape_strings: state = state(0x5c) # \ state = state(c) state = state(0x22) # " self.assertIsNone(state)
def test5hex_machine_started_from_string_machine(self): """Test if the hex_machine is started from the string_machine.""" def check_value(_data): # skipcq: PY-D0003, PTC-W0049 pass string = b"\u02FF" # skipcq: PYL-W1402 state = string_machine(check_value) hex_machine_found = False for c in string: state = state(c) if state.__name__ == '_hex': hex_machine_found = True self.assertIsNone(state(ord(b'"'))) self.assertTrue(hex_machine_found) string = b"\uff02" # skipcq: PYL-W1402 state = string_machine(check_value) hex_machine_found = False for c in string: state = state(c) if state.__name__ == '_hex': hex_machine_found = True self.assertIsNone(state(ord(b'"'))) self.assertTrue(hex_machine_found)
def test13string_machine_valid_values(self): """Test the string_machine with all valid characters.""" def check_value(data): # skipcq: PY-D0003 self.assertEqual(data, allowed_chars) allowed_chars = "\n" for c in range(0x20, 0x80): if c in (0x22, 0x5c): # skip "\ continue allowed_chars += chr(c) state = string_machine(check_value) for c in allowed_chars.encode(): state = state(c) self.assertEqual(state.__name__, "_string") state = state(ord('"')) self.assertIsNone(state)