Пример #1
0
    def test_to_dict(self):
        trie = NsTrie((
            'com.example.foo',
            'org.example.foo',
            'org.example.bar',
            'com.foo.Baz',
        ))

        expected = {
            'com': {
                'example': {
                    'foo': 'com.example.foo',
                },
                'foo': {
                    'Baz': 'com.foo.Baz',
                },
            },
            'org': {
                'example': {
                    'foo': 'org.example.foo',
                    'bar': 'org.example.bar',
                },
            },
        }

        self.assertDictEqual(expected, trie.to_dict())
Пример #2
0
    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")
Пример #3
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")
Пример #4
0
    def test_add(self):
        trie1 = NsTrie()
        trie1.add('com.example.foo')

        trie2 = NsTrie()
        trie2['com.example.foo'] = True

        expected = {
            'com': {
                'example': {
                    'foo': 'com.example.foo'
                }
            }
        }

        self.assertDictEqual(expected, self.to_py(trie1._NsTrie__struct))
        self.assertDictEqual(expected, self.to_py(trie2._NsTrie__struct))

        expected = set(('com.example.foo',))

        self.assertSetEqual(expected, set(self.to_py(s) for s in trie1._NsTrie__child_nodes))
        self.assertSetEqual(expected, set(self.to_py(s) for s in trie2._NsTrie__child_nodes))

        with self.assertRaises(KeyError):
            trie1.add('com.example.foo')
Пример #5
0
    def test_discard(self):
        trie = NsTrie((
            'com.example.foo',
            'com.example.bar'
            ))

        try:
            trie.discard('org.example.foo')
        except KeyError:
            self.fail("discard should not produce a KeyError")
Пример #6
0
    def test_get(self):
        trie = NsTrie(('com.example.foo', 'com.example.bar'))

        expected = NsNode([
            ('foo', String('com.example.foo')),
            ('bar', String('com.example.bar'))
        ])

        self.assertEqual(expected, trie.get('com.example'))
        self.assertEqual(expected, trie['com.example'])

        with self.assertRaises(KeyError):
            trie.get('org.example.foo')
Пример #7
0
    def test_has(self):
        trie = NsTrie(('com.example.foo', 'com.example.bar'))

        self.assertFalse(trie.has('com'))
        self.assertFalse(trie.has('com.example'))
        self.assertTrue(trie.has('com.example.foo'))
        self.assertTrue(trie.has('com.example.bar'))

        self.assertTrue(trie.has('com', False))
        self.assertTrue(trie.has('com.example', False))
        self.assertTrue(trie.has('com.example.foo', False))
        self.assertTrue(trie.has('com.example.bar', False))
Пример #8
0
    def test_remove(self):
        trie = NsTrie((
            'com.example.foo',
            'com.example.bar'
        ))

        trie.remove('com.example')

        self.assertDictEqual({'com': 'com'}, trie.to_dict())
        self.assertSetEqual(set(('com',)), trie.child_nodes)
        self.assertEqual(1, len(trie))

        with self.assertRaises(KeyError):
            trie.remove('org.example.foo')
Пример #9
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)