2019-06-29 14:18:59 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include <catch.hpp>
|
|
|
|
#include "../src/Script.hpp"
|
2019-08-10 09:55:45 +00:00
|
|
|
#include "../../src/ScriptOptions.hpp"
|
2019-06-29 14:18:59 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using namespace Porygon;
|
|
|
|
|
|
|
|
TEST_CASE( "Error func throws error", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(u"error('foo bar')");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
try{
|
|
|
|
script -> Evaluate();
|
|
|
|
throw;
|
|
|
|
} catch (const EvaluationException& e){
|
|
|
|
auto err = e.what();
|
|
|
|
REQUIRE(std::strcmp(err, "An evaluation exception occurred: foo bar") == 0);
|
|
|
|
}
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Assert func throws error if argument is false", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(u"assert(false)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
try{
|
|
|
|
script -> Evaluate();
|
|
|
|
throw;
|
|
|
|
} catch (const EvaluationException& e){
|
|
|
|
auto err = e.what();
|
|
|
|
REQUIRE(std::strcmp(err, "An evaluation exception occurred: assertion failed!") == 0);
|
|
|
|
}
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Assert func does not throw if argument is true", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(u"assert(true)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script -> Evaluate();
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-07-04 13:56:42 +00:00
|
|
|
TEST_CASE( "Assert func works with second argument", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(u"assert(false, 'foobar')");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
try{
|
|
|
|
script -> Evaluate();
|
|
|
|
throw;
|
|
|
|
} catch (const EvaluationException& e){
|
|
|
|
auto err = e.what();
|
|
|
|
REQUIRE(std::strcmp(err, "An evaluation exception occurred: foobar") == 0);
|
|
|
|
}
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Print func works", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(u"print('foobar')");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
auto stream = new std::stringstream();
|
2019-08-10 09:55:45 +00:00
|
|
|
ScriptOptions::GetDefaultScriptOptions()->SetPrintStream(stream);
|
2019-07-04 13:56:42 +00:00
|
|
|
script->Evaluate();
|
|
|
|
auto printVal = stream->str();
|
|
|
|
REQUIRE(printVal == "foobar\n");
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-07-04 15:18:07 +00:00
|
|
|
TEST_CASE( "toint func works", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(u"return toint('5846321')");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
auto result = script->Evaluate();
|
|
|
|
REQUIRE(result->EvaluateInteger() == 5846321);
|
|
|
|
delete script;
|
|
|
|
}
|
2019-06-29 14:18:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|