def testTemplate(self): pluginklass = PluginManager.get_plugin_class('calendar') plugin = pluginklass() plugin.preferences['namespace'] = Path('Calendar') notebook = tests.new_notebook() plugin.extend(notebook) dumper = get_dumper('wiki') zim.datetimetz.FIRST_DAY_OF_WEEK = \ zim.datetimetz.MONDAY for path in ( Path('Calendar:2012'), Path('Calendar:2012:04:27'), Path('Calendar:2012:Week 17'), Path('Calendar:2012:04'), ): tree = notebook.get_template(path) lines = dumper.dump(tree) #~ print lines self.assertTrue(not 'Created' in ''.join(lines)) # No fall back if 'Week' in path.name: days = [l for l in lines if l.startswith('=== ')] self.assertEqual(len(days), 7)
def testTaskListTreeView(self): klass = PluginManager.get_plugin_class('tasklist') plugin = klass() notebook = tests.new_notebook() plugin.extend(notebook.index) index_ext = plugin.get_extension(IndexExtension) self.assertIsNotNone(index_ext) notebook.index.flush() notebook.index.update() from zim.plugins.tasklist import TaskListTreeView opener = tests.MockObject() treeview = TaskListTreeView(index_ext, opener) menu = treeview.get_popup() # Check these do not cause errors - how to verify state ? tests.gtk_activate_menu_item(menu, _("Expand _All")) tests.gtk_activate_menu_item(menu, _("_Collapse All")) # Copy tasklist -> csv from zim.gui.clipboard import Clipboard tests.gtk_activate_menu_item(menu, 'gtk-copy') text = Clipboard.get_text() lines = text.splitlines() self.assertTrue(len(lines) > 10) self.assertTrue(len(lines[0].split(',')) > 3) self.assertFalse(any('<span' in l for l in lines)) # make sure encoding is removed
def testNamespace(self): pluginklass = PluginManager.get_plugin_class('calendar') plugin = pluginklass() today = dateclass.today() for namespace in (Path('Calendar'), Path(':')): plugin.preferences['namespace'] = namespace path = plugin.path_from_date(today) self.assertTrue(isinstance(path, Path)) self.assertTrue(path.ischild(namespace)) date = plugin.date_from_path(path) self.assertTrue(isinstance(date, dateclass)) self.assertEqual(date, today) from zim.plugins.calendar import DAY, WEEK, MONTH, YEAR zim.datetimetz.FIRST_DAY_OF_WEEK = \ zim.datetimetz.MONDAY plugin.preferences['namespace'] = Path('Calendar') date = dateclass(2012, 4, 27) for setting, wanted, start in ( (DAY, 'Calendar:2012:04:27', dateclass(2012, 4, 27)), (WEEK, 'Calendar:2012:Week 17', dateclass(2012, 4, 23)), (MONTH, 'Calendar:2012:04', dateclass(2012, 4, 1)), (YEAR, 'Calendar:2012', dateclass(2012, 1, 1)), ): plugin.preferences['granularity'] = setting path = plugin.path_from_date(date) self.assertEqual(path.name, wanted) self.assertEqual(plugin.date_from_path(path), start) path = plugin.path_for_month_from_date(date) self.assertEqual(path.name, 'Calendar:2012:04')
def testTemplate(self): pluginklass = PluginManager.get_plugin_class('calendar') plugin = pluginklass() plugin.preferences['namespace'] = Path('Calendar') notebook = tests.new_notebook() plugin.extend(notebook) dumper = get_dumper('wiki') zim.datetimetz.FIRST_DAY_OF_WEEK = \ zim.datetimetz.MONDAY for path in ( Path('Calendar:2012'), Path('Calendar:2012:04:27'), Path('Calendar:2012:Week 17'), Path('Calendar:2012:04'), ): tree = notebook.get_template(path) lines = dumper.dump(tree) #~ print lines self.assertTrue(not 'Created' in ''.join(lines)) # No fall back if 'Week' in path.name: days = [l for l in lines if l.startswith('=== ')] self.assertEqual(len(days), 7)
def testTaskListTreeView(self): klass = PluginManager.get_plugin_class('tasklist') plugin = klass() notebook = tests.new_notebook() plugin.extend(notebook.index) index_ext = plugin.get_extension(IndexExtension) self.assertIsNotNone(index_ext) notebook.index.flush() notebook.index.update() from zim.plugins.tasklist import TaskListTreeView opener = tests.MockObject() treeview = TaskListTreeView(index_ext, opener) menu = treeview.get_popup() # Check these do not cause errors - how to verify state ? tests.gtk_activate_menu_item(menu, _("Expand _All")) tests.gtk_activate_menu_item(menu, _("_Collapse All")) # Copy tasklist -> csv from zim.gui.clipboard import Clipboard tests.gtk_activate_menu_item(menu, 'gtk-copy') text = Clipboard.get_text() lines = text.splitlines() self.assertTrue(len(lines) > 10) self.assertTrue(len(lines[0].split(',')) > 3) self.assertFalse(any('<span' in l for l in lines)) # make sure encoding is removed
def testTaskListTreeView(self): klass = PluginManager.get_plugin_class('tasklist') plugin = klass() notebook = tests.new_notebook() plugin.extend(notebook) notebook.index.check_and_update() from zim.plugins.tasklist.gui import TaskListTreeView view = TasksView.new_from_index(notebook.index) opener = tests.MockObject() treeview = TaskListTreeView(view, opener, task_labels=['TODO', 'FIXME']) menu = treeview.get_popup() # Check these do not cause errors - how to verify state ? tests.gtk_activate_menu_item(menu, _("Expand _All")) tests.gtk_activate_menu_item(menu, _("_Collapse All")) # Copy tasklist -> csv from zim.gui.clipboard import Clipboard tests.gtk_activate_menu_item(menu, 'gtk-copy') text = Clipboard.get_text() lines = text.splitlines() self.assertTrue(len(lines) > 10) self.assertTrue(len(lines[0].split(',')) > 3) self.assertFalse(any('<span' in l for l in lines)) # make sure encoding is removed # Test tags tags = treeview.get_tags() for tag in ('home', 'FIXME', '__no_tags__', 'tags'): self.assertIn(tag, tags) self.assertGreater(tags[tag], 0)
def testNamespace(self): pluginklass = PluginManager.get_plugin_class('journal') plugin = pluginklass() notebook = self.setUpNotebook() properties = plugin.notebook_properties(notebook) today = dateclass.today() for namespace in (Path('Calendar'), Path(':')): properties['namespace'] = namespace path = plugin.path_from_date(notebook, today) self.assertTrue(isinstance(path, Path)) self.assertTrue(path.ischild(namespace)) date = plugin.date_from_path(path) self.assertTrue(isinstance(date, dateclass)) self.assertEqual(date, today) from zim.plugins.journal import DAY, WEEK, MONTH, YEAR zim.datetimetz.FIRST_DAY_OF_WEEK = \ zim.datetimetz.MONDAY properties['namespace'] = Path('Calendar') date = dateclass(2012, 4, 27) for setting, wanted, start in ( (DAY, 'Calendar:2012:04:27', dateclass(2012, 4, 27)), (WEEK, 'Calendar:2012:Week 17', dateclass(2012, 4, 23)), (MONTH, 'Calendar:2012:04', dateclass(2012, 4, 1)), (YEAR, 'Calendar:2012', dateclass(2012, 1, 1)), ): properties['granularity'] = setting path = plugin.path_from_date(notebook, date) self.assertEqual(path.name, wanted) self.assertEqual(plugin.date_from_path(path), start) path = plugin.path_for_month_from_date(notebook, date) self.assertEqual(path.name, 'Calendar:2012:04')
def runTest(self): 'Test PrintToBrowser plugin' pluginklass = PluginManager.get_plugin_class('printtobrowser') plugin = pluginklass() notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK) page = notebook.get_page(Path('Test:foo')) file = plugin.print_to_file(notebook, page) self.assertTrue(file.exists()) content = file.read() self.assertTrue('<h1>Foo</h1>' in content)
def runTest(self): 'Test PrintToBrowser plugin' pluginklass = PluginManager.get_plugin_class('printtobrowser') plugin = pluginklass() notebook = tests.new_notebook() page = notebook.get_page(Path('Test:foo')) file = plugin.print_to_file(notebook, page) self.assertTrue(file.exists()) content = file.read() self.assertTrue('<h1>Foo</h1>' in content)
def _find_plugin(name): plugins = PluginManager() for plugin_name in plugins.list_installed_plugins(): try: klass = plugins.get_plugin_class(plugin_name) for objtype in klass.discover_classes(InsertedObjectTypeExtension): if objtype.name == name: activatable = klass.check_dependencies_ok() return (plugin_name, klass.plugin_info['name'], activatable, klass) except: continue return None
def testMainWindowExtensions(self): pluginklass = PluginManager.get_plugin_class('journal') plugin = pluginklass() notebook = self.setUpNotebook() mainwindow = setUpMainWindow(notebook) plugin.extend(mainwindow.pageview) plugin.preferences.changed() # make sure no errors are triggered list(plugin.extensions)[0].go_page_today() self.assertTrue(mainwindow.page.name.startswith('Journal:'))
def testNotebookExtension(self): pluginklass = PluginManager.get_plugin_class('journal') plugin = pluginklass() notebook = self.setUpNotebook() plugin.extend(notebook) ext = list(plugin.extensions) self.assertEqual(len(ext), 1) self.assertIsInstance(ext[0], NotebookExtension) page = Path('Foo') link = notebook.suggest_link(page, '2014-01-06') self.assertEqual(link.name, 'Journal:2014:01:06') link = notebook.suggest_link(page, 'foo') self.assertIsNone(link)
def testNotebookExtension(self): pluginklass = PluginManager.get_plugin_class('calendar') plugin = pluginklass() notebook = tests.new_notebook(self.get_tmp_name()) plugin.extend(notebook) ext = list(plugin.extensions) self.assertEqual(len(ext), 1) self.assertIsInstance(ext[0], NotebookExtension) page = Path('Foo') link = notebook.suggest_link(page, '2014-01-06') self.assertEqual(link.name, 'Journal:2014:01:06') link = notebook.suggest_link(page, 'foo') self.assertIsNone(link)
def testIndexing(self): '''Check indexing of tasklist plugin''' klass = PluginManager.get_plugin_class('tasklist') plugin = klass() notebook = tests.new_notebook() plugin.extend(notebook.index) index_ext = plugin.get_extension(IndexExtension) self.assertIsNotNone(index_ext) # Test indexing based on index signals notebook.index.flush() notebook.index.update() self.assertTrue(index_ext.db_initialized) tasks = list(index_ext.list_tasks()) self.assertTrue(len(tasks) > 5) for task in tasks: path = index_ext.get_path(task) self.assertTrue(not path is None)
def testMainWindowExtensions(self): pluginklass = PluginManager.get_plugin_class('calendar') plugin = pluginklass() notebook = tests.new_notebook(self.get_tmp_name()) ui = setupGtkInterface(self, notebook=notebook) mainwindow = ui._mainwindow # XXX plugin.preferences['embedded'] = True self.assertEqual(plugin.extension_classes['MainWindow'], MainWindowExtensionEmbedded) plugin.extend(mainwindow) ext = list(plugin.extensions) self.assertEqual(len(ext), 1) self.assertIsInstance(ext[0], MainWindowExtensionEmbedded) plugin.preferences.changed() # make sure no errors are triggered ext[0].go_page_today() self.assertTrue(ui.page.name.startswith('Journal:')) plugin.preferences['embedded'] = False self.assertEqual(plugin.extension_classes['MainWindow'], MainWindowExtensionDialog) plugin.extend( mainwindow) # plugin does not remember objects, manager does that ext = list(plugin.extensions) self.assertEqual(len(ext), 1) self.assertIsInstance(ext[0], MainWindowExtensionDialog) plugin.preferences.changed() # make sure no errors are triggered def test_dialog(dialog): self.assertIsInstance(dialog, CalendarDialog) dialog.do_today('xxx') ui.open_page(Path('foo')) with tests.DialogContext(test_dialog): ext[0].show_calendar() plugin.preferences['embedded'] = True # switch back
def testIndexing(self): '''Check indexing of tasklist plugin''' klass = PluginManager.get_plugin_class('tasklist') plugin = klass() notebook = tests.new_notebook() plugin.extend(notebook.index) index_ext = plugin.get_extension(IndexExtension) self.assertIsNotNone(index_ext) # Test indexing based on index signals notebook.index.flush() notebook.index.update() self.assertTrue(index_ext.db_initialized) tasks = list(index_ext.list_tasks()) self.assertTrue(len(tasks) > 5) for task in tasks: path = index_ext.get_path(task) self.assertTrue(not path is None)
def testIndexing(self): '''Check indexing of tasklist plugin''' klass = PluginManager.get_plugin_class('tasklist') plugin = klass() notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK) plugin.extend(notebook) # Test indexing based on index signals notebook.index.check_and_update() view = TasksView.new_from_index(notebook.index) for tasks in ( list(view.list_open_tasks()), list(view.list_open_tasks_flatlist()), ): self.assertTrue(len(tasks) > 5) for task in tasks: path = view.get_path(task) self.assertTrue(not path is None)
def testMainWindowExtensions(self): pluginklass = PluginManager.get_plugin_class('calendar') plugin = pluginklass() notebook = tests.new_notebook(self.get_tmp_name()) ui = setupGtkInterface(self, notebook=notebook) plugin.preferences['embedded'] = True self.assertEqual(plugin.extension_classes['MainWindow'], MainWindowExtensionEmbedded) plugin.extend(ui.mainwindow) ext = list(plugin.extensions) self.assertEqual(len(ext), 1) self.assertIsInstance(ext[0], MainWindowExtensionEmbedded) plugin.preferences.changed() # make sure no errors are triggered ext[0].go_page_today() self.assertTrue(ui.page.name.startswith('Journal:')) plugin.preferences['embedded'] = False self.assertEqual(plugin.extension_classes['MainWindow'], MainWindowExtensionDialog) plugin.extend(ui.mainwindow) # plugin does not remember objects, manager does that ext = list(plugin.extensions) self.assertEqual(len(ext), 1) self.assertIsInstance(ext[0], MainWindowExtensionDialog) plugin.preferences.changed() # make sure no errors are triggered def test_dialog(dialog): self.assertIsInstance(dialog, CalendarDialog) dialog.do_today('xxx') ui.open_page(Path('foo')) with tests.DialogContext(test_dialog): ext[0].show_calendar() plugin.preferences['embedded'] = True # switch back
def runTest(self): pluginklass = PluginManager.get_plugin_class('inlinecalculator') plugin = pluginklass() for text, wanted in ( ('3 + 4 =', '3 + 4 = 7'), ('3 + 4 = 1', '3 + 4 = 7'), ('3 + 4 = 1 ', '3 + 4 = 7 '), ('10 / 3 =', '10 / 3 = 3.3333333333333335'), # divide integers to float ! ('milage: 3 + 4 =', 'milage: 3 + 4 = 7'), ('3 + 4 = 7 + 0.5 = ', '3 + 4 = 7 + 0.5 = 7.5'), ('''\ 5.5 4.3 3.1 --- + ''', '''\ 5.5 4.3 3.1 --- + 12.9 '''), ): result = plugin.process_text(text) self.assertEqual(result, wanted) # Tests from clac.py self test for test in '''\ 1+2 == 3 sqrt(-1) == j -2*asin(-1) == pi abs(sin(pi)) < 1e-9 abs(1-cos(0)) < 1e-9 round( 3.1 + -4.8j) == (3-5j) ceil( 3.1 + -4.8j) == (4-4j) abs( 3-4j) == 5 degrees(pi) == 180 radians(180) == pi abs( exp(j*pi) + 1 ) < 1e-9 # pow(1.2,3.4) == 1.2**3.4 ldexp(1.2,3) == 1.2 * 2 ** 3 modf(1.2)[1] == 1 log(81,3) == 4 gcd(6,8) == 2 lcm(6,8) == 24 angle( exp( j*pi ) ) == pi # log(-1)**2 == -1*pow(pi,2) round( degrees(phase( e**(2j)))) == 115 # sum( [ round(42 * exp(j*2*x*pi/4)) for x in range(4)] ) == 0 oct(8) == '0o10' 0x42-0o42-42 == -10 # 1k == 1024 # 1m == 2**20 # 1g == 2**30 2**10-1 == 1023 # 2**1k == 2**1024 '''.splitlines(): if test.startswith('#'): continue # print('TESTING:', test) self.assertTrue(plugin.safe_eval(test)) self.assertRaises(Exception, plugin.process_text, 'open("/etc/passwd")') # global self.assertRaises(Exception, plugin.process_text, 'self') # local
def testParsing(self): klass = PluginManager.get_plugin_class('tasklist') plugin = klass() notebook = tests.new_notebook() plugin.extend(notebook.index) index_ext = plugin.get_extension(IndexExtension) self.assertIsNotNone(index_ext) # Test correctnest of parsing NO_DATE = '9999' def extract_tasks(text): # Returns a nested list of tuples, where each node is # like "(TASK, [CHILD, ...]) where each task (and child) # is a tuple like (open, actionable, prio, due, description) parser = zim.formats.get_format('wiki').Parser() tree = parser.parse(text) origtree = tree.tostring() #~ print 'TREE', origtree tasks = index_ext._extract_tasks(tree) self.assertEqual(tree.tostring(), origtree) # extract should not modify the tree return tasks def t(label, open=True, due=NO_DATE, prio=0, tags='', actionable=True): # Generate a task tuple # (open, actionable, prio, due, tags, description) if tags: tags = set(unicode(tags).split(',')) else: tags = set() return [open, actionable, prio, due, tags, unicode(label)] # Note that this same text is in the test notebook # so it gets run through the index as well - keep in sync text = '''\ Try all kind of combos - see if the parser trips TODO: [ ] A [ ] B [ ] C [ ] D [ ] E FIXME: dus ~~FIXME:~~ foo [ ] Simple [ ] List [ ] List with [ ] Nested items [*] Some are done [*] Done but with open child [x] Others not [ ] FOOOOO [ ] Bar [ ] And then there are @tags [ ] Next: And due dates [ ] Date [d: 11/12] [ ] Date [d: 11/12/2012] [ ] TODO: BAR !!! TODO @home: [ ] Some more tasks !!! [ ] Foo ! * some sub item * some other item [ ] Bar TODO: dus FIXME: jaja - TODO !! @FIXME ~~TODO~~: Ignore this one - it is strike out * TODO: dus - list item * FIXME: jaja - TODO !! @FIXME - list item * ~~TODO~~: Ignore this one - it is strike out - list item * Bullet list * With tasks as sub items [ ] Sub item bullets * dus 1. Numbered list 2. With tasks as sub items [ ] Sub item numbered 3. dus Test task inheritance: [ ] Main @tag1 @tag2 ! [*] Sub1 [ ] Sub2 @tag3 !!!! [*] Sub2-1 [*] Sub2-2 @tag4 [ ] Sub2-3 [ ] Sub3 TODO: @someday [ ] A [ ] B [ ] B-1 [ ] C TODO @home [ ] main task [x] do this [ ] Next: do that [ ] Next: do something else ''' mydate = '%04i-%02i-%02i' % parse_date('11/12') wanted = [ (t('A'), []), (t('B'), []), (t('C'), []), (t('D'), []), (t('E'), []), (t('FIXME: dus'), []), (t('Simple'), []), (t('List'), []), (t('List with'), [ (t('Nested items'), []), (t('Some are done', open=False), []), (t('Done but with open child', open=True), [ (t('Others not', open=False), []), (t('FOOOOO'), []), ]), ]), (t('Bar'), []), (t('And then there are @tags', tags='tags'), []), (t('Next: And due dates', actionable=False), []), (t('Date [d: 11/12]', due=mydate), []), (t('Date [d: 11/12/2012]', due='2012-12-11'), [ (t('TODO: BAR !!!', prio=3, due='2012-12-11'), []), # due date is inherited ]), # this list inherits the @home tag - and inherits prio (t('Some more tasks !!!', prio=3, tags='home'), [ (t('Foo !', prio=1, tags='home'), []), (t('Bar', prio=3, tags='home'), []), ]), (t('TODO: dus'), []), (t('FIXME: jaja - TODO !! @FIXME', prio=2, tags='FIXME'), []), (t('TODO: dus - list item'), []), (t('FIXME: jaja - TODO !! @FIXME - list item', prio=2, tags='FIXME'), []), (t('Sub item bullets'), []), (t('Sub item numbered'), []), (t('Main @tag1 @tag2 !', prio=1, tags='tag1,tag2'), [ (t('Sub1', prio=1, open=False, tags='tag1,tag2'), []), (t('Sub2 @tag3 !!!!', prio=4, tags='tag1,tag2,tag3'), [ (t('Sub2-1', prio=4, open=False, tags='tag1,tag2,tag3'), []), (t('Sub2-2 @tag4', prio=4, open=False, tags='tag1,tag2,tag3,tag4'), []), (t('Sub2-3', prio=4, tags='tag1,tag2,tag3'), []), ]), (t('Sub3', prio=1, tags='tag1,tag2'), []), ]), (t('A', tags='someday', actionable=False), []), (t('B', tags='someday', actionable=False), [ (t('B-1', tags='someday', actionable=False), []), ]), (t('C', tags='someday', actionable=False), []), (t('main task', tags='home'), [ (t('do this', open=False, tags='home'), []), (t('Next: do that', tags='home'), []), (t('Next: do something else', tags='home', actionable=False), []), ]) ] plugin.preferences['nonactionable_tags'] = '@someday, @maybe' index_ext._set_preferences() tasks = extract_tasks(text) self.assertEqual(tasks, wanted) plugin.preferences['all_checkboxes'] = False wanted = [ (t('A'), []), (t('B'), []), (t('C'), []), (t('FIXME: dus'), []), (t('Next: And due dates', actionable=False), []), (t('TODO: BAR !!!', prio=3), []), # this list inherits the @home tag - and inherits prio (t('Some more tasks !!!', prio=3, tags='home'), [ (t('Foo !', prio=1, tags='home'), []), (t('Bar', prio=3, tags='home'), []), ]), (t('TODO: dus'), []), (t('FIXME: jaja - TODO !! @FIXME', prio=2, tags='FIXME'), []), (t('TODO: dus - list item'), []), (t('FIXME: jaja - TODO !! @FIXME - list item', prio=2, tags='FIXME'), []), (t('A', tags='someday', actionable=False), []), (t('B', tags='someday', actionable=False), [ (t('B-1', tags='someday', actionable=False), []), ]), (t('C', tags='someday', actionable=False), []), (t('main task', tags='home'), [ (t('do this', open=False, tags='home'), []), (t('Next: do that', tags='home'), []), (t('Next: do something else', tags='home', actionable=False), []), ]) ] tasks = extract_tasks(text) self.assertEqual(tasks, wanted)
def runTest(self): 'Test InlineCalculator plugin' pluginklass = PluginManager.get_plugin_class('inlinecalculator') plugin = pluginklass() for text, wanted in ( ('3 + 4 =', '3 + 4 = 7'), ('3 + 4 = 1', '3 + 4 = 7'), ('3 + 4 = 1 ', '3 + 4 = 7 '), ('10 / 3 =', '10 / 3 = 3.33333333333'), # divide integers to float ! ('milage: 3 + 4 =', 'milage: 3 + 4 = 7'), ('3 + 4 = 7 + 0.5 = ', '3 + 4 = 7 + 0.5 = 7.5'), ('''\ 5.5 4.3 3.1 --- + ''', '''\ 5.5 4.3 3.1 --- + 12.9 ''' ), ): result = plugin.process_text(text) self.assertEqual(result, wanted) # Tests from clac.py self test for test in '''\ 1+2 == 3 sqrt(-1) == j -2*asin(-1) == pi abs(sin(pi)) < 1e-9 abs(1-cos(0)) < 1e-9 round( 3.1 + -4.8j) == (3-5j) ceil( 3.1 + -4.8j) == (4-4j) abs( 3-4j) == 5 degrees(pi) == 180 radians(180) == pi abs( exp(j*pi) + 1 ) < 1e-9 # pow(1.2,3.4) == 1.2**3.4 ldexp(1.2,3) == 1.2 * 2 ** 3 modf(1.2)[1] == 1 log(81,3) == 4 gcd(6,8) == 2 lcm(6,8) == 24 angle( exp( j*pi ) ) == pi # log(-1)**2 == -1*pow(pi,2) round( degrees(phase( e**(2j)))) == 115 # sum( [ round(42 * exp(j*2*x*pi/4)) for x in range(4)] ) == 0 oct(8) == '010' 0x42-042-42 == -10 # 1k == 1024 # 1m == 2**20 # 1g == 2**30 2**10-1 == 1023 # 2**1k == 2**1024 '''.splitlines(): if test.startswith('#'): continue #~ print 'TESTING:', test self.assertTrue(plugin.safe_eval(test)) self.assertRaises(Exception, plugin.process_text, 'open("/etc/passwd")') # global self.assertRaises(Exception, plugin.process_text, 'self') # local
def testParsing(self): klass = PluginManager.get_plugin_class('tasklist') plugin = klass() notebook = tests.new_notebook() plugin.extend(notebook.index) index_ext = plugin.get_extension(IndexExtension) self.assertIsNotNone(index_ext) # Test correctnest of parsing NO_DATE = '9999' def extract_tasks(text): # Returns a nested list of tuples, where each node is # like "(TASK, [CHILD, ...]) where each task (and child) # is a tuple like (open, actionable, prio, due, description) parser = zim.formats.get_format('wiki').Parser() tree = parser.parse(text) origtree = tree.tostring() #~ print 'TREE', origtree tasks = index_ext._extract_tasks(tree) self.assertEqual(tree.tostring(), origtree) # extract should not modify the tree return tasks def t(label, open=True, due=NO_DATE, prio=0, tags='', actionable=True): # Generate a task tuple # (open, actionable, prio, due, tags, description) if tags: tags = set(unicode(tags).split(',')) else: tags = set() return [open, actionable, prio, due, tags, unicode(label)] # Note that this same text is in the test notebook # so it gets run through the index as well - keep in sync text = '''\ Try all kind of combos - see if the parser trips TODO: [ ] A [ ] B [ ] C [ ] D [ ] E FIXME: dus ~~FIXME:~~ foo [ ] Simple [ ] List [ ] List with [ ] Nested items [*] Some are done [*] Done but with open child [x] Others not [ ] FOOOOO [ ] Bar [ ] And then there are @tags [ ] And due dates [ ] Date [d: 11/12] [ ] Date [d: 11/12/2012] [ ] TODO: BAR !!! TODO @home: [ ] Some more tasks !!! [ ] Foo ! * some sub item * some other item [ ] Bar TODO: dus FIXME: jaja - TODO !! @FIXME ~~TODO~~: Ignore this one - it is strike out * TODO: dus - list item * FIXME: jaja - TODO !! @FIXME - list item * ~~TODO~~: Ignore this one - it is strike out - list item * Bullet list * With tasks as sub items [ ] Sub item bullets * dus 1. Numbered list 2. With tasks as sub items [ ] Sub item numbered 3. dus Test task inheritance: [ ] Main @tag1 @tag2 ! [*] Sub1 [ ] Sub2 @tag3 !!!! [*] Sub2-1 [*] Sub2-2 @tag4 [ ] Sub2-3 [ ] Sub3 TODO: @someday [ ] A [ ] B [ ] B-1 [ ] C ''' mydate = '%04i-%02i-%02i' % parse_date('11/12') wanted = [ (t('A'), []), (t('B'), []), (t('C'), []), (t('D'), []), (t('E'), []), (t('FIXME: dus'), []), (t('Simple'), []), (t('List'), []), (t('List with'), [ (t('Nested items'), []), (t('Some are done', open=False), []), (t('Done but with open child', open=True), [ (t('Others not', open=False), []), (t('FOOOOO'), []), ]), ]), (t('Bar'), []), (t('And then there are @tags', tags='tags'), []), (t('And due dates'), []), (t('Date [d: 11/12]', due=mydate), []), (t('Date [d: 11/12/2012]', due='2012-12-11'), [ (t('TODO: BAR !!!', prio=3, due='2012-12-11'), []), # due date is inherited ]), # this list inherits the @home tag - and inherits prio (t('Some more tasks !!!', prio=3, tags='home'), [ (t('Foo !', prio=1, tags='home'), []), (t('Bar', prio=3, tags='home'), []), ]), (t('TODO: dus'), []), (t('FIXME: jaja - TODO !! @FIXME', prio=2, tags='FIXME'), []), (t('TODO: dus - list item'), []), (t('FIXME: jaja - TODO !! @FIXME - list item', prio=2, tags='FIXME'), []), (t('Sub item bullets'), []), (t('Sub item numbered'), []), (t('Main @tag1 @tag2 !', prio=1, tags='tag1,tag2'), [ (t('Sub1', prio=1, open=False, tags='tag1,tag2'), []), (t('Sub2 @tag3 !!!!', prio=4, tags='tag1,tag2,tag3'), [ (t('Sub2-1', prio=4, open=False, tags='tag1,tag2,tag3'), []), (t('Sub2-2 @tag4', prio=4, open=False, tags='tag1,tag2,tag3,tag4'), []), (t('Sub2-3', prio=4, tags='tag1,tag2,tag3'), []), ]), (t('Sub3', prio=1, tags='tag1,tag2'), []), ]), (t('A', tags='someday', actionable=False), []), (t('B', tags='someday', actionable=False), [ (t('B-1', tags='someday', actionable=False), []), ]), (t('C', tags='someday', actionable=False), []), ] plugin.preferences['nonactionable_tags'] = '@someday, @maybe' index_ext._set_preferences() tasks = extract_tasks(text) self.assertEqual(tasks, wanted) plugin.preferences['all_checkboxes'] = False wanted = [ (t('A'), []), (t('B'), []), (t('C'), []), (t('FIXME: dus'), []), (t('TODO: BAR !!!', prio=3), []), # this list inherits the @home tag - and inherits prio (t('Some more tasks !!!', prio=3, tags='home'), [ (t('Foo !', prio=1, tags='home'), []), (t('Bar', prio=3, tags='home'), []), ]), (t('TODO: dus'), []), (t('FIXME: jaja - TODO !! @FIXME', prio=2, tags='FIXME'), []), (t('TODO: dus - list item'), []), (t('FIXME: jaja - TODO !! @FIXME - list item', prio=2, tags='FIXME'), []), (t('A', tags='someday', actionable=False), []), (t('B', tags='someday', actionable=False), [ (t('B-1', tags='someday', actionable=False), []), ]), (t('C', tags='someday', actionable=False), []), ] tasks = extract_tasks(text) self.assertEqual(tasks, wanted)