Pigweed
 
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
41 public:
47 TestRecordEventHandler(int64_t seconds_since_epoch)
48 : seconds_since_epoch_(seconds_since_epoch) {}
49
56 void TestCaseEnd(const TestCase& test_case, TestResult result) override {
57 test_record_trie_.AddTestResult(test_case, result);
58 }
59
64 void RunAllTestsEnd(const RunTestsSummary& summary) override {
65 run_tests_summary_ = summary;
66 }
67
79 void TestCaseExpect(const TestCase& test_case,
80 const TestExpectation& expectation) override {
81 // TODO: b/329688428 - Check for test skips directly rather than doing a
82 // string comparison
83 if (std::string_view(json_impl::kSkipMacroIndicator) ==
84 expectation.expression) {
85 test_record_trie_.AddTestResultExpectation(test_case,
86 TestResult::kSkipped);
87 }
88 }
89
99 std::string GetTestRecordJsonString(size_t max_json_buffer_size,
100 bool failing_results_only = false) {
101 return test_record_trie_.GetTestRecordJsonString(run_tests_summary_,
102 seconds_since_epoch_,
103 max_json_buffer_size,
104 failing_results_only);
105 }
106
107 void RunAllTestsStart() override {}
108 void TestProgramStart(const ProgramSummary&) override {}
109 void EnvironmentsSetUpEnd() override {}
110 void TestSuiteStart(const TestSuite&) override {}
111 void TestSuiteEnd(const TestSuite&) override {}
112 void EnvironmentsTearDownEnd() override {}
113 void TestProgramEnd(const ProgramSummary&) override {}
114 void TestCaseStart(const TestCase&) override {}
115 void TestCaseDisabled(const TestCase&) override {}
116
117 private:
118 // Seconds since epoch from the start of the test run.
119 int64_t seconds_since_epoch_;
120
121 // A summary of the test run. Set once RunAllTestsEnd is called and used when
122 // the consumer of this event handler wants to generate the test record json
123 // string.
124 RunTestsSummary run_tests_summary_;
125
126 // The entrypoint for interacting with the test record trie.
127 json_impl::TestRecordTrie test_record_trie_;
128};
129
130} // namespace pw::unit_test
Definition: event_handler.h:115
Definition: test_record_event_handler.h:40
void EnvironmentsSetUpEnd() override
Called after environment setup for each iteration of tests ends.
Definition: test_record_event_handler.h:109
TestRecordEventHandler(int64_t seconds_since_epoch)
Definition: test_record_event_handler.h:47
void TestSuiteStart(const TestSuite &) override
Called before the test suite starts.
Definition: test_record_event_handler.h:110
std::string GetTestRecordJsonString(size_t max_json_buffer_size, bool failing_results_only=false)
Definition: test_record_event_handler.h:99
void TestCaseDisabled(const TestCase &) override
Called when a disabled test case is encountered.
Definition: test_record_event_handler.h:115
void EnvironmentsTearDownEnd() override
Called after environment teardown for each iteration of tests ends.
Definition: test_record_event_handler.h:112
void TestProgramEnd(const ProgramSummary &) override
Called after all test activities have ended.
Definition: test_record_event_handler.h:113
void TestCaseEnd(const TestCase &test_case, TestResult result) override
Definition: test_record_event_handler.h:56
void TestCaseExpect(const TestCase &test_case, const TestExpectation &expectation) override
Definition: test_record_event_handler.h:79
void TestProgramStart(const ProgramSummary &) override
Called before any test activity starts.
Definition: test_record_event_handler.h:108
void TestCaseStart(const TestCase &) override
Called when a new test case is started.
Definition: test_record_event_handler.h:114
void RunAllTestsEnd(const RunTestsSummary &summary) override
Definition: test_record_event_handler.h:64
void TestSuiteEnd(const TestSuite &) override
Called after the test suite ends.
Definition: test_record_event_handler.h:111
void RunAllTestsStart() override
Called before all tests are run.
Definition: test_record_event_handler.h:107
Definition: event_handler.h:66
Definition: event_handler.h:52
Definition: event_handler.h:27
Definition: event_handler.h:38
Definition: event_handler.h:77