Exemplo n.º 1
0
 def test_setattr_type_check(self):
     # setattr requires that options be the right type and doesn't
     # parse from string formats.
     options = OptionParser()
     options.define('foo', default=1, type=int)
     with self.assertRaises(Error):
         options.foo = '2'
Exemplo n.º 2
0
    def test_mock_patch(self):
        # ensure that our setattr hooks don't interfere with mock.patch
        options = OptionParser()
        options.define('foo', default=1)
        options.parse_command_line(['main.py', '--foo=2'])
        self.assertEqual(options.foo, 2)

        with mock.patch.object(options.mockable(), 'foo', 3):
            self.assertEqual(options.foo, 3)
        self.assertEqual(options.foo, 2)

        # Try nested patches mixed with explicit sets
        with mock.patch.object(options.mockable(), 'foo', 4):
            self.assertEqual(options.foo, 4)
            options.foo = 5
            self.assertEqual(options.foo, 5)
            with mock.patch.object(options.mockable(), 'foo', 6):
                self.assertEqual(options.foo, 6)
            self.assertEqual(options.foo, 5)
        self.assertEqual(options.foo, 2)
Exemplo n.º 3
0
 def test_setattr(self):
     options = OptionParser()
     options.define('foo', default=1, type=int)
     options.foo = 2
     self.assertEqual(options.foo, 2)
Exemplo n.º 4
0
 def test_setattr_with_callback(self):
     values = []
     options = OptionParser()
     options.define('foo', default=1, type=int, callback=values.append)
     options.foo = 2
     self.assertEqual(values, [2])