From 0e6661f36a306b8be5d704a522c764db97be24c0 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Sat, 23 Mar 2024 13:48:03 -0400 Subject: [PATCH] chore(:SyncListTest): Improved tests --- .../Editor/SyncCollections/SyncListTest.cs | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/Assets/Mirror/Tests/Editor/SyncCollections/SyncListTest.cs b/Assets/Mirror/Tests/Editor/SyncCollections/SyncListTest.cs index 7596f1897..0516981e4 100644 --- a/Assets/Mirror/Tests/Editor/SyncCollections/SyncListTest.cs +++ b/Assets/Mirror/Tests/Editor/SyncCollections/SyncListTest.cs @@ -115,9 +115,20 @@ public void TestClear() [Test] public void TestInsert() { + bool called = false; + clientSyncList.Callback = (op, index, oldItem, newItem) => + { + called = true; + + Assert.That(op, Is.EqualTo(SyncList.Operation.OP_INSERT)); + Assert.That(index, Is.EqualTo(0)); + Assert.That(newItem, Is.EqualTo("yay")); + }; + serverSyncList.Insert(0, "yay"); SerializeDeltaTo(serverSyncList, clientSyncList); Assert.That(clientSyncList, Is.EquivalentTo(new[] { "yay", "Hello", "World", "!" })); + Assert.That(called, Is.True); } [Test] @@ -131,19 +142,47 @@ public void TestInsertRange() [Test] public void TestSet() { + bool called = false; + clientSyncList.Callback = (op, index, oldItem, newItem) => + { + called = true; + + Assert.That(op, Is.EqualTo(SyncList.Operation.OP_SET)); + Assert.That(index, Is.EqualTo(1)); + Assert.That(oldItem, Is.EqualTo("World")); + Assert.That(newItem, Is.EqualTo("yay")); + }; + serverSyncList[1] = "yay"; SerializeDeltaTo(serverSyncList, clientSyncList); Assert.That(clientSyncList[1], Is.EqualTo("yay")); Assert.That(clientSyncList, Is.EquivalentTo(new[] { "Hello", "yay", "!" })); + Assert.That(called, Is.True); } [Test] public void TestSetNull() { + bool called = false; + clientSyncList.Callback = (op, index, oldItem, newItem) => + { + called = true; + + Assert.That(op, Is.EqualTo(SyncList.Operation.OP_SET)); + Assert.That(index, Is.EqualTo(1)); + Assert.That(oldItem, Is.EqualTo("World")); + Assert.That(newItem, Is.EqualTo(null)); + }; + serverSyncList[1] = null; SerializeDeltaTo(serverSyncList, clientSyncList); Assert.That(clientSyncList[1], Is.EqualTo(null)); Assert.That(clientSyncList, Is.EquivalentTo(new[] { "Hello", null, "!" })); + Assert.That(called, Is.True); + + // clear callback so we don't get called again + clientSyncList.Callback = null; + serverSyncList[1] = "yay"; SerializeDeltaTo(serverSyncList, clientSyncList); Assert.That(clientSyncList, Is.EquivalentTo(new[] { "Hello", "yay", "!" })); @@ -152,9 +191,22 @@ public void TestSetNull() [Test] public void TestRemoveAll() { + bool called = false; + clientSyncList.Callback = (op, index, oldItem, newItem) => + { + called = true; + + Assert.That(op, Is.EqualTo(SyncList.Operation.OP_REMOVEAT)); + Assert.That(index, Is.EqualTo(0)); + Assert.That(oldItem, Is.Not.EqualTo("!")); + Assert.That(newItem, Is.EqualTo(default(string))); + }; + + // This will remove "Hello" and "World" serverSyncList.RemoveAll(entry => entry.Contains("l")); SerializeDeltaTo(serverSyncList, clientSyncList); Assert.That(clientSyncList, Is.EquivalentTo(new[] { "!" })); + Assert.That(called, Is.True); } [Test] @@ -168,17 +220,41 @@ public void TestRemoveAllNone() [Test] public void TestRemoveAt() { + bool called = false; + clientSyncList.Callback = (op, index, oldItem, newItem) => + { + called = true; + + Assert.That(op, Is.EqualTo(SyncList.Operation.OP_REMOVEAT)); + Assert.That(index, Is.EqualTo(1)); + Assert.That(oldItem, Is.EqualTo("World")); + Assert.That(newItem, Is.EqualTo(default(string))); + }; + serverSyncList.RemoveAt(1); SerializeDeltaTo(serverSyncList, clientSyncList); Assert.That(clientSyncList, Is.EquivalentTo(new[] { "Hello", "!" })); + Assert.That(called, Is.True); } [Test] public void TestRemove() { + bool called = false; + clientSyncList.Callback = (op, index, oldItem, newItem) => + { + called = true; + + Assert.That(op, Is.EqualTo(SyncList.Operation.OP_REMOVEAT)); + Assert.That(index, Is.EqualTo(1)); + Assert.That(oldItem, Is.EqualTo("World")); + Assert.That(newItem, Is.EqualTo(default(string))); + }; + serverSyncList.Remove("World"); SerializeDeltaTo(serverSyncList, clientSyncList); Assert.That(clientSyncList, Is.EquivalentTo(new[] { "Hello", "!" })); + Assert.That(called, Is.True); } [Test]