Skip to content

Commit

Permalink
test: golden tests print diff
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Jun 11, 2024
1 parent 3fa59db commit 073d85b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/test/TestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "lib/Lib/CorpusImpl.hpp"
#include "lib/Lib/MrDocsCompilationDatabase.hpp"
#include "lib/Lib/SingleFileDB.hpp"
#include "test_suite/diff.hpp"
#include <mrdocs/Config.hpp>
#include <mrdocs/Generators.hpp>
#include <mrdocs/Platform.hpp>
Expand Down
11 changes: 7 additions & 4 deletions src/test_suite/diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace test_suite {
// Diff two strings and return the result as a string with additional stats
DiffStringsResult
diffStrings(std::string_view str1, std::string_view str2, std::size_t context_size = 3)
diffStrings(std::string_view str1, std::string_view str2, std::size_t context_size)
{
static constexpr auto splitLines =
[](std::string_view text, std::vector<std::string_view> &lines)
Expand Down Expand Up @@ -280,9 +280,12 @@ BOOST_TEST_DIFF(
DiffStringsResult diff = diffStrings(expected_contents, rendered_contents);
if (diff.added > 0 || diff.removed > 0)
{
std::ofstream out((std::string(error_output_path)));
BOOST_TEST(out);
out << rendered_contents;
if (!error_output_path.empty())
{
std::ofstream out((std::string(error_output_path)));
BOOST_TEST(out);
out << rendered_contents;
}
#ifdef MRDOCS_TEST_HAS_FMT
fmt::println("DIFF:\n=====================\n{}\n=====================", diff.diff);
#else
Expand Down
9 changes: 8 additions & 1 deletion src/test_suite/diff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ struct DiffStringsResult
int unmodified{0};
};

/// Diff two strings and return the result as a string with additional stats
DiffStringsResult
diffStrings(
std::string_view str1,
std::string_view str2,
std::size_t context_size = 3);

/** Perform a diff between two strings and check if they are equal
This function is used to compare the contents of a file with the expected
Expand Down Expand Up @@ -65,7 +72,7 @@ BOOST_TEST_DIFF(
std::string_view expected_contents,
std::string_view expected_contents_path,
std::string_view rendered_contents,
std::string_view error_output_path);
std::string_view error_output_path = {});

}

Expand Down

0 comments on commit 073d85b

Please sign in to comment.