Skip to content

Commit

Permalink
Improve Text API (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart authored Nov 7, 2023
1 parent 89d4b85 commit e7c729e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/pycrdt/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,11 @@ def __setitem__(self, key: int | slice, value: str) -> None:
else:
raise RuntimeError(f"Index not supported: {key}")

def clear(self) -> None:
del self[:]

def insert(self, index, text: str) -> None:
self[index:index] = text


integrated_types[_Text] = Text
11 changes: 11 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ def test_str():
map2 = Map({"key": text2})
array2.append(map2)
assert str(array2) == '[{"key":"val"}]'


def test_api():
doc = Doc()
text = Text(hello + punct)
doc["text"] = text
assert str(text) == hello + punct
text.insert(len(hello), world)
assert str(text) == hello + world + punct
text.clear()
assert len(text) == 0

0 comments on commit e7c729e

Please sign in to comment.