def test_iterbreadth(self): modules = [ 'com.example.foo.Foo', 'com.example.foo.Bar', 'com.example.Baz', 'com.example.baz.Boo', 'org.example.foo' ] trie = NsTrie(modules) expected = [ 'com', 'org', 'com.example', 'org.example', 'com.example.foo', 'com.example.Baz', 'com.example.baz', 'org.example.foo', 'com.example.foo.Foo', 'com.example.foo.Bar', 'com.example.baz.Boo', ] actual = list(trie.iterbreadth()) self.assertListEqual(actual, expected, msg="breadth first iteration should be level-wise " "and respect insertion order")
def test_sorted(self): modules = [ 'com.example.foo.Foo', 'com.example.foo.Bar', 'com.example.Baz', 'com.example.baz.Boo', 'org.example.foo' ] trie = NsTrie(modules) expected = [ 'com', 'org', 'com.example', 'org.example', 'com.example.foo', 'com.example.Baz', 'com.example.baz', 'org.example.foo', 'com.example.foo.Foo', 'com.example.foo.Bar', 'com.example.baz.Boo', ] sorted_by_depth_with_depth = sorted(trie.iterdepth(), key=lambda s: s.count('.')) sorted_by_depth_with_breadth = sorted(trie.iterbreadth(), key=lambda s: s.count('.')) self.assertEqual(expected, sorted_by_depth_with_depth) self.assertListEqual(sorted_by_depth_with_depth, sorted_by_depth_with_breadth) expected = [ 'com', 'com.example', 'com.example.Baz', 'com.example.baz', 'com.example.baz.Boo', 'com.example.foo', 'com.example.foo.Bar', 'com.example.foo.Foo', 'org', 'org.example', 'org.example.foo', ] self.assertListEqual(expected, sorted(trie.iterdepth())) expected = [ ('com', [ ('example', [ ('foo', [ ('Foo', 'com.example.foo.Foo'), ('Bar', 'com.example.foo.Bar'), ]), ('Baz', 'com.example.Baz'), ('baz', [ ('Boo', 'com.example.baz.Boo'), ]), ]), ]), ('org', [ ('example', [ ('foo', 'org.example.foo'), ]), ]), ] self.assertListEqual(sorted(trie), expected) expected = [expected[1], expected[0]] self.assertListEqual(sorted(trie, reverse=True), expected)