File: spell.py

package info (click to toggle)
zim 0.76.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,952 kB
  • sloc: python: 68,612; xml: 1,270; javascript: 512; sh: 101; makefile: 47
file content (136 lines) | stat: -rw-r--r-- 3,802 bytes parent folder | download | duplicates (2)
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

# Copyright 2015,2023 Jaap Karssenberg <jaap.karssenberg@gmail.com>




import tests

try:
	import gi
	gi.require_version('GtkSpell', '3.0')
	from gi.repository import GtkSpell as gtkspell
except:
	gtkspell = None


try:
	import gi
	gi.require_version('Gspell', '1')
	from gi.repository import Gspell
except:
	Gspell = None


try:
	import gtkspellcheck
except:
	gtkspellcheck = None


import zim.plugins.spell

from zim.plugins import find_extension, PluginManager
from zim.notebook import Path

from tests.mainwindow import setUpMainWindow
from tests.pageview import TextBufferTestCaseMixin


class TestSpell(object):

	def setUp(self):
		self._restore = (
			zim.plugins.spell.gtkspell,
			zim.plugins.spell.Gspell,
			zim.plugins.spell.gtkspellcheck
		)

	def tearDown(self):
		zim.plugins.spell.gtkspell, zim.plugins.spell.Gspell, zim.plugins.spell.gtkspellcheck = \
			self._restore

	def runTest(self, adapterclass):
		with tests.LoggingFilter(logger='zim.plugins.spell'): # Hide exceptions
			window = setUpMainWindow(self.setUpNotebook(content=('Test', 'Foo', 'Bar')))

			plugin = PluginManager.load_plugin('spell')
			ext = find_extension(window.pageview, zim.plugins.spell.SpellPageViewExtension)

			self.assertIs(ext._adapter_cls, adapterclass) # ensure switching library worked

			ext.toggle_spellcheck()
			ext.toggle_spellcheck()
			ext.toggle_spellcheck()

			window.open_page(Path('Foo'))
			window.open_page(Path('Bar'))
			ext.toggle_spellcheck()

			window.open_page(Path('Foo'))
			window.open_page(Path('Bar'))
			#ext.toggle_spellcheck()

			# TODO check it actually shows on screen ...


@tests.skipIf(gtkspell is None, 'gtkspell not installed')
class TestGtkspell(TestSpell, tests.TestCase):

	def runTest(self):
		zim.plugins.spell.gtkspell = gtkspell
		zim.plugins.spell.Gspell = None
		zim.plugins.spell.gtkspellcheck = None
		TestSpell.runTest(self, zim.plugins.spell.GtkspellAdapter)


@tests.skipIf(Gspell is None, 'Gspell not installed')
class TestGspell(TestSpell, tests.TestCase):

	def runTest(self):
		zim.plugins.spell.Gspell = Gspell
		zim.plugins.spell.gtkspell = None
		zim.plugins.spell.gtkspellcheck = None
		TestSpell.runTest(self, zim.plugins.spell.GspellAdapter)


@tests.skipIf(gtkspellcheck is None, 'gtkspellcheck not installed')
class TestGtkspellchecker(TestSpell, tests.TestCase):

	def runTest(self):
		zim.plugins.spell.gtkspell = None
		zim.plugins.spell.Gspell = None
		zim.plugins.spell.gtkspellcheck = gtkspellcheck
		TestSpell.runTest(self, zim.plugins.spell.GtkspellcheckAdapter)


class TestReplaceWordWithFormatting(TextBufferTestCaseMixin, tests.TestCase):

	# Since spell checker is hard to trigger from test, copied relevant code from:
	# https://github.com/koehlma/pygtkspellcheck/blob/4a77ccea2e892d0379a31a66b0a863782a2ca44b/src/gtkspellcheck/spellcheck.py#L598
	def replace_word(self, buffer, start, end, new_word):
		offset = start.get_offset()
		buffer.begin_user_action()
		buffer.delete(start, end)
		buffer.insert(buffer.get_iter_at_offset(offset), new_word)
		buffer.end_user_action()

	def runTest(self):
		# Ensure that replacing a word the formatting is preserved
		buffer = self.get_buffer(
			'<strong>mispelld</strong>\n'
			'<emphasis>here misplled here as well</emphasis>\n\n'
		)
		new_word = "misspelled"
		start, end = buffer.get_iter_at_offset(0), buffer.get_iter_at_offset(8)
		self.replace_word(buffer, start, end, new_word)
		self.assertBufferEqual(buffer,
			'<strong>misspelled</strong>\n'
			'<emphasis>here misplled here as well</emphasis>\n\n'
		)
		start, end = buffer.get_iter_at_offset(16), buffer.get_iter_at_offset(24)
		self.replace_word(buffer, start, end, new_word)
		self.assertBufferEqual(buffer,
			'<strong>misspelled</strong>\n'
			'<emphasis>here misspelled here as well</emphasis>\n\n'
		)