NetworkServerTests: use call counts instead of bools to make sure that it's called exactly once

This commit is contained in:
vis2k 2020-02-25 11:00:35 +01:00
parent 69bb1574f0
commit a497803de0

View File

@ -8,19 +8,21 @@ namespace Mirror.Tests
{ {
public class CommandTestNetworkBehaviour : NetworkBehaviour public class CommandTestNetworkBehaviour : NetworkBehaviour
{ {
public bool commandCalled; // counter to make sure that it's called exactly once
public int called;
// weaver generates this from [Command] // weaver generates this from [Command]
// but for tests we need to add it manually // but for tests we need to add it manually
public static void CommandGenerated(NetworkBehaviour comp, NetworkReader reader) public static void CommandGenerated(NetworkBehaviour comp, NetworkReader reader)
{ {
((CommandTestNetworkBehaviour)comp).commandCalled = true; ++((CommandTestNetworkBehaviour)comp).called;
} }
} }
public class OnStartClientTestNetworkBehaviour : NetworkBehaviour public class OnStartClientTestNetworkBehaviour : NetworkBehaviour
{ {
public bool onStartClientCalled; // counter to make sure that it's called exactly once
public override void OnStartClient() { onStartClientCalled = true; } public int called;
public override void OnStartClient() { ++called; }
} }
[TestFixture] [TestFixture]
@ -584,9 +586,9 @@ public void CommandMessageCallsCommandTest()
identity.netId = 42; identity.netId = 42;
identity.connectionToClient = connection; // for authority check identity.connectionToClient = connection; // for authority check
CommandTestNetworkBehaviour comp0 = go.AddComponent<CommandTestNetworkBehaviour>(); CommandTestNetworkBehaviour comp0 = go.AddComponent<CommandTestNetworkBehaviour>();
Assert.That(comp0.commandCalled, Is.False); Assert.That(comp0.called, Is.EqualTo(0));
CommandTestNetworkBehaviour comp1 = go.AddComponent<CommandTestNetworkBehaviour>(); CommandTestNetworkBehaviour comp1 = go.AddComponent<CommandTestNetworkBehaviour>();
Assert.That(comp1.commandCalled, Is.False); Assert.That(comp1.called, Is.EqualTo(0));
connection.identity = identity; connection.identity = identity;
// register the command delegate, otherwise it's not found // register the command delegate, otherwise it's not found
@ -613,11 +615,11 @@ public void CommandMessageCallsCommandTest()
Transport.activeTransport.OnServerDataReceived.Invoke(0, segment, 0); Transport.activeTransport.OnServerDataReceived.Invoke(0, segment, 0);
// was the command called in the first component, not in the second one? // was the command called in the first component, not in the second one?
Assert.That(comp0.commandCalled, Is.True); Assert.That(comp0.called, Is.EqualTo(1));
Assert.That(comp1.commandCalled, Is.False); Assert.That(comp1.called, Is.EqualTo(0));
// reset, then send another command for the second component // send another command for the second component
comp0.commandCalled = false; comp0.called = 0;
message.componentIndex = 1; message.componentIndex = 1;
writer = new NetworkWriter(); writer = new NetworkWriter();
MessagePacker.Pack(message, writer); MessagePacker.Pack(message, writer);
@ -625,8 +627,8 @@ public void CommandMessageCallsCommandTest()
Transport.activeTransport.OnServerDataReceived.Invoke(0, segment, 0); Transport.activeTransport.OnServerDataReceived.Invoke(0, segment, 0);
// was the command called in the second component, not in the first one? // was the command called in the second component, not in the first one?
Assert.That(comp0.commandCalled, Is.False); Assert.That(comp0.called, Is.EqualTo(0));
Assert.That(comp1.commandCalled, Is.True); Assert.That(comp1.called, Is.EqualTo(1));
// clean up // clean up
NetworkIdentity.spawned.Clear(); NetworkIdentity.spawned.Clear();
@ -648,7 +650,7 @@ public void ActivateHostSceneCallsOnStartClient()
identity.netId = 42; identity.netId = 42;
//identity.connectionToClient = connection; // for authority check //identity.connectionToClient = connection; // for authority check
OnStartClientTestNetworkBehaviour comp = go.AddComponent<OnStartClientTestNetworkBehaviour>(); OnStartClientTestNetworkBehaviour comp = go.AddComponent<OnStartClientTestNetworkBehaviour>();
Assert.That(comp.onStartClientCalled, Is.False); Assert.That(comp.called, Is.EqualTo(0));
//connection.identity = identity; //connection.identity = identity;
NetworkIdentity.spawned[identity.netId] = identity; NetworkIdentity.spawned[identity.netId] = identity;
@ -656,7 +658,7 @@ public void ActivateHostSceneCallsOnStartClient()
NetworkServer.ActivateHostScene(); NetworkServer.ActivateHostScene();
// was OnStartClient called for all .spawned networkidentities? // was OnStartClient called for all .spawned networkidentities?
Assert.That(comp.onStartClientCalled, Is.True); Assert.That(comp.called, Is.EqualTo(1));
// clean up // clean up
NetworkIdentity.spawned.Clear(); NetworkIdentity.spawned.Clear();