Ejemplo n.º 1
0
    def test_chooseName(self):
        container = SampleContainer()
        container['foo.old.rst'] = 'rst doc'
        nc = NameChooser(container)

        # correct name without changes
        self.assertEqual(nc.chooseName('foobar.rst', None), u'foobar.rst')
        self.assertEqual(nc.chooseName(u'\xe9', None), u'\xe9')

        # automatically modified named
        self.assertEqual(nc.chooseName('foo.old.rst', None), u'foo.old-2.rst')
        self.assertEqual(nc.chooseName('+@[email protected]', None),
                         u'foo.old-2.rst')
        self.assertEqual(nc.chooseName('+@+@foo/foo+@', None), u'foo-foo+@')

        # empty name
        self.assertEqual(nc.chooseName('', None), u'NoneType')
        self.assertEqual(nc.chooseName('@+@', []), u'list')

        # if the name is not a string it is converted
        self.assertEqual(nc.chooseName(None, None), u'None')
        self.assertEqual(nc.chooseName(2, None), u'2')
        self.assertEqual(nc.chooseName([], None), u'[]')
        container['None'] = 'something'
        self.assertEqual(nc.chooseName(None, None), u'None-2')
        container['None-2'] = 'something'
        self.assertEqual(nc.chooseName(None, None), u'None-3')

        # even if the given name cannot be converted to unicode
        class BadBoy:
            def __unicode__(self):
                raise Exception

        self.assertEqual(nc.chooseName(BadBoy(), set()), u'set')
Ejemplo n.º 2
0
    def test_checkName(self):
        container = SampleContainer()
        container['foo'] = 'bar'
        checkName = NameChooser(container).checkName

        # invalid type for the name
        self.assertRaises(TypeError, checkName, 2, object())
        self.assertRaises(TypeError, checkName, [], object())
        self.assertRaises(TypeError, checkName, None, object())
        self.assertRaises(TypeError, checkName, None, None)

        # invalid names
        self.assertRaises(ValueError, checkName, '+foo', object())
        self.assertRaises(ValueError, checkName, '@foo', object())
        self.assertRaises(ValueError, checkName, 'f/oo', object())
        self.assertRaises(ValueError, checkName, '', object())

        # existing names
        self.assertRaises(KeyError, checkName, 'foo', object())
        self.assertRaises(KeyError, checkName, u'foo', object())

        # correct names
        self.assertEqual(True, checkName('2', object()))
        self.assertEqual(True, checkName(u'2', object()))
        self.assertEqual(True, checkName('other', object()))
        self.assertEqual(True, checkName(u'reserved', object()))
        self.assertEqual(True, checkName(u'r\xe9served', object()))

        # reserved names
        class ReservedNames(object):
            zope.component.adapts(IContainer)
            zope.interface.implements(IReservedNames)

            def __init__(self, context):
                self.reservedNames = set(('reserved', 'other'))

        zope.component.getSiteManager().registerAdapter(ReservedNames)

        self.assertRaises(NameReserved, checkName, 'reserved', object())
        self.assertRaises(NameReserved, checkName, 'other', object())
        self.assertRaises(NameReserved, checkName, u'reserved', object())
        self.assertRaises(NameReserved, checkName, u'other', object())