def test_keys_values_items_containment_and_length(self): d = FancyDict(a=2, b=3, c=4, d=5) self.assertEqual(set(d.keys()), {'a', 'b', 'c', 'd'}) self.assertEqual(set(d.values()), {2, 3, 4, 5}) self.assertEqual( set(d.items()), {('a', 2), ('b', 3), ('c', 4), ('d', 5)}, ) self.assertEqual(len(d), 4) self.assertTrue('a' in d) self.assertFalse('a' not in d) self.assertFalse('e' in d) self.assertTrue('e' not in d) self.assertNotIn('get', d) with self.assertRaises(KeyError): d['get'] with self.assertRaises(KeyError): d['keys'] self.assertNotIn('values', d) self.assertNotIn('setdefault', d) self.assertEqual(d.pop('b', None), 3) self.assertNotIn('b', d) self.assertEqual(d.pop('b', None), None) self.assertNotIn('b', d) with self.assertRaises(KeyError): d.pop('b')
def test_allow_setting_keys_and_attributes(self): d = FancyDict({'a': 2, 'b': 3}) d['a'] = 4 self.assertEqual(d['a'], 4) self.assertEqual(d.a, 4) d.c = 9 self.assertEqual(d['c'], 9) self.assertEqual(d.c, 9) self.assertEqual(d['b'], 3) x = FancyDict() y = FancyDict() x.a = 4 y.a = 5 self.assertEqual(x.a, 4)
def test_normalize_attribute(self): d = FancyDict({'greeting 1': 'hi'}, normalize=True) self.assertEqual(d['greeting 1'], 'hi') self.assertEqual(d.greeting_1, 'hi') d.greeting_2 = 'hello' self.assertEqual(d['greeting 2'], 'hello') self.assertEqual(d.greeting_2, 'hello') d['greeting 2'] = 'hey' self.assertEqual(d['greeting 2'], 'hey') self.assertEqual(d.get('greeting 2'), 'hey') self.assertEqual(d.greeting_2, 'hey') with self.assertRaises(AttributeError): d.greeting2 d = FancyDict({'greeting 1': 'hi'}) self.assertEqual(d['greeting 1'], 'hi') with self.assertRaises(AttributeError): d.greeting_1
def test_dir(self): d = FancyDict(a=2, b=3, c=4, d=5) self.assertIn('a', dir(d)) self.assertIn('b', dir(d)) self.assertIn('c', dir(d)) self.assertIn('d', dir(d))
def test_keyword_arguments_equality_and_get_method(self): d = FancyDict(a=2, b=3, c=4, d=5) self.assertEqual(d.a, 2) self.assertEqual(d.b, 3) self.assertEqual(d['c'], 4) self.assertEqual(d['d'], 5) x = FancyDict({'a': 2, 'b': 3}) y = FancyDict({'a': 2, 'b': 4}) self.assertNotEqual(x, y) y.b = 3 self.assertEqual(x, y) x.c = 5 self.assertNotEqual(x, y) y.c = 5 self.assertEqual(x, y) self.assertIsNone(y.get('d')) self.assertEqual(y.get('c'), 5) self.assertEqual(y.get('d', 5), 5)
def test_original_dictionary_unchanged(self): mapping = {'a': 2, 'b': 3} d = FancyDict(mapping) d.c = 4 self.assertEqual(mapping, {'a': 2, 'b': 3})
def test_attribute_access(self): d = FancyDict({'a': 2, 'b': 3}) self.assertEqual(d.a, 2) self.assertEqual(d.b, 3)
def test_key_access(self): d = FancyDict({'a': 2, 'b': 3}) self.assertEqual(d['a'], 2) self.assertEqual(d['b'], 3)
def test_constructor(self): FancyDict() FancyDict({'a': 2, 'b': 3})