|
|
|
@ -19,25 +19,29 @@
|
|
|
|
|
# along with key-mapper. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
import unittest
|
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
|
|
from keymapper.dev.macros import parse
|
|
|
|
|
from keymapper.dev.macros import parse, _Macro
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMacros(unittest.TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.result = []
|
|
|
|
|
self.handler = lambda char, value: self.result.append((char, value))
|
|
|
|
|
self.loop = asyncio.get_event_loop()
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
self.result = []
|
|
|
|
|
|
|
|
|
|
def test_0(self):
|
|
|
|
|
parse('k(1)', self.handler).run()
|
|
|
|
|
self.loop.run_until_complete(parse('k(1)', self.handler).run())
|
|
|
|
|
self.assertListEqual(self.result, [(1, 1), (1, 0)])
|
|
|
|
|
|
|
|
|
|
def test_1(self):
|
|
|
|
|
parse('k(1 2).k(a).k(3)', self.handler).run()
|
|
|
|
|
macro = 'k(1 2).k(a).k(3)'
|
|
|
|
|
self.loop.run_until_complete(parse(macro, self.handler).run())
|
|
|
|
|
self.assertListEqual(self.result, [
|
|
|
|
|
('1 2', 1), ('1 2', 0),
|
|
|
|
|
('a', 1), ('a', 0),
|
|
|
|
@ -45,14 +49,20 @@ class TestMacros(unittest.TestCase):
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
def test_2(self):
|
|
|
|
|
parse('r(1, k(k))', self.handler).run()
|
|
|
|
|
start = time.time()
|
|
|
|
|
macro = 'r(1, k(k))'
|
|
|
|
|
self.loop.run_until_complete(parse(macro, self.handler).run())
|
|
|
|
|
self.assertLess(time.time() - start, 0.1)
|
|
|
|
|
self.assertListEqual(self.result, [
|
|
|
|
|
('k', 1), ('k', 0),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
def test_3(self):
|
|
|
|
|
parse('r(3, k(m).w(200, 400))', self.handler).run()
|
|
|
|
|
# TODO test passed time
|
|
|
|
|
start = time.time()
|
|
|
|
|
macro = 'r(3, k(m).w(100, 200))'
|
|
|
|
|
self.loop.run_until_complete(parse(macro, self.handler).run())
|
|
|
|
|
self.assertGreater(time.time() - start, 0.1 * 3)
|
|
|
|
|
self.assertLess(time.time() - start, 0.21 * 3)
|
|
|
|
|
self.assertListEqual(self.result, [
|
|
|
|
|
('m', 1), ('m', 0),
|
|
|
|
|
('m', 1), ('m', 0),
|
|
|
|
@ -60,7 +70,8 @@ class TestMacros(unittest.TestCase):
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
def test_4(self):
|
|
|
|
|
parse(' r(2,\nk(\rr ).k(-\n )).k(m) ', self.handler).run()
|
|
|
|
|
macro = ' r(2,\nk(\rr ).k(-\n )).k(m) '
|
|
|
|
|
self.loop.run_until_complete(parse(macro, self.handler).run())
|
|
|
|
|
self.assertListEqual(self.result, [
|
|
|
|
|
('r', 1), ('r', 0),
|
|
|
|
|
('-', 1), ('-', 0),
|
|
|
|
@ -70,19 +81,22 @@ class TestMacros(unittest.TestCase):
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
def test_5(self):
|
|
|
|
|
parse('w(400).r(2,m(w,\rr(2,\tk(r))).w(10).k(k))', self.handler).run()
|
|
|
|
|
start = time.time()
|
|
|
|
|
macro = 'w(200).r(2,m(w,\rr(2,\tk(r))).w(10).k(k))'
|
|
|
|
|
self.loop.run_until_complete(parse(macro, self.handler).run())
|
|
|
|
|
self.assertLess(time.time() - start, 0.23)
|
|
|
|
|
self.assertGreater(time.time() - start, 0.21)
|
|
|
|
|
expected = [('w', 1)]
|
|
|
|
|
expected += [('r', 1), ('r', 0)] * 2
|
|
|
|
|
expected += [('w', 0)]
|
|
|
|
|
expected += [('k', 1), ('k', 0)]
|
|
|
|
|
expected *= 2
|
|
|
|
|
# TODO test passed time
|
|
|
|
|
# TODO test asyncio
|
|
|
|
|
self.assertListEqual(self.result, expected)
|
|
|
|
|
|
|
|
|
|
def test_6(self):
|
|
|
|
|
# prints nothing without .run
|
|
|
|
|
parse('k(a).r(3, k(b))', self.handler)
|
|
|
|
|
# does nothing without .run
|
|
|
|
|
ret = parse('k(a).r(3, k(b))', self.handler)
|
|
|
|
|
self.assertIsInstance(ret, _Macro)
|
|
|
|
|
self.assertListEqual(self.result, [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|