def test_list(self): # List. x = [1, 2, 3, [10]] x_const = cpp_const.to_const(x) with self._ex(): x_const[0] = 10 with self._ex(): x_const[:] = [] with self._ex(): del x_const[0] with self._ex(): x_const.append(10) with self._ex(): x_const.pop() # N.B. Access does not propagate. for i in x_const: self.assertFalse(cpp_const.is_const_test(i)) self.assertFalse(cpp_const.is_const_test(x_const[3]))
def test_dict(self): # Dictionary. d = {"a": 0, "b": 1, "z": [25]} d_const = cpp_const.to_const(d) self.assertEqual(d_const["a"], 0) with self._ex(): d_const["c"] = 2 with self._ex(): d_const.clear() # N.B. Access does not implicitly propagate. self.assertFalse(cpp_const.is_const_test(d_const["z"]))
def test_advanced(self): obj = Advanced() obj.add("a", 0) obj.add("b", 1) obj.add("z", [10]) obj_const = cpp_const.to_const(obj) self.assertTrue(cpp_const.is_const_test(obj_const.get_values())) self.assertEqual(obj_const.get("a"), 0) with self._ex(): obj_const.add("c", 2) with self._ex(): obj_const.get_values()["c"] = 2 with self._ex(): obj_const.mutate() with self._ex(): obj_const.mutate_indirect() # N.B. Access does not implicitly propagate. self.assertFalse(cpp_const.is_const_test(obj_const.get("z"))) self.assertFalse(cpp_const.is_const_test( obj_const.__dict__["_values"]))
def test_advanced(self): obj = Advanced() obj.add("a", 0) obj.add("b", 1) obj.add("z", [10]) obj_const = cpp_const.to_const(obj) self.assertTrue(cpp_const.is_const_test(obj_const.get_values())) self.assertEqual(obj_const.get("a"), 0) with self._ex(): obj_const.add("c", 2) with self._ex(): obj_const.get_values()["c"] = 2 with self._ex(): obj_const.mutate() with self._ex(): obj_const.mutate_indirect() # N.B. Access does not implicitly propagate. self.assertFalse(cpp_const.is_const_test(obj_const.get("z"))) self.assertFalse( cpp_const.is_const_test(obj_const.__dict__["_values"]))
def test_basic(self): # Basic class. obj = Basic("Tim") obj_const = cpp_const.to_const(obj) obj.new_attr = "Something" self.assertEqual(obj_const.get_name(), "Tim") self.assertEqual(obj_const.__dict__["_name"], "Tim") with self._ex(): obj_const.set_name("Bob") with self._ex(): obj_const.name = "Bob" with self._ex(): obj_const._name = "Bob" with self._ex(): obj_const.__dict__["_name"] = "Bob" with self._ex(): obj_const.new_attr = "Something Else" # N.B. Access does not implicitly propagate. self.assertFalse(cpp_const.is_const_test(obj_const.value))