From 960097096f2781a8c1b7afce219972182670e060 Mon Sep 17 00:00:00 2001 From: John Wiggins Date: Fri, 26 Feb 2021 11:59:41 +0100 Subject: [PATCH] Fix KeySpec.from_string (#638) --- enable/base_tool.py | 2 +- enable/tests/test_key_spec.py | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 enable/tests/test_key_spec.py diff --git a/enable/base_tool.py b/enable/base_tool.py index 088ad2045..82f6be7a0 100644 --- a/enable/base_tool.py +++ b/enable/base_tool.py @@ -73,7 +73,7 @@ def from_string(cls, s): codes = s.split("+") key = codes[-1] modifiers = set(code.lower() for code in codes[:-1]) - ignore = set("alt", "shift", "control") - modifiers + ignore = {"alt", "shift", "control"} - modifiers return cls(key, *modifiers, ignore=ignore) diff --git a/enable/tests/test_key_spec.py b/enable/tests/test_key_spec.py new file mode 100644 index 000000000..61be2b51f --- /dev/null +++ b/enable/tests/test_key_spec.py @@ -0,0 +1,41 @@ +# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! +import collections +import unittest + +from enable.api import KeySpec + +Event = collections.namedtuple( + "Event", "character,alt_down,control_down,shift_down" +) + + +class TestKeySpec(unittest.TestCase): + def test_basics(self): + spec = KeySpec("Right", "control", ignore=["shift"]) + + self.assertEqual(spec.key, "Right") + self.assertTrue(spec.control) + self.assertSetEqual(spec.ignore, {"shift"}) + self.assertFalse(spec.alt) + self.assertFalse(spec.shift) + + event = Event("k", False, True, False) + self.assertFalse(spec.match(event)) + + event = Event("Right", False, True, False) + self.assertTrue(spec.match(event)) + + def test_from_string(self): + spec = KeySpec.from_string("Shift+Control+z") + self.assertSetEqual(spec.ignore, {"alt"}) + + event = Event("z", False, True, True) + self.assertTrue(spec.match(event))