• 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 
23 namespace pw::unit_test {
24 
25 // An event handler implementation which produces human-readable test output.
26 //
27 // Example output:
28 //
29 //   >>> Running MyTestSuite.TestCase1
30 //   [SUCCESS] 128 <= 129
31 //   [FAILURE] 'a' == 'b'
32 //     at ../path/to/my/file_test.cc:4831
33 //   <<< Test MyTestSuite.TestCase1 failed
34 //
35 class SimplePrintingEventHandler : public EventHandler {
36  public:
37   // Function for writing output as a string.
38   using WriteFunction = void (*)(const std::string_view& string,
39                                  bool append_newline);
40 
41   // Instantiates an event handler with a function to which to output results.
42   // If verbose is set, information for successful tests is written as well as
43   // failures.
44   SimplePrintingEventHandler(WriteFunction write_function, bool verbose = false)
write_(write_function)45       : write_(write_function), verbose_(verbose) {}
46 
47   void RunAllTestsStart() override;
48   void RunAllTestsEnd(const RunTestsSummary& run_tests_summary) override;
49   void TestCaseStart(const TestCase& test_case) override;
50   void TestCaseEnd(const TestCase& test_case, TestResult result) override;
51   void TestCaseExpect(const TestCase& test_case,
52                       const TestExpectation& expectation) override;
53   void TestCaseDisabled(const TestCase& test_case) override;
54 
55  private:
56   void WriteLine(const char* format, ...) PW_PRINTF_FORMAT(2, 3);
57 
58   WriteFunction write_;
59   bool verbose_;
60   char buffer_[512];
61 };
62 
63 }  // namespace pw::unit_test
64