Пример #1
0
 def test_clear(self):
     d = ComponentStore(self.one_dict, self.observer)
     self.out.truncate(0)
     d.clear()
     output = self.out.getvalue().strip()
     expected_reaction = self.obs_dict_clear + \
         " ({}, " + str(self.one_dict) + ")"
     # Check that the correct method is issued to the observer
     self.assertEqual(output, expected_reaction)
Пример #2
0
    def test_pop(self):
        d = ComponentStore(self.one_dict, self.observer)

        # Pop an existing value
        self.out.truncate(0)
        value = d.pop(2)
        output = self.out.getvalue().strip()
        expected_reaction = (self.obs_dict_pop + " (2, 'two')")
        self.assertEqual(output, expected_reaction)
        self.assertEqual('two', value)

        # pop a missing value
        value = d.pop(2)
        self.assertEqual(None, value)
Пример #3
0
    def test_setdefault(self):
        d = ComponentStore(self.one_dict, self.observer)

        # Set a default that does not exist
        self.out.truncate(0)
        value = d.setdefault(5, "five")
        output = self.out.getvalue().strip()
        expected_reaction = (self.obs_dict_setdefault +
                             " ({1: 'one', 2: 'two', 5: 'five'}, 5, 'five')")
        self.assertEqual(output, expected_reaction)
        self.assertEqual("five", value)

        # now set one that exists
        value = d.setdefault(5, "cinco")
        self.assertEqual("five", value)
Пример #4
0
    def test_setitem(self):

        d = ComponentStore(observer=self.observer)

        # Set a new item. We need to clear the buffer if the StringIO as well
        self.out.truncate(0)
        d[1] = "one"
        output = self.out.getvalue().strip()
        expected_reaction = self.obs_dict_create + " (1, 'one')"
        # Check that the correct method is issued to the observer
        self.assertEqual(output, expected_reaction)

        # Set a second item
        self.out.truncate(0)
        d[2] = "two"
        output = self.out.getvalue().strip()
        expected_reaction = self.obs_dict_create + " (2, 'two')"
        # Check that the correct method is issued to the observer
        self.assertEqual(output, expected_reaction)

        # Now replace a value
        self.out.truncate(0)
        d[2] = "dos"
        output = self.out.getvalue().strip()
        expected_reaction = self.obs_dict_set + " (2, 'dos', 'two')"
        # Check that the correct method is issued to the observer
        self.assertEqual(output, expected_reaction)
Пример #5
0
    def test_delitem(self):
        d = ComponentStore(self.one_dict, self.observer)
        self.out.truncate(0)

        del d[2]
        output = self.out.getvalue().strip()
        expected_reaction = self.obs_dict_del + " (2, 'two')"
        # Check that the correct method is issued to the observer
        self.assertEqual(output, expected_reaction)
Пример #6
0
 def test_constructor_with_arguments(self):
     self.out.truncate(0)
     d = ComponentStore(self.one_dict, self.observer)
     # When using arguments, the constructor needs to inform the observer
     # that the dict was initialized
     output = self.out.getvalue().strip()
     expected_reaction = self.obs_dict_init + \
         ' (' + str(self.one_dict) + (',)')
     self.assertEqual(output, expected_reaction)
     self.assertIsNotNone(d)
Пример #7
0
    def test_popitem(self):
        d = ComponentStore(self.one_dict, self.observer)

        test_dict = self.one_dict.copy()

        # Pop a value, Python decides
        self.out.truncate(0)
        key, value = d.popitem()
        test_dict.pop(key)

        output = self.out.getvalue().strip()
        expected_reaction = (self.obs_dict_popitem + " (" + str(key) + ", '" +
                             str(value) + "')")
        self.assertEqual(output, expected_reaction)

        # pop another item
        self.out.truncate(0)
        key, value = d.popitem()
        test_dict.pop(key)

        output = self.out.getvalue().strip()
        expected_reaction = (self.obs_dict_popitem + " (" + ", " + str(key) +
                             ", " + str(value) + ")")
Пример #8
0
    def test_update(self):
        d = ComponentStore(self.one_dict, self.observer)

        # Update without replacement
        self.out.truncate(0)
        d.update(self.other_dict)
        output = self.out.getvalue().strip()
        expected_reaction = (self.obs_dict_update +
                             " ([(3, 'three'), (4, 'four')], [])")
        self.assertEqual(output, expected_reaction)

        # Now with a replacement
        self.out.truncate(0)
        d.update({1: "one", 2: "dos"})
        output = self.out.getvalue().strip()
        expected_reaction = (self.obs_dict_update +
                             " ([], [(1, 'one', 'one'), (2, 'dos', 'two')])")
        self.assertEqual(output, expected_reaction)