Skip to content

Commit 7885a43

Browse files
committed
Added create and remove action classes
1 parent 0cb519d commit 7885a43

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

TreeRoot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def _install_actions(self):
1919
self.install_action(SetAction())
2020
self.install_action(ListAction())
2121
self.install_action(ExistsAction())
22-
self.install_action(ActionNode('create', BasicActions.create))
23-
self.install_action(ActionNode('remove', BasicActions.remove))
22+
self.install_action(CreateAction())
23+
self.install_action(RemoveAction())
2424

2525
def install_action(self, action_node: ActionNode):
2626
if action_node is None:

actions/BasicActions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ def __init__(self):
5050
def __call__(self, target: Node, node_name: str) -> bool:
5151
return target.contains(node_name)
5252

53+
54+
class CreateAction(ActionNode):
55+
def __init__(self):
56+
super().__init__(path='create')
57+
58+
def __call__(self, target: Node, name: str, value=None):
59+
target.append(Node(value), name)
60+
61+
62+
class RemoveAction(ActionNode):
63+
def __init__(self):
64+
super().__init__(path='remove')
65+
66+
def __call__(self, target: Node, node_name: str):
67+
target.remove(node_name)
68+
69+
5370
class ListAction(ActionNode):
5471
def __init__(self):
5572
super().__init__(path='list')
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unittest
2+
3+
from Node import Node
4+
from NodePath import NodePath
5+
from actions.BasicActions import CreateAction
6+
7+
8+
class CreateActionTests(unittest.TestCase):
9+
def setUp(self):
10+
self.root = Node()
11+
self.root.append(Node(123), 'existing')
12+
13+
self.create = CreateAction()
14+
15+
def test_action_name(self):
16+
self.assertEqual(NodePath('create'), self.create.path)
17+
18+
def test_default_value(self):
19+
self.assertFalse(self.root.contains('default'))
20+
self.create(self.root, 'default')
21+
self.assertTrue(self.root.contains('default'))
22+
self.assertIsNone(self.root['default'].get())
23+
24+
def test_with_value(self):
25+
self.assertFalse(self.root.contains('value'))
26+
self.create(self.root, 'value', 258)
27+
self.assertTrue(self.root.contains('value'))
28+
self.assertEqual(258, self.root['value'].get())
29+
30+
def test_existing(self):
31+
self.assertTrue(self.root.contains('existing'))
32+
with self.assertRaises(NameError):
33+
self.create(self.root, 'existing')
34+
with self.assertRaises(NameError):
35+
self.create(self.root, 'existing', 'value')
36+
37+
def test_surplus_arguments(self):
38+
with self.assertRaises(TypeError):
39+
self.create(self.root, 'surplus', 'value', 'value2')
40+
41+
def test_no_arguments(self):
42+
with self.assertRaises(TypeError):
43+
self.create(self.root)
44+
45+
if __name__ == '__main__':
46+
unittest.main()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
3+
from Node import Node
4+
from NodePath import NodePath
5+
from actions.BasicActions import RemoveAction
6+
7+
8+
class RemoveActionTests(unittest.TestCase):
9+
def setUp(self):
10+
self.root = Node()
11+
self.root.append(Node(123), 'existing')
12+
13+
self.remove = RemoveAction()
14+
15+
def test_action_name(self):
16+
self.assertEqual(NodePath('remove'), self.remove.path)
17+
18+
def test_existing(self):
19+
self.assertTrue(self.root.contains('existing'))
20+
self.remove(self.root, 'existing')
21+
self.assertFalse(self.root.contains('existing'))
22+
23+
def test_nonexistent(self):
24+
self.assertFalse(self.root.contains('unknown'))
25+
with self.assertRaises(NameError):
26+
self.remove(self.root, 'unknown')
27+
28+
def test_surplus_arguments(self):
29+
with self.assertRaises(TypeError):
30+
self.remove(self.root, 'existing', 'name')
31+
32+
def test_no_argument(self):
33+
with self.assertRaises(TypeError):
34+
self.remove(self.root)
35+
36+
37+
if __name__ == '__main__':
38+
unittest.main()

0 commit comments

Comments
 (0)