Skip to content

Commit f04b6c7

Browse files
committed
Moved get and set actions to the separate classes
1 parent 13faac9 commit f04b6c7

File tree

4 files changed

+138
-3
lines changed

4 files changed

+138
-3
lines changed

TreeRoot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def start(self):
1515
self._install_actions()
1616

1717
def _install_actions(self):
18-
self.install_action(ActionNode('get', BasicActions.get))
19-
self.install_action(ActionNode('set', BasicActions.set))
18+
self.install_action(GetAction())
19+
self.install_action(SetAction())
2020
self.install_action(ListAction())
2121
self.install_action(ActionNode('exists', BasicActions.exists))
2222
self.install_action(ActionNode('create', BasicActions.create))

actions/BasicActions.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from ActionNode import *
22

3+
34
class BasicActions:
45
@staticmethod
56
def get(target: Node):
@@ -26,8 +27,24 @@ def remove(target: Node, name_to_remove: str):
2627
target.remove(name_to_remove)
2728

2829

30+
class GetAction(ActionNode):
31+
def __init__(self):
32+
super().__init__(path='get')
33+
34+
def __call__(self, target: Node):
35+
return target.get()
36+
37+
38+
class SetAction(ActionNode):
39+
def __init__(self):
40+
super().__init__(path='set')
41+
42+
def __call__(self, target: Node, new_value):
43+
target.set(new_value)
44+
45+
2946
class ListAction(ActionNode):
30-
def __init__(self=None):
47+
def __init__(self):
3148
super().__init__(path='list')
3249

3350
def __call__(self, target: Node):

tests/action_tests/GetActionTests.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import unittest
2+
3+
from Node import Node
4+
from NodePath import NodePath
5+
from TreeRoot import TreeRoot
6+
from actions.BasicActions import GetAction
7+
8+
9+
class GetActionTests(unittest.TestCase):
10+
def setUp(self):
11+
self.root = Node()
12+
self.root.append(Node(123), 'integer')
13+
self.root.append(Node('foobar'), 'string')
14+
self.root.append(Node(), 'empty')
15+
16+
self.get = GetAction()
17+
18+
def test_get(self):
19+
self.assertEqual(123, self.get(self.root['integer']))
20+
self.assertEqual('foobar', self.get(self.root['string']))
21+
self.assertEqual(None, self.get(self.root['empty']))
22+
23+
def test_get_any_arguments(self):
24+
with self.assertRaises(TypeError):
25+
self.get(self.root['empty'], 1 ,2 ,3)
26+
27+
28+
class GetActionNodeTests(unittest.TestCase):
29+
def setUp(self):
30+
self.root = TreeRoot()
31+
self.root.create('.integer', 123)
32+
self.root.create('.string', 'foobar')
33+
self.root.create('.empty')
34+
35+
def test_get(self):
36+
integer = self.root.execute('.integer', NodePath('get'))
37+
self.assertEqual(123, integer)
38+
39+
string = self.root.execute('.string', NodePath('get'))
40+
self.assertEqual('foobar', string)
41+
42+
empty = self.root.execute('.empty', NodePath('get'))
43+
self.assertEqual(None, empty)
44+
45+
def test_get_any_arguments(self):
46+
with self.assertRaises(TypeError):
47+
self.root.execute('.', NodePath('get'), 3, 2, 1)
48+
49+
50+
if __name__ == '__main__':
51+
unittest.main()

tests/action_tests/SetActionTests.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import unittest
2+
3+
from Node import Node
4+
from NodePath import NodePath
5+
from TreeRoot import TreeRoot
6+
from actions.BasicActions import SetAction
7+
8+
9+
class SetActionTests(unittest.TestCase):
10+
def setUp(self):
11+
self.root = Node()
12+
self.root.append(Node(123), 'integer')
13+
self.root.append(Node('foobar'), 'string')
14+
self.root.append(Node(), 'empty')
15+
16+
self.set = SetAction()
17+
18+
def test_set(self):
19+
self.set(self.root['integer'], 321)
20+
self.assertEqual(321, self.root['integer'].get())
21+
self.set(self.root['string'], 'barfoo')
22+
self.assertEqual('barfoo', self.root['string'].get())
23+
24+
def test_set_different_type(self):
25+
with self.assertRaises(TypeError):
26+
self.set(self.root['integer'], 'string')
27+
28+
def test_set_none(self):
29+
# This behaviour is subject to change
30+
with self.assertRaises(TypeError):
31+
self.set(self.root['empty'], 23)
32+
33+
def test_set_too_many_arguments(self):
34+
with self.assertRaises(TypeError):
35+
self.set(self.root['integer'], 1, 2, 3)
36+
37+
38+
class SetActionNodeTests(unittest.TestCase):
39+
def setUp(self):
40+
self.root = TreeRoot()
41+
self.root.create('.integer', 123)
42+
self.root.create('.string', 'foobar')
43+
self.root.create('.empty')
44+
45+
def test_set(self):
46+
self.root.execute('.integer', NodePath('set'), 444)
47+
self.assertEqual(444, self.root.get('.integer'))
48+
49+
self.root.execute('.string', NodePath('set'), 'barfoo')
50+
self.assertEqual('barfoo', self.root.get('.string'))
51+
52+
def test_set_different_type(self):
53+
with self.assertRaises(TypeError):
54+
self.root.execute('.integer', NodePath('set'), 'string')
55+
56+
def test_set_none(self):
57+
# This behaviour is subject to change
58+
with self.assertRaises(TypeError):
59+
self.root.execute('.empty', NodePath('set'), 444)
60+
61+
def test_set_any_arguments(self):
62+
with self.assertRaises(TypeError):
63+
self.root.execute('.integer', NodePath('set'), 3, 2, 1)
64+
65+
66+
if __name__ == '__main__':
67+
unittest.main()

0 commit comments

Comments
 (0)