|
| 1 | +#include <scratchcpp/script.h> |
| 2 | +#include <scratchcpp/virtualmachine.h> |
| 3 | +#include <scratchcpp/target.h> |
| 4 | +#include <scratchcpp/list.h> |
| 5 | +#include <enginemock.h> |
| 6 | + |
| 7 | +#include "../common.h" |
| 8 | + |
| 9 | +using namespace libscratchcpp; |
| 10 | + |
| 11 | +class ScriptTest : public testing::Test |
| 12 | +{ |
| 13 | + public: |
| 14 | + Target m_target; |
| 15 | + EngineMock m_engine; |
| 16 | +}; |
| 17 | + |
| 18 | +TEST_F(ScriptTest, Bytecode) |
| 19 | +{ |
| 20 | + Script script(nullptr, nullptr); |
| 21 | + ASSERT_EQ(script.bytecode(), nullptr); |
| 22 | + ASSERT_TRUE(script.bytecodeVector().empty()); |
| 23 | + |
| 24 | + script.setBytecode({ vm::OP_START, vm::OP_HALT }); |
| 25 | + ASSERT_EQ(script.bytecode()[0], vm::OP_START); |
| 26 | + ASSERT_EQ(script.bytecode()[1], vm::OP_HALT); |
| 27 | + ASSERT_EQ(script.bytecodeVector(), std::vector<unsigned int>({ vm::OP_START, vm::OP_HALT })); |
| 28 | +} |
| 29 | + |
| 30 | +unsigned int testFunction(VirtualMachine *) |
| 31 | +{ |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +TEST_F(ScriptTest, Start) |
| 36 | +{ |
| 37 | + static std::vector<unsigned int> bytecode = { vm::OP_START, vm::OP_HALT }; |
| 38 | + static std::vector<unsigned int *> procedures = { bytecode.data() }; |
| 39 | + static std::vector<BlockFunc> functions = { &testFunction }; |
| 40 | + static std::vector<Value> constValues = { "test" }; |
| 41 | + |
| 42 | + std::unique_ptr<Value> var = std::make_unique<Value>(); |
| 43 | + static std::vector<Value *> variables = { var.get() }; |
| 44 | + |
| 45 | + std::unique_ptr<List> list = std::make_unique<List>("", ""); |
| 46 | + static std::vector<List *> lists = { list.get() }; |
| 47 | + |
| 48 | + Script script1(nullptr, nullptr); |
| 49 | + |
| 50 | + std::shared_ptr<VirtualMachine> vm = script1.start(); |
| 51 | + ASSERT_TRUE(vm); |
| 52 | + ASSERT_EQ(vm->target(), nullptr); |
| 53 | + ASSERT_EQ(vm->engine(), nullptr); |
| 54 | + ASSERT_EQ(vm->bytecode(), nullptr); |
| 55 | + ASSERT_EQ(vm->procedures(), nullptr); |
| 56 | + ASSERT_EQ(vm->functions(), nullptr); |
| 57 | + ASSERT_EQ(vm->constValues(), nullptr); |
| 58 | + ASSERT_EQ(vm->variables(), nullptr); |
| 59 | + ASSERT_EQ(vm->lists(), nullptr); |
| 60 | + |
| 61 | + Script script2(&m_target, &m_engine); |
| 62 | + |
| 63 | + vm = script2.start(); |
| 64 | + ASSERT_TRUE(vm); |
| 65 | + ASSERT_EQ(vm->target(), &m_target); |
| 66 | + ASSERT_EQ(vm->engine(), &m_engine); |
| 67 | + |
| 68 | + Script script3(&m_target, &m_engine); |
| 69 | + script3.setBytecode(bytecode); |
| 70 | + script3.setProcedures(procedures); |
| 71 | + script3.setFunctions(functions); |
| 72 | + script3.setConstValues(constValues); |
| 73 | + script3.setVariables(variables); |
| 74 | + script3.setLists(lists); |
| 75 | + |
| 76 | + vm = script3.start(); |
| 77 | + ASSERT_TRUE(vm); |
| 78 | + ASSERT_EQ(vm->bytecode()[0], bytecode[0]); |
| 79 | + ASSERT_EQ(vm->procedures()[0], procedures[0]); |
| 80 | + ASSERT_EQ(vm->functions()[0], functions[0]); |
| 81 | + ASSERT_EQ(vm->constValues()[0].toString(), constValues[0].toString()); |
| 82 | + ASSERT_EQ(vm->variables()[0], variables[0]); |
| 83 | + ASSERT_EQ(vm->lists()[0], lists[0]); |
| 84 | +} |
0 commit comments