BirdTest.cs
5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using UnityEngine;
using NUnit.Framework;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.TestTools;
using NSubstitute;
using UnityEditor.Animations;
using UnityEngine.UI;
namespace FlappyBird.PlayModeTest
{
public class BirdTest
{
private Bird bird;
[SetUp]
public void BeforeEveryTest()
{
var instance = new GameObject("Bird");
instance.AddComponent<Rigidbody2D>();
instance.AddComponent<Animator>();
bird = instance.AddComponent<Bird>();
bird.upForce = 500f;
GameControl.Instance.Reset();
}
[UnityTest]
public IEnumerator _Bird_Jump_to_Play_Flap_Animation()
{
var animatorController = Resources.Load<AnimatorController>("Bird");
var animator = bird.GetComponent<Animator>();
animator.runtimeAnimatorController = animatorController;
bird.Jump();
yield return null;
Assert.IsTrue(animator.GetCurrentAnimatorStateInfo(0).IsName("Idle"));
}
[Test]
public void _Bird_Collide_to_Bird_Died()
{
bird.SendMessage("OnCollisionEnter2D", new Collision2D());
Assert.AreEqual(true, bird.IsDead);
}
[UnityTest]
public IEnumerator _Down_Fire1_Button_to_Bird_Jump_Up()
{
float posY = bird.transform.position.y;
var unityInput = Substitute.For<IUnityInputService>();
unityInput.GetButtonDown("Fire1").Returns(true);
bird.UnityInput = unityInput;
yield return null;
yield return new WaitForFixedUpdate();
Assert.Greater(bird.transform.position.y,posY);
}
[UnityTest]
public IEnumerator _Bird_Jump_to_Bird_Speed_Not_Negative()
{
bird.Jump();
yield return null;
yield return new WaitForFixedUpdate();
Assert.GreaterOrEqual(bird.GetComponent<Rigidbody2D>().velocity.y,0);
}
[UnityTest]
public IEnumerator _Jump_After_2sec_to_Bird_Y_Speed_Not_Negative()
{
yield return new WaitForSeconds(2.0f);
bird.Jump();
yield return null;
yield return new WaitForFixedUpdate();
Assert.GreaterOrEqual(bird.GetComponent<Rigidbody2D>().velocity.y,0);
}
[UnityTest]
public IEnumerator _Bird_Trigger_Collide_With_Column_Tag_to_Add_Score()
{
var score = GameControl.Instance.score;
var collider = new GameObject().AddComponent<BoxCollider2D>();
collider.tag = "Column";
bird.SendMessage("OnTriggerEnter2D",collider);
yield return null;
Assert.AreEqual(score + 1,GameControl.Instance.score);
}
[UnityTest]
public IEnumerator _Bird_Trigger_Collide_With_Not_Column_Tag_to_Not_Change_Score()
{
var score = GameControl.Instance.score;
var collider = new GameObject().AddComponent<BoxCollider2D>();
yield return null;
bird.SendMessage("OnTriggerEnter2D",collider);
yield return null;
Assert.AreEqual(GameControl.Instance.score,score);
}
}
public class GameControlTest
{
[SetUp]
public void BeforeEveryTest()
{
GameControl.Instance.scoreText = new GameObject("Score Text").AddComponent<Text>();
GameControl.Instance.gameOvertext = new GameObject("Game Over Text");
GameControl.Instance.Reset();
}
[Test]
public void _Bird_Scored_then_Displayd_Text_Contain_Updated_Score()
{
GameControl.Instance.BirdScored();
Assert.IsTrue(GameControl.Instance.scoreText.text.Contains(GameControl.Instance.score.ToString()));
}
[UnityTest]
public IEnumerator _Set_Control_ScroollSpeed_0_then_Scrolling_Object_Move_0_Horizontal_Distance()
{
var scrollingObject = new GameObject().AddComponent<ScrollingObject>();
GameControl.Instance.scrollSpeed = 0f;
yield return null;
yield return new WaitForFixedUpdate();
Assert.AreEqual(0f, scrollingObject.GetComponent<Rigidbody2D>().velocity.x);
}
[UnityTest]
public IEnumerator _Set_GameOver_True_then_Scrolling_Object_Move_0_Distance()
{
var scrollingObject = new GameObject().AddComponent<ScrollingObject>();
GameControl.Instance.gameOver = true;
yield return null;
yield return new WaitForFixedUpdate();
Assert.AreEqual(Vector2.zero, scrollingObject.GetComponent<Rigidbody2D>().velocity);
}
}
}