def test_parse(self): ex = Expression() self.assertEqual(ex.parse("a"), (["sequence" ,["literal", ["button", "a"]]], "")) self.assertEqual(ex.parse("ab"), (["sequence" ,["literal", ["button", "a"]], ["sequence", ["literal", ["button", "b"]]]], "")) self.assertEqual(ex.parse("a*b"), (["sequence" ,["literal", ["button", "a"]], ["sequence", ["literal", ["wild", "*"]], ["sequence", ["literal", ["button", "b"]]]]], "")) self.assertEqual(ex.parse("<Enter>a"), (["sequence" ,["literal", ["button", "<Enter>"]], ["sequence", ["literal", ["button", "a"]]]], ""))
def load_expressions(self, sceme="default", flush=False): if flush: self.expressions = [] Expression.set_wildchar(self.config["processor"]["input_end_key"]) for handler_header, expressions in self.config["expressions"][sceme].items(): handler_name, handler_args = self.config.split_header(handler_header) Handler = HandlerFactory(handler_name, handler_args) for pattern, command in expressions.items(): self.expressions.append((Expression(pattern), Handler(command))) # for resetting purposes self.reset_expression = Expression("*", self.config["processor"]["reset_key"])
def test_function(self): ex = Expression() btn = ex.button(25) k = KeyEvent(InputEvent(0,0,0,25,1)) k1 = KeyEvent(InputEvent(0,0,0,26,1)) k2 = KeyEvent(InputEvent(0,0,0,25,0)) self.assertEqual(btn([k]), (ex.state_accept, [], [])) self.assertEqual(btn([k,k1]), (ex.state_accept, [k1], [])) self.assertEqual(btn([k1,k]), (ex.state_reject, [k1,k], [])) self.assertEqual(btn([k2,k]), (ex.state_accept, [], [])) btn1 = ex.button(26) seq = ex.sequence(btn, btn1) self.assertEqual(seq([k]), (ex.state_partial, [], [])) self.assertEqual(seq([k,k1]), (ex.state_accept, [], [])) self.assertEqual(seq([k,k2]), (ex.state_partial, [], [])) self.assertEqual(seq([k,k1,k2]), (ex.state_accept, [k2], [])) self.assertEqual(seq([k,k2,k1]), (ex.state_accept, [], [])) wild = ex.sequence(ex.wild(28)) k3 = KeyEvent(InputEvent(0,0,0,28,1)) self.assertEqual(wild([k,k1,k2,k3]), (ex.state_accept, [], [[k,k1]])) self.assertEqual(wild([k1,k3,k2,k]), (ex.state_accept, [k2,k], [[k1]])) self.assertEqual(wild([k1,k2]), (ex.state_partial, [], [])) seq1 = ex.sequence(seq, wild) seq2 = ex.sequence(wild, seq1) self.assertEqual(seq1([k,k1,k2,k3]), (ex.state_accept, [], [[]])) self.assertEqual(seq2([k,k1,k3,k,k1,k2,k3]), (ex.state_accept, [], [[k,k1], []]))
def test_compile(self): ex = Expression() btn_a = ex.button(30) btn_b = ex.button(48) btn_enter = ex.button(28) btn_wild = ex.wild(28) key = lambda code, state: KeyEvent(InputEvent(0, 0, 0, code, state)) keys = [key(code,state) for code in [28,30,46,48] for state in [0,1,2]] keysets = [list(i) for i in permutations(keys, 3)] matches = { "a": btn_a, "<Enter>": btn_enter, "ab": ex.sequence(btn_a, btn_b), "a*b": ex.sequence(btn_a, ex.sequence(btn_wild, btn_b)) } for line, expected in matches.items(): result_expected = map(expected, keysets) result_actual = map(ex.compile(ex.parse(line)[0]), keysets) data = [(e, a, keysets[i], ex.parse(line))\ for i, (a, e) in enumerate(zip(result_actual, result_expected))\ if a[0] != e[0] or a[2] != e[2] ] self.assertEquals(data, [])