Add some component tests to reference unity project (#86)
* Add some component tests to reference unity project * Use action/setup-node@v2 to avoid problems with buildpull/90/head v2.0-alpha-2
parent
26807aaf05
commit
e1be8325cd
|
@ -0,0 +1,17 @@
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class SampleComponent : MonoBehaviour
|
||||||
|
{
|
||||||
|
public BasicCounter Counter;
|
||||||
|
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
Counter = new BasicCounter(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update is called once per frame
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
Counter.Increment();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7a5f63b9ea4b465194653c4d681faf42
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -0,0 +1,18 @@
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class TimerComponent : MonoBehaviour
|
||||||
|
{
|
||||||
|
public BasicCounter Counter = new BasicCounter();
|
||||||
|
public float Timer = 1f;
|
||||||
|
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
Timer -= Time.deltaTime;
|
||||||
|
|
||||||
|
if (Timer > 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Counter.Increment();
|
||||||
|
Timer = 1f;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b668d68a45bb48108ccda73269e3da7b
|
||||||
|
timeCreated: 1610056748
|
|
@ -2,7 +2,8 @@
|
||||||
"name": "PlayModeTests",
|
"name": "PlayModeTests",
|
||||||
"references": [
|
"references": [
|
||||||
"UnityEngine.TestRunner",
|
"UnityEngine.TestRunner",
|
||||||
"UnityEditor.TestRunner"
|
"UnityEditor.TestRunner",
|
||||||
|
"MyScripts"
|
||||||
],
|
],
|
||||||
"includePlatforms": [],
|
"includePlatforms": [],
|
||||||
"excludePlatforms": [],
|
"excludePlatforms": [],
|
||||||
|
@ -16,4 +17,4 @@
|
||||||
"UNITY_INCLUDE_TESTS"
|
"UNITY_INCLUDE_TESTS"
|
||||||
],
|
],
|
||||||
"versionDefines": []
|
"versionDefines": []
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
using System.Collections;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.TestTools;
|
||||||
|
|
||||||
|
namespace Tests
|
||||||
|
{
|
||||||
|
public class SampleComponentTest
|
||||||
|
{
|
||||||
|
private GameObject target;
|
||||||
|
private SampleComponent component;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
target = GameObject.Instantiate(new GameObject());
|
||||||
|
component = target.AddComponent<SampleComponent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator TestIncrementOnUpdateAfterNextFrame()
|
||||||
|
{
|
||||||
|
// Save the current value, since it was updated after component Start() method called
|
||||||
|
var count = component.Counter.Count;
|
||||||
|
|
||||||
|
// Skip frame and assert the new value
|
||||||
|
yield return null;
|
||||||
|
Assert.AreEqual(count + 1, component.Counter.Count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: c688799d17b14d35ad515bff9de8d12c
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -1,7 +1,5 @@
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using UnityEngine;
|
|
||||||
using UnityEngine.TestTools;
|
using UnityEngine.TestTools;
|
||||||
|
|
||||||
namespace Tests
|
namespace Tests
|
||||||
|
@ -12,8 +10,14 @@ namespace Tests
|
||||||
[Test]
|
[Test]
|
||||||
public void NewTestScriptSimplePasses()
|
public void NewTestScriptSimplePasses()
|
||||||
{
|
{
|
||||||
// Use the Assert class to test conditions
|
// Given
|
||||||
Assert.True(true);
|
var counter = new BasicCounter(0);
|
||||||
|
|
||||||
|
// When
|
||||||
|
counter.Increment();
|
||||||
|
|
||||||
|
// Then
|
||||||
|
Assert.AreEqual(1, counter.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
// A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use
|
// A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use
|
||||||
|
@ -21,9 +25,18 @@ namespace Tests
|
||||||
[UnityTest]
|
[UnityTest]
|
||||||
public IEnumerator NewTestScriptWithEnumeratorPasses()
|
public IEnumerator NewTestScriptWithEnumeratorPasses()
|
||||||
{
|
{
|
||||||
|
// Given
|
||||||
|
var counter = new BasicCounter(3);
|
||||||
|
|
||||||
// Use the Assert class to test conditions.
|
// Use the Assert class to test conditions.
|
||||||
// Use yield to skip a frame.
|
// Use yield to skip a frame.
|
||||||
yield return null;
|
yield return null;
|
||||||
|
|
||||||
|
// When
|
||||||
|
counter.Increment();
|
||||||
|
|
||||||
|
// Then
|
||||||
|
Assert.AreEqual(4, counter.Count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
using System.Collections;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.TestTools;
|
||||||
|
|
||||||
|
namespace Tests
|
||||||
|
{
|
||||||
|
public class TimerComponentTest
|
||||||
|
{
|
||||||
|
private GameObject target;
|
||||||
|
private TimerComponent component;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
target = GameObject.Instantiate(new GameObject());
|
||||||
|
component = target.AddComponent<TimerComponent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator TestIncrementAfterSomeTime()
|
||||||
|
{
|
||||||
|
// Save the current value, since it was updated after component Start() method called
|
||||||
|
var count = component.Counter.Count;
|
||||||
|
|
||||||
|
// Skip frame and assert the new value
|
||||||
|
yield return null;
|
||||||
|
Assert.AreEqual(count, component.Counter.Count);
|
||||||
|
|
||||||
|
yield return new WaitForSeconds(1.1f);
|
||||||
|
Assert.AreEqual(count + 1, component.Counter.Count);
|
||||||
|
|
||||||
|
yield return new WaitForSeconds(1.1f);
|
||||||
|
Assert.AreEqual(count + 2, component.Counter.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator TestTimeScaleIsAffectingIncrement()
|
||||||
|
{
|
||||||
|
// Save the current value, since it was updated after component Start() method called
|
||||||
|
var count = component.Counter.Count;
|
||||||
|
Time.timeScale = .5f;
|
||||||
|
|
||||||
|
// Skip frame and assert the new value
|
||||||
|
yield return null;
|
||||||
|
Assert.AreEqual(count, component.Counter.Count);
|
||||||
|
|
||||||
|
yield return WaitForRealSeconds(1.1f);
|
||||||
|
Assert.AreEqual(count, component.Counter.Count);
|
||||||
|
|
||||||
|
yield return WaitForRealSeconds(1.1f);
|
||||||
|
Assert.AreEqual(count + 1, component.Counter.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skipping time ignoring Time.scale
|
||||||
|
// https://answers.unity.com/questions/301868/yield-waitforseconds-outside-of-timescale.html
|
||||||
|
public static IEnumerator WaitForRealSeconds(float time)
|
||||||
|
{
|
||||||
|
float start = Time.realtimeSinceStartup;
|
||||||
|
while (Time.realtimeSinceStartup < start + time)
|
||||||
|
{
|
||||||
|
yield return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 818b22370d404398b87b47a922a435c9
|
||||||
|
timeCreated: 1610056889
|
Loading…
Reference in New Issue