def test_demo_init_set_children(self): # Test if there are children, the first one will be selected. resources = [DemoPath(), DemoPath()] model = DemoVirtualDirectory(resources=resources) demo = Demo(model=model) demo.selected_node = None # when info = UIInfo(ui=UI(handler=Handler())) demo.init(info) # then self.assertIs(demo.selected_node, resources[0])
def test_demo_parent_button_default(self): # Make sure the parent button behaves in the default state. # This ensures the enable flag can be computed (instead of crashing), # and the action allowed by it does not crash. demo = Demo(model=DemoPath()) if get_action_enabled(parent_tool, demo): demo.perform(None, parent_tool, None)
def test_init_dict_with_empty_directory(self): with tempfile.TemporaryDirectory() as directory: model = DemoPath(name=directory, ) # traits api is still imported. init_dic = model.init_dic self.assertIsInstance(init_dic, dict) self.assertIn("HasTraits", init_dic)
def to_node(self): """ Return an instance of DemoTreeNodeObject from this response. """ return DemoPath( nice_name=self.name, name=self.root, )
def test_init_dict_used_by_children(self): # Test the __init__.py in a directory (if exists) is visible # by the example scripts in that folder. # Not sure if this is really needed, but this is an existing feature. with tempfile.TemporaryDirectory() as directory: init_content = "CONSTANT = 'HELLO'" example_content = textwrap.dedent(""" from . import CONSTANT from . import HasTraits """) subdir = os.path.join(directory, "Examples") os.makedirs(subdir) init_py = os.path.join(subdir, "__init__.py") with open(init_py, "w", encoding="utf-8") as f: f.write(init_content) example_py = os.path.join(subdir, "example.py") with open(example_py, "w", encoding="utf-8") as f: f.write(example_content) model = DemoPath( name=directory, use_files=False, ) # sanity check: # This is one subdirectory children = model.get_children() self.assertEqual(len(children), 1) # In that subdirectory, there is one Python file that is not # __init__.py subdir_node, = children file_nodes = subdir_node.get_children() self.assertEqual(len(file_nodes), 1) example, = file_nodes self.assertEqual(example.name, "example.py") # This is the test objective: The __init__.py and traits api are # loaded and accessible by the example script. # Try running the code example.run_code() self.assertIn("CONSTANT", example.locals) self.assertIn("HasTraits", example.locals)
def test_init_dict_with_init_py(self): with tempfile.TemporaryDirectory() as directory: init_py = os.path.join(directory, "__init__.py") with open(init_py, "w", encoding="utf-8") as f: f.write("a = 1\n") model = DemoPath(name=directory, ) self.assertIn("a", model.init_dic) self.assertEqual(model.init_dic["a"], 1) # traits api is still imported. self.assertIn("HasTraits", model.init_dic)
def test_description_with_empty_directory(self): # If the directory is empty, the content of the description should # be empty. with tempfile.TemporaryDirectory() as directory: model = DemoPath(name=directory, ) tree = ET.fromstring(model.description) body_node = next(tree.iter(get_html_tag("body"))) div_node, = list(body_node) self.assertEqual(list(div_node), [])
def test_description_use_css(self): with tempfile.TemporaryDirectory() as directory: model = DemoPath( name=directory, css_filename="default.css", ) tree = ET.fromstring(model.description) link_node = next(tree.iter(get_html_tag("link"))) self.assertEqual(link_node.attrib["href"], "default.css")
def test_use_index_rst(self): with tempfile.TemporaryDirectory() as directory: index_rst = os.path.join(directory, "index.rst") with open(index_rst, "w", encoding="utf-8") as f: f.write(".. image:: any_image.jpg\n") model = DemoPath(name=directory, ) tree = ET.fromstring(model.description) img_node = next(tree.iter(get_html_tag("img"))) self.assertEqual(img_node.attrib["src"], "any_image.jpg")
def test_description_contains_file_uri(self): with tempfile.NamedTemporaryFile() as file_obj: dirname, basename = os.path.split(file_obj.name) parent = DemoPath(name=dirname) image_node = DemoImageFile(parent=parent, name=basename) # when image_node.init() tree = ET.fromstring(image_node.description) # then # The image path should be either a fully specified absolute path # following the file scheme, or a file name relative to the # base_url given to the HTML editor. img_xml = next(tree.iter(get_html_tag("img"))) self.assertEqual(img_xml.attrib["src"], basename) self.assertEqual(image_node.base_url, dirname)
def test_some_resources(self): node = DemoVirtualDirectory(resources=[DemoPath()]) self.assertTrue(node.tno_has_children(None)) self.assertEqual(node.tno_get_children(None), node.resources) self.assertTrue(node.tno_allows_children(None))