Skip to content

Commit 0cb519d

Browse files
committed
Added ExistsAction
1 parent 1a071a7 commit 0cb519d

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

TreeRoot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def _install_actions(self):
1818
self.install_action(GetAction())
1919
self.install_action(SetAction())
2020
self.install_action(ListAction())
21-
self.install_action(ActionNode('exists', BasicActions.exists))
21+
self.install_action(ExistsAction())
2222
self.install_action(ActionNode('create', BasicActions.create))
2323
self.install_action(ActionNode('remove', BasicActions.remove))
2424

actions/BasicActions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ def __call__(self, target: Node, new_value):
4343
target.set(new_value)
4444

4545

46+
class ExistsAction(ActionNode):
47+
def __init__(self):
48+
super().__init__(path='exists')
49+
50+
def __call__(self, target: Node, node_name: str) -> bool:
51+
return target.contains(node_name)
52+
4653
class ListAction(ActionNode):
4754
def __init__(self):
4855
super().__init__(path='list')
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import unittest
2+
3+
from Node import Node
4+
from NodePath import NodePath
5+
from actions.BasicActions import ExistsAction
6+
7+
8+
class ExistsActionTests(unittest.TestCase):
9+
def setUp(self):
10+
self.root = Node()
11+
self.root.append(Node(123), 'integer')
12+
self.root.append(Node('foobar'), 'string')
13+
self.root.append(Node(), 'empty')
14+
15+
self.exists = ExistsAction()
16+
17+
def test_action_name(self):
18+
self.assertEqual(NodePath('exists'), self.exists.path)
19+
20+
def test_existing(self):
21+
self.assertTrue(self.exists(self.root, 'integer'))
22+
self.assertTrue(self.exists(self.root, 'empty'))
23+
24+
def test_nonexistent(self):
25+
self.assertFalse(self.exists(self.root, 'unknown'))
26+
27+
def test_surplus_arguments(self):
28+
with self.assertRaises(TypeError):
29+
self.exists(self.root, 'unknown', 'name')
30+
31+
def test_no_argument(self):
32+
with self.assertRaises(TypeError):
33+
self.exists(self.root)
34+
35+
36+
if __name__ == '__main__':
37+
unittest.main()

tests/action_tests/SetActionTests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def test_set_none(self):
3232
with self.assertRaises(TypeError):
3333
self.set(self.root['empty'], 23)
3434

35+
def test_no_arguments(self):
36+
with self.assertRaises(TypeError):
37+
self.set(self.root['integer'])
38+
3539
def test_too_many_arguments(self):
3640
with self.assertRaises(TypeError):
3741
self.set(self.root['integer'], 1, 2, 3)

0 commit comments

Comments
 (0)