PorygonLang/tests/standardLibraries/BasicLibrary.cpp

115 lines
3.6 KiB
C++
Raw Normal View History

2019-06-29 14:18:59 +00:00
#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../src/Script.hpp"
#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;
}
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();
ScriptOptions::GetDefaultScriptOptions()->SetPrintStream(stream);
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
TEST_CASE( "tofloat func works", "[integration]" ) {
Script* script = Script::Create(u"return tofloat('5.128')");
REQUIRE(!script->Diagnostics -> HasErrors());
auto result = script->Evaluate();
REQUIRE(result->EvaluateFloat() == 5.128);
delete script;
}
TEST_CASE( "tostring func works", "[integration]" ) {
Script* script = Script::Create(u"return tostring(5.128)");
REQUIRE(!script->Diagnostics -> HasErrors());
auto result = script->Evaluate();
REQUIRE(result->EvaluateString() == u"5.128000");
delete script;
}
2019-06-29 14:18:59 +00:00
TEST_CASE( "type func works", "[integration]" ) {
Script* script = Script::Create(u"return type(5.128)");
REQUIRE(!script->Diagnostics -> HasErrors());
auto result = script->Evaluate();
REQUIRE(result->EvaluateString() == u"number");
delete script;
}
TEST_CASE( "isfloat returns true for floats", "[integration]" ) {
Script* script = Script::Create(u"return isfloat(5.128)");
REQUIRE(!script->Diagnostics -> HasErrors());
auto result = script->Evaluate();
REQUIRE(result->EvaluateBool());
delete script;
}
TEST_CASE( "isfloat returns false for integers", "[integration]" ) {
Script* script = Script::Create(u"return isfloat(5)");
REQUIRE(!script->Diagnostics -> HasErrors());
auto result = script->Evaluate();
REQUIRE_FALSE(result->EvaluateBool());
delete script;
}
2019-06-29 14:18:59 +00:00
#endif