xref: /aosp_15_r20/external/pigweed/pw_unit_test/public/pw_unit_test/googletest_style_event_handler.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 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 "pw_preprocessor/compiler.h"
18 #include "pw_unit_test/event_handler.h"
19 
20 // Define the test messages and string formats as literal strings so they
21 // work with different log databases.
22 #define PW_UNIT_TEST_GOOGLETEST_RUN_ALL_TESTS_START \
23   "[==========] Running all tests."
24 #define PW_UNIT_TEST_GOOGLETEST_RUN_ALL_TESTS_END \
25   "[==========] Done running all tests."
26 
27 #define PW_UNIT_TEST_GOOGLETEST_TEST_PROGRAM_START \
28   "[==========] Running %d tests from %d test suite%s."
29 
30 #define PW_UNIT_TEST_GOOGLETEST_TEST_PROGRAM_END \
31   "[==========] %d / %d tests from %d test suite%s ran."
32 
33 #define PW_UNIT_TEST_GOOGLETEST_ENVIRONMENTS_SETUP_END \
34   "[----------] Global test environments setup."
35 
36 #define PW_UNIT_TEST_GOOGLETEST_ENVIRONMENTS_TEAR_DOWN_END \
37   "[----------] Global test environments tear-down."
38 
39 #define PW_UNIT_TEST_GOOGLETEST_TEST_SUITE_START \
40   "[----------] %d tests from %s."
41 
42 #define PW_UNIT_TEST_GOOGLETEST_TEST_SUITE_END "[----------] %d tests from %s."
43 
44 #define PW_UNIT_TEST_GOOGLETEST_PASSED_SUMMARY "[  PASSED  ] %d test(s)."
45 #define PW_UNIT_TEST_GOOGLETEST_DISABLED_SUMMARY "[ DISABLED ] %d test(s)."
46 #define PW_UNIT_TEST_GOOGLETEST_FAILED_SUMMARY "[  FAILED  ] %d test(s)."
47 
48 #define PW_UNIT_TEST_GOOGLETEST_CASE_START "[ RUN      ] %s.%s"
49 #define PW_UNIT_TEST_GOOGLETEST_CASE_OK "[       OK ] %s.%s"
50 #define PW_UNIT_TEST_GOOGLETEST_CASE_FAILED "[  FAILED  ] %s.%s"
51 #define PW_UNIT_TEST_GOOGLETEST_CASE_DISABLED "[ DISABLED ] %s.%s"
52 
53 namespace pw {
54 namespace unit_test {
55 
56 /// Provides GoogleTest-style output for ``pw_unit_test:light`` events. Must
57 /// be extended to define how to output the results. See
58 /// ``pw::unit_test::EventHandler`` for an explanation of each event and
59 /// ``pw::unit_test::SimplePrintingEventHandler`` for an example of a
60 /// concrete implementation of this interface.
61 class GoogleTestStyleEventHandler : public EventHandler {
62  public:
63   void TestProgramStart(const ProgramSummary& program_summary) override;
64   void EnvironmentsSetUpEnd() override;
65   void TestSuiteStart(const TestSuite& test_suite) override;
66   void TestSuiteEnd(const TestSuite& test_suite) override;
67   void EnvironmentsTearDownEnd() override;
68   void TestProgramEnd(const ProgramSummary& program_summary) override;
69 
70   void RunAllTestsStart() override;
71   void RunAllTestsEnd(const RunTestsSummary& run_tests_summary) override;
72   void TestCaseStart(const TestCase& test_case) override;
73   void TestCaseEnd(const TestCase& test_case, TestResult result) override;
74   void TestCaseExpect(const TestCase& test_case,
75                       const TestExpectation& expectation) override;
76   void TestCaseDisabled(const TestCase& test_case) override;
77 
78  protected:
GoogleTestStyleEventHandler(bool verbose)79   constexpr GoogleTestStyleEventHandler(bool verbose) : verbose_(verbose) {}
80 
verbose()81   bool verbose() const { return verbose_; }
82 
83   // Writes the content without a trailing newline.
84   virtual void Write(const char* content) = 0;
85 
86   // Writes the formatted content and appends a newline character.
87   virtual void WriteLine(const char* format, ...) PW_PRINTF_FORMAT(2, 3) = 0;
88 
89  private:
90   bool verbose_;
91 };
92 
93 }  // namespace unit_test
94 }  // namespace pw
95