コード例 #1
0
    def test_class_gt_instance(self):
        t1 = test_classes.TestClass()
        t2 = test_classes.TestClass()

        self.assertEqual(test_classes.TestClass.rw_attr, "change_me")
        self.assertEqual(t1.rw_attr, "change_me")
        self.assertEqual(t2.rw_attr, "change_me")

        # change the instance value
        t1.rw_attr = "new"
        self.assertEqual(test_classes.TestClass.rw_attr, "new")
        self.assertEqual(t1.rw_attr, "new")
        self.assertEqual(t2.rw_attr, "new")

        # change the class value
        test_classes.TestClass.rw_attr = "another_one"
        self.assertEqual(test_classes.TestClass.rw_attr, "another_one")
        self.assertEqual(t1.rw_attr, "another_one")
        self.assertEqual(t2.rw_attr, "another_one")

        # delete the instance value
        del t1.rw_attr
        with self.assertRaises(AttributeError):
            test_classes.TestClass.rw_attr
        with self.assertRaises(AttributeError):
            t1.rw_attr
        with self.assertRaises(AttributeError):
            t2.rw_attr
コード例 #2
0
    def test_read_only_property(self):
        # basic test
        self.assertEqual(test_classes.TestClass.ro_attr, "read-only")

        # try changing or deleting
        with self.assertRaises(AttributeError):
            test_classes.TestClass.ro_attr = "changed"
        with self.assertRaises(AttributeError):
            del test_classes.TestClass.ro_attr

        # do the same with an instance
        t = test_classes.TestClass()
        with self.assertRaises(AttributeError):
            t.ro_attr = "changed"
        with self.assertRaises(AttributeError):
            del t.ro_attr
コード例 #3
0
    def test_property_is_bound_to_class(self):
        t = test_classes.TestClass()

        self.assertEqual(test_classes.TestClass.rw_attr, "change_me")
        self.assertEqual(t.rw_attr, "change_me")

        # change class value
        test_classes.TestClass.rw_attr = "new"
        self.assertEqual(test_classes.TestClass.rw_attr, "new")
        self.assertEqual(t.rw_attr, "new")

        # delete the value
        del test_classes.TestClass.rw_attr
        with self.assertRaises(AttributeError):
            test_classes.TestClass.rw_attr
        with self.assertRaises(AttributeError):
            t.rw_attr