Bytes assertions¶
Assertions for bytes and bytearray values.
Assertions for bytes and bytearray values.
is_valid_utf8 ¶
Assert that val is valid UTF-8.
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not valid UTF-8 |
Source code in assertpy2/bytes_mixin.py
is_valid_encoding ¶
Assert that val is valid in the given encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoding
|
str
|
the encoding to validate against (e.g. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val cannot be decoded with the given encoding |
Source code in assertpy2/bytes_mixin.py
starts_with_bytes ¶
Assert that val starts with the given byte prefix.
The bytes-only spelling of starts_with(), which it
delegates to. It carried its own implementation until starts_with learned to handle bytes:
before that, a byte string fell through to the element-wise branch and b"foo" compared its
first element, the int 102, against b"f". Two implementations of one relation is how they
drift, and these two already had: this one accepted an empty prefix, which asserts nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
bytes
|
the expected byte prefix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
TypeError
|
if val is not |
ValueError
|
if the prefix is empty, which no value can fail |
AssertionError
|
if val does not start with the prefix |
Source code in assertpy2/bytes_mixin.py
contains_bytes ¶
Assert that val contains the given byte subsequence.
The bytes-only spelling of contains(), which it
delegates to. Delegating is what gives the failure the value under test and a structured diff,
neither of which its own implementation produced.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sub
|
bytes
|
the byte subsequence to find. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
TypeError
|
if val is not |
AssertionError
|
if val does not contain the subsequence |
Source code in assertpy2/bytes_mixin.py
has_byte_at ¶
Assert that the byte at the given index equals the expected value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
zero-based byte index. |
required |
expected
|
int
|
expected byte value (0-255). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if the byte at index does not match |
Source code in assertpy2/bytes_mixin.py
is_hex_equal_to ¶
Assert that val equals the given hex string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected_hex
|
str
|
hex string without prefix (e.g. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not match the hex string |
Source code in assertpy2/bytes_mixin.py
decoded_as ¶
Decode val and return a new builder with the decoded string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoding
|
str
|
the encoding to use (default |
'utf-8'
|
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
a new instance with the decoded string as val |
Raises:
| Type | Description |
|---|---|
UnicodeDecodeError
|
if val cannot be decoded |