From 7971ecdc7e8f8d654a223c066cdfdb81ce4c69c8 Mon Sep 17 00:00:00 2001 From: Stiopa Koltsov Date: Wed, 9 Oct 2024 14:21:36 -0700 Subject: [PATCH] Test special cases of dict.update Summary: Planning to optimize `dict.update`, making sure these special cases don't regress. Reviewed By: IanChilds Differential Revision: D63601248 fbshipit-source-id: b1cd170e0067429b5263ce05db8829ee0f61520a --- starlark/src/values/types/dict/methods.rs | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/starlark/src/values/types/dict/methods.rs b/starlark/src/values/types/dict/methods.rs index 95459a153..02263b731 100644 --- a/starlark/src/values/types/dict/methods.rs +++ b/starlark/src/values/types/dict/methods.rs @@ -374,6 +374,7 @@ pub(crate) fn dict_methods(registry: &mut MethodsBuilder) { #[cfg(test)] mod tests { use crate::assert; + use crate::assert::Assert; #[test] fn test_error_codes() { @@ -394,4 +395,41 @@ mod tests { // Also check we fail if the entire dictionary is static (a different code path). assert::fails("{42: 2, 42: 3}", &["key repeated", "42"]); } + + #[test] + fn test_dict_update_with_self_pos() { + assert::eq("{3: 4, 1: 2}", "d = {3: 4, 1: 2}; d.update(d); d"); + } + + #[test] + fn test_dict_update_with_self_as_kwargs() { + assert::eq("{'a': 1, 'b': 2}", "d = {'a': 1, 'b': 2}; d.update(**d); d"); + } + + #[test] + fn test_frozen_dict_cannot_be_updated_with_self_pos() { + let mut a = Assert::new(); + a.module("d.star", "D = {7: 8, 9: 0}"); + a.fail( + r#" +load('d.star', 'D') + +D.update(D) +"#, + "Immutable", + ); + } + + #[test] + fn test_frozen_dict_cannot_be_updated_with_self_as_kwargs() { + let mut a = Assert::new(); + a.module("d.star", "D = {'x': 17, 'y': 19}"); + a.fail( + r#" +load('d.star', 'D') +D.update(**D) +"#, + "Immutable", + ); + } }