From 073d85bccd9093099e2d20d1a2973a4296e80288 Mon Sep 17 00:00:00 2001 From: alandefreitas Date: Fri, 7 Jun 2024 16:11:10 -0300 Subject: [PATCH] test: golden tests print diff --- src/test/TestRunner.cpp | 1 + src/test_suite/diff.cpp | 11 +++++++---- src/test_suite/diff.hpp | 9 ++++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/test/TestRunner.cpp b/src/test/TestRunner.cpp index 00781f236..dfd49b395 100644 --- a/src/test/TestRunner.cpp +++ b/src/test/TestRunner.cpp @@ -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 #include #include diff --git a/src/test_suite/diff.cpp b/src/test_suite/diff.cpp index 9981f82ed..bb8a01718 100644 --- a/src/test_suite/diff.cpp +++ b/src/test_suite/diff.cpp @@ -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 &lines) @@ -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 diff --git a/src/test_suite/diff.hpp b/src/test_suite/diff.hpp index 474d7e904..50665b457 100644 --- a/src/test_suite/diff.hpp +++ b/src/test_suite/diff.hpp @@ -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 @@ -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 = {}); }