Revert Tree.clear and add Tree.reset

After some internal discussion we've decided to keep `Tree.clear` as it was,
and add a `Tree.reset`, which does a `Tree.clear` but resets the label and
data of `Tree.root` to the values given, while mirroring how `Tree.__init__`
takes those parameters.
This commit is contained in:
Dave Pearson
2023-02-01 15:08:03 +00:00
parent 8b4a833e26
commit d350374e59
3 changed files with 24 additions and 31 deletions

View File

@@ -41,7 +41,7 @@ class TreeClearApp(App[None]):
async def test_tree_simple_clear() -> None:
"""Clearing a tree should keep the old label and data."""
"""Clearing a tree should keep the old root label and data."""
async with TreeClearApp().run_test() as pilot:
tree = pilot.app.query_one(VerseTree)
assert len(tree.root.children) > 1
@@ -51,34 +51,23 @@ async def test_tree_simple_clear() -> None:
assert isinstance(tree.root.data, VerseStar)
async def test_tree_new_label_clear() -> None:
"""Clearing a tree with a new label should use the new label and keep the old data."""
async def test_tree_reset_with_label() -> None:
"""Resetting a tree with a new label should use the new label and set the data to None."""
async with TreeClearApp().run_test() as pilot:
tree = pilot.app.query_one(VerseTree)
assert len(tree.root.children) > 1
pilot.app.query_one(VerseTree).clear(label="Jiangyin")
pilot.app.query_one(VerseTree).reset(label="Jiangyin")
assert len(tree.root.children) == 0
assert str(tree.root.label) == "Jiangyin"
assert isinstance(tree.root.data, VerseStar)
assert tree.root.data is None
async def test_tree_new_data_clear() -> None:
"""Clearing a tree with data should keep the old label and use the new data."""
async def test_tree_reset_with_label_and_data() -> None:
"""Resetting a tree with a label and data have that label and data used."""
async with TreeClearApp().run_test() as pilot:
tree = pilot.app.query_one(VerseTree)
assert len(tree.root.children) > 1
pilot.app.query_one(VerseTree).clear(data=VersePlanet())
assert len(tree.root.children) == 0
assert str(tree.root.label) == "White Sun"
assert isinstance(tree.root.data, VersePlanet)
async def test_tree_new_labal_and_data_clear() -> None:
"""Clearing a tree with label and data should replace the label and data."""
async with TreeClearApp().run_test() as pilot:
tree = pilot.app.query_one(VerseTree)
assert len(tree.root.children) > 1
pilot.app.query_one(VerseTree).clear(label="Jiangyin", data=VersePlanet())
pilot.app.query_one(VerseTree).reset(label="Jiangyin", data=VersePlanet())
assert len(tree.root.children) == 0
assert str(tree.root.label) == "Jiangyin"
assert isinstance(tree.root.data, VersePlanet)