Skip to content

Commit 8694811

Browse files
committed
Add test for InputValue::compile()
1 parent a92a911 commit 8694811

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

test/scratch_classes/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ add_executable(
133133
target_link_libraries(
134134
inputvalue_test
135135
GTest::gtest_main
136+
GTest::gmock_main
136137
scratchcpp
138+
scratchcpp_mocks
137139
)
138140

139141
gtest_discover_tests(inputvalue_test)

test/scratch_classes/inputvalue_test.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include <scratchcpp/broadcast.h>
44
#include <scratchcpp/variable.h>
55
#include <scratchcpp/list.h>
6+
#include <scratchcpp/compiler.h>
7+
#include <scratchcpp/input.h>
8+
#include <enginemock.h>
69

710
#include "../common.h"
811

@@ -102,3 +105,60 @@ TEST(InputValueTest, ValuePtr)
102105
ASSERT_EQ(value2.valueId(), "hello");
103106
ASSERT_EQ(value2.type(), InputValue::Type::Variable);
104107
}
108+
109+
TEST(InputValueTest, Compile)
110+
{
111+
EngineMock engine;
112+
Compiler compiler(&engine);
113+
114+
auto block = std::make_shared<Block>("", "");
115+
auto input = std::make_shared<Input>("", Input::Type::Shadow);
116+
input->setPrimaryValue(5);
117+
block->addInput(input);
118+
119+
InputValue *value = input->primaryValue();
120+
121+
compiler.init();
122+
compiler.setBlock(block);
123+
value->setType(InputValue::Type::Number);
124+
value->compile(&compiler);
125+
value->setType(InputValue::Type::PositiveNumber);
126+
value->compile(&compiler);
127+
value->setType(InputValue::Type::PositiveInteger);
128+
value->compile(&compiler);
129+
value->setType(InputValue::Type::Integer);
130+
value->compile(&compiler);
131+
value->setType(InputValue::Type::Angle);
132+
value->compile(&compiler);
133+
// TODO: Add support for colors
134+
/*value->setType(InputValue::Type::Color);
135+
value->compile(&compiler);*/
136+
value->setType(InputValue::Type::String);
137+
value->compile(&compiler);
138+
value->setType(InputValue::Type::Broadcast);
139+
value->compile(&compiler);
140+
compiler.end();
141+
142+
ASSERT_EQ(
143+
compiler.bytecode(),
144+
std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_CONST, 0, vm::OP_CONST, 0, vm::OP_CONST, 0, vm::OP_CONST, 0, vm::OP_CONST, 0, vm::OP_CONST, 0, vm::OP_HALT }));
145+
ASSERT_EQ(compiler.constValues().size(), 1);
146+
ASSERT_EQ(compiler.constValues()[0].toDouble(), 5);
147+
148+
auto variable = std::make_shared<Variable>("", "");
149+
auto list = std::make_shared<List>("", "");
150+
151+
compiler.init();
152+
compiler.setBlock(block);
153+
value->setType(InputValue::Type::Variable);
154+
value->setValuePtr(variable);
155+
value->compile(&compiler);
156+
value->setType(InputValue::Type::List);
157+
value->setValuePtr(list);
158+
value->compile(&compiler);
159+
compiler.end();
160+
161+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_READ_VAR, 0, vm::OP_READ_LIST, 0, vm::OP_HALT }));
162+
ASSERT_EQ(compiler.variables(), std::vector<Variable *>({ variable.get() }));
163+
ASSERT_EQ(compiler.lists(), std::vector<List *>({ list.get() }));
164+
}

0 commit comments

Comments
 (0)