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)
5 
6 //  See http://www.boost.org/libs/test for the library home page.
7 //
8 //  tests Unit Test Framework logging facilities against
9 //  pattern file
10 // ***************************************************************************
11 
12 
13 #ifndef BOOST_TEST_TESTS_LOGGER_FOR_TESTS_HPP__
14 #define BOOST_TEST_TESTS_LOGGER_FOR_TESTS_HPP__
15 
16 #include <boost/test/tools/output_test_stream.hpp>
17 #include <boost/test/utils/algorithm.hpp>
18 
19 using namespace boost::unit_test;
20 using boost::test_tools::output_test_stream;
21 
22 class output_test_stream_for_loggers : public output_test_stream {
23 
24     std::string const source_filename;
25     std::string const basename;
26 
27 public:
output_test_stream_for_loggers(boost::unit_test::const_string pattern_file_name=boost::unit_test::const_string (),bool match_or_save=true,bool text_or_binary=true,const std::string & source_filename_=__FILE__)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_))
36     {}
37 
normalize_path(const std::string & str)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(
42                     str,
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])
45               );
46     }
47 
get_basename(const std::string & source_filename)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);
53         }
54         return basename;
55     }
56 
get_stream_string_representation() const57     virtual std::string get_stream_string_representation() const {
58         std::string current_string = output_test_stream::get_stream_string_representation();
59 
60         std::string pathname_fixes;
61         {
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(
65                 current_string,
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])
68             );
69         }
70 
71         std::string other_vars_fixes;
72         {
73             std::ostringstream s_version;
74             s_version << BOOST_VERSION/100000 << "." << BOOST_VERSION/100 % 1000 << "." << BOOST_VERSION % 100;
75 
76             const std::string to_look_for[] = {"time=\"*\"",
77                                                basename + "(*):",
78                                                "unknown location(*):",
79                                                "; testing time: *us\n", // removing this is far more easier than adding a testing time
80                                                "; testing time: *ms\n",
81                                                "<TestingTime>*</TestingTime>",
82                                                "condition 2>3 is not satisfied\n",
83                                                "condition 2>3 is not satisfied]",
84                                                BOOST_PLATFORM,
85                                                BOOST_STDLIB,
86                                                BOOST_COMPILER,
87                                                s_version.str()
88                                                };
89 
90             const std::string to_replace[]  = {"time=\"0.1234\"",
91                                                basename + ":*:" ,
92                                                "unknown location:*:",
93                                                "\n",
94                                                "\n",
95                                                "<TestingTime>ZZZ</TestingTime>",
96                                                "condition 2>3 is not satisfied [2 <= 3]\n",
97                                                "condition 2>3 is not satisfied [2 <= 3]]",
98                                                "BOOST_SOME_PLATFORM",
99                                                "BOOST_SOME_STDLIB",
100                                                "BOOST_SOME_COMPILER",
101                                                "BOOST_1.XX.Y_SOME_VERSION",
102                                                };
103 
104             other_vars_fixes = utils::replace_all_occurrences_with_wildcards(
105                 pathname_fixes,
106                 to_look_for, to_look_for + sizeof(to_look_for)/sizeof(to_look_for[0]),
107                 to_replace, to_replace + sizeof(to_replace)/sizeof(to_replace[0])
108             );
109         }
110 
111         return other_vars_fixes;
112     }
113 
114 };
115 
116 #endif /* BOOST_TEST_TESTS_LOGGER_FOR_TESTS_HPP__ */
117