def setup(self): config = { "input-method": "telex", "output-charset": "utf-8", "skip-non-vietnamese": True, "auto-capitalize-abbreviations": False, "input-method-definition": { "a": "a^", "o": "o^", "e": "e^", "w": ["u*", "o*", "a+", "<ư"], "d": "d-", "f": "\\", "s": "/", "r": "?", "x": "~", "j": ".", "]": "<ư", "[": "<ơ", "}": "<Ư", "{": "<Ơ" }, } self.eng = BaseBackend( config=config, abbr_expander=AbbreviationExpander(config), auto_corrector=None)
def setup(self): self.expander = Mock() self.corrector = Mock() self.config = { "enable-text-expansion": False, "skip-non-vietnamese": True } self.backend = BaseBackend(config=self.config, abbr_expander=self.expander, auto_corrector=self.corrector)
def setup(self): self.expander = Mock() self.corrector = Mock() self.config = { "enable-text-expansion": False, "skip-non-vietnamese": True } self.backend = BaseBackend( config=self.config, abbr_expander=self.expander, auto_corrector=self.corrector)
class TestBaseBackend(): def setup(self): self.expander = Mock() self.corrector = Mock() self.config = { "enable-text-expansion": False, "skip-non-vietnamese": True } self.backend = BaseBackend( config=self.config, abbr_expander=self.expander, auto_corrector=self.corrector) def test_update_composition(self): """ update_composition() should create a new history entry. """ string = "blah" self.backend.update_composition(string) expected = { "type": "update-composition", "raw-string": "", "editing-string": string } eq_(self.backend.last_action(), expected) def test_commit_composition(self): """ commit_composition() should create a new history entry based on the last editing_string. """ string = "blah" self.backend.commit_composition(string) expected = { "type": "commit-composition", "raw-string": "", "editing-string": string } eq_(self.backend.last_action(), expected) def test_text_expansion(self): """ It should expand text if possible and allowed on space key press. FIXME: Just space key? """ string = "bl" expanded = "blah" self.expander.expand = Mock(return_value=expanded) self.config["enable-text-expansion"] = True self.backend.update_composition = Mock() self.backend.editing_string = string self.backend.on_space_pressed() expected = { "type": "string-expansion", "raw-string": "", "editing-string": expanded } eq_(self.backend.last_action(), expected) self.backend.update_composition.assert_called_once_with(expanded) def test_reset(self): """ reset() should create a history entry. """ self.backend.reset() expected = { "type": "reset", "raw-string": "", "editing-string": "" } eq_(self.backend.last_action(), expected) def test_correction(self): """ Should auto-correct. """ string = "casl" corrected = "cas" self.expander.expand = Mock(return_value=string) self.corrector.suggest = Mock(return_value=corrected) self.backend.history.append({ "type": "update-composition", "raw-string": string, "editing-string": string }) self.backend.on_space_pressed() last_action = self.backend.last_action() eq_(last_action["type"], "string-correction") eq_(last_action["editing-string"], corrected + ' ') def test_backspace_undo_correction(self): """ Pressing backspace immediately after a correction should undo it. """ # First the original, non-Vietnamese is shown self.backend.history.append({ "type": "update-composition", "raw-string": "casl", "editing-string": "casl" }) # Then it got corrected, says to "cá " self.backend.history.append({ "type": "update-composition", "raw-string": "casltheotunheou", "editing-string": "cá " }) self.backend.history.append({ "type": "string-correction", "raw-string": "casltheotunheou", # gibberish "editing-string": "cá " }) self.backend.commit_composition = Mock() # Then we press backspace self.backend.on_backspace_pressed() last_action = self.backend.last_action() eq_(last_action["type"], "undo") eq_(last_action["editing-string"], "casl") self.backend.commit_composition.assert_called_once_with("casl") def test_non_vietnamese(self): """ It should keep non-Vietnamese strings intact. """ string = "casl" self.corrector.suggest = Mock(return_value=string) self.backend.history.append({ "type": "update-composition", "raw-string": string, "editing-string": string }) self.backend.on_space_pressed() last_action = self.backend.last_action() eq_(last_action["type"], "update-composition") eq_(last_action["editing-string"], string)
class TestBaseBackend(): def setup(self): self.expander = Mock() self.corrector = Mock() self.config = { "enable-text-expansion": False, "skip-non-vietnamese": True } self.backend = BaseBackend(config=self.config, abbr_expander=self.expander, auto_corrector=self.corrector) def test_update_composition(self): """ update_composition() should create a new history entry. """ string = "blah" self.backend.update_composition(string) expected = { "type": "update-composition", "raw-string": "", "editing-string": string } eq_(self.backend.last_action(), expected) def test_commit_composition(self): """ commit_composition() should create a new history entry based on the last editing_string. """ string = "blah" self.backend.commit_composition(string) expected = { "type": "commit-composition", "raw-string": "", "editing-string": string } eq_(self.backend.last_action(), expected) def test_text_expansion(self): """ It should expand text if possible and allowed on space key press. FIXME: Just space key? """ string = "bl" expanded = "blah" self.expander.expand = Mock(return_value=expanded) self.config["enable-text-expansion"] = True self.backend.update_composition = Mock() self.backend.editing_string = string self.backend.on_space_pressed() expected = { "type": "string-expansion", "raw-string": "", "editing-string": expanded } eq_(self.backend.last_action(), expected) self.backend.update_composition.assert_called_once_with(expanded) def test_reset(self): """ reset() should create a history entry. """ self.backend.reset() expected = {"type": "reset", "raw-string": "", "editing-string": ""} eq_(self.backend.last_action(), expected) def test_correction(self): """ Should auto-correct. """ string = "casl" corrected = "cas" self.expander.expand = Mock(return_value=string) self.corrector.suggest = Mock(return_value=corrected) self.backend.history.append({ "type": "update-composition", "raw-string": string, "editing-string": string }) self.backend.on_space_pressed() last_action = self.backend.last_action() eq_(last_action["type"], "string-correction") eq_(last_action["editing-string"], corrected + ' ') def test_backspace_undo_correction(self): """ Pressing backspace immediately after a correction should undo it. """ # First the original, non-Vietnamese is shown self.backend.history.append({ "type": "update-composition", "raw-string": "casl", "editing-string": "casl" }) # Then it got corrected, says to "cá " self.backend.history.append({ "type": "update-composition", "raw-string": "casltheotunheou", "editing-string": "cá " }) self.backend.history.append({ "type": "string-correction", "raw-string": "casltheotunheou", # gibberish "editing-string": "cá " }) self.backend.commit_composition = Mock() # Then we press backspace self.backend.on_backspace_pressed() last_action = self.backend.last_action() eq_(last_action["type"], "undo") eq_(last_action["editing-string"], "casl") self.backend.commit_composition.assert_called_once_with("casl") def test_non_vietnamese(self): """ It should keep non-Vietnamese strings intact. """ string = "casl" self.corrector.suggest = Mock(return_value=string) self.backend.history.append({ "type": "update-composition", "raw-string": string, "editing-string": string }) self.backend.on_space_pressed() last_action = self.backend.last_action() eq_(last_action["type"], "update-composition") eq_(last_action["editing-string"], string)
class TestEngine(): def setup(self): config = { "input-method": "telex", "output-charset": "utf-8", "skip-non-vietnamese": True, "auto-capitalize-abbreviations": False, "input-method-definition": { "a": "a^", "o": "o^", "e": "e^", "w": ["u*", "o*", "a+", "<ư"], "d": "d-", "f": "\\", "s": "/", "r": "?", "x": "~", "j": ".", "]": "<ư", "[": "<ơ", "}": "<Ư", "{": "<Ơ" }, } self.eng = BaseBackend( config=config, abbr_expander=AbbreviationExpander(config), auto_corrector=None) def send_keys(self, input, engine): [self.send_key(character, engine) for character in input] return self def send_key(self, input, engine): engine.process_key_event(ord(input), 0) def send_bksp(self, engine): engine.on_backspace_pressed() return self def send_space(self, engine): engine.on_space_pressed() return self def test_1_bug_117(self): """ baa + bksp => {new_string: b, raw_string: b} """ self.send_keys("baa", self.eng).send_bksp(self.eng) eq_(self.eng.last_action()["editing-string"], 'b') eq_(self.eng.last_action()["raw-string"], 'b') def test_2_bug_117(self): """ bana + bksp => {new_string: bâ, raw_string: baa} """ self.send_keys("bana", self.eng).send_bksp(self.eng) eq_(self.eng.last_action()["editing-string"], 'bâ') eq_(self.eng.last_action()["raw-string"], 'baa') def test_3_bug_117(self): """ ba + bksp + a => {new_string: ba, raw_string: ba} """ self.send_keys("ba", self.eng) \ .send_bksp(self.eng).send_keys("a", self.eng) eq_(self.eng.last_action()["editing-string"], 'ba') eq_(self.eng.last_action()["raw-string"], 'ba') def test_4_bug_117(self): """ thuow + bksp => {new_string: thu, raw_string: thu} """ self.send_keys("thuow", self.eng).send_bksp(self.eng) eq_(self.eng.last_action()["editing-string"], 'thu') eq_(self.eng.last_action()["raw-string"], 'thu') def test_bug_123(self): """ Should not raise IndexError when backspace is sent repeatedly """ self.send_bksp(self.eng) self.send_bksp(self.eng) self.send_bksp(self.eng) self.send_bksp(self.eng)