Skip to content

Commit a0d1e57

Browse files
committed
check slash in Container names
1 parent b9fc2ea commit a0d1e57

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/pynwb/form/container.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ class Container(with_metaclass(abc.ABCMeta, object)):
99
{'name': 'parent', 'type': 'Container', 'doc': 'the Container that holds this Container', 'default': None},
1010
{'name': 'container_source', 'type': str, 'doc': 'the source of this container', 'default': None})
1111
def __init__(self, **kwargs):
12-
self.__name = getargs('name', kwargs)
12+
name = getargs('name', kwargs)
13+
if '/' in name:
14+
raise ValueError("name '" + name + "' cannot contain '/'")
15+
self.__name = name
1316
self.__parent = getargs('parent', kwargs)
1417
self.__container_source = getargs('container_source', kwargs)
1518
self.__children = list()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import unittest2 as unittest
2+
3+
from pynwb.form.container import Container
4+
5+
6+
class MyTestClass(Container):
7+
8+
def __init__(self, src, name, parent=None):
9+
super(MyTestClass, self).__init__(src, name, parent=parent)
10+
11+
def basic_add(self, **kwargs):
12+
return kwargs
13+
14+
def basic_add2(self, **kwargs):
15+
return kwargs
16+
17+
def basic_add2_kw(self, **kwargs):
18+
return kwargs
19+
20+
21+
class MyTestSubclass(MyTestClass):
22+
23+
def basic_add(self, **kwargs):
24+
return kwargs
25+
26+
def basic_add2_kw(self, **kwargs):
27+
return kwargs
28+
29+
30+
class TestNWBContainer(unittest.TestCase):
31+
32+
def test_constructor(self):
33+
"""Test that constructor properly sets parent
34+
"""
35+
parent_obj = MyTestClass('test source', 'obj1')
36+
child_obj = MyTestSubclass('test source', 'obj2', parent=parent_obj)
37+
self.assertIs(child_obj.parent, parent_obj)
38+
39+
def test_set_parent_parent(self):
40+
"""Test that parent setter properly sets parent
41+
"""
42+
parent_obj = MyTestClass('test source', 'obj1')
43+
child_obj = MyTestSubclass('test source', 'obj2')
44+
child_obj.parent = parent_obj
45+
self.assertIs(child_obj.parent, parent_obj)
46+
47+
def test_slash_restriction(self):
48+
self.assertRaises(ValueError, Container, 'bad/name')
49+
50+
51+
if __name__ == '__main__':
52+
unittest.main()

0 commit comments

Comments
 (0)