2023-08-26 18:15:04 +01:00
|
|
|
using System.Collections;
|
2022-05-13 08:54:00 +01:00
|
|
|
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 22:51:04 +01:00
|
|
|
Assert.That(instance, Is.Not.Null);
|
|
|
|
Assert.That(instance.Flag);
|
2022-05-13 08:54:00 +01:00
|
|
|
}
|
|
|
|
|
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 22:51:04 +01:00
|
|
|
Assert.That(instance, Is.Not.Null);
|
|
|
|
Assert.That(Singleton<TestBehaviour>.Instance, Is.EqualTo(instance));
|
2022-05-13 08:54:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[UnityTest]
|
|
|
|
public IEnumerator Singleton_ShouldReturnNewInstance_WhenDestroyed()
|
|
|
|
{
|
|
|
|
TestBehaviour instance = Singleton<TestBehaviour>.Instance;
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(instance, Is.Not.Null);
|
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;
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(instance, Is.Not.Null);
|
2022-05-13 08:54:00 +01:00
|
|
|
Assert.IsTrue(instance.Flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|