<commit_before>/*
 * (C) 2015-2016 <NAME>
 * All rights reserved. Distributed under the terms of the MIT license.
 */

#include "Tester.h"

#include "script/Interpreter.h"

#include "util/FSUtil.h"
#include "util/StringUtil.h"

using std::vector;
using std::string;

int main(int argc, char* argv[])
{
	vector<string> files = FSUtil::searchForFiles(FSUtil::combinePaths({__FILE__,
		"..", "script-tests"}), {""}, false);
	Tester t(true);

	// Global function instatiation (copied from GlobalLanguageObject.cpp)
	Script::GlobalFunctions.insert({"Map", Script::Function([](Script::Stack*,
			Script::Object*, Script::ObjectMap& params) -> Script::Object {
		return Script::MapObject(new Script::ObjectMap(params));
	})});

	const string opener = "#EXPECT: ";
	const size_t openerLen = opener.length();
	for (const string& i : files) {
		const string test = FSUtil::getContents(i);
		const string name = i.substr(i.find_last_of("/") + 1, string::npos);

		if (!StringUtil::startsWith(test, opener)) {
			t.result(false, name + " (failed to get expectation)");
			continue;
		}
		string expect = test.substr(openerLen, test.find_first_of("\n") - openerLen);

		Script::Stack stack;
		Script::Object result;
		bool threwException = false;
		Script::Exception exceptionResult(Script::Exception::InternalError, "");
		try {
			result = Script::Run(&stack, i);
		} catch (Script::Exception e) {
			threwException = true;
			exceptionResult = e;
		}

		if (threwException) {
			if (expect[0] != 'E') {
				t.result(false, name + " (unexpected exception)");
				exceptionResult.print();
			} else
				t.result((exceptionResult.what() == expect.substr(1, string::npos)), name);
			continue;
		} else {
			bool res = (result.asStringPretty() == expect);
			if (res)
				t.result(true, name);
			else
				t.result(false, name + " (got " + result.asStringPretty() + ", expected " + expect + ")");
		}
	}
	return t.done();
}<commit_msg>ScriptTest: Print the exception we did not expect in case of failure.<commit_after>/*
 * (C) 2015-2016 <NAME>
 * All rights reserved. Distributed under the terms of the MIT license.
 */

#include "Tester.h"

#include "script/Interpreter.h"

#include "util/FSUtil.h"
#include "util/StringUtil.h"

using std::vector;
using std::string;

int main(int argc, char* argv[])
{
	vector<string> files = FSUtil::searchForFiles(FSUtil::combinePaths({__FILE__,
		"..", "script-tests"}), {""}, false);
	Tester t(true);

	// Global function instatiation (copied from GlobalLanguageObject.cpp)
	Script::GlobalFunctions.insert({"Map", Script::Function([](Script::Stack*,
			Script::Object*, Script::ObjectMap& params) -> Script::Object {
		return Script::MapObject(new Script::ObjectMap(params));
	})});

	const string opener = "#EXPECT: ";
	const size_t openerLen = opener.length();
	for (const string& i : files) {
		const string test = FSUtil::getContents(i);
		const string name = i.substr(i.find_last_of("/") + 1, string::npos);

		if (!StringUtil::startsWith(test, opener)) {
			t.result(false, name + " (failed to get expectation)");
			continue;
		}
		string expect = test.substr(openerLen, test.find_first_of("\n") - openerLen);

		Script::Stack stack;
		Script::Object result;
		bool threwException = false;
		Script::Exception exceptionResult(Script::Exception::InternalError, "");
		try {
			result = Script::Run(&stack, i);
		} catch (Script::Exception e) {
			threwException = true;
			exceptionResult = e;
		}

		if (threwException) {
			if (expect[0] != 'E') {
				t.result(false, name + " (unexpected exception)");
				exceptionResult.print();
			} else {
				bool result = (exceptionResult.what() == expect.substr(1, string::npos));
				t.result(result, name +
					(result ? "" : " (expected '" + expect.substr(1, string::npos) + "', got '" +
						exceptionResult.what() + "')"));
			}
			continue;
		} else {
			bool res = (result.asStringPretty() == expect);
			if (res)
				t.result(true, name);
			else
				t.result(false, name + " (got " + result.asStringPretty() + ", expected " + expect + ")");
		}
	}
	return t.done();
}
{'fraction_non_alphanumeric': 0.17443868739205526, 'fraction_numerical': 0.005674808783617074, 'mean_word_length': 4.278645833333333, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6233999', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1322, 'n_words': 312}