<commit_before>#include "test_process_reader.h"
#include "logic/process_reader.h"
#include "logic/settings.h"
#include "test.h"
#include "ui/mainwindow.h"

#include <QProcess>
#include <QString>
#include <QStringList>
#include <fstream>

void test_args_construction() {
	struct Test_cases {
		QString arg_text;
		QStringList args;
	} test_cases[] = {
		{"", {}},
		{"test", {"test"}},
		{R"("test")", {"test"}},
		{"multi arg", {"multi", "arg"}},
		{"many multi arg", {"many", "multi", "arg"}},
		{R"("multi arg")", {"multi arg"}},
		{R"(many "multi arg" things)", {"many", "multi arg", "things"}},
		{R"("many multi" "arg things")", {"many multi", "arg things"}},
	};

	for (const auto &test_case : test_cases) {
		assert_equal(detail::create_arguments_list(test_case.arg_text), test_case.args);
	}
}

void test_process_reading() {
	struct Test_cases {
		Tool tool;
		std::string_view code;
		std::string_view expected_output;
		std::string_view expected_error;
	} test_cases[] = {
		{.tool = {}, .code = R"(int main(){})"},                                                                                                        //
		{.tool = {}, .code = R"(#include <iostream>
							 int main(){
								std::cout << "test";
							 })", .expected_output = "test"},                                                                        //
		{.tool = {}, .code = R"(#include <iostream>
							 int main(){
								std::cerr << "test";
							 })", .expected_output = "", .expected_error = "test"},                                                  //
		{.tool = {}, .code = R"(#include <iostream>
							 int main(){
								 std::cout << "multi\nline\ntest\noutput\n";
								 std::cerr << "multi\nline\ntest\nerror\n";
							 })", .expected_output = "multi\nline\ntest\noutput\n", .expected_error = "multi\nline\ntest\nerror\n"}, //
		{.tool = {}, .code = R"(#include <iostream>
							 #include <thread>
							 #include <chrono>

							 int main(){
								 std::cout << "Hello" << std::flush;
								 std::this_thread::sleep_for(std::chrono::milliseconds{500});
								 std::cout << "World";
							 })", .expected_output = "HelloWorld"},                                                                  //
	};

	for (auto &test_case : test_cases) {
		const auto cpp_file = "/tmp/SCE_test_process_code.cpp";
		const auto exe_file = "/tmp/SCE_test_process_exe";
		assert_true(std::ofstream{cpp_file} << test_case.code);
		assert_equal(QProcess::execute("g++", {"-std=c++17", cpp_file, "-o", exe_file}), 0);
		test_case.tool.path = exe_file;
		Process_reader p{test_case.tool};
		assert_equal(p.get_output(), test_case.expected_output);
		assert_equal(p.get_error(), test_case.expected_error);
	}
}

void test_process_reader() {
	test_args_construction();
	test_process_reading();
}
<commit_msg>Refactored process reader test cases.<commit_after>#include "test_process_reader.h"
#include "logic/process_reader.h"
#include "logic/settings.h"
#include "test.h"
#include "ui/mainwindow.h"

#include <QProcess>
#include <QString>
#include <QStringList>
#include <fstream>

void test_args_construction() {
	struct Test_cases {
		QString arg_text;
		QStringList args;
	} test_cases[] = {
		{"", {}},
		{"test", {"test"}},
		{R"("test")", {"test"}},
		{"multi arg", {"multi", "arg"}},
		{"many multi arg", {"many", "multi", "arg"}},
		{R"("multi arg")", {"multi arg"}},
		{R"(many "multi arg" things)", {"many", "multi arg", "things"}},
		{R"("many multi" "arg things")", {"many multi", "arg things"}},
	};

	for (const auto &test_case : test_cases) {
		assert_equal(detail::create_arguments_list(test_case.arg_text), test_case.args);
	}
}

void test_process_reading() {
	struct Test_cases {
		std::string_view code;
		std::string_view expected_output;
		std::string_view expected_error;
	} test_cases[] = {
		{.code = R"(int main(){})"},
		{.code = R"(#include <iostream>
				 int main(){
					std::cout << "test";
				 })",
		 .expected_output = "test"},
		{.code = R"(#include <iostream>
				 int main(){
					std::cerr << "test";
				 })",
		 .expected_output = "",
		 .expected_error = "test"},
		{.code = R"(#include <iostream>
				 int main(){
					 std::cout << "multi\nline\ntest\noutput\n";
					 std::cerr << "multi\nline\ntest\nerror\n";
				 })",
		 .expected_output = "multi\nline\ntest\noutput\n",
		 .expected_error = "multi\nline\ntest\nerror\n"},
		{.code = R"(#include <iostream>
				 #include <thread>
				 #include <chrono>

				 int main(){
					 std::cout << "Hello" << std::flush;
					 std::this_thread::sleep_for(std::chrono::milliseconds{500});
					 std::cout << "World";
				 })",
		 .expected_output = "HelloWorld"},
	};

	for (auto &test_case : test_cases) {
		const auto cpp_file = "/tmp/SCE_test_process_code.cpp";
		const auto exe_file = "/tmp/SCE_test_process_exe";
		assert_true(std::ofstream{cpp_file} << test_case.code);
		assert_equal(QProcess::execute("g++", {"-std=c++17", cpp_file, "-o", exe_file}), 0);
		Tool tool;
		tool.path = exe_file;
		Process_reader p{tool};
		assert_equal(p.get_output(), test_case.expected_output);
		assert_equal(p.get_error(), test_case.expected_error);
	}
}

void test_process_reader() {
	test_args_construction();
	test_process_reading();
}

{'fraction_non_alphanumeric': 0.19133435582822086, 'fraction_numerical': 0.0023006134969325155, 'mean_word_length': 3.1339144215530905, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7461456', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1752, 'n_words': 339}