C/C++ API Reference
Loading...
Searching...
No Matches
test_record_event_handler.h
1// Copyright 2024 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#pragma once
15
16#include <string_view>
17
18#include "pw_json/builder.h"
19#include "pw_unit_test/event_handler.h"
20#include "pw_unit_test/internal/test_record_trie.h"
21
22namespace pw::unit_test {
23namespace json_impl {
24
25inline constexpr const char* kSkipMacroIndicator = "(test skipped)";
26
27} // namespace json_impl
28
30
43 public:
49 TestRecordEventHandler(int64_t seconds_since_epoch)
50 : seconds_since_epoch_(seconds_since_epoch) {}
51
58 void TestCaseEnd(const TestCase& test_case, TestResult result) override {
59 test_record_trie_.AddTestResult(test_case, result);
60 }
61
66 void RunAllTestsEnd(const RunTestsSummary& summary) override {
67 run_tests_summary_ = summary;
68 }
69
81 void TestCaseExpect(const TestCase& test_case,
82 const TestExpectation& expectation) override {
83 // TODO: b/329688428 - Check for test skips directly rather than doing a
84 // string comparison
85 if (std::string_view(json_impl::kSkipMacroIndicator) ==
86 expectation.expression) {
87 test_record_trie_.AddTestResultExpectation(test_case,
88 TestResult::kSkipped);
89 }
90 }
91
101 std::string GetTestRecordJsonString(size_t max_json_buffer_size,
102 bool failing_results_only = false) {
103 return test_record_trie_.GetTestRecordJsonString(run_tests_summary_,
104 seconds_since_epoch_,
105 max_json_buffer_size,
106 failing_results_only);
107 }
108
109 void RunAllTestsStart() override {}
110 void TestProgramStart(const ProgramSummary&) override {}
111 void EnvironmentsSetUpEnd() override {}
112 void TestSuiteStart(const TestSuite&) override {}
113 void TestSuiteEnd(const TestSuite&) override {}
114 void EnvironmentsTearDownEnd() override {}
115 void TestProgramEnd(const ProgramSummary&) override {}
116 void TestCaseStart(const TestCase&) override {}
117 void TestCaseDisabled(const TestCase&) override {}
118
119 private:
120 // Seconds since epoch from the start of the test run.
121 int64_t seconds_since_epoch_;
122
123 // A summary of the test run. Set once RunAllTestsEnd is called and used when
124 // the consumer of this event handler wants to generate the test record json
125 // string.
126 RunTestsSummary run_tests_summary_;
127
128 // The entrypoint for interacting with the test record trie.
129 json_impl::TestRecordTrie test_record_trie_;
130};
131
133
134} // namespace pw::unit_test
Definition: event_handler.h:118
Definition: test_record_event_handler.h:42
void EnvironmentsSetUpEnd() override
Called after environment setup for each iteration of tests ends.
Definition: test_record_event_handler.h:111
TestRecordEventHandler(int64_t seconds_since_epoch)
Definition: test_record_event_handler.h:49
void TestSuiteStart(const TestSuite &) override
Called before the test suite starts.
Definition: test_record_event_handler.h:112
std::string GetTestRecordJsonString(size_t max_json_buffer_size, bool failing_results_only=false)
Definition: test_record_event_handler.h:101
void TestCaseDisabled(const TestCase &) override
Called when a disabled test case is encountered.
Definition: test_record_event_handler.h:117
void EnvironmentsTearDownEnd() override
Called after environment teardown for each iteration of tests ends.
Definition: test_record_event_handler.h:114
void TestProgramEnd(const ProgramSummary &) override
Called after all test activities have ended.
Definition: test_record_event_handler.h:115
void TestCaseEnd(const TestCase &test_case, TestResult result) override
Definition: test_record_event_handler.h:58
void TestCaseExpect(const TestCase &test_case, const TestExpectation &expectation) override
Definition: test_record_event_handler.h:81
void TestProgramStart(const ProgramSummary &) override
Called before any test activity starts.
Definition: test_record_event_handler.h:110
void TestCaseStart(const TestCase &) override
Called when a new test case is started.
Definition: test_record_event_handler.h:116
void RunAllTestsEnd(const RunTestsSummary &summary) override
Definition: test_record_event_handler.h:66
void TestSuiteEnd(const TestSuite &) override
Called after the test suite ends.
Definition: test_record_event_handler.h:113
void RunAllTestsStart() override
Called before all tests are run.
Definition: test_record_event_handler.h:109
TestResult
The result of a complete test run.
Definition: event_handler.h:23
Definition: event_handler.h:69
Definition: event_handler.h:55
Definition: event_handler.h:30
Definition: event_handler.h:41
const char * expression
The source code for the expression which was run.
Definition: event_handler.h:43
Definition: event_handler.h:80