Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 516 behavior for ~= and ~! #2212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Content.Tests/DMProject/Tests/Builtins/params2list.dm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/proc/RunTest()
ASSERT(params2list("a;b;c") ~= list(a="", b="", c=""))
ASSERT(params2list("a;a;a") ~= list(a=list("", "", ""))) // Crazy
ASSERT(json_encode(params2list("a;a;a")) == @#{"a":["","",""]}#)

ASSERT(params2list("a=1;b=2") ~= list(a="1", b="2"))
ASSERT(params2list("a=1;a=2") ~= list(a="2"))
2 changes: 1 addition & 1 deletion Content.Tests/DMProject/Tests/List/AssocListCombine.dm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/proc/RunTest()
var/list/L = list("A")
L |= list("B" = 1)
ASSERT(L ~= list("A", "B"))
ASSERT(L ~= list("A", "B" = 1))
ASSERT(L["A"] == null)
ASSERT(L["B"] == 1)

Expand Down
2 changes: 1 addition & 1 deletion Content.Tests/DMProject/Tests/List/ListCut.dm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

A = list(a = 10, b = 20)
A.Cut(1, 2)
ASSERT(A ~= list("b"))
ASSERT(A ~= list("b" = 20))
ASSERT(A["a"] == null)
ASSERT(A["b"] == 20)
7 changes: 7 additions & 0 deletions Content.Tests/DMProject/Tests/Operators/equivalence.dm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
ASSERT((l1 ~! l2) == TRUE)
ASSERT((l1 ~! l3) == TRUE)
ASSERT((l1 ~! l4) == TRUE)

// As of BYOND 516, we now care about assoc values for equivalence
var/list/one = list(a=1,b=3,c="hi")
var/list/two = list(a=1,b=2,c="hi")
var/list/three = list(a=1,b=3,c="hi")
ASSERT((one ~! two) == TRUE)
ASSERT((one ~= three) == TRUE)

var/matrix/m1 = matrix(1,2,3,4,5,6)
var/matrix/m2 = matrix(-1,-2,-3,-4,-5,6)
Expand Down
11 changes: 11 additions & 0 deletions OpenDreamRuntime/Objects/Types/DreamList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ public override DreamValue OperatorEquivalent(DreamValue b) {
return DreamValue.False;
if (GetLength() != secondList.GetLength())
return DreamValue.False;
if(IsAssociative && (!secondList.IsAssociative || secondList.GetAssociativeValues().Count != GetAssociativeValues().Count))
Copy link
Member

@wixoaGit wixoaGit Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list(a=null) ~= list("a") is TRUE so returning early if IsAssociative != secondList.IsAssociative is incorrect. At least as long as IsAssociative can still treat all-null associative values as associative.

return DreamValue.False;

var firstValues = GetValues();
var secondValues = secondList.GetValues();
Expand All @@ -380,6 +382,15 @@ public override DreamValue OperatorEquivalent(DreamValue b) {
return DreamValue.False;
}

// Starting with 516, equivalence checks assoc values
if (IsAssociative) {
var secondListAssoc = secondList.GetAssociativeValues();
foreach (var kvp in GetAssociativeValues()) {
if(!secondListAssoc.TryGetValue(kvp.Key, out var assocValue) || !assocValue.Equals(kvp.Value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lack of a value in either assoc lists should be treated as null.

return DreamValue.False;
}
}

return DreamValue.True;
}

Expand Down
Loading