pyo3/pytests/tests/test_buf_and_str.py

37 lines
1.1 KiB
Python
Raw Normal View History

2023-10-13 12:42:27 +00:00
from pyo3_pytests.buf_and_str import BytesExtractor, return_memoryview
2021-07-18 19:03:45 +00:00
def test_extract_bytes():
extractor = BytesExtractor()
2021-07-18 19:03:45 +00:00
message = b'\\(-"-;) A message written in bytes'
assert extractor.from_bytes(message) == len(message)
2021-07-18 19:03:45 +00:00
def test_extract_str():
extractor = BytesExtractor()
message = '\\(-"-;) A message written as a string'
assert extractor.from_str(message) == len(message)
2021-07-18 19:03:45 +00:00
def test_extract_str_lossy():
extractor = BytesExtractor()
message = '\\(-"-;) A message written with a trailing surrogate \ud800'
rust_surrogate_len = extractor.from_str_lossy("\ud800")
assert extractor.from_str_lossy(message) == len(message) - 1 + rust_surrogate_len
2021-07-22 08:18:12 +00:00
def test_extract_buffer():
extractor = BytesExtractor()
message = b'\\(-"-;) A message written in bytes'
assert extractor.from_buffer(message) == len(message)
arr = bytearray(b'\\(-"-;) A message written in bytes')
assert extractor.from_buffer(arr) == len(arr)
2023-10-13 12:42:27 +00:00
def test_return_memoryview():
view = return_memoryview()
assert view.readonly
assert view.contiguous
assert view.tobytes() == b"hello world"