def test_startswith(self): kc = KeyChain('a', 'b', 'c') self.assertTrue(kc.startswith(())) self.assertTrue(kc.startswith(('a',))) self.assertTrue(kc.startswith(('a', 'b'))) self.assertTrue(kc.startswith(('a', 'b', 'c'))) self.assertFalse(kc.startswith(('a', 'b', 'c', 'd'))) self.assertFalse(kc.startswith(('a', 'b', 'X'))) self.assertFalse(kc.startswith(('a', 'X'))) self.assertFalse(kc.startswith(('X')))
def test_startswith(self): kc = KeyChain('a', 'b', 'c') self.assertEqual(kc.startswith(('a',)), True) self.assertEqual(kc.startswith(('a', 'b')), True) self.assertEqual(kc.startswith(('a', 'b', 'c')), True) self.assertEqual(kc.startswith(('a', 'b', 'c', 'x')), False) self.assertEqual(kc.startswith(('a', 'b', 'x')), False) self.assertEqual(kc.startswith(('a', 'x')), False) self.assertEqual(kc.startswith(('x')), False) self.assertEqual(kc.startswith(()), True)
def test_next_key(self): kc = KeyChain('a', 'b', 'c') for i in range(10): self.assertEqual(kc.next_key, 'a') kc.advance() self.assertEqual(kc.next_key, 'b') kc.advance() self.assertEqual(kc.next_key, 'c') kc.advance() self.assertEqual(kc.next_key, None) # chain is complete kc.advance() # same as reset() if complete
def test_advance(self): kc = KeyChain('a', 'b', 'c') for _ in range(10): self.assertEqual(kc.given, ()) kc.advance() self.assertEqual(kc.given, ('a',)) kc.advance() self.assertEqual(kc.given, ('a', 'b')) kc.advance() self.assertEqual(kc.given, ('a', 'b', 'c')) kc.advance()
def test_is_complete(self): kc = KeyChain('a', 'b', 'c') for i in range(10): self.assertEqual(kc.is_complete, False) kc.advance() self.assertEqual(kc.is_complete, False) kc.advance() self.assertEqual(kc.is_complete, False) kc.advance() self.assertEqual(kc.is_complete, True) kc.advance()
def test_unbind(self): kc = '1 2 3' for keys in ('1', '1 2', '1 2 3'): self.km.bind(kc, 'foo') self.assertIn(KeyChain('1', '2', '3'), self.km.keys()) self.km.unbind(keys) self.assertNotIn(KeyChain('1', '2', '3'), self.km.keys()) for keys in ('1 3', '1 2 4', '1 2 3 4', '2', '3', '2 1', '3 2 1'): self.km.bind(kc, 'foo') self.assertIn(KeyChain('1', '2', '3'), self.km.keys()) with self.assertRaises(ValueError) as cm: self.km.unbind(keys) self.assertIn('Key not mapped in context', str(cm.exception)) self.assertIn(str(self.km.mkkey(keys)), str(cm.exception))
def test_mkkey(self): self.assertEqual(self.km.mkkey(Key('x')), Key('x')) self.assertEqual(self.km.mkkey(KeyChain('1', '2', '3')), KeyChain('1', '2', '3')) self.assertEqual(self.km.mkkey('x'), Key('x')) self.assertEqual(self.km.mkkey('x y z'), KeyChain('x', 'y', 'z')) self.assertEqual(self.km.mkkey('x+y+z'), KeyChain('x', 'y', 'z')) self.assertEqual(self.km.mkkey('x +'), KeyChain('x', '+')) self.assertEqual(self.km.mkkey('+ x'), KeyChain('+', 'x')) self.assertEqual(self.km.mkkey('x y +'), KeyChain('x', 'y', '+')) self.assertEqual(self.km.mkkey('+ y z'), KeyChain('+', 'y', 'z')) self.assertEqual(self.km.mkkey('+ + +'), KeyChain('+', '+', '+'))
def test_equality(self): kc = KeyChain('F1', 'space', 'page down') self.assertEqual(kc, ('F1', 'space', 'page down')) self.assertEqual(kc, ['F1', ' ', 'pgdn']) self.assertEqual(kc, (k for k in ('F1', ' ', 'page down'))) self.assertNotEqual(kc, ('F1', 'space', 'page down', 'X')) self.assertNotEqual(kc, ('F1', 'space')) self.assertNotEqual(kc, ('F1', 'space', 'X')) self.assertNotEqual(kc, ())
def test_feed_with_wrong_chain(self): kc = KeyChain('a', 'b', 'c') self.assertEqual(kc.feed('x'), KeyChain.REFUSED) self.assertEqual(kc.feed('a'), KeyChain.ADVANCED) self.assertEqual(kc.feed('x'), KeyChain.ABORTED) self.assertEqual(kc.feed('a'), KeyChain.ADVANCED) self.assertEqual(kc.feed('b'), KeyChain.ADVANCED) self.assertEqual(kc.feed('x'), KeyChain.ABORTED)
def test_minimum_number_of_keys(self): with self.assertRaises(ValueError) as cm: KeyChain('a') self.assertIn('Not enough keys', str(cm.exception))
def test_reset(self): kc = KeyChain('a', 'b', 'c') for i in range(10): for _ in range(i): kc.advance() kc.reset() self.assertEqual(kc.given, ())
def test_feed_with_correct_chain(self): kc = KeyChain('a', 'b', 'c') self.assertEqual(kc.feed('a'), KeyChain.ADVANCED) self.assertEqual(kc.feed('b'), KeyChain.ADVANCED) self.assertEqual(kc.feed('c'), KeyChain.COMPLETED)