예제 #1
0
 def test_getattr(self):
     scope = {}
     placeholder = demandload.Placeholder(scope, 'foo', list)
     self.assertEqual(scope, object.__getattribute__(placeholder, '_scope'))
     self.assertEqual(placeholder.__doc__, [].__doc__)
     self.assertEqual(scope['foo'], [])
     demandload._protection_enabled = lambda: True
     self.assertRaises(ValueError, getattr, placeholder, '__doc__')
예제 #2
0
 def test_getattr(self):
     scope = {}
     placeholder = demandload.Placeholder(scope, 'foo', list)
     assert scope == object.__getattribute__(placeholder, '_scope')
     assert placeholder.__doc__ == [].__doc__
     assert scope['foo'] == []
     demandload._protection_enabled = lambda: True
     with pytest.raises(ValueError):
         getattr(placeholder, '__doc__')
예제 #3
0
 def test_call(self):
     def passthrough(*args, **kwargs):
         return args, kwargs
     def get_func():
         return passthrough
     scope = {}
     placeholder = demandload.Placeholder(scope, 'foo', get_func)
     assert scope == object.__getattribute__(placeholder, '_scope')
     assert (('arg',), {'kwarg': 42}) == placeholder('arg', kwarg=42)
     assert passthrough is scope['foo']
예제 #4
0
    def test_setattr(self):
        class Struct(object):
            pass

        scope = {}
        placeholder = demandload.Placeholder(scope, 'foo', Struct)
        placeholder.val = 7
        demandload._protection_enabled = lambda: True
        self.assertRaises(ValueError, getattr, placeholder, 'val')
        self.assertEqual(7, scope['foo'].val)
예제 #5
0
    def test_setattr(self):
        class Struct:
            pass

        scope = {}
        placeholder = demandload.Placeholder(scope, 'foo', Struct)
        placeholder.val = 7
        demandload._protection_enabled = lambda: True
        with pytest.raises(ValueError):
            getattr(placeholder, 'val')
        assert 7 == scope['foo'].val
예제 #6
0
    def test_call(self):
        def passthrough(*args, **kwargs):
            return args, kwargs

        def get_func():
            return passthrough

        scope = {}
        placeholder = demandload.Placeholder(scope, 'foo', get_func)
        self.assertEqual(scope, object.__getattribute__(placeholder, '_scope'))
        self.assertEqual((('arg', ), {
            'kwarg': 42
        }), placeholder('arg', kwarg=42))
        self.assertIdentical(passthrough, scope['foo'])
예제 #7
0
 def test__str__(self):
     scope = {}
     placeholder = demandload.Placeholder(scope, 'foo', list)
     assert scope == object.__getattribute__(placeholder, '_scope')
     assert str(placeholder) == str([])
     assert scope['foo'] == []
예제 #8
0
 def test__str__(self):
     scope = {}
     placeholder = demandload.Placeholder(scope, 'foo', list)
     self.assertEqual(scope, object.__getattribute__(placeholder, '_scope'))
     self.assertEqual(str(placeholder), str([]))
     self.assertEqual(scope['foo'], [])
예제 #9
0
 def test__str__(self):
     scope = {}
     placehold = demandload.Placeholder(scope, 'foo', list)
     self.assertEqual({}, scope)
     self.assertEqual(str(placehold), str([]))
     self.assertEqual(scope['foo'], [])