|
4 | 4 | #include <scratchcpp/sprite.h> |
5 | 5 | #include <scratchcpp/block.h> |
6 | 6 | #include <scratchcpp/input.h> |
| 7 | +#include <scratchcpp/script.h> |
| 8 | +#include <scratchcpp/thread.h> |
7 | 9 | #include <enginemock.h> |
8 | 10 |
|
9 | 11 | #include "../common.h" |
@@ -250,3 +252,83 @@ TEST_F(ControlBlocksTest, IfElse) |
250 | 252 | builder.run(); |
251 | 253 | } |
252 | 254 | } |
| 255 | + |
| 256 | +TEST_F(ControlBlocksTest, Stop) |
| 257 | +{ |
| 258 | + auto target = std::make_shared<Sprite>(); |
| 259 | + |
| 260 | + // Stop all |
| 261 | + { |
| 262 | + ScriptBuilder builder(m_extension.get(), m_engine, target); |
| 263 | + |
| 264 | + builder.addBlock("control_stop"); |
| 265 | + builder.addDropdownField("STOP_OPTION", "all"); |
| 266 | + auto block = builder.currentBlock(); |
| 267 | + |
| 268 | + Compiler compiler(&m_engineMock, target.get()); |
| 269 | + auto code = compiler.compile(block); |
| 270 | + Script script(target.get(), block, &m_engineMock); |
| 271 | + script.setCode(code); |
| 272 | + Thread thread(target.get(), &m_engineMock, &script); |
| 273 | + |
| 274 | + EXPECT_CALL(m_engineMock, stop()); |
| 275 | + thread.run(); |
| 276 | + } |
| 277 | + |
| 278 | + m_engine->clear(); |
| 279 | + target = std::make_shared<Sprite>(); |
| 280 | + |
| 281 | + // Stop this script |
| 282 | + { |
| 283 | + ScriptBuilder builder(m_extension.get(), m_engine, target); |
| 284 | + |
| 285 | + builder.addBlock("control_stop"); |
| 286 | + builder.addDropdownField("STOP_OPTION", "this script"); |
| 287 | + builder.addBlock("test_print_test"); |
| 288 | + |
| 289 | + builder.build(); |
| 290 | + |
| 291 | + testing::internal::CaptureStdout(); |
| 292 | + builder.run(); |
| 293 | + ASSERT_TRUE(testing::internal::GetCapturedStdout().empty()); |
| 294 | + } |
| 295 | + |
| 296 | + m_engine->clear(); |
| 297 | + target = std::make_shared<Sprite>(); |
| 298 | + |
| 299 | + // Stop other scripts in sprite |
| 300 | + { |
| 301 | + ScriptBuilder builder(m_extension.get(), m_engine, target); |
| 302 | + |
| 303 | + builder.addBlock("control_stop"); |
| 304 | + builder.addDropdownField("STOP_OPTION", "other scripts in sprite"); |
| 305 | + auto block = builder.currentBlock(); |
| 306 | + |
| 307 | + Compiler compiler(&m_engineMock, target.get()); |
| 308 | + auto code = compiler.compile(block); |
| 309 | + Script script(target.get(), block, &m_engineMock); |
| 310 | + script.setCode(code); |
| 311 | + Thread thread(target.get(), &m_engineMock, &script); |
| 312 | + |
| 313 | + EXPECT_CALL(m_engineMock, stopTarget(target.get(), &thread)); |
| 314 | + thread.run(); |
| 315 | + } |
| 316 | + |
| 317 | + // Stop other scripts in stage |
| 318 | + { |
| 319 | + ScriptBuilder builder(m_extension.get(), m_engine, target); |
| 320 | + |
| 321 | + builder.addBlock("control_stop"); |
| 322 | + builder.addDropdownField("STOP_OPTION", "other scripts in stage"); |
| 323 | + auto block = builder.currentBlock(); |
| 324 | + |
| 325 | + Compiler compiler(&m_engineMock, target.get()); |
| 326 | + auto code = compiler.compile(block); |
| 327 | + Script script(target.get(), block, &m_engineMock); |
| 328 | + script.setCode(code); |
| 329 | + Thread thread(target.get(), &m_engineMock, &script); |
| 330 | + |
| 331 | + EXPECT_CALL(m_engineMock, stopTarget(target.get(), &thread)); |
| 332 | + thread.run(); |
| 333 | + } |
| 334 | +} |
0 commit comments