Esempio n. 1
0
    def test_iterdepth(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',
            'com.example',
            'com.example.foo',
            'com.example.foo.Foo',
            'com.example.foo.Bar',
            'com.example.Baz',
            'com.example.baz',
            'com.example.baz.Boo',
            'org',
            'org.example',
            'org.example.foo',
        ]

        actual = list(trie.iterdepth())

        self.assertListEqual(actual, expected,
                         msg="iterdepth should behave exactly the same as "
                             "default iteration")
Esempio n. 2
0
    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)