def test_iter_with_unmatched_simple3(self): t = Trie() t.add('AND', 'AND') t.make_automaton() test_string = 'AND an a andersom' result = list(t.iter(test_string)) assert ['AND'] == [r.value for r in result]
def get_test_automaton(): words = '( AND ) OR'.split() t = Trie() for w in words: t.add(w, w) t.make_automaton() return t
def test_add_existing_WordShouldReplaceAssociatedValue(self): t = Trie() t.add('python', 'value') assert ('python', 'value') == t.get('python') t.add('python', 'other') assert ('python', 'other') == t.get('python')
def get_test_automaton(): words = 'he her hers his she hi him man himan'.split() t = Trie() for w in words: t.add(w, w) t.make_automaton() return t
def testAddingExistingWordShouldReplaceAssociatedValue(self): t = Trie() t.add('python', 'value') assert Output('python', 'value') == t.get('python') t.add('python', 'other') assert Output('python', 'other') == t.get('python')
def test_exists_ShouldDetectAddedWords(self): t = Trie() t.add('python', 'value') t.add('ada', 'value') self.assertTrue(t.exists('python')) self.assertTrue(t.exists('ada'))
def test_iter_with_unmatched_simple2(self): t = Trie() t.add('AND', 'AND') t.make_automaton() test_string = 'AND an a and' result = list(t.iter(test_string)) assert 'ANDand' == ''.join(r.string for r in result)
def test_values_ShouldReturnAllValuesAlreadyAddedToTheTrie(self): t = Trie() t.add('python', 1) t.add('ada', 2) t.add('perl', 3) t.add('pascal', 4) t.add('php', 5) result = list(t.values()) self.assertIn(1, result) self.assertIn(2, result) self.assertIn(3, result) self.assertIn(4, result) self.assertIn(5, result)
def testKeysShouldReturnAllKeysAlreadyAddedToTheTrie(self): t = Trie() t.add('python', 1) t.add('ada', 2) t.add('perl', 3) t.add('pascal', 4) t.add('php', 5) result = list(t.keys()) self.assertIn('python', result) self.assertIn('ada', result) self.assertIn('perl', result) self.assertIn('pascal', result) self.assertIn('php', result)
def testItemsShouldReturnAllItemsAlreadyAddedToTheTrie(self): t = Trie() t.add('python', 1) t.add('ada', 2) t.add('perl', 3) t.add('pascal', 4) t.add('php', 5) result = list(t.items()) self.assertIn(('python', 1), result) self.assertIn(('ada', 2), result) self.assertIn(('perl', 3), result) self.assertIn(('pascal', 4), result) self.assertIn(('php', 5), result)
def test_is_prefix_ShouldDetecAllPrefixesIncludingWord(self): t = Trie() t.add('python', 'value') t.add('ada', 'value') self.assertTrue(t.is_prefix('a')) self.assertTrue(t.is_prefix('ad')) self.assertTrue(t.is_prefix('ada')) self.assertTrue(t.is_prefix('p')) self.assertTrue(t.is_prefix('py')) self.assertTrue(t.is_prefix('pyt')) self.assertTrue(t.is_prefix('pyth')) self.assertTrue(t.is_prefix('pytho')) self.assertTrue(t.is_prefix('python'))
def test_iter_simple(self): t = Trie() t.add('AND', 'AND') t.add('OR', 'OR') t.add('WITH', 'WITH') t.add('(', '(') t.add(')', ')') t.add('GPL-2.0', 'GPL-2.0') t.add('mit', 'MIT') t.add('Classpath', 'Classpath') t.make_automaton() test_string = '(GPL-2.0 with Classpath) or (gpl-2.0) and (classpath or gpl-2.0 OR mit) ' # 111111111122222222223333333333444444444455555555556666666666777 # 0123456789012345678901234567890123456789012345678901234567890123456789012 result = list(t.iter(test_string)) expected = [ Token(0, 0, u'(', u'('), Token(1, 7, u'GPL-2.0', u'GPL-2.0'), Token(9, 12, u'with', u'WITH'), Token(14, 22, u'Classpath', u'Classpath'), Token(23, 23, u')', u')'), Token(25, 26, u'or', u'OR'), Token(28, 28, u'(', u'('), Token(29, 35, u'gpl-2.0', u'GPL-2.0'), Token(36, 36, u')', u')'), Token(38, 40, u'and', u'AND'), Token(42, 42, u'(', u'('), Token(43, 51, u'classpath', u'Classpath'), Token(53, 54, u'or', u'OR'), Token(57, 63, u'gpl-2.0', u'GPL-2.0'), Token(65, 66, u'OR', u'OR'), Token(68, 70, u'mit', u'MIT'), Token(71, 71, u')', u')') ] assert expected == result
def test_get_UnknowWordWithDefaultValueShouldReturnDefault(self): t = Trie() self.assertEqual(t.get('python', 'default'), 'default')
def test_get_UnknowWordWithoutDefaultValueShouldRaiseException(self): t = Trie() with self.assertRaises(KeyError): t.get('python')
def testAddedWordShouldBeCountedAndAvailableForRetrieval(self): t = Trie() t.add('python', 'value') assert Output('python', 'value') == t.get('python')
def test_exists_ShouldReturnFailOnUnknownWord(self): t = Trie() t.add('python', 'value') self.assertFalse(t.exists('ada'))
def test_add_can_get(self): t = Trie() t.add('python', 'value') assert ('python', 'value') == t.get('python')