Skip to content

Commit

Permalink
[Core] Make Dictionary.get_or_add check for read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
AThousandShips committed Oct 24, 2024
1 parent ff9fb0a commit e686002
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/variant/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ Variant Dictionary::get_or_add(const Variant &p_key, const Variant &p_default) {
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get"), p_default);
const Variant *result = getptr(key);
if (!result) {
ERR_FAIL_COND_V_MSG(_p->read_only, Variant(), "Dictionary is in read-only state.");
Variant value = p_default;
ERR_FAIL_COND_V(!_p->typed_value.validate(value, "add"), value);
operator[](key) = value;
Expand Down
5 changes: 5 additions & 0 deletions tests/core/variant/test_dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ TEST_CASE("[Dictionary] Assignment using bracket notation ([])") {
map.make_read_only();
CHECK(int(map["This key does not exist"].get_type()) == Variant::NIL);
CHECK(map.size() == length);

ERR_PRINT_OFF;
CHECK(int(map.get_or_add("This key does not exist", String()).get_type()) == Variant::NIL);
CHECK(map.size() == length);
ERR_PRINT_ON;
}

TEST_CASE("[Dictionary] get_key_lists()") {
Expand Down

0 comments on commit e686002

Please sign in to comment.