2019-05-28 15:49:03 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include <catch.hpp>
|
|
|
|
#include "../src/Script.hpp"
|
|
|
|
|
|
|
|
TEST_CASE( "Create script variable", "[integration]" ) {
|
|
|
|
Script* script = Script::Create("foo = true");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"foo");
|
2019-05-28 15:49:03 +00:00
|
|
|
REQUIRE(variable == nullptr);
|
2019-05-29 12:55:03 +00:00
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
variable = script->GetVariable(u"foo");
|
2019-05-29 12:55:03 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
REQUIRE(variable->EvaluateBool());
|
2019-05-28 16:50:23 +00:00
|
|
|
delete script;
|
2019-05-28 15:49:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 13:10:16 +00:00
|
|
|
TEST_CASE( "Create local variable", "[integration]" ) {
|
|
|
|
Script* script = Script::Create("local foo = true");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
2019-06-15 15:20:27 +00:00
|
|
|
REQUIRE_FALSE(script->HasVariable(u"foo"));
|
2019-05-29 13:10:16 +00:00
|
|
|
script->Evaluate();
|
|
|
|
delete script;
|
|
|
|
}
|
2019-05-28 15:49:03 +00:00
|
|
|
|
2019-05-30 13:23:48 +00:00
|
|
|
TEST_CASE( "Create script variable and use", "[integration]" ) {
|
|
|
|
Script* script = Script::Create("foo = false\n"
|
|
|
|
"bar = not foo");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"bar");
|
2019-05-30 13:23:48 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
CHECK(variable->EvaluateBool());
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Create local variable and use", "[integration]" ) {
|
|
|
|
Script* script = Script::Create("local foo = false\n"
|
|
|
|
"bar = not foo");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"bar");
|
2019-05-30 13:23:48 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
CHECK(variable->EvaluateBool());
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-06-12 13:19:28 +00:00
|
|
|
TEST_CASE( "Local variables in upmost scope persist", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(R"(
|
|
|
|
result = 0
|
|
|
|
local foo = 0
|
|
|
|
function bar()
|
|
|
|
foo = foo + 1
|
|
|
|
result = foo
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
script -> CallFunction(u"bar", {});
|
|
|
|
script -> CallFunction(u"bar", {});
|
|
|
|
auto variable = script->GetVariable(u"result");
|
2019-06-12 13:19:28 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
CHECK(variable->EvaluateInteger() == 2);
|
|
|
|
delete script;
|
|
|
|
}
|
2019-05-28 15:49:03 +00:00
|
|
|
#endif
|