Debian Codegraph @

tests/unit_tests/test_pronunciation_dictionary_manager.py

raw
1 # Unit tests for pronunciation_dictionary_manager.py.
2 #
3 # Copyright 2026 Igalia, S.L.
4 # Author: Joanmarie Diggs <[email protected]>
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., Franklin Street, Fifth Floor,
19 # Boston MA 02110-1301 USA.
20
21 # pylint: disable=wrong-import-position
22 # pylint: disable=protected-access
23 # pylint: disable=import-outside-toplevel
24
25 """Unit tests for pronunciation_dictionary_manager.py."""
26
27 from __future__ import annotations
28
29 from typing import TYPE_CHECKING
30
31 import pytest
32
33 if TYPE_CHECKING:
34 from .orca_test_context import OrcaTestContext
35
36
37 @pytest.mark.unit
38 class TestPronunciationDictionaryManager:
39 """Test PronunciationDictionaryManager class."""
40
41 def _setup_manager(self, test_context: OrcaTestContext):
42 """Set up mocks for pronunciation_dictionary_manager dependencies."""
43
44 additional_modules = [
45 "orca.guilabels",
46 "orca.messages",
47 "orca.preferences_grid_base",
48 "orca.script_manager",
49 "orca.speech_manager",
50 ]
51 essential_modules = test_context.setup_shared_dependencies(additional_modules)
52
53 # Set up guilabels with required constants
54 guilabels = essential_modules["orca.guilabels"]
55 guilabels.PRONUNCIATION = "Pronunciation"
56 guilabels.PRONUNCIATION_DICTIONARY = "Pronunciation Dictionary"
57 guilabels.PRONUNCIATION_DICTIONARY_INFO = "Add custom pronunciations."
58 guilabels.DICTIONARY_NEW_ENTRY = "New Entry"
59 guilabels.DICTIONARY_DELETE = "Delete"
60 guilabels.DICTIONARY_EMPTY = "No entries"
61 guilabels.DICTIONARY_ACTUAL_STRING = "Actual String"
62 guilabels.DICTIONARY_REPLACEMENT_STRING = "Replacement String"
63 guilabels.ADD_NEW_PRONUNCIATION = "Add New Pronunciation"
64 guilabels.EDIT_PRONUNCIATION = "Edit Pronunciation"
65 guilabels.DIALOG_CANCEL = "Cancel"
66 guilabels.DIALOG_ADD = "Add"
67 guilabels.DIALOG_EDIT = "Edit"
68
69 # Set up messages
70 messages = essential_modules["orca.messages"]
71 messages.PRONUNCIATION_DELETED = "Pronunciation %s deleted"
72
73 # Set up speech_manager
74 speech_manager_mock = essential_modules["orca.speech_manager"]
75 speech_instance = speech_manager_mock.get_manager.return_value
76 speech_instance.get_use_pronunciation_dictionary.return_value = True
77 speech_instance.set_use_pronunciation_dictionary.return_value = True
78
79 # Import and return the module
80 from orca import pronunciation_dictionary_manager
81
82 return pronunciation_dictionary_manager, essential_modules
83
84 def test_get_manager_returns_singleton(self, test_context: OrcaTestContext) -> None:
85 """Test get_manager returns the same instance."""
86
87 module, _mocks = self._setup_manager(test_context)
88 manager1 = module.get_manager()
89 manager2 = module.get_manager()
90
91 assert manager1 is manager2
92
93 def test_manager_initial_state(self, test_context: OrcaTestContext) -> None:
94 """Test manager initializes with empty dictionary."""
95
96 module, _mocks = self._setup_manager(test_context)
97 manager = module.get_manager()
98
99 # Reset the dictionary to ensure clean state
100 manager.set_dictionary({})
101
102 assert manager.get_dictionary() == {}
103
104 def test_get_pronunciation_returns_word_when_not_found(
105 self,
106 test_context: OrcaTestContext,
107 ) -> None:
108 """Test get_pronunciation returns the original word when not in dictionary."""
109
110 module, _mocks = self._setup_manager(test_context)
111 manager = module.get_manager()
112 manager.set_dictionary({})
113
114 result = manager.get_pronunciation("hello")
115
116 assert result == "hello"
117
118 def test_get_pronunciation_returns_replacement_when_found(
119 self,
120 test_context: OrcaTestContext,
121 ) -> None:
122 """Test get_pronunciation returns replacement when word is in dictionary."""
123
124 module, _mocks = self._setup_manager(test_context)
125 manager = module.get_manager()
126 manager.set_dictionary({})
127
128 manager.set_pronunciation("hello", "hi there")
129 result = manager.get_pronunciation("hello")
130
131 assert result == "hi there"
132
133 def test_get_pronunciation_is_case_insensitive(self, test_context: OrcaTestContext) -> None:
134 """Test get_pronunciation lookup is case insensitive."""
135
136 module, _mocks = self._setup_manager(test_context)
137 manager = module.get_manager()
138 manager.set_dictionary({})
139
140 manager.set_pronunciation("HELLO", "hi there")
141
142 assert manager.get_pronunciation("hello") == "hi there"
143 assert manager.get_pronunciation("Hello") == "hi there"
144 assert manager.get_pronunciation("HELLO") == "hi there"
145
146 def test_set_pronunciation_stores_lowercase_key(self, test_context: OrcaTestContext) -> None:
147 """Test set_pronunciation stores key as lowercase."""
148
149 module, _mocks = self._setup_manager(test_context)
150 manager = module.get_manager()
151 manager.set_dictionary({})
152
153 manager.set_pronunciation("MixedCase", "replacement")
154
155 dictionary = manager.get_dictionary()
156 assert "mixedcase" in dictionary
157 assert "MixedCase" not in dictionary
158
159 def test_set_dictionary_replaces_entire_dictionary(self, test_context: OrcaTestContext) -> None:
160 """Test set_dictionary replaces the entire dictionary."""
161
162 module, _mocks = self._setup_manager(test_context)
163 manager = module.get_manager()
164
165 manager.set_pronunciation("old", "old replacement")
166 manager.set_dictionary({"new": "new replacement"})
167
168 dictionary = manager.get_dictionary()
169 assert "old" not in dictionary
170 assert dictionary.get("new") == "new replacement"
171
172 def test_multiple_pronunciations(self, test_context: OrcaTestContext) -> None:
173 """Test manager handles multiple pronunciations correctly."""
174
175 module, _mocks = self._setup_manager(test_context)
176 manager = module.get_manager()
177 manager.set_dictionary({})
178
179 manager.set_pronunciation("word1", "replacement1")
180 manager.set_pronunciation("word2", "replacement2")
181 manager.set_pronunciation("word3", "replacement3")
182
183 assert manager.get_pronunciation("word1") == "replacement1"
184 assert manager.get_pronunciation("word2") == "replacement2"
185 assert manager.get_pronunciation("word3") == "replacement3"
186 assert len(manager.get_dictionary()) == 3
187
188 def test_set_pronunciation_overwrites_existing(self, test_context: OrcaTestContext) -> None:
189 """Test set_pronunciation overwrites existing entry for same word."""
190
191 module, _mocks = self._setup_manager(test_context)
192 manager = module.get_manager()
193 manager.set_dictionary({})
194
195 manager.set_pronunciation("word", "first replacement")
196 manager.set_pronunciation("word", "second replacement")
197
198 assert manager.get_pronunciation("word") == "second replacement"
199 assert len(manager.get_dictionary()) == 1
200
201
202 @pytest.mark.unit
203 class TestPronunciationDictionaryManagerIntegration:
204 """Integration tests for pronunciation dictionary manager."""
205
206 def _setup_manager(self, test_context: OrcaTestContext):
207 """Set up mocks for pronunciation_dictionary_manager dependencies."""
208
209 additional_modules = [
210 "orca.guilabels",
211 "orca.messages",
212 "orca.preferences_grid_base",
213 "orca.script_manager",
214 "orca.speech_manager",
215 ]
216 essential_modules = test_context.setup_shared_dependencies(additional_modules)
217
218 # Set up guilabels with required constants
219 guilabels = essential_modules["orca.guilabels"]
220 guilabels.PRONUNCIATION = "Pronunciation"
221 guilabels.PRONUNCIATION_DICTIONARY = "Pronunciation Dictionary"
222 guilabels.PRONUNCIATION_DICTIONARY_INFO = "Add custom pronunciations."
223 guilabels.DICTIONARY_NEW_ENTRY = "New Entry"
224 guilabels.DICTIONARY_DELETE = "Delete"
225 guilabels.DICTIONARY_EMPTY = "No entries"
226 guilabels.DICTIONARY_ACTUAL_STRING = "Actual String"
227 guilabels.DICTIONARY_REPLACEMENT_STRING = "Replacement String"
228 guilabels.ADD_NEW_PRONUNCIATION = "Add New Pronunciation"
229 guilabels.EDIT_PRONUNCIATION = "Edit Pronunciation"
230 guilabels.DIALOG_CANCEL = "Cancel"
231 guilabels.DIALOG_ADD = "Add"
232 guilabels.DIALOG_EDIT = "Edit"
233
234 # Set up messages
235 messages = essential_modules["orca.messages"]
236 messages.PRONUNCIATION_DELETED = "Pronunciation %s deleted"
237
238 # Set up speech_manager
239 speech_manager_mock = essential_modules["orca.speech_manager"]
240 speech_instance = speech_manager_mock.get_manager.return_value
241 speech_instance.get_use_pronunciation_dictionary.return_value = True
242 speech_instance.set_use_pronunciation_dictionary.return_value = True
243
244 # Import and return the module
245 from orca import pronunciation_dictionary_manager
246
247 return pronunciation_dictionary_manager, essential_modules
248
249 def test_pronunciation_application_to_text(self, test_context: OrcaTestContext) -> None:
250 """Test that pronunciation dictionary can be used to process text."""
251
252 module, _mocks = self._setup_manager(test_context)
253 manager = module.get_manager()
254 manager.set_dictionary({})
255
256 manager.set_pronunciation("github", "git hub")
257 manager.set_pronunciation("cli", "command line interface")
258
259 # Simulate applying pronunciations to a sentence
260 words = ["Check", " ", "out", " ", "the", " ", "GitHub", " ", "CLI"]
261 result = "".join(map(manager.get_pronunciation, words))
262
263 # GitHub becomes "git hub" because lookup is case insensitive
264 assert "git hub" in result
265 # CLI becomes "command line interface"
266 assert "command line interface" in result
267
268 def test_empty_dictionary_returns_original_words(self, test_context: OrcaTestContext) -> None:
269 """Test that empty dictionary returns all original words unchanged."""
270
271 module, _mocks = self._setup_manager(test_context)
272 manager = module.get_manager()
273 manager.set_dictionary({})
274
275 words = ["Hello", " ", "world", "!"]
276 result = "".join(map(manager.get_pronunciation, words))
277
278 assert result == "Hello world!"