2022-05-13 08:54:00 +01:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.TestTools;
|
|
|
|
|
|
|
|
|
|
namespace X10D.Unity.Tests
|
|
|
|
|
{
|
|
|
|
|
public class SingletonTests
|
|
|
|
|
{
|
2023-04-05 19:18:06 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void Singleton_ShouldReturnNewInstance_WhenNoInstanceExists()
|
2022-05-13 08:54:00 +01:00
|
|
|
|
{
|
|
|
|
|
TestBehaviour instance = Singleton<TestBehaviour>.Instance;
|
2023-04-05 19:18:06 +01:00
|
|
|
|
Assert.IsNotNull(instance);
|
2022-05-13 08:54:00 +01:00
|
|
|
|
Assert.IsTrue(instance.Flag);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 19:18:06 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void Singleton_ShouldReturnSameInstance_WhenAccessedTwice()
|
2022-05-13 08:54:00 +01:00
|
|
|
|
{
|
|
|
|
|
TestBehaviour instance = Singleton<TestBehaviour>.Instance;
|
2023-04-05 19:18:06 +01:00
|
|
|
|
Assert.IsNotNull(instance);
|
2022-05-13 08:54:00 +01:00
|
|
|
|
Assert.AreEqual(instance, Singleton<TestBehaviour>.Instance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnityTest]
|
|
|
|
|
public IEnumerator Singleton_ShouldReturnNewInstance_WhenDestroyed()
|
|
|
|
|
{
|
|
|
|
|
TestBehaviour instance = Singleton<TestBehaviour>.Instance;
|
2023-04-05 19:18:06 +01:00
|
|
|
|
Assert.IsNotNull(instance);
|
2022-05-13 08:54:00 +01:00
|
|
|
|
Object.Destroy(instance);
|
2023-04-05 19:18:06 +01:00
|
|
|
|
|
2022-05-13 08:54:00 +01:00
|
|
|
|
yield return null;
|
2023-04-05 19:18:06 +01:00
|
|
|
|
|
2022-05-13 08:54:00 +01:00
|
|
|
|
Assert.IsFalse(instance);
|
|
|
|
|
|
|
|
|
|
// ReSharper disable once HeuristicUnreachableCode
|
|
|
|
|
instance = Singleton<TestBehaviour>.Instance;
|
|
|
|
|
Assert.IsNotNull(instance);
|
|
|
|
|
Assert.IsTrue(instance.Flag);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|