1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
# Copyright 2009-2019 Jaap Karssenberg <jaap.karssenberg@gmail.com>
import tests
from tests.mainwindow import setUpMainWindow
from tests.pageview import setUpPageView
from zim.formats.wiki import Dumper as WikiDumper
from gi.repository import Gtk
from zim.newfs import LocalFolder
from zim.notebook import Path
from zim.plugins import PluginManager
from zim.plugins.base.imagegenerator import \
ImageGeneratorClass, ImageGeneratorDialog, BackwardImageGeneratorObjectType
from zim.plugins.equationeditor import InsertEquationPlugin
from zim.plugins.diagrameditor import InsertDiagramPlugin
from zim.plugins.gnu_r_ploteditor import InsertGNURPlotPlugin
from zim.plugins.gnuplot_ploteditor import InsertGnuplotPlugin
from zim.plugins.scoreeditor import InsertScorePlugin
from zim.plugins.ditaaeditor import InsertDitaaPlugin
from zim.plugins.sequencediagrameditor import InsertSequenceDiagramPlugin
def assertIsPNG(file):
with open(file.path, "rb") as fh:
data = fh.read(8)
assert data == b'\x89PNG\x0d\x0a\x1a\x0a', 'Not a PNG file: %s' % file.path
@tests.slowTest
class TestBackwardImageGeneratorNoPlugins(tests.TestCase):
def testDumpWiki(self):
attachment_dir = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
tests.ZIM_DATA_FOLDER.file('zim.png').copyto(attachment_dir.file('test.png'))
notebook = self.setUpNotebook()
notebook.get_attachments_dir = lambda *a: attachment_dir
pageview = setUpPageView(notebook, text='{{./test.png?type=equation}}')
pageview.textview.get_buffer().set_modified(True)
tree = pageview.page.get_parsetree()
text = WikiDumper().dump(tree)
self.assertEqual(text, ['{{./test.png?type=equation}}'])
@tests.skipUnless(InsertEquationPlugin.check_dependencies_ok(), 'Missing dependencies')
class TestBackwardImageGeneratorWithPlugin(TestBackwardImageGeneratorNoPlugins):
def setUp(self):
PluginManager.load_plugin('equationeditor')
def testInsertObjectDialog(self):
attachment_dir = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
notebook = self.setUpNotebook()
notebook.get_attachments_dir = lambda *a: attachment_dir
page = notebook.get_page(Path('Test'))
otype = PluginManager.insertedobjects['image+equation']
def edit_dialog(dialog):
self.assertIsInstance(dialog, ImageGeneratorDialog)
dialog.set_text(r'c = \sqrt{ a^2 + b^2 }')
dialog.update_image()
dialog.assert_response_ok()
with tests.DialogContext(edit_dialog):
model = otype.new_model_interactive(None, notebook, page)
attrib, data = otype.data_from_model(model)
self.assertTrue(attrib['src'])
self.assertEqual(attachment_dir.file('equation.tex').read(), r'c = \sqrt{ a^2 + b^2 }')
assertIsPNG(attachment_dir.file('equation.png'))
def testEditObjectDialog(self):
attachment_dir = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
tests.ZIM_DATA_FOLDER.file('zim.png').copyto(attachment_dir.file('test.png'))
notebook = self.setUpNotebook()
notebook.get_attachments_dir = lambda *a: attachment_dir
pageview = setUpPageView(notebook, text='{{./test.png?type=equation}}')
def edit_dialog(dialog):
self.assertIsInstance(dialog, ImageGeneratorDialog)
dialog.set_text(r'c = \sqrt{ a^2 + b^2 }')
dialog.update_image()
dialog.assert_response_ok()
with tests.DialogContext(edit_dialog):
pageview.edit_object()
self.assertEqual(attachment_dir.file('test.tex').read(), r'c = \sqrt{ a^2 + b^2 }')
assertIsPNG(attachment_dir.file('test.png'))
def testNewFile(self):
attachment_dir = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
notebook = self.setUpNotebook()
notebook.get_attachments_dir = lambda *a: attachment_dir
page = notebook.get_page(Path('Test'))
for name in (
'equation.png', 'equation.tex',
'equation-001.png', 'equation-001.tex',
'equation-002.tex',
'equation-003.png',
'equation-004.png', 'equation-004.tex',
):
attachment_dir.file(name).touch()
otype = PluginManager.insertedobjects['image+equation']
model = otype.model_from_data(notebook, page, {}, '')
self.assertEqual(model.image_file.basename, 'equation-005.png')
self.assertEqual(model.script_file.basename, 'equation-005.tex')
@tests.slowTest
class TestImageGeneratorPluginMixin(object):
plugin = None
object_types = None
validinput = None
invalidinput = None
def testObjectTypes(self):
PluginManager.load_plugin(self.plugin)
notebook = self.setUpNotebook()
page = notebook.get_page(Path('Test'))
for name in self.object_types:
otype = PluginManager.insertedobjects[name]
attrib, data = otype.new_object()
model = otype.model_from_data(notebook, page, attrib, data)
self.assertIsNotNone(model)
widget = otype.create_widget(model)
self.assertIsNotNone(widget)
attrib, data = otype.data_from_model(model)
self.assertIsNotNone(otype.label)
if isinstance(model, BackwardImageGeneratorObjectType):
self.assertIsNotNone(otype.scriptname)
self.assertIsNotNone(otype.imagefile_extension)
def testGenerator(self):
plugin = PluginManager.load_plugin(self.plugin)
notebook = self.setUpNotebook()
page = notebook.get_page(Path('Test'))
generator_classes = list(plugin.discover_classes(ImageGeneratorClass))
assert len(generator_classes) == 1
generator_class = generator_classes[0]
for name in self.object_types:
if name.startswith('image+'):
backward_otype = PluginManager.insertedobjects[name]
break
else:
backward_otype = None
# Input OK
generator = generator_class(plugin, notebook, page)
generator.cleanup() # ensure files did not yet exist
imagefile, logfile = generator.generate_image(self.validinput)
if imagefile.path.endswith('.png'):
assertIsPNG(imagefile)
# else: TODO other types
self.assertTrue(imagefile.exists())
if logfile is not None:
self.assertTrue(logfile.exists())
# Cleanup
generator.cleanup()
self.assertFalse(imagefile.exists())
if logfile is not None:
self.assertFalse(logfile.exists())
# Input NOK
if self.invalidinput is not None:
generator = generator_class(plugin, notebook, page)
imagefile, logfile = generator.generate_image(self.invalidinput)
self.assertIsNone(imagefile)
if logfile is not None:
self.assertTrue(logfile.exists())
@tests.skipUnless(InsertEquationPlugin.check_dependencies_ok(), 'Missing dependencies')
class TestEquationEditor(TestImageGeneratorPluginMixin, tests.TestCase):
plugin = 'equationeditor'
object_types = ['image+equation']
validinput = r'''
c = \sqrt{ a^2 + b^2 }
\int_{-\infty}^{\infty} \frac{1}{x} \, dx
f(x) = \sum_{n = 0}^{\infty} \alpha_n x^n
x_{1,2}=\frac{-b\pm\sqrt{\color{Red}b^2-4ac}}{2a}
\hat a \bar b \vec c x' \dot{x} \ddot{x}
'''
invalidinput = r'\int_{'
def testLatexExport(self):
from zim.formats.wiki import Parser as WikiParser
from zim.formats.latex import Dumper as LatexDumper
folder = self.setUpFolder()
folder.file('equation001.tex').write('a + b')
# equation002.tex does not exist - check fallback to image
wiki = '{{./equation001.png?type=equation}}\n{{./equation002.png?type=equation}}\n'
wanted = '\\begin{math}\na + b\n\\end{math}\n\n\\includegraphics[]{./equation002.png}\n\n'
linker = tests.MockObject()
linker.resolve_source_file = lambda name: folder.file(name)
linker.img = lambda name: name
tree = WikiParser().parse(wiki)
latex = LatexDumper(linker).dump(tree)
self.assertEqual(latex, wanted.splitlines(True))
@tests.skipUnless(InsertDiagramPlugin.check_dependencies_ok(), 'Missing dependencies')
class TestDiagramEditor(TestImageGeneratorPluginMixin, tests.TestCase):
plugin = 'diagrameditor'
object_types = ['image+diagram']
validinput = r'''
digraph G {
foo -> bar
bar -> baz
baz -> foo
}
'''
invalidinput = r'sdf sdfsdf sdf'
@tests.skipUnless(InsertGNURPlotPlugin.check_dependencies_ok(), 'Missing dependencies')
class TestGNURPlotEditor(TestImageGeneratorPluginMixin, tests.TestCase):
plugin = 'gnu_r_ploteditor'
object_types = ['image+gnu_r_plot']
validinput = r'''
x = seq(-4,4,by=0.01)
y = sin(x) + 1
plot(x,y,type='l')
'''
invalidinput = r'sdf sdfsdf sdf'
@tests.skipUnless(InsertGnuplotPlugin.check_dependencies_ok(), 'Missing dependencies')
class TestGnuplotEditor(TestImageGeneratorPluginMixin, tests.TestCase):
plugin = 'gnuplot_ploteditor'
object_types = ['image+gnuplot']
validinput = r'plot sin(x), cos(x)'
invalidinput = r'sdf sdfsdf sdf'
@tests.skipUnless(InsertScorePlugin.check_dependencies_ok(), 'Missing dependencies')
class TestScoreEditor(TestImageGeneratorPluginMixin, tests.TestCase):
plugin = 'scoreeditor'
object_types = ['image+score']
validinput = r'''
\version "2.18.2"
\relative c {
\clef bass
\key d \major
\time 4/4
d4 a b fis
g4 d g a
}
'''
invalidinput = r'sdf sdfsdf sdf'
@tests.skipUnless(InsertDitaaPlugin.check_dependencies_ok(), 'Missing dependencies')
class TestDitaaEditor(TestImageGeneratorPluginMixin, tests.TestCase):
plugin = 'ditaaeditor'
object_types = ['image+ditaa']
def setUp(self):
self.validinput = r'''
+--------+ +-------+ +-------+
| | --+ ditaa +--> | |
| Text | +-------+ |diagram|
|Document| |!magic!| | |
| {d}| | | | |
+---+----+ +-------+ +-------+
: ^
| Lots of work |
+-------------------------+
'''
self.invalidinput = None # ditaa seems to render anything ...
@tests.skipUnless(InsertSequenceDiagramPlugin.check_dependencies_ok(), 'Missing dependencies')
class TestSequenceDiagramEditor(TestImageGeneratorPluginMixin, tests.TestCase):
plugin = 'sequencediagrameditor'
object_types = ['image+seqdiagram']
def setUp(self):
self.validinput = r'''
seqdiag {
browser -> webserver [label = "GET /index.html"];
browser <-- webserver;
browser -> webserver [label = "POST /blog/comment"];
webserver -> database [label = "INSERT comment"];
webserver <-- database;
browser <-- webserver;
}
'''
self.invalidinput = 'sdfsdf sdfsdf'
|