Test that mutable dicts are tracked correctly. Args: mutable_state: A test state.
(mutable_state: MutableTestState)
| 2844 | |
| 2845 | |
| 2846 | def test_mutable_dict(mutable_state: MutableTestState): |
| 2847 | """Test that mutable dicts are tracked correctly. |
| 2848 | |
| 2849 | Args: |
| 2850 | mutable_state: A test state. |
| 2851 | """ |
| 2852 | assert not mutable_state.dirty_vars |
| 2853 | |
| 2854 | def assert_hashmap_dirty(): |
| 2855 | assert mutable_state.dirty_vars == {"hashmap"} |
| 2856 | mutable_state._clean() |
| 2857 | assert not mutable_state.dirty_vars |
| 2858 | |
| 2859 | # Test all dict operations |
| 2860 | mutable_state.hashmap.update({"new_key": "43"}) |
| 2861 | assert_hashmap_dirty() |
| 2862 | assert mutable_state.hashmap.setdefault("another_key", "66") == "another_value" |
| 2863 | assert_hashmap_dirty() |
| 2864 | assert mutable_state.hashmap.setdefault("setdefault_key", "67") == "67" |
| 2865 | assert_hashmap_dirty() |
| 2866 | assert mutable_state.hashmap.setdefault("setdefault_key", "68") == "67" |
| 2867 | assert_hashmap_dirty() |
| 2868 | assert mutable_state.hashmap.pop("new_key") == "43" |
| 2869 | assert_hashmap_dirty() |
| 2870 | mutable_state.hashmap.popitem() |
| 2871 | assert_hashmap_dirty() |
| 2872 | mutable_state.hashmap.clear() |
| 2873 | assert_hashmap_dirty() |
| 2874 | mutable_state.hashmap["new_key"] = "42" |
| 2875 | assert_hashmap_dirty() |
| 2876 | del mutable_state.hashmap["new_key"] |
| 2877 | assert_hashmap_dirty() |
| 2878 | mutable_state.hashmap |= {"new_key": "44"} |
| 2879 | assert_hashmap_dirty() |
| 2880 | |
| 2881 | # Test nested dict operations |
| 2882 | mutable_state.hashmap["array"] = [] |
| 2883 | assert_hashmap_dirty() |
| 2884 | mutable_state.hashmap["array"].append("1") |
| 2885 | assert_hashmap_dirty() |
| 2886 | mutable_state.hashmap["dict"] = {} |
| 2887 | assert_hashmap_dirty() |
| 2888 | mutable_state.hashmap["dict"]["key"] = "42" |
| 2889 | assert_hashmap_dirty() |
| 2890 | mutable_state.hashmap["dict"]["dict"] = {} |
| 2891 | assert_hashmap_dirty() |
| 2892 | mutable_state.hashmap["dict"]["dict"]["key"] = "43" |
| 2893 | assert_hashmap_dirty() |
| 2894 | |
| 2895 | # Test proxy returned from `setdefault` and `get` |
| 2896 | mutable_value = mutable_state.hashmap.setdefault("setdefault_mutable_key", []) |
| 2897 | assert_hashmap_dirty() |
| 2898 | assert mutable_value == [] |
| 2899 | assert isinstance(mutable_value, MutableProxy) |
| 2900 | mutable_value.append("foo") |
| 2901 | assert_hashmap_dirty() |
| 2902 | mutable_value_other_ref = mutable_state.hashmap.get("setdefault_mutable_key") |
| 2903 | assert isinstance(mutable_value_other_ref, MutableProxy) |
nothing calls this directly
no test coverage detected