1 // (C) Copyright Raffi Enficiaud 2017.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
8 // tests Unit Test Framework logging facilities against
10 // ***************************************************************************
13 #ifndef BOOST_TEST_TESTS_LOGGER_FOR_TESTS_HPP__
14 #define BOOST_TEST_TESTS_LOGGER_FOR_TESTS_HPP__
16 #include <boost/test/tools/output_test_stream.hpp>
17 #include <boost/test/utils/algorithm.hpp>
19 using namespace boost::unit_test;
20 using boost::test_tools::output_test_stream;
22 class output_test_stream_for_loggers : public output_test_stream {
24 std::string const source_filename;
25 std::string const basename;
28 explicit output_test_stream_for_loggers(
29 boost::unit_test::const_string pattern_file_name = boost::unit_test::const_string(),
30 bool match_or_save = true,
31 bool text_or_binary = true,
32 const std::string& source_filename_ = __FILE__)
33 : output_test_stream(pattern_file_name, match_or_save, text_or_binary)
34 , source_filename(source_filename_)
35 , basename(get_basename(source_filename_))
38 static std::string normalize_path(const std::string &str) {
39 const std::string to_look_for[] = {"\\"};
40 const std::string to_replace[] = {"/"};
41 return utils::replace_all_occurrences_of(
43 to_look_for, to_look_for + sizeof(to_look_for)/sizeof(to_look_for[0]),
44 to_replace, to_replace + sizeof(to_replace)/sizeof(to_replace[0])
48 static std::string get_basename(const std::string &source_filename) {
49 std::string basename = normalize_path(source_filename);
50 std::string::size_type basename_pos = basename.rfind('/');
51 if(basename_pos != std::string::npos) {
52 basename = basename.substr(basename_pos+1);
57 virtual std::string get_stream_string_representation() const {
58 std::string current_string = output_test_stream::get_stream_string_representation();
60 std::string pathname_fixes;
62 const std::string to_look_for[] = {normalize_path(source_filename)};
63 const std::string to_replace[] = {"xxx/" + basename };
64 pathname_fixes = utils::replace_all_occurrences_of(
66 to_look_for, to_look_for + sizeof(to_look_for)/sizeof(to_look_for[0]),
67 to_replace, to_replace + sizeof(to_replace)/sizeof(to_replace[0])
71 std::string other_vars_fixes;
73 const std::string to_look_for[] = {"time=\"*\"",
75 "unknown location(*):",
76 "; testing time: *us\n", // removing this is far more easier than adding a testing time
77 "; testing time: *ms\n",
78 "<TestingTime>*</TestingTime>",
79 "condition 2>3 is not satisfied\n",
80 "condition 2>3 is not satisfied]",
83 const std::string to_replace[] = {"time=\"0.1234\"",
85 "unknown location:*:",
88 "<TestingTime>ZZZ</TestingTime>",
89 "condition 2>3 is not satisfied [2 <= 3]\n",
90 "condition 2>3 is not satisfied [2 <= 3]]",
93 other_vars_fixes = utils::replace_all_occurrences_with_wildcards(
95 to_look_for, to_look_for + sizeof(to_look_for)/sizeof(to_look_for[0]),
96 to_replace, to_replace + sizeof(to_replace)/sizeof(to_replace[0])
100 return other_vars_fixes;
105 #endif /* BOOST_TEST_TESTS_LOGGER_FOR_TESTS_HPP__ */