def test_lazy_setdefault(self):
     d = LazyDict()
     self.assertIs(d.setdefault('key0'), None)
     d.set_stub('key0', lambda x:'value0')
     self.assertIs(d.setdefault('key0'), 'value0')
     d.set_stub('key0', lambda x:'value0')
     self.assertEqual(d.setdefault('key1', 'value1'), 'value1')
     self.assertEqual(d['key1'], 'value1')
     self.assertEqual(d.setdefault('key0', 'value2'), 'value0')
    def test_setdefault(self):
        # dict.setdefault()
        d = LazyDict()
        self.assertIs(d.setdefault('key0'), None)
        d.setdefault('key0', [])
        self.assertIs(d.setdefault('key0'), None)
        d.setdefault('key', []).append(3)
        self.assertEqual(d['key'][0], 3)
        d.setdefault('key', []).append(4)
        self.assertEqual(len(d['key']), 2)
        self.assertRaises(TypeError, d.setdefault)

        class Exc(Exception): pass

        class BadHash(object):
            fail = False
            def __hash__(self):
                if self.fail:
                    raise Exc()
                else:
                    return 42

        x = BadHash()
        d[x] = 42
        x.fail = True
        self.assertRaises(Exc, d.setdefault, x, [])