Skip to content

Commit c251456

Browse files
committed
Script: Implement runHatPredicate()
1 parent 8a1cc32 commit c251456

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/engine/script.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,8 @@ bool Script::runHatPredicate(Target *target)
6363
if (!target || !impl->engine)
6464
return false;
6565

66-
// TODO: Implement this
67-
// auto thread = std::make_shared<Thread>(target, impl->engine, this);
68-
69-
return false;
66+
auto thread = std::make_shared<Thread>(target, impl->engine, this);
67+
return thread->runPredicate();
7068
}
7169

7270
/*! Starts the script (creates a thread). */

test/script/script_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <scratchcpp/stage.h>
66
#include <scratchcpp/variable.h>
77
#include <scratchcpp/list.h>
8+
#include <scratchcpp/executioncontext.h>
89
#include <enginemock.h>
910
#include <executablecodemock.h>
1011

@@ -14,6 +15,7 @@ using namespace libscratchcpp;
1415

1516
using ::testing::Return;
1617
using ::testing::ReturnRef;
18+
using ::testing::Invoke;
1719
using ::testing::_;
1820

1921
class ScriptTest : public testing::Test
@@ -51,6 +53,28 @@ TEST_F(ScriptTest, HatPredicateCode)
5153
ASSERT_EQ(script.hatPredicateCode(), code.get());
5254
}
5355

56+
TEST_F(ScriptTest, RunHatPredicate)
57+
{
58+
Script script(nullptr, nullptr, &m_engine);
59+
auto code = std::make_shared<ExecutableCodeMock>();
60+
std::shared_ptr<ExecutionContext> ctx;
61+
script.setHatPredicateCode(code);
62+
63+
EXPECT_CALL(*code, createExecutionContext(_)).WillRepeatedly(Invoke([&ctx](Thread *thread) {
64+
ctx = std::make_shared<ExecutionContext>(thread);
65+
return ctx;
66+
}));
67+
68+
EXPECT_CALL(*code, runPredicate(_)).WillOnce(Return(true));
69+
ASSERT_TRUE(script.runHatPredicate(&m_target));
70+
71+
EXPECT_CALL(*code, runPredicate(_)).WillOnce(Return(true));
72+
ASSERT_TRUE(script.runHatPredicate(&m_target));
73+
74+
EXPECT_CALL(*code, runPredicate(_)).WillOnce(Return(false));
75+
ASSERT_FALSE(script.runHatPredicate(&m_target));
76+
}
77+
5478
TEST_F(ScriptTest, Start)
5579
{
5680
Script script1(nullptr, nullptr, nullptr);

0 commit comments

Comments
 (0)