"Fossies" - the Fresh Open Source Software Archive

Member "zim-0.71.1/tests/tableeditor.py" (1 Apr 2019, 7256 Bytes) of package /linux/privat/zim-0.71.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "tableeditor.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.69.1_vs_0.70.

    1 
    2 # Copyright 2015 Tobias Haupenthal
    3 # Copyright 2016-2018 Jaap Karssenberg <jaap.karssenberg@gmail.com>
    4 
    5 
    6 import tests
    7 
    8 from zim.formats import ParseTree, StubLinker, OldParseTreeBuilder
    9 from zim.formats.wiki import Parser as WikiParser
   10 from zim.formats.wiki import Dumper as WikiDumper
   11 from zim.formats.html import Dumper as HtmlDumper
   12 from zim.notebook import Path
   13 
   14 from zim.plugins import PluginManager
   15 from zim.plugins.tableeditor import *
   16 
   17 from zim.gui.insertedobjects import UnknownInsertedObject
   18 
   19 from tests.mainwindow import setUpMainWindow
   20 from tests.pageview import setUpPageView
   21 
   22 
   23 def get_gtk_action(uimanager, name):
   24     for group in uimanager.get_action_groups():
   25         action = group.get_action(name)
   26         if action is not None:
   27             return action
   28     else:
   29         raise ValueError
   30 
   31 
   32 TABLE_WIKI_TEXT = '''\
   33 
   34 |        H1       <|         H2 h2 | H3                    <|
   35 |:----------------:|--------------:|:-----------------------|
   36 |    Column A1     |     Column A2 | a                      |
   37 | a very long cell | **bold text** | b                      |
   38 |    hyperlinks    |   [[wp?wiki]] | [[http://x.org\|Xorg]] |
   39 
   40 '''
   41 
   42 TABLE_OBJECT_TEXT = '''\
   43 
   44 {{{table:
   45 %s
   46 }}}
   47 
   48 ''' % TABLE_WIKI_TEXT.strip()
   49 
   50 TABLE_TOKENS = [
   51     ('zim-tree', {}),
   52         ('T', '\n'),
   53         ('table', {'aligns': 'center,right,left', 'wraps': '1,0,1'}),
   54             ('thead', {}),
   55                 ('th', {}), ('T', 'H1'), ('/', 'th'),
   56                 ('th', {}), ('T', 'H2 h2'), ('/', 'th'),
   57                 ('th', {}), ('T', 'H3'), ('/', 'th'),
   58             ('/', 'thead'),
   59             ('trow', {}),
   60                 ('td', {}), ('T', 'Column A1'), ('/', 'td'),
   61                 ('td', {}), ('T', 'Column A2'), ('/', 'td'),
   62                 ('td', {}), ('T', 'a'), ('/', 'td'),
   63             ('/', 'trow'),
   64             ('trow', {}),
   65                 ('td', {}), ('T', 'a very long cell'), ('/', 'td'),
   66                 ('td', {}), ('strong', {}), ('T', 'bold text'), ('/', 'strong'), ('/', 'td'),
   67                 ('td', {}), ('T', 'b'), ('/', 'td'),
   68             ('/', 'trow'),
   69             ('trow', {}),
   70                 ('td', {}), ('T', 'hyperlinks'), ('/', 'td'),
   71                 ('td', {}), ('link', {'href': 'wp?wiki'}), ('T', 'wp?wiki'), ('/', 'link'), ('/', 'td'),
   72                 ('td', {}), ('link', {'href': 'http://x.org'}), ('T', 'Xorg'), ('/', 'link'), ('/', 'td'),
   73             ('/', 'trow'),
   74         ('/', 'table'),
   75         ('T', '\n'),
   76     ('/', 'zim-tree')
   77 ]
   78 
   79 
   80 class TestWikiSyntaxNoPlugin(tests.TestCase):
   81 
   82     def parseAndDump(self, text):
   83         tree = WikiParser().parse(text)
   84         self.assertEquals(list(tree.iter_tokens()), TABLE_TOKENS)
   85 
   86     def testWikiText(self):
   87         self.parseAndDump(TABLE_WIKI_TEXT)
   88 
   89     def testObectText(self):
   90         # This test is important to ensure backward compatibility with previous
   91         # versions that would write out an object when the table plugin wasn't
   92         # loaded
   93         self.parseAndDump(TABLE_OBJECT_TEXT)
   94 
   95 
   96 class TestWikiSyntaxWithPlugin(TestWikiSyntaxNoPlugin):
   97 
   98     def setUp(self):
   99         PluginManager.load_plugin('tableeditor')
  100 
  101 
  102 class TestTableObjectType(tests.TestCase):
  103 
  104     def setUp(self):
  105         PluginManager.load_plugin('tableeditor')
  106         self.otype = PluginManager.insertedobjects['table']
  107 
  108     def testModelFromElement(self):
  109         tree = WikiParser().parse(TABLE_WIKI_TEXT)
  110         element = tree._etree.getroot().find('table')
  111         self.assertIsNotNone(element)
  112         model = self.otype.model_from_element(element.attrib, element)
  113 
  114         builder = OldParseTreeBuilder() # XXX
  115         builder.start('zim-tree')
  116         self.otype.dump(builder, model)
  117         builder.end('zim-tree')
  118         tree = ParseTree(builder.close())
  119 
  120         #self.assertEquals(list(tree.iter_tokens()), TABLE_TOKENS) -- XXX should work but doesn;t :(
  121         self.assertEquals(''.join(WikiDumper().dump(tree)), TABLE_WIKI_TEXT[1:-1])
  122 
  123     def testModelFromData(self):
  124         notebook = self.setUpNotebook()
  125         page = notebook.get_page(Path('Test'))
  126         model = self.otype.model_from_data(notebook, page, {}, TABLE_WIKI_TEXT)
  127 
  128         builder = OldParseTreeBuilder() # XXX
  129         builder.start('zim-tree')
  130         self.otype.dump(builder, model)
  131         builder.end('zim-tree')
  132         tree = ParseTree(builder.close())
  133 
  134         #self.assertEquals(list(tree.iter_tokens()), TABLE_TOKENS) -- XXX should work but doesn;t :(
  135         self.assertEquals(''.join(WikiDumper().dump(tree)), TABLE_WIKI_TEXT[1:-1])
  136 
  137 
  138 class TestPageViewNoPlugin(tests.TestCase):
  139 
  140     def setUp(self):
  141         self.assertNotIn('table', PluginManager.insertedobjects)
  142 
  143     def testLoadAndDump(self):
  144         pageview = setUpPageView(
  145             self.setUpNotebook(),
  146             text=TABLE_WIKI_TEXT,
  147         )
  148         pageview.textview.get_buffer().set_modified(True) # Force re-interpretation of the buffer
  149         tree = pageview.get_parsetree()
  150         self.assertEquals(list(tree.iter_tokens()), TABLE_TOKENS)
  151 
  152 
  153 class TestPageViewWithPlugin(TestPageViewNoPlugin):
  154 
  155     def setUp(self):
  156         PluginManager.load_plugin('tableeditor')
  157         self.assertIn('table', PluginManager.insertedobjects)
  158 
  159     def testWidget(self):
  160         pageview = setUpPageView(
  161             self.setUpNotebook(),
  162             text=TABLE_WIKI_TEXT,
  163         )
  164         # test widget loaded
  165         objects = list(pageview.textview._object_widgets) # XXX
  166         self.assertIsInstance(objects[0], TableViewWidget)
  167 
  168         # test modify
  169         #widget = objects[0]
  170         #self.assertFalse(pageview.textview.get_buffer().get_modified())
  171         # FIXME: change content via widget
  172         #self.assertTrue(pageview.textview.get_buffer().get_modified())
  173 
  174         # test modification ends up in page
  175         tree = pageview.get_parsetree()
  176         #print(tree.tostring())
  177         elt = tree.find('table')
  178         self.assertIsNotNone(elt)
  179         # FIXME: test content
  180 
  181     def testInsertDialog(self):
  182         window = setUpMainWindow(self.setUpNotebook(content={'Test': 'Test 123'}), path='Test')
  183         action = get_gtk_action(window.uimanager, 'insert_table')
  184 
  185         def insert_table(dialog):
  186             self.assertIsInstance(dialog, EditTableDialog)
  187             dialog.assert_response_ok()
  188 
  189         with tests.DialogContext(insert_table):
  190             action.activate()
  191 
  192         tree = window.pageview.get_parsetree()
  193         elt = tree.find('table')
  194         self.assertIsNotNone(elt)
  195 
  196     def testInsertDialogCancelled(self):
  197         window = setUpMainWindow(self.setUpNotebook(content={'Test': 'Test 123'}), path='Test')
  198         action = get_gtk_action(window.uimanager, 'insert_table')
  199 
  200         def cancel_dialog(dialog):
  201             self.assertIsInstance(dialog, EditTableDialog)
  202             dialog.response(Gtk.ResponseType.CANCEL)
  203 
  204         with tests.DialogContext(cancel_dialog):
  205             action.activate()
  206 
  207         tree = window.pageview.get_parsetree()
  208         elt = tree.find('table')
  209         self.assertIsNone(elt)
  210 
  211 
  212 
  213 class TestEditTable(tests.TestCase):
  214 
  215     def checkUpdateTableDialog(self, dialog):
  216         self.assertIsInstance(dialog, EditTableDialog)
  217         dialog.assert_response_ok()
  218 
  219     def runTest(self):
  220         attrib = {'aligns': 'normal,normal', 'wraps': '0,0'}
  221         headers = ['h1', 'h2']
  222         rows = [['t1', 't2'], ]
  223 
  224         model = TableModel(attrib, headers, rows)
  225         widget = TableViewWidget(model)
  226 
  227         with tests.DialogContext(self.checkUpdateTableDialog):
  228             widget.on_change_columns(None)
  229 
  230         self.assertTrue(isinstance(widget.treeview, Gtk.TreeView))
  231 
  232 
  233 class TestTableFunctions(tests.TestCase):
  234 
  235     def testCellFormater(self):
  236         self.assertEqual(CellFormatReplacer.input_to_cell('**hello**', with_pango=True), '<b>hello</b>')
  237         self.assertEqual(CellFormatReplacer.cell_to_input('<span background="yellow">highlight</span>', with_pango=True),
  238                          '__highlight__')
  239         self.assertEqual(CellFormatReplacer.zim_to_cell('<link href="./alink">hello</link>'),
  240                          '<span foreground="blue">hello<span size="0">./alink</span></span>')
  241         self.assertEqual(CellFormatReplacer.cell_to_zim('<tt>code-block</tt>'), '<code>code-block</code>')