Skip to content

Commit

Permalink
Add single suite summary output format
Browse files Browse the repository at this point in the history
  • Loading branch information
cegonse committed Oct 11, 2024
1 parent dc82dca commit 6c832ff
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/arg-parser.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
#include "types.hpp"
#include <stdexcept>
#include <string>
#include <iostream>
#include <cstring>

namespace cest
{
Expand All @@ -28,6 +29,11 @@ namespace cest
options.generate_test_report = true;
}

if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--only-suite-result") == 0)
{
options.only_test_suite_result = true;
}

if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--seed") == 0)
{
if (i + 1 < argc)
Expand All @@ -39,6 +45,7 @@ namespace cest
}
catch (const std::invalid_argument &err)
{
std::cout << "Invalid seed value: " << argv[i + 1] << std::endl;
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ int main(int argc, const char *argv[])

cest::configureFocusedTestSuite(root_suite);
cest::runTestSuite(root_suite);
cest::printTestSuiteResult(root_suite);

if (command_line_options.only_test_suite_result)
cest::printSuiteSummaryResult(root_suite);
else
cest::printTestSuiteResult(root_suite);

return 0;
}
34 changes: 29 additions & 5 deletions src/output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,30 @@ namespace cest
<< std::endl;
std::cout << "Command line options:" << std::endl;
std::cout << " -r/--randomize: Randomize test executions" << std::endl;
std::cout << " -o/--only-suite-result: Only output the test suite result" << std::endl;
std::cout << " -s/--seed <seed>: Inject seed for randomization uses (unsigned integer)";
std::cout << std::endl;
}

void printTestCaseResult(cest::TestCase *test_case)
void printTestBadge(bool failed, bool skipped = false)
{
if (test_case->failed)
if (failed)
{
std::cout << ASCII_BACKGROUND_RED << ASCII_BLACK << ASCII_BOLD << " FAIL " << ASCII_RESET;
}
else if (test_case->condition == cest::TestCaseCondition::Skipped)
else if (skipped)
{
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;
}
}

void printTestCaseResult(cest::TestCase *test_case)
{
printTestBadge(test_case->failed, test_case->condition == cest::TestCaseCondition::Skipped);
std::cout << ASCII_GRAY << " " << test_case->fn.file << ":" << test_case->fn.line << ASCII_RESET << ASCII_BOLD << " " << test_case->name << ASCII_RESET << std::endl;

if (test_case->failed)
Expand All @@ -53,12 +58,31 @@ namespace cest

void printTestSuiteResult(cest::TestSuite *suite)
{
std::cout << ASCII_BOLD << suite->name << ASCII_RESET << std::endl;

for (cest::TestCase *test_case : suite->test_cases)
printTestCaseResult(test_case);

for (auto &pair : suite->test_suites)
printTestSuiteResult(pair.second);
}

void printSuiteSummaryResult(cest::TestSuite *suite)
{
bool any_test_failed = false;

for (size_t i = 0; i < suite->test_cases.size(); ++i)
{
cest::TestCase *test_case = suite->test_cases[i];
if (test_case->failed)
{
any_test_failed = true;
break;
}
}

printTestBadge(any_test_failed);
std::cout << ASCII_BOLD << " " << suite->name << ASCII_RESET << std::endl;

for (auto &pair : suite->test_suites)
printSuiteSummaryResult(pair.second);
}
}
1 change: 1 addition & 0 deletions src/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace cest
unsigned int random_seed;
bool random_seed_present;
bool generate_test_report;
bool only_test_suite_result;
};

struct CestGlobals
Expand Down

0 comments on commit 6c832ff

Please sign in to comment.