BirdTest.cs
5.53 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
197
198
199
200
201
202
203
204
205
206
207
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;
}
[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 _Down_Fire1_Button_to_Bird_Speed_Not_Negative()
{
var unityInput = Substitute.For<IUnityInputService>();
unityInput.GetButtonDown("Fire1").Returns(true);
bird.UnityInput = unityInput;
yield return null;
yield return new WaitForFixedUpdate();
Assert.GreaterOrEqual(bird.GetComponent<Rigidbody2D>().velocity.y,0);
}
[UnityTest]
public IEnumerator _Down_Fire1_After_2sec_to_Bird_Y_Speed_Not_Negative()
{
yield return new WaitForSeconds(2.0f);
var unityInput = Substitute.For<IUnityInputService>();
unityInput.GetButtonDown("Fire1").Returns(true);
bird.UnityInput = unityInput;
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 gameContorl = new GameObject().AddComponent<GameControl>();
yield return null;
var score = gameContorl.score;
var collider = new GameObject().AddComponent<BoxCollider2D>();
collider.tag = "Column";
bird.SendMessage("OnTriggerEnter2D",collider);
yield return null;
Assert.Greater(gameContorl.score,score);
}
[UnityTest]
public IEnumerator _Bird_Trigger_Collide_With_Not_Column_Tag_to_Not_Change_Score()
{
var gameContorl = new GameObject().AddComponent<GameControl>();
yield return null;
var score = gameContorl.score;
bird.SendMessage("OnTriggerEnter2D",new Collider2D());
yield return null;
Assert.AreEqual(gameContorl.score,score);
}
}
public class GameControlTest
{
private GameControl gameControl;
[SetUp]
public void BeforeEveryTest()
{
if (GameControl.instance != null)
{
GameObject.Destroy(GameControl.instance.gameObject);
}
gameControl = new GameObject("GameControl").AddComponent<GameControl>();
gameControl.scoreText = new GameObject().AddComponent<Text>();
gameControl.gameOvertext = new GameObject();
}
[Test]
public void _Bird_Scored_then_Displayd_Text_Contain_Updated_Score()
{
gameControl.BirdScored();
Assert.IsTrue(gameControl.scoreText.text.Contains(gameControl.score.ToString()));
}
[UnityTest]
public IEnumerator _Set_Control_ScroollSpeed_0_then_Scrolling_Object_Move_0_Horizontal_Distance()
{
var scrollingObject = new GameObject().AddComponent<ScrollingObject>();
gameControl.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.gameOver = true;
yield return null;
yield return new WaitForFixedUpdate();
Assert.AreEqual(Vector2.zero, scrollingObject.GetComponent<Rigidbody2D>().velocity);
}
}
}