Skip to content

Commit

Permalink
RNET-1133: Fixed equality comparison for collections in mixed (#3573)
Browse files Browse the repository at this point in the history
* Fixed equality comparison for collections in mixed

* Fix

* Corrections

* Reinstatet cases

* Added missing cases plus explanation
  • Loading branch information
papafe authored Apr 16, 2024
1 parent cc537e6 commit ea60689
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Realm/Realm/DatabaseTypes/RealmValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,8 @@ public bool Equals(RealmValue other)
RealmValueType.ObjectId => AsObjectId() == other.AsObjectId(),
RealmValueType.Guid => AsGuid() == other.AsGuid(),
RealmValueType.Object => AsIRealmObject().Equals(other.AsIRealmObject()),
RealmValueType.List => AsList().Equals(other.AsList()),
RealmValueType.Dictionary => AsDictionary().Equals(other.AsDictionary()),
RealmValueType.Null => true,
_ => false,
};
Expand Down
59 changes: 53 additions & 6 deletions Tests/Realm.Tests/Database/RealmValueWithCollections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void List_WhenRetrieved_WorksWithAllTypes([Values(true, false)] bool isMa
}

[Test]
public void List_InRealmValue_NotEqualToAnything([Values(true, false)] bool isManaged)
public void List_InRealmValue_Equality([Values(true, false)] bool isManaged)
{
var innerList = ListGenerator(1);
var innerDict = DictGenerator(1);
Expand All @@ -113,15 +113,44 @@ public void List_InRealmValue_NotEqualToAnything([Values(true, false)] bool isMa
innerDict,
};

var copyOriginalList = new List<RealmValue>
{
RealmValue.Null,
1,
true,
"string",
new byte[] { 0, 1, 2 },
new DateTimeOffset(1234, 5, 6, 7, 8, 9, TimeSpan.Zero),
1f,
2d,
3m,
new ObjectId("5f63e882536de46d71877979"),
Guid.Parse("3809d6d9-7618-4b3d-8044-2aa35fd02f31"),
new InternalObject { IntProperty = 10, StringProperty = "brown" },
innerList,
innerDict,
};

RealmValue rv = originalList;
RealmValue rv2 = copyOriginalList;

if (isManaged)
{
rv = PersistAndFind(rv).RealmValueProperty;
}

Assert.That(rv == originalList, Is.False);
Assert.That(rv.Equals(originalList), Is.False);
#pragma warning disable CS1718 // Comparison made to same variable
Assert.That(rv == rv, Is.True);
#pragma warning restore CS1718 // Comparison made to same variable
Assert.That(rv.Equals(rv), Is.True);

// They contains the same values, but the collections do not point to the same object reference
Assert.That(rv == rv2, Is.False);
Assert.That(rv.Equals(rv2), Is.False);

// If the object is unmanaged, the RealmValue just wraps the collection
Assert.That(rv == originalList, isManaged ? Is.False : Is.True);
Assert.That(rv.Equals(originalList), isManaged ? Is.False : Is.True);
}

[Test]
Expand Down Expand Up @@ -577,7 +606,7 @@ public void Dictionary_WhenRetrieved_WorksWithAllTypes([Values(true, false)] boo
}

[Test]
public void Dictionary_InRealmValue_NotEqualToAnything([Values(true, false)] bool isManaged)
public void Dictionary_InRealmValue_Equality([Values(true, false)] bool isManaged)
{
var innerList = ListGenerator(1);
var innerDict = DictGenerator(1);
Expand All @@ -589,15 +618,33 @@ public void Dictionary_InRealmValue_NotEqualToAnything([Values(true, false)] boo
{ "e", innerDict },
};

var copyOriginalDict = new Dictionary<string, RealmValue>
{
{ "c", new InternalObject { IntProperty = 10, StringProperty = "brown" } },
{ "d", innerList },
{ "e", innerDict },
};

RealmValue rv = originalDict;
RealmValue rv2 = copyOriginalDict;

if (isManaged)
{
rv = PersistAndFind(rv).RealmValueProperty;
}

Assert.That(rv == originalDict, Is.False);
Assert.That(rv.Equals(originalDict), Is.False);
#pragma warning disable CS1718 // Comparison made to same variable
Assert.That(rv == rv, Is.True);
#pragma warning restore CS1718 // Comparison made to same variable
Assert.That(rv.Equals(rv), Is.True);

// They contains the same values, but the collections do not point to the same object reference
Assert.That(rv == rv2, Is.False);
Assert.That(rv.Equals(rv2), Is.False);

// If the object is unmanaged, the RealmValue just wraps the collection
Assert.That(rv == originalDict, isManaged ? Is.False : Is.True);
Assert.That(rv.Equals(originalDict), isManaged ? Is.False : Is.True);
}

[Test]
Expand Down

0 comments on commit ea60689

Please sign in to comment.