File: test_macro.py

package info (click to toggle)
autokey 0.96.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,468 kB
  • sloc: python: 11,473; xml: 3,260; javascript: 249; sh: 24; makefile: 5
file content (244 lines) | stat: -rw-r--r-- 10,744 bytes parent folder | download
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
# Copyright (C) 2019 Thomas Hess <thomas.hess@udo.edu>

# 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 typing
import pathlib
import os
import sys
from datetime import date

from unittest.mock import MagicMock, patch
import unittest.mock

import pytest
from hamcrest import *

import autokey.model.folder
import autokey.service
from autokey.service import PhraseRunner
from autokey.configmanager.configmanager import ConfigManager
from autokey.scripting import Engine

from autokey.macro import *


def get_autokey_dir():
    return os.path.join(os.path.dirname(os.path.realpath(__file__)),
    "..")


path =  get_autokey_dir() + "/tests/dummy_file.txt"


def create_engine() -> typing.Tuple[Engine, autokey.model.folder.Folder]:
    # Make sure to not write to the hard disk
    test_folder = autokey.model.folder.Folder("Test folder")
    test_folder.persist = MagicMock()

    # Mock load_global_config to add the test folder to the known folders. This causes the ConfigManager to skip it’s
    # first-run logic.
    with patch("autokey.model.phrase.Phrase.persist"), patch("autokey.model.folder.Folder.persist"),\
         patch("autokey.configmanager.configmanager.ConfigManager.load_global_config",
               new=(lambda self: self.folders.append(test_folder))):
        engine = Engine(ConfigManager(MagicMock()), MagicMock(spec=PhraseRunner))
    # service = autokey.service.Service(engine.configManager.app)
        engine.configManager.config_altered(False)

    return engine, test_folder


# For mocking dates. Usage:
    # @mock.patch('datetime.datetime', FakeDate)
    # def test():
    #     from datetime import datetime
    #     FakeDate.now = classmethod(lambda cls: now(2010, 1, 1))
    #     return date.now()
class FakeDate(date):
    """A manipulable date replacement"""
    def __new__(cls, *args, **kwargs):
        return date.__new__(date, *args, **kwargs)


def expandMacro(engine, phrase):
    manager = MacroManager(engine)
    return manager.process_expansion_macros(phrase)


@pytest.mark.parametrize("test_input, expected, error_msg", [
    ("name='test name' args='long arg with spaces and ='", {"name": "test name", "args": "long arg with spaces and ="}, "Macro arg can't contain equals"),
    ("name='test name' args='long arg with spaces and \"'",
            {"name": "test name", "args": "long arg with spaces and \""},
                "Macro arg can't contain opposite quote"),
    ('name="test name" args="long arg with spaces and \\""',
            {"name": "test name", "args": 'long arg with spaces and "'},
                "Macro arg can't contain escaped quote quote"),
    ('name="test name" args="long arg with spaces and >"',
            {"name": "test name", "args": 'long arg with spaces and >'},
                "Macro arg can't contain > when handleg just by get_args"),
])
def test_arg_parse(test_input, expected, error_msg):
    engine, folder = create_engine()
    macro = ScriptMacro(engine)
    assert_that(macro._get_args(test_input), is_(equal_to(expected)),
                error_msg)


# Using date for this because it's the easiest macro with args to expand,
# without throwing errors on bad args.
@unittest.mock.patch('datetime.datetime', FakeDate)
@pytest.mark.parametrize("test, expected, error_msg", [
    (r"<date format=%m\>%y>", "01>19",
     r"Macro arg can't handle '\>'"),
    (r"<date format=%m\<%y>", "01<19",
     r"Macro arg can't handle '\<'"),
    (r"<date format=\<%m%y\>>", "<0119>",
     r"Macro arg can't handle being enclosed in angle brackets '\<arg\>'"),
    (r"before <date format=\<%m%y\>> macro",
     "before <0119> macro",
     "Macro arg in angle brackets breaks overall phrase splitting"),
    (r"before <date format=\<%m%y\>> between <date format=\<%m%y\>> macro",
     "before <0119> between <0119> macro",
     "Macro arg in angle brackets breaks overall phrase splitting with two macros"),
])
def test_arg_parse_with_escaped_gt_lt_symbols(test, expected, error_msg):
    from datetime import datetime
    FakeDate.now = classmethod(lambda cls: datetime(2019, 1, 1))
    engine, folder = create_engine()
    assert_that(expandMacro(engine, test), is_(equal_to(expected)),
                error_msg)


@unittest.mock.patch('datetime.datetime', FakeDate)
@pytest.mark.parametrize("test, expected, error_msg", [
    ("Today < is <date format=%m/%y>", "Today < is 01/19", "Phrase with extra < before macro breaks macros"),
    ("Today > is <date format=%m/%y>", "Today > is 01/19", "Phrase with extra > before macro breaks macros"),
    ("Today is <date format=%m/%y>, horray<", "Today is 01/19, horray<", "Phrase with extra < after macro breaks macros"),
    ("Today is <date format=%m/%y>, horray>", "Today is 01/19, horray>", "Phrase with extra > after macro breaks macros"),
    ("Today is <<date format=%m/%y>", "Today is <01/19", "Phrase with extra < right before macro breaks macros"),
    ("Today is <date format=%m/%y><", "Today is 01/19<", "Phrase with extra < right after macro breaks macros"),
    ("Today is <date format=%m/%y>>", "Today is 01/19>", "Phrase with extra > right after macro breaks macros"),
    ("Today <> is <date format=%m/%y>", "Today <> is 01/19", "Phrase with extra <> before macro breaks macros"),
    ("Today <is <date format=%m/%y>,>", "Today <is 01/19,>", "Phrase with extra <> loosely around macro breaks macros"),
    ("Today is <<date format=%m/%y>>", "Today is <01/19>", "Phrase with extra <> right around macro breaks macros"),
])
def test_phrase_with_gt_lt_symbols_and_macro(test, expected, error_msg):
    from datetime import datetime
    FakeDate.now = classmethod(lambda cls: datetime(2019, 1, 1))
    engine, folder = create_engine()
    assert_that(expandMacro(engine, test), is_(equal_to(expected)),
                error_msg)


@pytest.mark.skip(reason="For this to work, engine needs to be initialised with a PhraseRunner that isn't a mock. Sadly, that requires an app that isn't a mock.")
def test_script_macro():
    # Makes use of running script from absolute path.
    engine, folder = create_engine()
    with patch("autokey.model.phrase.Phrase.persist"), patch("autokey.model.folder.Folder.persist"):
        dummy_folder = autokey.model.folder.Folder("dummy")
        # This is a duplicate of the phrase added by the target script, but in a
        # different folder.
        dummy = engine.create_phrase(dummy_folder, "arg 1", "arg2", temporary=True)
        assert_that(folder.items, not_(has_item(dummy)))

        script = get_autokey_dir() + "/tests/create_single_phrase.py"
        test = "<script name='{}' args='arg 1',arg2>".format(script)

        expandMacro(engine, test)
        assert_that(folder.items, has_item(dummy))


def test_script_macro_spaced_quoted_args():
    pass


def test_cursor_macro():
    engine, folder = create_engine()
    test="one<cursor>two"
    expected="onetwo<left><left><left>"
    assert_that(expandMacro(engine, test), is_(equal_to(expected)),
                "cursor macro returns wrong text")


@pytest.mark.parametrize("test_input, expected, error_msg", [
    ("<cursor><file name={}> types".format(path),
     "test result macro expansion\n types" + "<left>"*(28 + 6),
     "Cursor macro before another macro doesn't expand properly"),
    ("<cursor><file name={}> types".format(path),
     "test result macro expansion\n types" + "<left>"*(28 + 6),
     "Cursor macro before another macro doesn't expand properly"),
    ("<cursor><file name={}><file name={}> types".format(path, path),
     "test result macro expansion\ntest result macro expansion\n types" + "<left>"*(28 + 28 + 6),
     "Cursor macro before another 2 macros doesn't expand properly"),
    ("<file name={}><cursor><file name={}> types".format(path, path),
     "test result macro expansion\ntest result macro expansion\n types" + "<left>"*(28 + 6),
     "Cursor macro between another 2 macros doesn't expand properly"),
])
def test_cursor_before_another_macro(test_input, expected, error_msg):
    engine, folder = create_engine()
    assert_that(expandMacro(engine, test_input), is_(equal_to(expected)),
                error_msg)


@unittest.mock.patch('datetime.datetime', FakeDate)
def test_date_macro():
    from datetime import datetime
    FakeDate.now = classmethod(lambda cls: datetime(2019, 1, 1))

    engine, folder = create_engine()
    test="<date format=%d/%m/%y>"
    expected="01/01/19"
    assert_that(expandMacro(engine, test), is_(equal_to(expected)),
                "Date macro fails to expand")


def test_file_macro():
    engine, folder = create_engine()
    path =  get_autokey_dir() + "/tests/dummy_file.txt"
    test="<file name={}>".format(path)
    expected="test result macro expansion\n"
    assert_that(expandMacro(engine, test), is_(equal_to(expected)),
                "file macro does not expand correctly")


@pytest.mark.parametrize("test_input, expected, error_msg", [
    ("No macro", "No macro",
            "Error on phrase without macros"),
    ("middle <file name={}> macro".format(path),
            "middle test result macro expansion\n macro",
            "Macros between other parts don't expand properly"),
    ("<file name={}> two macros this time <file name={}>".format(path, path),
            "test result macro expansion\n two macros this time test result macro expansion\n".format(path, path),
            "Two macros per phrase don't expand properly"),
    ("<file name={}> mixed macro <cursor> types".format(path),
        "test result macro expansion\n mixed macro  types<left><left><left><left><left><left>",
        "mixed macros don't expand properly")
])
def test_macro_expansion(test_input, expected, error_msg):
    engine, folder = create_engine()
    assert_that(expandMacro(engine, test_input), is_(equal_to(expected)), error_msg)


def test_system_macro():
    engine, folder = create_engine()
    lang=os.environ['LANG']
    test="one<system command='echo $LANG'>two"
    expected="one{}two".format(lang)
    assert_that(expandMacro(engine, test), is_(equal_to(expected)),
                "system macro fails")


# def test_nested_macro_raises_error():
#     contents="<date format=<cursor>>"
#     # TODO