PorygonLang/tests/integration/TablesTests.cpp

85 lines
2.3 KiB
C++
Raw Normal View History

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"
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();
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();
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();
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'}
return table[3]
2019-06-09 18:15:09 +00:00
)");
REQUIRE(!script->Diagnostics -> HasErrors());
auto variable = script->Evaluate();
2019-06-09 18:15:09 +00:00
REQUIRE(variable != nullptr);
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();
auto variable = script->GetVariable(u"table");
2019-06-12 13:19:28 +00:00
REQUIRE(variable != nullptr);
auto table = (TableEvalValue*)variable;
CHECK(table->IndexValue("foo")->EvaluateString() == u"test");
2019-06-12 13:19:28 +00:00
CHECK(table->IndexValue("bar")->EvaluateInteger() == 100);
delete script;
}
TEST_CASE( "Complex table with function", "[integration]" ) {
Script* script = Script::Create(
R"(
table = {
local foo = 'test'
function getFoo()
return foo
end
}
return table["getFoo"]()
)");
REQUIRE(!script->Diagnostics -> HasErrors());
auto variable = script->Evaluate();
REQUIRE(variable != nullptr);
delete script;
}
2019-06-09 18:15:09 +00:00
#endif