Skip to content

Commit 073d85b

Browse files
committed
test: golden tests print diff
1 parent 3fa59db commit 073d85b

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/test/TestRunner.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "lib/Lib/CorpusImpl.hpp"
1717
#include "lib/Lib/MrDocsCompilationDatabase.hpp"
1818
#include "lib/Lib/SingleFileDB.hpp"
19+
#include "test_suite/diff.hpp"
1920
#include <mrdocs/Config.hpp>
2021
#include <mrdocs/Generators.hpp>
2122
#include <mrdocs/Platform.hpp>

src/test_suite/diff.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
namespace test_suite {
2424
// Diff two strings and return the result as a string with additional stats
2525
DiffStringsResult
26-
diffStrings(std::string_view str1, std::string_view str2, std::size_t context_size = 3)
26+
diffStrings(std::string_view str1, std::string_view str2, std::size_t context_size)
2727
{
2828
static constexpr auto splitLines =
2929
[](std::string_view text, std::vector<std::string_view> &lines)
@@ -280,9 +280,12 @@ BOOST_TEST_DIFF(
280280
DiffStringsResult diff = diffStrings(expected_contents, rendered_contents);
281281
if (diff.added > 0 || diff.removed > 0)
282282
{
283-
std::ofstream out((std::string(error_output_path)));
284-
BOOST_TEST(out);
285-
out << rendered_contents;
283+
if (!error_output_path.empty())
284+
{
285+
std::ofstream out((std::string(error_output_path)));
286+
BOOST_TEST(out);
287+
out << rendered_contents;
288+
}
286289
#ifdef MRDOCS_TEST_HAS_FMT
287290
fmt::println("DIFF:\n=====================\n{}\n=====================", diff.diff);
288291
#else

src/test_suite/diff.hpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ struct DiffStringsResult
3737
int unmodified{0};
3838
};
3939

40+
/// Diff two strings and return the result as a string with additional stats
41+
DiffStringsResult
42+
diffStrings(
43+
std::string_view str1,
44+
std::string_view str2,
45+
std::size_t context_size = 3);
46+
4047
/** Perform a diff between two strings and check if they are equal
4148
4249
This function is used to compare the contents of a file with the expected
@@ -65,7 +72,7 @@ BOOST_TEST_DIFF(
6572
std::string_view expected_contents,
6673
std::string_view expected_contents_path,
6774
std::string_view rendered_contents,
68-
std::string_view error_output_path);
75+
std::string_view error_output_path = {});
6976

7077
}
7178

0 commit comments

Comments
 (0)