Skip to content

Commit 1449512

Browse files
committed
Add examples and tests for reading from bytes objects
1 parent ce8bc1b commit 1449512

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,29 @@ try using a regular (non-editable) installation instead.
9191

9292
## Examples
9393

94-
TODO
94+
### Simple example
95+
96+
```python-repl
97+
>>> import typedstream
98+
>>> data = b"\x04\x0bstreamtyped\x81\xe8\x03\x84\x01@\x84\x84\x84\x08NSString\x01\x84\x84\x08NSObject\x00\x85\x84\x01+\x0cstring value\x86"
99+
>>> typedstream.unarchive_from_data(data)
100+
NSString('string value')
101+
>>> _.value
102+
'string value'
103+
```
104+
105+
### Low-level stream reading
106+
107+
```python-repl
108+
>>> from typedstream.stream import TypedStreamReader
109+
>>> ts = TypedStreamReader.from_data(data)
110+
>>> for event in ts:
111+
... print(event)
112+
typedstream.stream.BeginTypedValues([b"@"])
113+
typedstream.stream.BeginObject()
114+
typedstream.stream.SingleClass(name=b'NSString', version=1)
115+
...
116+
```
95117

96118
### Command-line interface
97119

tests/test_typedstream.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import typedstream.archiving
2222
import typedstream.stream
23+
import typedstream.types.foundation
2324

2425

2526
DATA_DIR = pathlib.Path(__file__).parent / "data"
@@ -29,9 +30,37 @@
2930
"Empty2D macOS 13.gcx",
3031
]
3132

33+
STRING_TEST_DATA = b"\x04\x0bstreamtyped\x81\xe8\x03\x84\x01@\x84\x84\x84\x08NSString\x01\x84\x84\x08NSObject\x00\x85\x84\x01+\x0cstring value\x86"
34+
3235

3336
class TypedstreamReadTests(unittest.TestCase):
34-
def test_read_stream(self) -> None:
37+
def test_read_data_stream(self) -> None:
38+
"""Some simple test data can be read as a low-level stream."""
39+
40+
with typedstream.stream.TypedStreamReader.from_data(STRING_TEST_DATA) as ts:
41+
events = list(ts)
42+
43+
self.assertEqual(events, [
44+
typedstream.stream.BeginTypedValues([b"@"]),
45+
typedstream.stream.BeginObject(),
46+
typedstream.stream.SingleClass(name=b"NSString", version=1),
47+
typedstream.stream.SingleClass(name=b"NSObject", version=0),
48+
None,
49+
typedstream.stream.BeginTypedValues([b"+"]),
50+
b"string value",
51+
typedstream.stream.EndTypedValues(),
52+
typedstream.stream.EndObject(),
53+
typedstream.stream.EndTypedValues(),
54+
])
55+
56+
def test_read_data_unarchive(self) -> None:
57+
"""Some simple test data can be unarchived into an object."""
58+
59+
root = typedstream.unarchive_from_data(STRING_TEST_DATA)
60+
self.assertEqual(type(root), typedstream.types.foundation.NSString)
61+
self.assertEqual(root.value, "string value")
62+
63+
def test_read_file_stream(self) -> None:
3564
"""All the test files can be read as a low-level stream."""
3665

3766
for name in READ_TEST_FILE_NAMES:
@@ -40,7 +69,7 @@ def test_read_stream(self) -> None:
4069
for _ in ts:
4170
pass
4271

43-
def test_read_unarchive(self) -> None:
72+
def test_read_file_unarchive(self) -> None:
4473
"""All the test files can be unarchived into objects."""
4574

4675
for name in READ_TEST_FILE_NAMES:

0 commit comments

Comments
 (0)