Skip to content

Commit

Permalink
Test special cases of dict.update
Browse files Browse the repository at this point in the history
Summary: Planning to optimize `dict.update`, making sure these special cases don't regress.

Reviewed By: IanChilds

Differential Revision: D63601248

fbshipit-source-id: b1cd170e0067429b5263ce05db8829ee0f61520a
  • Loading branch information
stepancheg authored and facebook-github-bot committed Oct 9, 2024
1 parent cabc03a commit 7971ecd
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions starlark/src/values/types/dict/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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",
);
}
}

0 comments on commit 7971ecd

Please sign in to comment.