class TestComponent(TestCase): def setUp(self): self.path = Path() self.index_c = Component(self.path, index=1) self.name_c = Component(self.path, name="name") def test_init_requires_index_or_name(self): with self.assertRaises(ValueError): Component(self.path) def test_init_requires_not_index_and_name(self): with self.assertRaises(ValueError): Component(self.path, index=1, name="haha") def test_is_index(self): self.assertTrue(self.index_c.is_index()) self.assertFalse(self.name_c.is_index()) def test_to_parent(self): p = self.index_c.to_parent() self.assertEqual(str(p), '^') def test_is_parent(self): p = self.index_c.to_parent() self.assertTrue(p.is_parent()) self.assertFalse(self.index_c.is_parent()) def test_eq(self): self.assertFalse(self.index_c == self.name_c) self.assertTrue(self.index_c == self.index_c) self.assertTrue(self.name_c == self.name_c) self.assertTrue(self.index_c == 1) self.assertFalse(self.index_c == 2) self.assertFalse(self.index_c == '1') self.assertTrue(self.name_c == 'name') self.assertFalse(self.name_c == 'othername')
def content_with_path_component( self, component: Component) -> Optional[RuntimeObject]: if component.is_index(): try: return self.content[component.index] except IndexError: return None elif component.is_parent(): return self.parent else: try: return self.named_content[component.name] except KeyError: raise StoryException("Content {} not found at path {}".format( component.name, self.path))
def path_to_first_leaf_content(self) -> Path: path = Path() container = self while container is not None: if len(container.content) > 0: path.components.append(Component(0)) container = container.content[0].as_container() # This check is not part of the c# source, but is needed to prevent an # infinite loop else: raise ValueError("Unexpectedly found empty container") return self.path + path
def setUp(self): self.empty = Path() self.one_path = Path([Component(name='this')]) self.two_path = Path([Component(name='this'), Component(name='that')]) self.three_path = Path([ Component(name='this'), Component(name='that'), Component(name='other') ])
def test_init_requires_index_or_name(self): with self.assertRaises(ValueError): Component(self.path)
def setUp(self): self.path = Path() self.index_c = Component(self.path, index=1) self.name_c = Component(self.path, name="name")
def test_init_requires_not_index_and_name(self): with self.assertRaises(ValueError): Component(self.path, index=1, name="haha")
def test_contains_named_component(self): self.assertFalse(self.empty.contains_named_component()) ixpath = Path([Component(index=1)]) self.assertFalse(ixpath.contains_named_component()) self.assertTrue(self.one_path.contains_named_component())
def test_content_with_path_component_for_parent(self): self.assertIs(self.named.content_with_path_component(Component(name="^")), self.parent)
def test_content_with_path_component_for_name(self): self.assertIs(self.parent.content_with_path_component(Component(name="name")), self.named)
def test_content_with_path_component_for_index(self): self.assertIs(self.parent.content_with_path_component(Component(index=0)), self.named) self.assertIs(self.parent.content_with_path_component(Component(index=1)), self.anon)