Skip to content

Commit

Permalink
Implement 516 behavior for ~= and ~!
Browse files Browse the repository at this point in the history
  • Loading branch information
ike709 committed Feb 11, 2025
1 parent d5bdf43 commit c47f62c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
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))
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))
return DreamValue.False;
}
}

return DreamValue.True;
}

Expand Down

0 comments on commit c47f62c

Please sign in to comment.