'Test missing keys'

def test_missing(self):

    self.assertEqual(hasattr(NumDict, u'__missing__'), False)
    class NumDictD(NumDict, ):
        u'\n                                                subclass    defines    __missing__    method    returning    a    value\n                                                '
        def __missing__(self, key):
            key = 42
            return key
    num_dict_d = NumDictD({1: 2, 3: 4})
    self.assertEqual(num_dict_d[1], 2)
    self.assertEqual(num_dict_d[3], 4)
    self.assertNotIn(2, num_dict_d)
    self.assertNotIn(2, num_dict_d.keys())
    self.assertEqual(num_dict_d[2], 42)
    class NumDictE(NumDict, ):
        u'\n                                                subclass    defines    __missing__    method    raising    RuntimeError\n                                                '
        def __missing__(self, key):
            raise RuntimeError(key)
    num_dict_e = NumDictE()
    try:
        num_dict_e[42]
    except RuntimeError as err:
        self.assertEqual(err.args, (42,))
    else:
        self.fail(u"num_dict_e[42]    didn't    raise    RuntimeError")
    class NumDictF(NumDict, ):
        u'\n                                                subclass    sets    __missing__    instance    variable    (no    effect)\n                                                '
        def __init__(self):
            self.__missing__ = (lambda key: None)
            NumDict.__init__(self)
    num_dict_f = NumDictF()
    try:
        num_dict_f[42]
    except KeyError as err:
        self.assertEqual(err.args, (42,))
    else:
        self.fail(u"num_dict_f[42]    didn't    raise    KeyError")
    class NumDictG(NumDict, ):
        u"\n                                                subclass    doesn't    define    __missing__    at    a    all\n                                                "
        pass
    num_dict_g = NumDictG()
    try:
        num_dict_g[42]
    except KeyError as err:
        self.assertEqual(err.args, (42,))
    else:
        self.fail(u"num_dict_g[42]    didn't    raise    KeyError")
    class NumDictH(NumDictD, ):
        u'\n                                                subclass    calls    super    classes    __missing__    and    modifies    the    value    before    returning    it\n                                                '
        def __missing__(self, key):
            return (super(NumDictH, self).__missing__(key) + 1)
    num_dict_h = NumDictH()
    self.assertEqual(num_dict_h[None], (num_dict_d[None] + 1))
