cloning mappings

This commit is contained in:
sezanzeb 2020-11-28 15:48:57 +01:00 committed by sezanzeb
parent ed48306f7f
commit 1a16d679cf
2 changed files with 17 additions and 3 deletions

View File

@ -160,12 +160,11 @@ class Mapping:
def clone(self):
"""Create a copy of the mapping."""
# TODO test
mapping = Mapping()
mapping._mapping = copy.deepcopy(self._mapping)
mapping.update_reverse_mapping()
mapping.changed = self.changed
return Mapping()
return mapping
def save(self, device, preset):
"""Dump as JSON into home."""

View File

@ -23,7 +23,6 @@ import unittest
from keymapper.mapping import Mapping
from keymapper.state import parse_xmodmap
from keymapper.paths import get_config_path
class TestMapping(unittest.TestCase):
@ -38,6 +37,22 @@ class TestMapping(unittest.TestCase):
self.assertEqual(self.mapping.get_keycode('1'), 10)
self.assertTrue(self.mapping.get_character(10).startswith('1'))
def test_clone(self):
mapping1 = Mapping()
mapping1.change(1, 'a')
mapping2 = mapping1.clone()
mapping1.change(2, 'b')
self.assertEqual(mapping1.get_character(1), 'a')
self.assertEqual(mapping1.get_keycode('a'), 1)
self.assertEqual(mapping1.get_character(2), 'b')
self.assertEqual(mapping1.get_keycode('b'), 2)
self.assertEqual(mapping2.get_character(1), 'a')
self.assertEqual(mapping2.get_keycode('a'), 1)
self.assertIsNone(mapping2.get_character(2))
self.assertIsNone(mapping2.get_keycode('b'))
def test_save_load(self):
self.mapping.change(10, '1')
self.mapping.change(11, '2')