Skip to content

Commit 8e44047

Browse files
committed
Add integration test examples
1 parent 74dfcd0 commit 8e44047

File tree

143 files changed

+12122
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+12122
-0
lines changed

Assets/InputExample.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/InputExample/Scripts.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/InputExample/Scripts/Runtime.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2021-2023 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
[assembly: InternalsVisibleTo("InputExample.Tests")]

Assets/InputExample/Scripts/Runtime/AssemblyInfo.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) 2021-2023 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
using TestHelper.Input;
5+
using UnityEngine;
6+
7+
namespace InputExample
8+
{
9+
[RequireComponent(typeof(CharacterController))]
10+
public class FirstPersonControllerLegacy : MonoBehaviour
11+
{
12+
private CharacterController _controller;
13+
14+
[SerializeField]
15+
private float moveSpeed = 7.0f;
16+
17+
[SerializeField]
18+
private float jumpSpeed = 5.0f;
19+
20+
private const float Gravity = -9.81f;
21+
private float _yVelocity = 0f;
22+
23+
// UnityEngine.Inputの代わりにInputWrapperを使用する
24+
internal IInput Input { private get; set; } = new InputWrapper();
25+
26+
private void Awake()
27+
{
28+
_controller = GetComponent<CharacterController>();
29+
}
30+
31+
private void Update()
32+
{
33+
Move();
34+
Look();
35+
Fall();
36+
Jump();
37+
}
38+
39+
private void Move()
40+
{
41+
var move = Vector3.zero;
42+
43+
if (Input.GetKey(KeyCode.W))
44+
{
45+
move = Vector3.forward;
46+
}
47+
48+
if (Input.GetKey(KeyCode.A))
49+
{
50+
move = Vector3.left;
51+
}
52+
53+
if (Input.GetKey(KeyCode.S))
54+
{
55+
move = Vector3.back;
56+
}
57+
58+
if (Input.GetKey(KeyCode.D))
59+
{
60+
move = Vector3.right;
61+
}
62+
63+
var direction = Quaternion.Euler(0f, transform.eulerAngles.y, 0f);
64+
_controller.Move(direction * move * (moveSpeed * Time.deltaTime));
65+
}
66+
67+
private void Look()
68+
{
69+
var input = Input.GetAxis("Mouse X");
70+
transform.Rotate(0f, input, 0f);
71+
}
72+
73+
private void Fall()
74+
{
75+
_yVelocity += Gravity * Time.deltaTime;
76+
var collisionFlags = _controller.Move(new Vector3(0f, _yVelocity * Time.deltaTime, 0f));
77+
if (collisionFlags == CollisionFlags.Below)
78+
{
79+
_yVelocity = 0f;
80+
}
81+
}
82+
83+
private void Jump()
84+
{
85+
if (Input.GetKey(KeyCode.Space) && _yVelocity == 0f)
86+
{
87+
_yVelocity += jumpSpeed;
88+
}
89+
}
90+
}
91+
}

Assets/InputExample/Scripts/Runtime/FirstPersonControllerLegacy.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "InputExample",
3+
"rootNamespace": "",
4+
"references": [
5+
"TestHelper.Input"
6+
],
7+
"includePlatforms": [],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": true,
13+
"defineConstraints": [],
14+
"versionDefines": [],
15+
"noEngineReferences": false
16+
}

Assets/InputExample/Scripts/Runtime/InputExample.asmdef.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/InputExample/Tests.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/InputExample/Tests/Runtime.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2021-2023 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
using NUnit.Framework;
5+
6+
[assembly: Category("Integration")]

Assets/InputExample/Tests/Runtime/AssemblyInfo.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2021-2023 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Cysharp.Threading.Tasks;
7+
using InputExample.TestDoubles;
8+
using NUnit.Framework;
9+
using UnityEngine;
10+
using UnityEngine.SceneManagement;
11+
using UnityEngine.TestTools;
12+
using UnityEngine.TestTools.Utils;
13+
using Object = UnityEngine.Object;
14+
#if UNITY_EDITOR
15+
using UnityEditor.SceneManagement;
16+
#endif
17+
18+
// ReSharper disable Unity.InefficientPropertyAccess
19+
20+
namespace InputExample
21+
{
22+
[TestFixture]
23+
[UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
24+
public class FirstPersonControllerLegacyTest
25+
{
26+
private const string SandboxScenePath = "Assets/InputExample/Tests/Scenes/InputExampleSandbox.unity";
27+
28+
private FirstPersonControllerLegacy _controller;
29+
30+
[SetUp]
31+
public async Task SetUp()
32+
{
33+
#if UNITY_EDITOR
34+
await EditorSceneManager.LoadSceneAsyncInPlayMode(
35+
SandboxScenePath,
36+
new LoadSceneParameters(LoadSceneMode.Single));
37+
// Note: Scenes in Buildに入れていないSceneなので、EditorSceneManagerでロード
38+
#endif
39+
_controller = Object.FindObjectOfType<FirstPersonControllerLegacy>();
40+
}
41+
42+
[Test]
43+
public async Task Wキーで前方に移動()
44+
{
45+
var beforePosition = _controller.transform.position;
46+
Assume.That(beforePosition, Is.EqualTo(Vector3.zero)
47+
.Using(new Vector3EqualityComparer(0.1f))); // 初期位置は原点
48+
49+
var stub = new StubInputKey();
50+
_controller.Input = stub; // テストスタブを注入
51+
52+
stub.PushedKeys = new[] { KeyCode.W }; // Wキーを押す
53+
await Task.Delay(500); // 0.5秒間保持
54+
stub.PushedKeys = Array.Empty<KeyCode>(); // 離す
55+
56+
var afterPosition = _controller.transform.position;
57+
Assert.That(afterPosition, Is.EqualTo(new Vector3(0f, 0f, 4f))
58+
.Using(new Vector3EqualityComparer(0.3f))); // Z+方向に約4単位移動
59+
}
60+
61+
[Test]
62+
public async Task マウス右移動で右方向を向く()
63+
{
64+
var beforeRotation = _controller.transform.rotation;
65+
Assume.That(beforeRotation, Is.EqualTo(Quaternion.identity)); // 初期向きは正面
66+
67+
var stub = new StubInputAxis();
68+
_controller.Input = stub; // テストスタブを注入
69+
70+
stub.Axes = new[] { new SimulateAxis("Mouse X", 2.0f) }; // 右方向に移動
71+
await Task.Delay(500); // 0.5秒間保持
72+
stub.Axes = Array.Empty<SimulateAxis>(); // 止める
73+
74+
var afterRotation = _controller.transform.rotation;
75+
Assert.That(afterRotation, Is.EqualTo(Quaternion.Euler(0f, 45f, 0f))
76+
.Using(new QuaternionEqualityComparer(0.05f))); // Y軸で右に約45度回転
77+
}
78+
}
79+
}

Assets/InputExample/Tests/Runtime/FirstPersonControllerLegacyTest.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "InputExample.Tests",
3+
"rootNamespace": "",
4+
"references": [
5+
"UnityEngine.TestRunner",
6+
"UnityEditor.TestRunner",
7+
"InputExample",
8+
"TestHelper.Input",
9+
"TestHelper",
10+
"UniTask"
11+
],
12+
"includePlatforms": [],
13+
"excludePlatforms": [],
14+
"allowUnsafeCode": false,
15+
"overrideReferences": true,
16+
"precompiledReferences": [
17+
"nunit.framework.dll"
18+
],
19+
"autoReferenced": false,
20+
"defineConstraints": [
21+
"UNITY_INCLUDE_TESTS"
22+
],
23+
"versionDefines": [],
24+
"noEngineReferences": false
25+
}

Assets/InputExample/Tests/Runtime/InputExample.Tests.asmdef.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/InputExample/Tests/Runtime/TestDoubles.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2021-2023 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
using System;
5+
using System.Linq;
6+
using TestHelper.Input;
7+
8+
namespace InputExample.TestDoubles
9+
{
10+
public struct SimulateAxis
11+
{
12+
public string Name { get; private set; }
13+
public float Value { get; private set; }
14+
15+
public SimulateAxis(string name, float value)
16+
{
17+
Name = name;
18+
Value = value;
19+
}
20+
}
21+
22+
public class StubInputAxis : InputWrapper
23+
{
24+
public SimulateAxis[] Axes { get; set; } = Array.Empty<SimulateAxis>();
25+
26+
public override float GetAxis(string axisName)
27+
{
28+
return Axes.FirstOrDefault(a => a.Name == axisName).Value;
29+
}
30+
}
31+
}

Assets/InputExample/Tests/Runtime/TestDoubles/StubInputAxis.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2021-2023 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
using System;
5+
using System.Linq;
6+
using TestHelper.Input;
7+
using UnityEngine;
8+
9+
namespace InputExample.TestDoubles
10+
{
11+
public class StubInputKey : InputWrapper
12+
{
13+
public KeyCode[] PushedKeys { get; set; } = Array.Empty<KeyCode>();
14+
15+
public override bool GetKey(KeyCode key)
16+
{
17+
return PushedKeys.Contains(key);
18+
}
19+
}
20+
}

Assets/InputExample/Tests/Runtime/TestDoubles/StubInputKey.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/InputExample/Tests/Scenes.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)