|
32 | 32 | #define TEST_PATH_FOLLOW_2D_H |
33 | 33 |
|
34 | 34 | #include "scene/2d/path_2d.h" |
| 35 | +#include "scene/main/window.h" |
35 | 36 |
|
36 | 37 | #include "tests/test_macros.h" |
37 | 38 |
|
38 | 39 | namespace TestPathFollow2D { |
39 | 40 |
|
40 | | -TEST_CASE("[PathFollow2D] Sampling with progress ratio") { |
41 | | - const Ref<Curve2D> &curve = memnew(Curve2D()); |
| 41 | +bool is_equal_approx(const Vector2 &p_a, const Vector2 &p_b) { |
| 42 | + const real_t tolerance = 0.001; |
| 43 | + return Math::is_equal_approx(p_a.x, p_b.x, tolerance) && |
| 44 | + Math::is_equal_approx(p_a.y, p_b.y, tolerance); |
| 45 | +} |
| 46 | + |
| 47 | +TEST_CASE("[SceneTree][PathFollow2D] Sampling with progress ratio") { |
| 48 | + Ref<Curve2D> curve = memnew(Curve2D); |
| 49 | + curve->set_bake_interval(1); |
42 | 50 | curve->add_point(Vector2(0, 0)); |
43 | 51 | curve->add_point(Vector2(100, 0)); |
44 | 52 | curve->add_point(Vector2(100, 100)); |
45 | 53 | curve->add_point(Vector2(0, 100)); |
46 | 54 | curve->add_point(Vector2(0, 0)); |
47 | | - const Path2D *path = memnew(Path2D); |
| 55 | + Path2D *path = memnew(Path2D); |
48 | 56 | path->set_curve(curve); |
49 | | - const PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
| 57 | + PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
| 58 | + path_follow_2d->set_loop(false); |
50 | 59 | path->add_child(path_follow_2d); |
| 60 | + SceneTree::get_singleton()->get_root()->add_child(path); |
51 | 61 |
|
52 | 62 | path_follow_2d->set_progress_ratio(0); |
53 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 0))); |
| 63 | + CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin())); |
54 | 64 |
|
55 | 65 | path_follow_2d->set_progress_ratio(0.125); |
56 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 0))); |
| 66 | + CHECK(is_equal_approx(Vector2(50, 0), path_follow_2d->get_transform().get_origin())); |
57 | 67 |
|
58 | 68 | path_follow_2d->set_progress_ratio(0.25); |
59 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 0))); |
| 69 | + CHECK(is_equal_approx(Vector2(100, 0), path_follow_2d->get_transform().get_origin())); |
60 | 70 |
|
61 | 71 | path_follow_2d->set_progress_ratio(0.375); |
62 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 50))); |
| 72 | + CHECK(is_equal_approx(Vector2(100, 50), path_follow_2d->get_transform().get_origin())); |
63 | 73 |
|
64 | 74 | path_follow_2d->set_progress_ratio(0.5); |
65 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 100))); |
| 75 | + CHECK(is_equal_approx(Vector2(100, 100), path_follow_2d->get_transform().get_origin())); |
66 | 76 |
|
67 | 77 | path_follow_2d->set_progress_ratio(0.625); |
68 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 100))); |
| 78 | + CHECK(is_equal_approx(Vector2(50, 100), path_follow_2d->get_transform().get_origin())); |
69 | 79 |
|
70 | 80 | path_follow_2d->set_progress_ratio(0.75); |
71 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 100))); |
| 81 | + CHECK(is_equal_approx(Vector2(0, 100), path_follow_2d->get_transform().get_origin())); |
72 | 82 |
|
73 | 83 | path_follow_2d->set_progress_ratio(0.875); |
74 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 50))); |
| 84 | + CHECK(is_equal_approx(Vector2(0, 50), path_follow_2d->get_transform().get_origin())); |
75 | 85 |
|
76 | 86 | path_follow_2d->set_progress_ratio(1); |
77 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 0))); |
| 87 | + CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin())); |
78 | 88 |
|
79 | 89 | memdelete(path); |
80 | 90 | } |
81 | 91 |
|
82 | | -TEST_CASE("[PathFollow2D] Sampling with progress") { |
83 | | - const Ref<Curve2D> &curve = memnew(Curve2D()); |
| 92 | +TEST_CASE("[SceneTree][PathFollow2D] Sampling with progress") { |
| 93 | + Ref<Curve2D> curve = memnew(Curve2D); |
| 94 | + curve->set_bake_interval(1); |
84 | 95 | curve->add_point(Vector2(0, 0)); |
85 | 96 | curve->add_point(Vector2(100, 0)); |
86 | 97 | curve->add_point(Vector2(100, 100)); |
87 | 98 | curve->add_point(Vector2(0, 100)); |
88 | 99 | curve->add_point(Vector2(0, 0)); |
89 | | - const Path2D *path = memnew(Path2D); |
| 100 | + Path2D *path = memnew(Path2D); |
90 | 101 | path->set_curve(curve); |
91 | | - const PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
| 102 | + PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
| 103 | + path_follow_2d->set_loop(false); |
92 | 104 | path->add_child(path_follow_2d); |
| 105 | + SceneTree::get_singleton()->get_root()->add_child(path); |
93 | 106 |
|
94 | 107 | path_follow_2d->set_progress(0); |
95 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 0))); |
| 108 | + CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin())); |
96 | 109 |
|
97 | 110 | path_follow_2d->set_progress(50); |
98 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 0))); |
| 111 | + CHECK(is_equal_approx(Vector2(50, 0), path_follow_2d->get_transform().get_origin())); |
99 | 112 |
|
100 | 113 | path_follow_2d->set_progress(100); |
101 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 0))); |
| 114 | + CHECK(is_equal_approx(Vector2(100, 0), path_follow_2d->get_transform().get_origin())); |
102 | 115 |
|
103 | 116 | path_follow_2d->set_progress(150); |
104 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 50))); |
| 117 | + CHECK(is_equal_approx(Vector2(100, 50), path_follow_2d->get_transform().get_origin())); |
105 | 118 |
|
106 | 119 | path_follow_2d->set_progress(200); |
107 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 100))); |
| 120 | + CHECK(is_equal_approx(Vector2(100, 100), path_follow_2d->get_transform().get_origin())); |
108 | 121 |
|
109 | 122 | path_follow_2d->set_progress(250); |
110 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 100))); |
| 123 | + CHECK(is_equal_approx(Vector2(50, 100), path_follow_2d->get_transform().get_origin())); |
111 | 124 |
|
112 | 125 | path_follow_2d->set_progress(300); |
113 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 100))); |
| 126 | + CHECK(is_equal_approx(Vector2(0, 100), path_follow_2d->get_transform().get_origin())); |
114 | 127 |
|
115 | 128 | path_follow_2d->set_progress(350); |
116 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 50))); |
| 129 | + CHECK(is_equal_approx(Vector2(0, 50), path_follow_2d->get_transform().get_origin())); |
117 | 130 |
|
118 | 131 | path_follow_2d->set_progress(400); |
119 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 0))); |
| 132 | + CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin())); |
120 | 133 |
|
121 | 134 | memdelete(path); |
122 | 135 | } |
123 | 136 |
|
124 | | -TEST_CASE("[PathFollow2D] Removal of a point in curve") { |
125 | | - const Ref<Curve2D> &curve = memnew(Curve2D()); |
| 137 | +TEST_CASE("[SceneTree][PathFollow2D] Removal of a point in curve") { |
| 138 | + Ref<Curve2D> curve = memnew(Curve2D); |
126 | 139 | curve->add_point(Vector2(0, 0)); |
127 | 140 | curve->add_point(Vector2(100, 0)); |
128 | 141 | curve->add_point(Vector2(100, 100)); |
129 | | - const Path2D *path = memnew(Path2D); |
| 142 | + Path2D *path = memnew(Path2D); |
130 | 143 | path->set_curve(curve); |
131 | | - const PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
| 144 | + PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
132 | 145 | path->add_child(path_follow_2d); |
| 146 | + SceneTree::get_singleton()->get_root()->add_child(path); |
133 | 147 |
|
134 | 148 | path_follow_2d->set_progress_ratio(0.5); |
135 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 0))); |
| 149 | + CHECK(is_equal_approx(Vector2(100, 0), path_follow_2d->get_transform().get_origin())); |
136 | 150 |
|
137 | 151 | curve->remove_point(1); |
138 | 152 |
|
| 153 | + path_follow_2d->set_progress_ratio(0.5); |
139 | 154 | CHECK_MESSAGE( |
140 | | - path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 50)), |
| 155 | + is_equal_approx(Vector2(50, 50), path_follow_2d->get_transform().get_origin()), |
141 | 156 | "Path follow's position should be updated after removing a point from the curve"); |
142 | 157 |
|
143 | 158 | memdelete(path); |
144 | 159 | } |
145 | 160 |
|
146 | | -TEST_CASE("[PathFollow2D] Setting h_offset and v_offset") { |
147 | | - const Ref<Curve2D> &curve = memnew(Curve2D()); |
| 161 | +TEST_CASE("[SceneTree][PathFollow2D] Setting h_offset and v_offset") { |
| 162 | + Ref<Curve2D> curve = memnew(Curve2D); |
148 | 163 | curve->add_point(Vector2(0, 0)); |
149 | 164 | curve->add_point(Vector2(100, 0)); |
150 | | - const Path2D *path = memnew(Path2D); |
| 165 | + Path2D *path = memnew(Path2D); |
151 | 166 | path->set_curve(curve); |
152 | | - const PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
| 167 | + PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
153 | 168 | path->add_child(path_follow_2d); |
| 169 | + SceneTree::get_singleton()->get_root()->add_child(path); |
154 | 170 |
|
155 | 171 | path_follow_2d->set_progress_ratio(0.5); |
156 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 0))); |
| 172 | + CHECK(is_equal_approx(Vector2(50, 0), path_follow_2d->get_transform().get_origin())); |
157 | 173 |
|
158 | 174 | path_follow_2d->set_h_offset(25); |
159 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(75, 0))); |
| 175 | + CHECK(is_equal_approx(Vector2(75, 0), path_follow_2d->get_transform().get_origin())); |
160 | 176 |
|
161 | 177 | path_follow_2d->set_v_offset(25); |
162 | | - CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(75, 25))); |
| 178 | + CHECK(is_equal_approx(Vector2(75, 25), path_follow_2d->get_transform().get_origin())); |
163 | 179 |
|
164 | 180 | memdelete(path); |
165 | 181 | } |
166 | 182 |
|
167 | | -TEST_CASE("[PathFollow2D] Unit offset out of range") { |
168 | | - const Ref<Curve2D> &curve = memnew(Curve2D()); |
| 183 | +TEST_CASE("[SceneTree][PathFollow2D] Progress ratio out of range") { |
| 184 | + Ref<Curve2D> curve = memnew(Curve2D); |
169 | 185 | curve->add_point(Vector2(0, 0)); |
170 | 186 | curve->add_point(Vector2(100, 0)); |
171 | | - const Path2D *path = memnew(Path2D); |
| 187 | + Path2D *path = memnew(Path2D); |
172 | 188 | path->set_curve(curve); |
173 | | - const PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
| 189 | + PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
174 | 190 | path->add_child(path_follow_2d); |
| 191 | + SceneTree::get_singleton()->get_root()->add_child(path); |
175 | 192 |
|
176 | 193 | path_follow_2d->set_loop(true); |
177 | 194 |
|
178 | 195 | path_follow_2d->set_progress_ratio(-0.3); |
179 | 196 | CHECK_MESSAGE( |
180 | | - path_follow_2d->get_progress_ratio() == 0.7, |
| 197 | + Math::is_equal_approx(path_follow_2d->get_progress_ratio(), (real_t)0.7), |
181 | 198 | "Progress Ratio should loop back from the end in the opposite direction"); |
182 | 199 |
|
183 | 200 | path_follow_2d->set_progress_ratio(1.3); |
184 | 201 | CHECK_MESSAGE( |
185 | | - path_follow_2d->get_progress_ratio() == 0.3, |
| 202 | + Math::is_equal_approx(path_follow_2d->get_progress_ratio(), (real_t)0.3), |
186 | 203 | "Progress Ratio should loop back from the end in the opposite direction"); |
187 | 204 |
|
188 | 205 | path_follow_2d->set_loop(false); |
189 | 206 |
|
190 | 207 | path_follow_2d->set_progress_ratio(-0.3); |
191 | 208 | CHECK_MESSAGE( |
192 | | - path_follow_2d->get_progress_ratio() == 0, |
| 209 | + Math::is_equal_approx(path_follow_2d->get_progress_ratio(), 0), |
193 | 210 | "Progress Ratio should be clamped at 0"); |
194 | 211 |
|
195 | 212 | path_follow_2d->set_progress_ratio(1.3); |
196 | 213 | CHECK_MESSAGE( |
197 | | - path_follow_2d->get_progress_ratio() == 1, |
| 214 | + Math::is_equal_approx(path_follow_2d->get_progress_ratio(), 1), |
198 | 215 | "Progress Ratio should be clamped at 1"); |
199 | 216 |
|
200 | 217 | memdelete(path); |
201 | 218 | } |
202 | 219 |
|
203 | | -TEST_CASE("[PathFollow2D] Progress out of range") { |
204 | | - const Ref<Curve2D> &curve = memnew(Curve2D()); |
| 220 | +TEST_CASE("[SceneTree][PathFollow2D] Progress out of range") { |
| 221 | + Ref<Curve2D> curve = memnew(Curve2D); |
205 | 222 | curve->add_point(Vector2(0, 0)); |
206 | 223 | curve->add_point(Vector2(100, 0)); |
207 | | - const Path2D *path = memnew(Path2D); |
| 224 | + Path2D *path = memnew(Path2D); |
208 | 225 | path->set_curve(curve); |
209 | | - const PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
| 226 | + PathFollow2D *path_follow_2d = memnew(PathFollow2D); |
210 | 227 | path->add_child(path_follow_2d); |
| 228 | + SceneTree::get_singleton()->get_root()->add_child(path); |
211 | 229 |
|
212 | 230 | path_follow_2d->set_loop(true); |
213 | 231 |
|
214 | 232 | path_follow_2d->set_progress(-50); |
215 | 233 | CHECK_MESSAGE( |
216 | | - path_follow_2d->get_progress() == 50, |
| 234 | + Math::is_equal_approx(path_follow_2d->get_progress(), 50), |
217 | 235 | "Progress should loop back from the end in the opposite direction"); |
218 | 236 |
|
219 | 237 | path_follow_2d->set_progress(150); |
220 | 238 | CHECK_MESSAGE( |
221 | | - path_follow_2d->get_progress() == 50, |
| 239 | + Math::is_equal_approx(path_follow_2d->get_progress(), 50), |
222 | 240 | "Progress should loop back from the end in the opposite direction"); |
223 | 241 |
|
224 | 242 | path_follow_2d->set_loop(false); |
225 | 243 |
|
226 | 244 | path_follow_2d->set_progress(-50); |
227 | 245 | CHECK_MESSAGE( |
228 | | - path_follow_2d->get_progress() == 0, |
| 246 | + Math::is_equal_approx(path_follow_2d->get_progress(), 0), |
229 | 247 | "Progress should be clamped at 0"); |
230 | 248 |
|
231 | 249 | path_follow_2d->set_progress(150); |
232 | 250 | CHECK_MESSAGE( |
233 | | - path_follow_2d->get_progress() == 100, |
| 251 | + Math::is_equal_approx(path_follow_2d->get_progress(), 100), |
234 | 252 | "Progress should be clamped at 1"); |
235 | 253 |
|
236 | 254 | memdelete(path); |
|
0 commit comments