def test_dictation(self): # Test dictation separately for SAPI5 because test_dictation.py # won't work with it. from dragonfly import Dictation, Literal, Sequence from dragonfly.test import ElementTester, RecognitionFailure seq = Sequence([Literal("hello"), Dictation("text")]) tester = ElementTester(seq) # Test one word. results = tester.recognize("hello world") assert results[0] == "hello" # Verify recognition returned dictation result. dictation = results[1] if not isinstance(dictation, DictationContainerBase): message = (u"Expected recognition result to be a dictation" u" container, but received %r" % (repr(dictation).decode("windows-1252"),)) self.fail(message.encode("windows-1252")) # Verifying dictation converts/encode successfully. self.assertEqual(str(dictation), "world") self.assertEqual(text_type(dictation), "world") self.assertTrue(isinstance(repr(dictation), string_types)) # Test incomplete. results = tester.recognize("hello") assert results is RecognitionFailure
def test_vim_grammar_switcher_mark_switches_to(engine): @VimGrammarSwitcher.mark_switches_to_mode(VimMode.NORMAL) class Rule1(MappingRule): mapping = {'foo': Key('f')} tester = ElementTester(RuleRef(Rule1()), engine) value = tester.recognize('foo') assert isinstance(value, MarkedAction) assert value.mark is VimMode.NORMAL
def test_repeat_action_rule(engine, typed_keys): class Rule1(MappingRule): mapping = {'foo': Key('a')} element = RuleRef(RepeatActionRule(RuleRef(Rule1()))) tester = ElementTester(element, engine) assert_same_typed_keys(typed_keys, tester.recognize('foo'), Key('a')) assert_same_typed_keys(typed_keys, tester.recognize('foo two times'), Key('a,a')) assert tester.recognize('blah') is RecognitionFailure
def test_literal(self): """ Verify that the text engine is usable. """ self.engine.speak("testing text") tester = ElementTester(Literal("hello world")) results = tester.recognize("hello world") assert results == "hello world" # Check that recognition failure is possible. results = tester.recognize("goodbye") assert results is RecognitionFailure
def test_get_engine_natlink_is_usable(self): """ Verify that the natlink engine is usable. """ engine = get_engine("natlink") assert isinstance(engine, EngineBase) assert engine.name == "natlink" engine.speak("testing natlink") from dragonfly import Literal from dragonfly.test import ElementTester tester = ElementTester(Literal("hello world")) results = tester.recognize("hello world") assert results == "hello world"
def test_get_engine_sapi5_is_usable(self): """ Verify that the sapi5 engine is usable. """ engine = get_engine() self.assert_(isinstance(engine, EngineBase)) self.assertEqual("sapi5", engine.name) engine.speak("testing WSR") from dragonfly import Literal, Sequence from dragonfly.test import ElementTester seq = Sequence([Literal("hello"), Literal("world")]) tester = ElementTester(seq, engine=engine) results = tester.recognize("hello world") self.assertEqual([u"hello", u"world"], results)
def test_get_engine_automatic_is_usable(self): """ Verify that the automatically selected engine is usable. """ engine = get_engine() engine.connect() try: engine.speak("testing automatic") from dragonfly import Literal from dragonfly.test import ElementTester tester = ElementTester(Literal("hello world")) results = tester.recognize("hello world") assert results == "hello world" finally: engine.disconnect()
def test_get_engine_sapi5_is_usable(self): """ Verify that the sapi5 engine is usable. """ engine = get_engine() self.assertTrue(isinstance(engine, EngineBase)) self.assertEqual("sapi5", engine.name) engine.speak("testing WSR") from dragonfly import Literal, Sequence from dragonfly.test import ElementTester seq = Sequence([Literal("hello"), Literal("world")]) tester = ElementTester(seq, engine=engine) results = tester.recognize("hello world") self.assertEqual([u"hello", u"world"], results)
def test_unicode_literals(self): """ Verify that the text engine can mimic literals using non-ascii characters. """ tester = ElementTester(Literal(u"Привет, как дела?")) # Test that strings and Unicode objects can be used. results = tester.recognize("Привет, как дела?") assert results == u"Привет, как дела?" results = tester.recognize(u"Привет, как дела?") assert results == u"Привет, как дела?" # Check that recognition failure is possible. results = tester.recognize(u"до свидания") assert results is RecognitionFailure
def test_get_engine_sphinx_is_usable(self): """ Verify that the sphinx engine is usable by testing that a simple rule is loaded correctly and works correctly. """ engine = get_engine() assert engine.name == "sphinx" assert isinstance(self.engine, EngineBase) engine.speak("testing sphinx") # Test that a basic rule can be loaded and recognized. seq = Sequence([Literal("hello"), Literal("world")]) tester = ElementTester(seq, engine=engine) results = tester.recognize("hello world") self.assertEqual(results, [u"hello", u"world"])
def test_get_engine_natlink_is_usable(self): """ Verify that the natlink engine is usable. """ engine = get_engine("natlink") assert isinstance(engine, EngineBase) assert engine.name == "natlink" engine.connect() try: engine.speak("testing natlink") from dragonfly import Literal from dragonfly.test import ElementTester tester = ElementTester(Literal("hello world")) results = tester.recognize("hello world") assert results == "hello world" finally: engine.disconnect()
def test_get_engine_sapi5_is_usable(self): """ Verify that the sapi5 engine is usable. """ engine = get_engine("sapi5") assert isinstance(engine, EngineBase) assert engine.name == "sapi5" engine.connect() try: engine.speak("testing WSR") from dragonfly import Literal from dragonfly.test import ElementTester tester = ElementTester(Literal("hello world"), engine=engine) results = tester.recognize("hello world") assert results == "hello world", "%r != %r" % (results, "hello world") finally: engine.disconnect()
def test_get_engine_kaldi_is_usable(self): """ Verify that the kaldi engine is usable by testing that a simple rule is loaded correctly and works correctly. """ engine = get_engine() assert engine.name == "kaldi" assert isinstance(self.engine, EngineBase) engine.speak("testing kaldi") # Test that a basic rule can be loaded and recognized. seq = Sequence([Literal("hello"), Literal("world")]) tester = ElementTester(seq, engine=engine) results = tester.recognize("hello world") self.assertEqual(results, [u"hello", u"world"])
def test_rule_alternative(engine): v1 = object() v2 = object() class Rule1(MappingRule): mapping = {'foo': v1} class Rule2(MappingRule): mapping = {'test': v2} rules = [Rule1(), Rule2()] element = RuleOrElemAlternative(rules) tester = ElementTester(element, engine) assert tester.recognize('foo') is v1 assert tester.recognize('test') is v2 assert tester.recognize('blah') is RecognitionFailure
def test_unicode_literals(self): """ Verify that the text engine can mimic literals using non-ascii characters. """ tester = ElementTester(Literal(u"touché")) # Test that strings and Unicode objects can be used. string = "touché" if isinstance(string, six.binary_type): encoding = locale.getpreferredencoding() string = string.decode("windows-1252").encode(encoding) results = tester.recognize(string) assert results == u"touché" results = tester.recognize(u"touché") assert results == u"touché" # Check that recognition failure is possible. results = tester.recognize(u"jalapeño") assert results is RecognitionFailure
def test_exclusion(engine): choice = Choice('choice', choices={'a': 1, 'b': 2}) repetition = Repetition(choice, min=2, max=5) to_exclude = Literal('a a') exclusion = Exclusion(element=repetition, exclusion=to_exclude) tester = ElementTester(exclusion, engine) assert tester.recognize('a a') is RecognitionFailure assert tester.recognize('c') is RecognitionFailure assert tester.recognize('b a a b') == [2, 1, 1, 2] assert tester.recognize('a b') == [1, 2] assert tester.recognize('b a') == [2, 1] assert tester.recognize('b b') == [2, 2]
def test_phrases_exclusion(engine): choice = Choice('choice', choices={'a': 1, 'b': 2}) repetition = Repetition(choice, min=2, max=5) phrases = ['a a', 'b b'] exclusion = PhrasesExclusion(element=repetition, phrases_to_exclude=phrases) tester = ElementTester(exclusion, engine) assert tester.recognize('a a') is RecognitionFailure assert tester.recognize('c') is RecognitionFailure assert tester.recognize('a b') == [1, 2] assert tester.recognize('b a') == [2, 1] assert tester.recognize('b a a b') is RecognitionFailure assert tester.recognize('b a b a') == [2, 1, 2, 1] assert tester.recognize('b b') is RecognitionFailure
def test_exclusion_by_function(engine): choice = Choice('choice', choices={'a': 1, 'b': 2}) repetition = Repetition(choice, min=2, max=5) exclude_if = lambda words: 'a a' in ' '.join(words) exclusion = Exclusion(element=repetition, exclude_if_func=exclude_if) tester = ElementTester(exclusion, engine) assert tester.recognize('a a') is RecognitionFailure assert tester.recognize('c') is RecognitionFailure assert tester.recognize('b a a b') is RecognitionFailure assert tester.recognize('b b a b') == [2, 2, 1, 2] assert tester.recognize('a b') == [1, 2] assert tester.recognize('b a') == [2, 1] assert tester.recognize('b b') == [2, 2]
def insert_mode_commands_tester(engine): element = RuleRef(InsertModeCommands()) tester = ElementTester(element, engine) return tester
def visual_mode_keystroke_tester(engine): element = RuleRef(VisualModeCommands()) tester = ElementTester(element, engine) return tester
def test_find_motion(engine): element = FindMotionRef('find') tester = ElementTester(element, engine) actual = tester.recognize('find alpha') expected = 'f,a' assert actual == expected
def ex_mode_commands_tester(engine): element = RuleRef(ExModeCommands()) tester = ElementTester(element, engine) return tester
def normal_mode_to_visual_mode_tester(engine): element = RuleRef(NormalModeToVisualModeCommands()) tester = ElementTester(element, engine) return tester
def pycharm_global_rule_tester(engine): element = RuleRef(PycharmGlobalRule()) tester = ElementTester(element, engine) return tester
def visual_mode_to_ex_mode_tester(engine): element = RuleRef(VisualModeToExModeCommands()) tester = ElementTester(element, engine) return tester
def insert_mode_to_normal_mode_tester(engine): element = RuleRef(InsertModeToNormalModeCommands()) tester = ElementTester(element, engine) return tester
def test_recognition_observers(self): # RecognitionObservers are a bit quirky for the sapi5 engines, # so the tests for them are repeated here to handle that. from dragonfly import (Integer, Literal, RecognitionHistory, RecognitionObserver) from dragonfly.test import ElementTester, RecognitionFailure class RecognitionObserverTester(RecognitionObserver): """ RecognitionObserver class from the recobs doctests. """ def __init__(self): RecognitionObserver.__init__(self) self.waiting = False self.words = None def on_begin(self): self.waiting = True def on_recognition(self, words): self.waiting = False self.words = words def on_failure(self): self.waiting = False self.words = False test_recobs = RecognitionObserverTester() test_recobs.register() results = test_recobs.waiting, test_recobs.words assert results == (False, None) # Test simple literal element recognitions. test_lit = ElementTester(Literal("hello world")) assert test_lit.recognize("hello world") == "hello world" results = test_recobs.waiting, test_recobs.words assert results == (False, (u'hello', u'world')) assert test_lit.recognize("hello universe") is RecognitionFailure results = test_recobs.waiting, test_recobs.words assert results == (False, False) # Test Integer element recognitions test_int = ElementTester(Integer(min=1, max=100)) assert test_int.recognize("seven") == 7 results = test_recobs.waiting, test_recobs.words assert results == (False, (u'seven',)) assert test_int.recognize("forty seven") == 47 results = test_recobs.waiting, test_recobs.words assert results == (False, (u'forty', u'seven')) assert test_int.recognize("one hundred") is RecognitionFailure results = test_recobs.waiting, test_recobs.words assert results == (False, False) assert test_lit.recognize("hello world") == u'hello world' # Now test RecognitionHistory. history = RecognitionHistory() assert test_lit.recognize("hello world") == u'hello world' # Not yet registered, so didn't receive previous recognition. assert history == [] history.register() assert test_lit.recognize("hello world") == u'hello world' # Now registered, so should have received previous recognition. assert history == [(u'hello', u'world')] assert test_lit.recognize("hello universe") is RecognitionFailure # Failed recognitions are ignored, so history is unchanged. assert history == [(u'hello', u'world')] assert test_int.recognize("eighty six") == 86 assert history == [(u'hello', u'world'), (u'eighty', u'six')] # The RecognitionHistory class allows its maximum length to be set. history = RecognitionHistory(3) history.register() assert history == [] for i, word in enumerate(["one", "two", "three", "four", "five"]): assert test_int.recognize(word) == i + 1 assert history == [(u'three',), (u'four',), (u'five',)] history = RecognitionHistory(1) history.register() assert history == [] for i, word in enumerate(["one", "two", "three", "four", "five"]): assert test_int.recognize(word) == i + 1 assert history == [(u'five',)]