def test_time(self): # A12-15<call>13-16*a c = KeyPress.compile("A<A>12-15<call>13-16*a<a>", A=lambda: f.append('A'), a=lambda: f.append('a'), call=lambda: f.append('12-15')) f = [] key_press = KeyPress(c) self.assertEqual({12,15}, key_press.process_input('A')) self.assertEqual({12,15}, key_press.process_input(4)) self.assertEqual({12,15}, key_press.process_input(8)) self.assertEqual({13,16}, key_press.process_input(12)) self.assertEqual({13,16}, key_press.process_input(13)) self.assertEqual(set(), key_press.process_input('a')) self.assertEqual(['A', '12-15', 'a'], f) self.assertEqual("match", key_press.status()) f = [] key_press.reset() self.assertEqual("in progress", key_press.status()) key_press.process_inputs(['A', 4, 8]) self.assertEqual(['A'], f) self.assertEqual("in progress", key_press.status()) key_press.process_inputs(['a']) self.assertEqual(['A'], f) self.assertEqual("failed", key_press.status()) f = [] key_press.reset() self.assertEqual("in progress", key_press.status()) key_press.process_inputs(['A', 4, 13, 20]) self.assertEqual(['A', '12-15'], f) self.assertEqual("failed", key_press.status())
def test_union(self): # A|a c = KeyPress.compile("A<A>|a<a>", A=lambda: f.append('A'), a=lambda: f.append('a')) f = [] key_press = KeyPress(c) key_press.process_input('A') self.assertEqual(['A'], f) self.assertEqual("match", key_press.status()) f = [] key_press.reset() key_press.process_inputs(['a']) self.assertEqual(['a'], f) self.assertEqual("match", key_press.status())
def test_zero_or_one(self): # AB?C c = KeyPress.compile("A<A>(B<B>)?C<C>", A=lambda: f.append('A'), B=lambda: f.append('B'), C=lambda: f.append('C')) f = [] key_press = KeyPress(c) key_press.process_inputs(['A']) self.assertEqual(['A'], f) self.assertEqual("in progress", key_press.status()) key_press.process_inputs(['B']) self.assertEqual(['A', 'B'], f) self.assertEqual("in progress", key_press.status()) key_press.process_inputs(['C']) self.assertEqual(['A', 'B', 'C'], f) self.assertEqual("match", key_press.status()) f = [] key_press.reset() key_press.process_inputs(['A']) self.assertEqual(['A'], f) self.assertEqual("in progress", key_press.status()) key_press.process_inputs(['C']) self.assertEqual(['A', 'C'], f) self.assertEqual("match", key_press.status()) f = [] key_press.reset() key_press.process_inputs(['A']) self.assertEqual(['A'], f) self.assertEqual("in progress", key_press.status()) key_press.process_inputs(['B']) self.assertEqual(['A', 'B'], f) self.assertEqual("in progress", key_press.status()) key_press.process_inputs(['B']) self.assertEqual(['A', 'B'], f) self.assertEqual("failed", key_press.status())
def test_any_order(self): #A&bc&D c = KeyPress.compile("A<A>&b<b>c<c>&D<D>", A=lambda: f.append('A'), b=lambda: f.append('b'), c=lambda: f.append('c'), D=lambda: f.append('D')) f = [] key_press = KeyPress(c) key_press.process_inputs(['A']) self.assertEqual(['A'], f) self.assertEqual("in progress", key_press.status()) key_press.process_inputs(['b', 'c']) self.assertEqual(['A', 'b', 'c'], f) self.assertEqual("in progress", key_press.status()) key_press.process_inputs(['D']) self.assertEqual(['A', 'b', 'c', 'D'], f) self.assertEqual("match", key_press.status()) f = [] key_press.reset() key_press.process_inputs(['D']) self.assertEqual(['D'], f) self.assertEqual("in progress", key_press.status()) key_press.process_inputs(['D']) self.assertEqual(['D'], f) self.assertEqual("failed", key_press.status()) #------------------ #test all combinations: f = [] key_press.reset() key_press.process_inputs(['A', 'b', 'c', 'D']) self.assertEqual("match", key_press.status()) f = [] key_press.reset() key_press.process_inputs(['A', 'D', 'b', 'c']) self.assertEqual("match", key_press.status()) f = [] key_press.reset() key_press.process_inputs(['b', 'c', 'A', 'D']) self.assertEqual("match", key_press.status()) f = [] key_press.reset() key_press.process_inputs(['b', 'c', 'D', 'A']) self.assertEqual("match", key_press.status()) f = [] key_press.reset() key_press.process_inputs(['D', 'A', 'b', 'c']) self.assertEqual("match", key_press.status()) f = [] key_press.reset() key_press.process_inputs(['D', 'b', 'c', 'A']) self.assertEqual("match", key_press.status()) #------------------ f = [] key_press.reset() key_press.process_inputs(['A', 'c', 'b', 'D']) self.assertEqual("failed", key_press.status())