Skip to content

Instantly share code, notes, and snippets.

@williamhammond
Created April 17, 2022 20:08
Show Gist options
  • Save williamhammond/857457a66d336eeae6287d4074fe0184 to your computer and use it in GitHub Desktop.
Save williamhammond/857457a66d336eeae6287d4074fe0184 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using Gameplay;
using NSubstitute;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;
using Object = UnityEngine.Object;
namespace Tests.Gameplay
{
public class PlayerTest
{
[UnityTest]
public IEnumerator PlayerMovesRight()
{
RenderSettings.skybox = null;
var root = new GameObject();
root = Object.Instantiate(root);
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/Player.prefab");
prefab = Object.Instantiate(prefab, new Vector2(0, 0), new Quaternion(0, 0, 0, 0));
if (!prefab)
{
throw new InvalidOperationException("Player prefab unable to instantiated");
}
var player = prefab.GetComponent<Player>();
player.PlayerInput = Substitute.For<IPlayerInput>();
player.PlayerInput.Horizontal.Returns(10f);
yield return WaitForNFrames(30);
Assert.IsTrue(player.transform.position.x > 0);
Object.Destroy(player);
Object.Destroy(root);
}
[UnityTest]
public IEnumerator PlayerMovesLeft()
{
RenderSettings.skybox = null;
var root = new GameObject();
root = Object.Instantiate(root);
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/Player.prefab");
prefab = Object.Instantiate(prefab, new Vector2(0, 0), new Quaternion(0, 0, 0, 0));
if (!prefab)
{
throw new InvalidOperationException("Player prefab unable to instantiated");
}
var player = prefab.GetComponent<Player>();
player.PlayerInput = Substitute.For<IPlayerInput>();
player.PlayerInput.Horizontal.Returns(-10f);
yield return WaitForNFrames(30);
Assert.IsTrue(player.transform.position.x < 0);
Object.Destroy(player);
Object.Destroy(root);
}
[UnityTest]
public IEnumerator PlayerJumps()
{
RenderSettings.skybox = null;
var root = new GameObject();
root = Object.Instantiate(root);
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/Player.prefab");
prefab = Object.Instantiate(prefab, new Vector2(0, 0), new Quaternion(0, 0, 0, 0));
if (!prefab)
{
throw new InvalidOperationException("Player prefab unable to instantiated");
}
var player = prefab.GetComponent<Player>();
player.PlayerInput = Substitute.For<IPlayerInput>();
player.PlayerInput.IsJumping.Returns(true);
yield return WaitForNFrames(60);
player.PlayerInput.IsJumping.Returns(false);
yield return WaitForNFrames(30);
Assert.IsTrue(player.transform.position.y > 0);
Object.Destroy(player);
Object.Destroy(root);
}
private IEnumerator WaitForNFrames(int n)
{
for (int i = 0; i < n; i++)
{
yield return new WaitForEndOfFrame();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment