Skip to content

Commit 350b9a2

Browse files
authored
test: add NetworkObject and NetworkBehaviour EditorTests (#607)
1 parent 91339ec commit 350b9a2

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using NUnit.Framework;
3+
using UnityEngine;
4+
using Object = UnityEngine.Object;
5+
6+
namespace MLAPI.EditorTests
7+
{
8+
public class NetworkBehaviourTests
9+
{
10+
[Test]
11+
public void HasNetworkObjectTest()
12+
{
13+
var gameObject = new GameObject(nameof(HasNetworkObjectTest));
14+
var networkBehaviour = gameObject.AddComponent<EmptyNetworkBehaviour>();
15+
16+
Assert.That(networkBehaviour.HasNetworkObject, Is.False);
17+
18+
var networkObject = gameObject.AddComponent<NetworkObject>();
19+
20+
Assert.That(networkBehaviour.HasNetworkObject, Is.True);
21+
22+
Object.DestroyImmediate(networkObject);
23+
24+
Assert.That(networkBehaviour.HasNetworkObject, Is.False);
25+
26+
// Cleanup
27+
Object.DestroyImmediate(gameObject);
28+
}
29+
30+
[Test]
31+
public void AccessNetworkObjectTest()
32+
{
33+
var gameObject = new GameObject(nameof(AccessNetworkObjectTest));
34+
var networkBehaviour = gameObject.AddComponent<EmptyNetworkBehaviour>();
35+
36+
// TODO: Do we really want to throw here?
37+
// Future API change: return null
38+
Assert.Throws<NullReferenceException>(() =>
39+
{
40+
var x = networkBehaviour.NetworkObject;
41+
});
42+
43+
var networkObject = gameObject.AddComponent<NetworkObject>();
44+
45+
Assert.That(networkBehaviour.NetworkObject, Is.EqualTo(networkObject));
46+
47+
Object.DestroyImmediate(networkObject);
48+
49+
// TODO: Do we really want to throw here?
50+
// Future API change: return null
51+
Assert.Throws<NullReferenceException>(() =>
52+
{
53+
var x = networkBehaviour.NetworkObject;
54+
});
55+
56+
// Cleanup
57+
Object.DestroyImmediate(gameObject);
58+
}
59+
60+
public class EmptyNetworkBehaviour : NetworkBehaviour
61+
{
62+
63+
}
64+
}
65+
}

com.unity.multiplayer.mlapi/Tests/Editor/NetworkBehaviourTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using NUnit.Framework;
2+
using UnityEngine;
3+
using UnityEngine.TestTools;
4+
5+
namespace MLAPI.EditorTests
6+
{
7+
public class NetworkObjectTests
8+
{
9+
[Test]
10+
public void GetBehaviourIndexNone()
11+
{
12+
var gameObject = new GameObject(nameof(GetBehaviourIndexNone));
13+
var networkObject = gameObject.AddComponent<NetworkObject>();
14+
15+
// TODO: Maybe not hardcode message?
16+
LogAssert.Expect(LogType.Error, $"[MLAPI] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
17+
LogAssert.Expect(LogType.Error, $"[MLAPI] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
18+
LogAssert.Expect(LogType.Error, $"[MLAPI] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
19+
20+
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(0), Is.Null);
21+
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(1), Is.Null);
22+
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(2), Is.Null);
23+
24+
// Cleanup
25+
Object.DestroyImmediate(gameObject);
26+
}
27+
28+
[Test]
29+
public void GetBehaviourIndexOne()
30+
{
31+
var gameObject = new GameObject(nameof(GetBehaviourIndexOne));
32+
var networkObject = gameObject.AddComponent<NetworkObject>();
33+
var networkBehaviour = gameObject.AddComponent<EmptyNetworkBehaviour>();
34+
35+
// TODO: Maybe not hardcode message?
36+
LogAssert.Expect(LogType.Error, $"[MLAPI] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
37+
LogAssert.Expect(LogType.Error, $"[MLAPI] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
38+
39+
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(0), Is.EqualTo(networkBehaviour));
40+
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(1), Is.Null);
41+
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(2), Is.Null);
42+
43+
// Cleanup
44+
Object.DestroyImmediate(gameObject);
45+
}
46+
47+
public class EmptyNetworkBehaviour : NetworkBehaviour
48+
{
49+
50+
}
51+
}
52+
}

com.unity.multiplayer.mlapi/Tests/Editor/NetworkObjectTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)