#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../src/Script.hpp"
#include "../../src/ScriptOptions.hpp"
#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;
}

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;
}

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;
}

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;
}
#endif

