Tests for static syncvar hooks (#1912)

* tests for static hooks

* csproj
This commit is contained in:
James Frowen 2020-05-21 09:40:53 +01:00 committed by GitHub
parent 0b2dd5fd36
commit 1c2bb2c6a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 6 deletions

View File

@ -44,6 +44,19 @@ void OnValueChanged(NetworkIdentity oldValue, NetworkIdentity newValue)
}
}
class StaticHookBehaviour : NetworkBehaviour
{
[SyncVar(hook = nameof(OnValueChanged))]
public int value = 0;
public static event Action<int, int> HookCalled;
static void OnValueChanged(int oldValue, int newValue)
{
HookCalled.Invoke(oldValue, newValue);
}
}
class VirtualHookBase : NetworkBehaviour
{
[SyncVar(hook = nameof(OnValueChanged))]
@ -202,6 +215,32 @@ public void Hook_NotCalledWhenSyncingSameValue(bool intialState)
Assert.That(callCount, Is.EqualTo(0));
}
[Test]
[TestCase(true)]
[TestCase(false)]
public void StaticMethod_HookCalledWhenSyncingChangedValue(bool intialState)
{
StaticHookBehaviour serverObject = CreateObject<StaticHookBehaviour>();
StaticHookBehaviour clientObject = CreateObject<StaticHookBehaviour>();
const int clientValue = 10;
const int serverValue = 24;
serverObject.value = serverValue;
clientObject.value = clientValue;
int hookcallCount = 0;
StaticHookBehaviour.HookCalled += (oldValue, newValue) =>
{
hookcallCount++;
Assert.That(oldValue, Is.EqualTo(clientValue));
Assert.That(newValue, Is.EqualTo(serverValue));
};
bool written = SyncToClient(serverObject, clientObject, intialState);
Assert.IsTrue(written);
Assert.That(hookcallCount, Is.EqualTo(1));
}
[Test]
[TestCase(true)]

View File

@ -198,6 +198,7 @@
<Compile Include="WeaverSyncVarHookTests~\ErrorWhenNoHookWithCorrectParametersFound.cs" />
<Compile Include="WeaverSyncVarHookTests~\ErrorWhenNoHookFound.cs" />
<Compile Include="WeaverSyncVarHookTests~\FindsHookWhenOverMethodsWithSameNameExist.cs" />
<Compile Include="WeaverSyncVarHookTests~\FindsStaticHook.cs" />
<Compile Include="WeaverSyncVarTests~\SyncVarsCantBeArray.cs" />
<Compile Include="WeaverSyncVarTests~\SyncVarsDerivedNetworkBehaviour.cs" />
<Compile Include="WeaverSyncVarTests~\SyncVarsDifferentModule.cs" />

View File

@ -10,6 +10,18 @@ public void FindsPrivateHook()
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void FindsPublicHook()
{
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void FindsStaticHook()
{
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void FindsHookWithGameObjects()
{
@ -22,12 +34,6 @@ public void FindsHookWithNetworkIdentity()
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void FindsPublicHook()
{
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void FindsHookWhenOverMethodsWithSameNameExist()
{

View File

@ -0,0 +1,15 @@
using Mirror;
namespace WeaverSyncVarHookTests.FindsStaticHook
{
class FindsStaticHook : NetworkBehaviour
{
[SyncVar(hook = nameof(onChangeHealth))]
int health;
static void onChangeHealth(int oldValue, int newValue)
{
}
}
}