Tests for SyncLists that use structs (#1583)

This commit is contained in:
James Frowen 2020-03-22 22:05:50 +00:00 committed by GitHub
parent d040928948
commit c7c3ee4ab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 2 deletions

View File

@ -0,0 +1,66 @@
using NUnit.Framework;
namespace Mirror.Tests
{
public class SyncListStructTest
{
[Test]
public void ListIsDirtyWhenModifingAndSettingStruct()
{
SyncListTestPlayer serverList = new SyncListTestPlayer();
SyncListTestPlayer clientList = new SyncListTestPlayer();
SyncListTest.SerializeAllTo(serverList, clientList);
serverList.Add(new TestPlayer { item = new TestItem { price = 10 } });
SyncListTest.SerializeDeltaTo(serverList, clientList);
Assert.That(serverList.IsDirty, Is.False);
TestPlayer player = serverList[0];
player.item.price = 15;
serverList[0] = player;
Assert.That(serverList.IsDirty, Is.True);
}
[Test]
public void OldValueShouldNotBeNewValue()
{
SyncListTestPlayer serverList = new SyncListTestPlayer();
SyncListTestPlayer clientList = new SyncListTestPlayer();
SyncListTest.SerializeAllTo(serverList, clientList);
serverList.Add(new TestPlayer { item = new TestItem { price = 10 } });
SyncListTest.SerializeDeltaTo(serverList, clientList);
TestPlayer player = serverList[0];
player.item.price = 15;
serverList[0] = player;
bool callbackCalled = false;
clientList.Callback += (SyncList<TestPlayer>.Operation op, int itemIndex, TestPlayer oldItem, TestPlayer newItem) =>
{
Assert.That(op == SyncList<TestPlayer>.Operation.OP_SET, Is.True);
Assert.That(itemIndex, Is.EqualTo(0));
Assert.That(oldItem.item.price, Is.EqualTo(10));
Assert.That(newItem.item.price, Is.EqualTo(15));
callbackCalled = true;
};
SyncListTest.SerializeDeltaTo(serverList, clientList);
Assert.IsTrue(callbackCalled);
}
}
public class SyncListTestPlayer : SyncList<TestPlayer>
{
}
public struct TestPlayer
{
public TestItem item;
}
public struct TestItem
{
public float price;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1cad81f2aee838a4ababa9c8ee23a700
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -9,7 +9,7 @@ public class SyncListTest
SyncListString serverSyncList;
SyncListString clientSyncList;
void SerializeAllTo<T>(T fromList, T toList) where T : SyncObject
public static void SerializeAllTo<T>(T fromList, T toList) where T : SyncObject
{
NetworkWriter writer = new NetworkWriter();
fromList.OnSerializeAll(writer);
@ -17,7 +17,7 @@ void SerializeAllTo<T>(T fromList, T toList) where T : SyncObject
toList.OnDeserializeAll(reader);
}
void SerializeDeltaTo<T>(T fromList, T toList) where T : SyncObject
public static void SerializeDeltaTo<T>(T fromList, T toList) where T : SyncObject
{
NetworkWriter writer = new NetworkWriter();
fromList.OnSerializeDelta(writer);