forked from autokey/autokey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_service.py
More file actions
90 lines (74 loc) · 3.17 KB
/
Copy pathtest_service.py
File metadata and controls
90 lines (74 loc) · 3.17 KB
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
# Copyright (C) 2021 BlueDrink9
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import inspect
import json
import typing
import sys
import os
from unittest.mock import MagicMock, patch
import pytest
from hamcrest import *
from tests.engine_helpers import *
import tests.helpers_for_tests as testhelpers
import autokey.model.folder
import autokey.model.helpers
import autokey.model.script
from autokey.configmanager import configmanager
from autokey.configmanager.configmanager import ConfigManager
from autokey.service import Service
from autokey.service import PhraseRunner
from autokey.service import ScriptRunner
import autokey.service
from autokey.scripting import Engine
confman_module_path = "autokey.configmanager.configmanager"
# These tests currently use the scripting API to create test phrases.
# If we can do it a better way, we probably should, to reduce dependencies for
# these tests.
def mock_configmanager(tmp_path):
backup = str(tmp_path / "autokey.json~")
config = str(testhelpers.make_dummy_config(tmp_path))
with \
patch(confman_module_path + ".CONFIG_DEFAULT_FOLDER",
str(tmp_path)), \
patch(confman_module_path + ".CONFIG_FILE",
config), \
patch(confman_module_path + ".CONFIG_FILE_BACKUP",
backup):
return ConfigManager(MagicMock())
def test_start(tmp_path):
cm = mock_configmanager(tmp_path)
app = MagicMock()
app.configManager = cm
s = Service(app)
s.start()
s.shutdown()
def create_script(abbreviation, trigger_immediately=False, ignore_case=False):
script = autokey.model.script.Script("script", "")
script.add_abbreviation(abbreviation)
script.set_modes([autokey.model.helpers.TriggerMode.ABBREVIATION])
script.immediate = trigger_immediately
script.ignoreCase = ignore_case
script.parent = MagicMock()
return script
@pytest.mark.parametrize("buffer, abbreviation, immediate, ignore_case, expected", [
("abbr ", "abbr", False, False, ("abbr", " ")),
("prefix abbr ", "abbr", False, False, ("abbr", " ")),
("abbr", "abbr", True, False, ("abbr", "")),
("ABBR", "abbr", True, True, ("ABBR", "")),
])
def test_set_triggered_abbreviation_uses_typed_abbreviation(
buffer, abbreviation, immediate, ignore_case, expected):
script = create_script(abbreviation, immediate, ignore_case)
_, trigger_character = script.process_buffer(buffer)
engine = MagicMock()
ScriptRunner._set_triggered_abbreviation({"engine": engine}, script, buffer, trigger_character)
engine._set_triggered_abbreviation.assert_called_once_with(*expected)