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;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete variable;
|
2019-06-09 18:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete variable;
|
2019-06-09 18:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete variable;
|
2019-06-09 18:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07-27 15:59:42 +00:00
|
|
|
REQUIRE(variable.Get() != nullptr);
|
2019-06-23 13:58:14 +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-07-27 15:59:42 +00:00
|
|
|
auto res = table->IndexValue("foo");
|
|
|
|
auto res2 = table->IndexValue("bar");
|
|
|
|
CHECK(res->EvaluateString() == u"test");
|
|
|
|
CHECK(res2->EvaluateInteger() == 100);
|
2019-06-12 13:19:28 +00:00
|
|
|
delete script;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete table;
|
|
|
|
delete res;
|
|
|
|
delete res2;
|
2019-06-12 13:19:28 +00:00
|
|
|
}
|
|
|
|
|
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-07-27 15:59:42 +00:00
|
|
|
REQUIRE(variable.Get() != nullptr);
|
2019-06-12 15:56:47 +00:00
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-09-15 11:08:11 +00:00
|
|
|
TEST_CASE( "Dynamic table size", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(
|
|
|
|
R"(
|
|
|
|
table = {}
|
|
|
|
table.foo = 500
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-09-19 09:28:00 +00:00
|
|
|
TEST_CASE( "Dynamic numerical table size", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(
|
|
|
|
R"(
|
|
|
|
table = {}
|
|
|
|
table[1] = 500
|
|
|
|
return table[1]
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
auto result = script->Evaluate();
|
|
|
|
CHECK(result -> EvaluateInteger() == 500);
|
|
|
|
delete script;
|
|
|
|
}
|
2019-09-15 11:08:11 +00:00
|
|
|
|
2019-06-09 18:15:09 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|