def test_string__dump_too_long_before_encoding__pad(): """``pad_byte`` shouldn't prevent a crash if a string is too long.""" field = stringlike.String(size=5, pad_byte=b"?") with pytest.raises(errors.ValueSizeError): field.to_bytes("abcdefgh")
def test_string__dump_too_long_after_encoding__pad(): """``pad_byte`` shouldn't prevent a crash if a string is too long after encoding it.""" field = stringlike.String(size=3, pad_byte=b"?", encoding="utf-16-le") with pytest.raises(errors.ValueSizeError): field.to_bytes("ab")
def test_string__dump_too_short_before_encoding__pad(): """Dumping a string that's too short before encoding is okay.""" field = stringlike.String(size=5, pad_byte=b" ") assert field.to_bytes("a") == b"a "
def test_string__dump_too_short_after_encoding__pad(): """Dumping a string that's too short but uses padding is okay.""" field = stringlike.String(size=8, pad_byte=b"\0", encoding="utf-32-be") assert field.to_bytes("a") == b"\0\0\0a\0\0\0\0"
def test_string__dump_too_long_after_encoding(): """Test dumping a string that's too long only after encoding to bytes.""" field = stringlike.String(size=4, encoding="utf-8") with pytest.raises(errors.ValueSizeError): assert field.to_bytes("très")
def test_string__dump_too_short_before_encoding(): """Basic test of dumping a string that's too short into a String.""" field = stringlike.String(size=5, encoding="utf-8") with pytest.raises(errors.ValueSizeError): assert field.to_bytes("a")
def test_string__dump_no_size(): """Try dumping a string without its size set.""" field = stringlike.String() with pytest.raises(errors.UndefinedSizeError): field.to_bytes("asdf")
def test_string__dump_variable_size(size_field): """Dumping a field with variable size should work.""" field = stringlike.String(size=size_field) assert field.to_bytes("asdf", all_fields={"size": 4}) == b"asdf"
def test_string__dump_basic(): """Basic test of dumping a String""" field = stringlike.String(size=13, encoding="utf-8") assert field.to_bytes( r"¯\_(ツ)_/¯") == b"\xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf"
def test_string__load_basic(): """Basic test of loading a String""" field = stringlike.String(size=13, encoding="utf-8") assert field.from_bytes( b"\xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf") == r"¯\_(ツ)_/¯"
def test_string__pad_default(): """The default value should be padded if necessary.""" field = stringlike.String(size=4, pad_byte=b" ", default="?") assert field.to_bytes() == b"? "
def test_string__pad_byte_too_long(): """The padding byte must be exactly one byte.""" with pytest.raises(errors.ConfigurationError): stringlike.String(size=4, pad_byte=b"0123")
def test_string__pad_byte_wrong_type(): """Trying to pass a regular string as pad_byte will explode.""" with pytest.raises(errors.ConfigurationError): stringlike.String(size=4, pad_byte=" ")