|
3 | 3 | #include <scratchcpp/broadcast.h> |
4 | 4 | #include <scratchcpp/variable.h> |
5 | 5 | #include <scratchcpp/list.h> |
| 6 | +#include <scratchcpp/compiler.h> |
| 7 | +#include <scratchcpp/input.h> |
| 8 | +#include <enginemock.h> |
6 | 9 |
|
7 | 10 | #include "../common.h" |
8 | 11 |
|
@@ -102,3 +105,60 @@ TEST(InputValueTest, ValuePtr) |
102 | 105 | ASSERT_EQ(value2.valueId(), "hello"); |
103 | 106 | ASSERT_EQ(value2.type(), InputValue::Type::Variable); |
104 | 107 | } |
| 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