Skip to content

Commit

Permalink
Extract output message to file
Browse files Browse the repository at this point in the history
  • Loading branch information
cegonse committed Aug 24, 2024
1 parent 8d49153 commit cd1207f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ backend.log
# macOS products
**/*.dSYM/
*_result_generation
.DS_store

build/

Expand Down
93 changes: 4 additions & 89 deletions src/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,10 @@
#include <chrono>
#include <random>

#include "types.hpp"
#include "output.hpp"
#include "arg-parser.hpp"

#define ASCII_BACKGROUND_GREEN "\u001b[42m"
#define ASCII_BACKGROUND_YELLOW "\u001b[43m"
#define ASCII_BACKGROUND_RED "\u001b[41m"
#define ASCII_BACKGROUND_MAGENTA "\u001b[45;1m"
#define ASCII_RED "\033[1m\033[31m"
#define ASCII_GREEN "\033[1m\033[32m"
#define ASCII_GRAY "\u001b[38;5;243m"
#define ASCII_BLACK "\u001b[38;5;232m"
#define ASCII_BOLD "\u001b[1m"
#define ASCII_RESET "\033[0m"
#define TEST_NAME_LENGTH 4096
#define CLIP_STRING_LENGTH 16

Expand All @@ -42,32 +34,6 @@
#define failTest() cest::forcedFailure(__FILE__, __LINE__)
#define Regex(x) x, std::regex(x)

namespace cest
{
struct TestCase
{
std::string name;
std::string file;
int line;
std::function<void()> test;
bool test_failed;
bool forced_pass;
bool skip;
bool fit;
std::string failure_message;

TestCase() : test_failed(false), forced_pass(false), skip(false)
{
}
};

struct TestSuite
{
std::vector<cest::TestCase *> test_cases;
std::string test_suite_name;
};
}

std::vector<cest::TestCase *> test_cases;
extern std::string test_suite_name;
std::function<void()> before_each;
Expand Down Expand Up @@ -678,27 +644,6 @@ namespace cest
return skipped_tests;
}

void printTestResult(TestCase *test_case)
{
if (test_case->test_failed)
{
std::cout << ASCII_BACKGROUND_RED << ASCII_BLACK << ASCII_BOLD << " FAIL " << ASCII_RESET;
}
else if (test_case->skip)
{
std::cout << ASCII_BACKGROUND_YELLOW << ASCII_BLACK << ASCII_BOLD << " SKIP " << ASCII_RESET;
}
else
{
std::cout << ASCII_BACKGROUND_GREEN << ASCII_BLACK << ASCII_BOLD << " PASS " << ASCII_RESET;
}

std::cout << ASCII_GRAY << " " << test_case->file << ":" << test_case->line << ASCII_RESET << ASCII_BOLD << " it " << test_case->name << ASCII_RESET << std::endl;

std::cout << assertion_failures.str();
assertion_failures.str(std::string());
}

bool anyTestFailed()
{
return std::any_of(test_cases.begin(), test_cases.end(), [](cest::TestCase *test_case)
Expand All @@ -719,16 +664,6 @@ namespace cest
handleFailedTest(test_case);
}

void showHelp(std::string binary)
{
std::cout << "usage: " << binary << " [options]" << std::endl
<< std::endl;
std::cout << "Command line options:" << std::endl;
std::cout << " -r/--randomize: Randomize test executions" << std::endl;
std::cout << " -s/--seed <seed>: Inject seed for randomization uses (unsigned integer)";
std::cout << std::endl;
}

void onSignalRaised(int sig)
{
std::string signal_as_string(strsignal(sig));
Expand Down Expand Up @@ -806,40 +741,28 @@ int main(int argc, const char *argv[])
configureFittedTests(&test_suite);

if (command_line_options.random_seed_present)
{
random_seed = command_line_options.random_seed;
}
else
{
random_seed = std::chrono::system_clock::now().time_since_epoch().count();
}

if (command_line_options.randomize)
{
configureRandomizedTests(&test_suite, random_seed);
}

if (before_all)
{
before_all();
}

for (TestCase *test_case : test_suite.test_cases)
{
current_test_failed = false;
current_test_case = test_case;

if (before_each)
{
before_each();
}

try
{
if (test_case->skip)
{
throw ForcedPassError();
}

test_case->test();
setjmp(jump_env);
Expand All @@ -851,11 +774,9 @@ int main(int argc, const char *argv[])
catch (const ForcedPassError &error)
{
if (after_each)
{
after_each();
}

printTestResult(test_case);
cest::printTestResult(test_case, assertion_failures);
continue;
}
catch (...)
Expand All @@ -864,26 +785,20 @@ int main(int argc, const char *argv[])
}

if (after_each)
{
after_each();
}

test_case->test_failed = current_test_failed;

printTestResult(test_case);
cest::printTestResult(test_case, assertion_failures);
}

if (after_all)
{
after_all();
}

return_code = anyTestFailed();

for (TestCase *test_case : test_cases)
{
delete test_case;
}

return return_code;
}
51 changes: 51 additions & 0 deletions src/output.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include <iostream>
#include <string>
#include <sstream>

#include "types.hpp"

#define ASCII_BACKGROUND_GREEN "\u001b[42m"
#define ASCII_BACKGROUND_YELLOW "\u001b[43m"
#define ASCII_BACKGROUND_RED "\u001b[41m"
#define ASCII_BACKGROUND_MAGENTA "\u001b[45;1m"
#define ASCII_RED "\033[1m\033[31m"
#define ASCII_GREEN "\033[1m\033[32m"
#define ASCII_GRAY "\u001b[38;5;243m"
#define ASCII_BLACK "\u001b[38;5;232m"
#define ASCII_BOLD "\u001b[1m"
#define ASCII_RESET "\033[0m"

namespace cest
{
void showHelp(std::string binary)
{
std::cout << "usage: " << binary << " [options]" << std::endl
<< std::endl;
std::cout << "Command line options:" << std::endl;
std::cout << " -r/--randomize: Randomize test executions" << std::endl;
std::cout << " -s/--seed <seed>: Inject seed for randomization uses (unsigned integer)";
std::cout << std::endl;
}

void printTestResult(TestCase *test_case, const std::stringstream & assertion_failures)
{
if (test_case->test_failed)
{
std::cout << ASCII_BACKGROUND_RED << ASCII_BLACK << ASCII_BOLD << " FAIL " << ASCII_RESET;
}
else if (test_case->skip)
{
std::cout << ASCII_BACKGROUND_YELLOW << ASCII_BLACK << ASCII_BOLD << " SKIP " << ASCII_RESET;
}
else
{
std::cout << ASCII_BACKGROUND_GREEN << ASCII_BLACK << ASCII_BOLD << " PASS " << ASCII_RESET;
}

std::cout << ASCII_GRAY << " " << test_case->file << ":" << test_case->line << ASCII_RESET << ASCII_BOLD << " it " << test_case->name << ASCII_RESET << std::endl;

std::cout << assertion_failures.str();
}
}
30 changes: 30 additions & 0 deletions src/types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <functional>
#include <string>

namespace cest
{
struct TestCase
{
std::string name;
std::string file;
int line;
std::function<void()> test;
bool test_failed;
bool forced_pass;
bool skip;
bool fit;
std::string failure_message;

TestCase() : test_failed(false), forced_pass(false), skip(false)
{
}
};

struct TestSuite
{
std::vector<cest::TestCase *> test_cases;
std::string test_suite_name;
};
}

0 comments on commit cd1207f

Please sign in to comment.