• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 
17 #include <cstddef>
18 #include <string_view>
19 
20 #include "pw_preprocessor/compiler.h"
21 #include "pw_unit_test/event_handler.h"
22 #include "pw_unit_test/googletest_style_event_handler.h"
23 
24 namespace pw::unit_test {
25 
26 // An event handler implementation which produces human-readable test output.
27 //
28 // Example output:
29 //
30 //   >>> Running MyTestSuite.TestCase1
31 //   [SUCCESS] 128 <= 129
32 //   [FAILURE] 'a' == 'b'
33 //     at ../path/to/my/file_test.cc:4831
34 //   <<< Test MyTestSuite.TestCase1 failed
35 //
36 class SimplePrintingEventHandler : public GoogleTestStyleEventHandler {
37  public:
38   // Function for writing output as a string.
39   using WriteFunction = void (*)(const std::string_view& string,
40                                  bool append_newline);
41 
42   // Instantiates an event handler with a function to which to output results.
43   // If verbose is set, information for successful tests is written as well as
44   // failures.
45   constexpr SimplePrintingEventHandler(WriteFunction write_function,
46                                        bool verbose = false)
GoogleTestStyleEventHandler(verbose)47       : GoogleTestStyleEventHandler(verbose),
48         write_(write_function),
49         buffer_{} {}
50 
51  private:
52   void WriteLine(const char* format, ...) override PW_PRINTF_FORMAT(2, 3);
53 
Write(const char * content)54   void Write(const char* content) override { write_(content, false); }
55 
56   WriteFunction write_;
57   char buffer_[512];
58 };
59 
60 }  // namespace pw::unit_test
61