xref: /aosp_15_r20/external/pigweed/pw_unit_test/public/pw_unit_test/simple_printing_event_handler.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 /// Predefined event handler implementation that produces human-readable
27 /// GoogleTest-style test output and sends it to a sink that you define.
28 /// See ``pw::unit_test::EventHandler`` for explanations of emitted events.
29 ///
30 /// Example:
31 ///
32 /// @code{.cpp}
33 ///   #include "pw_unit_test/framework.h"
34 ///   // pw_unit_test:light requires an event handler to be configured.
35 ///   #include "pw_unit_test/simple_printing_event_handler.h"
36 ///
37 ///   void WriteString(std::string_view string, bool newline) {
38 ///     printf("%s", string.data());
39 ///     if (newline) {
40 ///       printf("\n");
41 ///     }
42 ///   }
43 ///
44 ///   int main() {
45 ///     // The following line has no effect with pw_unit_test_light, but makes
46 ///     // this test compatible with upstream GoogleTest.
47 ///     testing::InitGoogleTest();
48 ///     // Since we are using pw_unit_test:light, set up an event handler.
49 ///     pw::unit_test::SimplePrintingEventHandler handler(WriteString);
50 ///     pw::unit_test::RegisterEventHandler(&handler);
51 ///     return RUN_ALL_TESTS();
52 ///   }
53 /// @endcode
54 ///
55 /// Example output:
56 ///
57 /// @code{.txt}
58 ///   >>> Running MyTestSuite.TestCase1
59 ///   [SUCCESS] 128 <= 129
60 ///   [FAILURE] 'a' == 'b'
61 ///     at ../path/to/my/file_test.cc:4831
62 ///   <<< Test MyTestSuite.TestCase1 failed
63 /// @endcode
64 class SimplePrintingEventHandler : public GoogleTestStyleEventHandler {
65  public:
66   // Function for writing output as a string.
67   using WriteFunction = void (*)(std::string_view string, bool append_newline);
68 
69   // Instantiates an event handler with a function to which to output results.
70   // If verbose is set, information for successful tests is written as well as
71   // failures.
72   constexpr SimplePrintingEventHandler(WriteFunction write_function,
73                                        bool verbose = false)
GoogleTestStyleEventHandler(verbose)74       : GoogleTestStyleEventHandler(verbose),
75         write_(write_function),
76         buffer_{} {}
77 
78  private:
79   void WriteLine(const char* format, ...) override PW_PRINTF_FORMAT(2, 3);
80 
Write(const char * content)81   void Write(const char* content) override { write_(content, false); }
82 
83   WriteFunction write_;
84   char buffer_[512];
85 };
86 
87 }  // namespace pw::unit_test
88