Skip to content

Commit

Permalink
Try printing failed test lines if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
cegonse committed Nov 11, 2024
1 parent 24bf8e3 commit d8c08c1
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions src/output.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

Expand All @@ -22,6 +23,41 @@

namespace cest
{
void tryPrintFailedLines(cest::TestCase *test_case)
{
if (!test_case->failed) return;

std::ifstream file(test_case->failure_file);
if (!file.is_open()) return;

std::string line;
std::vector<std::string> lines;
int start_line = std::max(test_case->failure_line - 3, 0);
int end_line = start_line + 6;
int current_line = 0;

while (current_line < start_line && std::getline(file, line)) current_line++;

for (int i=start_line; i<=end_line && std::getline(file, line); ++i) lines.push_back(line);

int line_counter = start_line;
for (auto l : lines)
{
if (line_counter == test_case->failure_line)
{
std::cout << " " << ASCII_RED << ASCII_BOLD << "> " << line_counter << ASCII_RESET << ASCII_GRAY << " | " << ASCII_RESET;
}
else
{
std::cout << " " << ASCII_GRAY << line_counter << " | " << ASCII_RESET;
}
std::cout << l << std::endl;
line_counter++;
}

file.close();
}

void showHelp(std::string binary)
{
std::cout << "usage: " << binary << " [options]" << std::endl
Expand Down Expand Up @@ -58,6 +94,7 @@ namespace cest
if (test_case->failed)
{
std::cout << " Failed at line " << test_case->failure_line << ": " << test_case->failure_message << std::endl;
tryPrintFailedLines(test_case);
}
}

Expand All @@ -84,7 +121,8 @@ namespace cest
}

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

std::cout << ASCII_BOLD << " " << suite->test_cases[0]->fn.file << ASCII_RESET << std::endl;
}

void printTreeSuiteResult(cest::TestSuite *suite, int indentation = 0)
Expand All @@ -99,13 +137,19 @@ namespace cest
for (cest::TestCase *test_case : suite->test_cases)
{
if (test_case->failed)
std::cout << spacing << ASCII_RED << ASCII_CROSS << ASCII_RESET;
std::cout << " " << spacing << ASCII_RED << ASCII_CROSS << ASCII_RESET;
else if (test_case->condition == cest::TestCaseCondition::Skipped)
std::cout << spacing << ASCII_YELLOW << ASCII_TRIANGLE << ASCII_RESET;
std::cout << " " << spacing << ASCII_YELLOW << ASCII_TRIANGLE << ASCII_RESET;
else
std::cout << spacing << ASCII_GREEN << ASCII_CHECK << ASCII_RESET;
std::cout << " " << spacing << ASCII_GREEN << ASCII_CHECK << ASCII_RESET;

std::cout << " " << ASCII_GRAY << test_case->name << ASCII_RESET << std::endl;

if (test_case->failed)
{
std::cout << " Failed at line " << test_case->failure_line << ": " << test_case->failure_message << std::endl;
tryPrintFailedLines(test_case);
}
}

for (auto &pair : suite->test_suites)
Expand Down

0 comments on commit d8c08c1

Please sign in to comment.