2019-06-09 18:15:09 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include <catch.hpp>
|
|
|
|
#include "../src/Script.hpp"
|
2019-06-12 13:19:28 +00:00
|
|
|
#include "../../src/Evaluator/EvalValues/TableEvalValue.hpp"
|
2019-06-17 16:35:12 +00:00
|
|
|
using namespace Porygon;
|
2019-06-09 18:15:09 +00:00
|
|
|
|
|
|
|
TEST_CASE( "Create empty table", "[integration]" ) {
|
|
|
|
Script* script = Script::Create("table = {}");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"table");
|
2019-06-09 18:15:09 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Create simple integer table", "[integration]" ) {
|
|
|
|
Script* script = Script::Create("table = {100, 50, 20, 5, -100, 50+50}");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"table");
|
2019-06-09 18:15:09 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Create simple string table", "[integration]" ) {
|
|
|
|
Script* script = Script::Create("table = {'bla', 'test', 'foo', 'bar'}");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"table");
|
2019-06-09 18:15:09 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Index string table", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(
|
|
|
|
R"(
|
|
|
|
table = {'bla', 'test', 'foo', 'bar'}
|
2019-06-12 16:45:47 +00:00
|
|
|
return table[3]
|
2019-06-09 18:15:09 +00:00
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
2019-06-12 16:45:47 +00:00
|
|
|
auto variable = script->Evaluate();
|
2019-06-09 18:15:09 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
2019-06-15 15:20:27 +00:00
|
|
|
REQUIRE(*variable->EvaluateString() == u"foo");
|
2019-06-09 18:15:09 +00:00
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-06-12 13:19:28 +00:00
|
|
|
TEST_CASE( "Create complex table", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(
|
|
|
|
R"(
|
|
|
|
table = {
|
|
|
|
foo = 'test'
|
|
|
|
bar = 100
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"table");
|
2019-06-12 13:19:28 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
auto table = (TableEvalValue*)variable;
|
2019-06-15 15:20:27 +00:00
|
|
|
CHECK(*table->IndexValue("foo")->EvaluateString() == u"test");
|
2019-06-12 13:19:28 +00:00
|
|
|
CHECK(table->IndexValue("bar")->EvaluateInteger() == 100);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-06-12 15:56:47 +00:00
|
|
|
TEST_CASE( "Complex table with function", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(
|
|
|
|
R"(
|
|
|
|
table = {
|
|
|
|
local foo = 'test'
|
|
|
|
function getFoo()
|
|
|
|
return foo
|
|
|
|
end
|
|
|
|
}
|
2019-06-12 16:45:47 +00:00
|
|
|
return table["getFoo"]()
|
2019-06-12 15:56:47 +00:00
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
2019-06-12 16:45:47 +00:00
|
|
|
auto variable = script->Evaluate();
|
2019-06-12 15:56:47 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-06-09 18:15:09 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|