xref: /aosp_15_r20/external/pigweed/pw_unit_test/multi_event_handler_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 
15 #include "pw_unit_test/multi_event_handler.h"
16 
17 #include "pw_unit_test/framework.h"
18 
19 namespace pw::unit_test {
20 namespace {
21 
22 // Fake event handler that keeps track of how many times its functions were
23 // invoked
24 class FakeEventHandler : public EventHandler {
25  public:
26   struct {
27     int TestProgramStart = 0;
28     int EnvironmentsSetUpEnd = 0;
29     int TestSuiteStart = 0;
30     int TestSuiteEnd = 0;
31     int EnvironmentsTearDownEnd = 0;
32     int TestProgramEnd = 0;
33     int RunAllTestsStart = 0;
34     int RunAllTestsEnd = 0;
35     int TestCaseStart = 0;
36     int TestCaseEnd = 0;
37     int TestCaseExpect = 0;
38     int TestCaseDisabled = 0;
39   } function_invocation_counts;
40 
TestProgramStart(const ProgramSummary &)41   void TestProgramStart(const ProgramSummary&) override {
42     function_invocation_counts.TestProgramStart++;
43   }
EnvironmentsSetUpEnd()44   void EnvironmentsSetUpEnd() override {
45     function_invocation_counts.EnvironmentsSetUpEnd++;
46   }
TestSuiteStart(const TestSuite &)47   void TestSuiteStart(const TestSuite&) override {
48     function_invocation_counts.TestSuiteStart++;
49   }
TestSuiteEnd(const TestSuite &)50   void TestSuiteEnd(const TestSuite&) override {
51     function_invocation_counts.TestSuiteEnd++;
52   }
EnvironmentsTearDownEnd()53   void EnvironmentsTearDownEnd() override {
54     function_invocation_counts.EnvironmentsTearDownEnd++;
55   }
TestProgramEnd(const ProgramSummary &)56   void TestProgramEnd(const ProgramSummary&) override {
57     function_invocation_counts.TestProgramEnd++;
58   }
RunAllTestsStart()59   void RunAllTestsStart() override {
60     function_invocation_counts.RunAllTestsStart++;
61   }
RunAllTestsEnd(const RunTestsSummary &)62   void RunAllTestsEnd(const RunTestsSummary&) override {
63     function_invocation_counts.RunAllTestsEnd++;
64   }
TestCaseStart(const TestCase &)65   void TestCaseStart(const TestCase&) override {
66     function_invocation_counts.TestCaseStart++;
67   }
TestCaseEnd(const TestCase &,TestResult)68   void TestCaseEnd(const TestCase&, TestResult) override {
69     function_invocation_counts.TestCaseEnd++;
70   }
TestCaseExpect(const TestCase &,const TestExpectation &)71   void TestCaseExpect(const TestCase&, const TestExpectation&) override {
72     function_invocation_counts.TestCaseExpect++;
73   }
TestCaseDisabled(const TestCase &)74   void TestCaseDisabled(const TestCase&) override {
75     function_invocation_counts.TestCaseDisabled++;
76   }
77 };
78 
79 // Helper method for ensuring all methods of an event handler were called x
80 // number of times
AssertFunctionInvocationCounts(FakeEventHandler handler,int num_invocations)81 void AssertFunctionInvocationCounts(FakeEventHandler handler,
82                                     int num_invocations) {
83   ASSERT_EQ(handler.function_invocation_counts.TestProgramStart,
84             num_invocations);
85   ASSERT_EQ(handler.function_invocation_counts.EnvironmentsSetUpEnd,
86             num_invocations);
87   ASSERT_EQ(handler.function_invocation_counts.TestSuiteStart, num_invocations);
88   ASSERT_EQ(handler.function_invocation_counts.TestSuiteEnd, num_invocations);
89   ASSERT_EQ(handler.function_invocation_counts.EnvironmentsTearDownEnd,
90             num_invocations);
91   ASSERT_EQ(handler.function_invocation_counts.TestProgramEnd, num_invocations);
92   ASSERT_EQ(handler.function_invocation_counts.RunAllTestsStart,
93             num_invocations);
94   ASSERT_EQ(handler.function_invocation_counts.RunAllTestsEnd, num_invocations);
95   ASSERT_EQ(handler.function_invocation_counts.TestCaseStart, num_invocations);
96   ASSERT_EQ(handler.function_invocation_counts.TestCaseEnd, num_invocations);
97   ASSERT_EQ(handler.function_invocation_counts.TestCaseExpect, num_invocations);
98   ASSERT_EQ(handler.function_invocation_counts.TestCaseDisabled,
99             num_invocations);
100 }
101 
TEST(AllEventHandlerMethodsCalled,InvokeMethodMultipleTimes)102 TEST(AllEventHandlerMethodsCalled, InvokeMethodMultipleTimes) {
103   FakeEventHandler h1;
104   FakeEventHandler h2;
105   MultiEventHandler<2> multi_handler(h1, h2);
106 
107   ASSERT_EQ(h1.function_invocation_counts.TestCaseStart, 0);
108   ASSERT_EQ(h2.function_invocation_counts.TestCaseEnd, 0);
109 
110   TestCase test_case{};
111   TestResult test_result = TestResult::kSuccess;
112   multi_handler.TestCaseStart(test_case);
113   multi_handler.TestCaseStart(test_case);
114   multi_handler.TestCaseStart(test_case);
115   multi_handler.TestCaseEnd(test_case, test_result);
116   multi_handler.TestCaseEnd(test_case, test_result);
117   multi_handler.TestCaseEnd(test_case, test_result);
118 
119   ASSERT_EQ(h1.function_invocation_counts.TestCaseStart, 3);
120   ASSERT_EQ(h2.function_invocation_counts.TestCaseEnd, 3);
121 }
122 
TEST(AllEventHandlerMethodsCalled,InvokeAllEventHandlerMethods)123 TEST(AllEventHandlerMethodsCalled, InvokeAllEventHandlerMethods) {
124   FakeEventHandler h1;
125   FakeEventHandler h2;
126   MultiEventHandler<2> multi_handler(h1, h2);
127 
128   AssertFunctionInvocationCounts(h1, 0);
129   AssertFunctionInvocationCounts(h2, 0);
130 
131   ProgramSummary program_summary{};
132   TestSuite test_suite{};
133   TestCase test_case{};
134   RunTestsSummary run_test_summary{};
135   TestExpectation expectation{};
136   TestResult test_result = TestResult::kSuccess;
137   multi_handler.TestProgramStart(program_summary);
138   multi_handler.EnvironmentsSetUpEnd();
139   multi_handler.TestSuiteStart(test_suite);
140   multi_handler.TestSuiteEnd(test_suite);
141   multi_handler.EnvironmentsTearDownEnd();
142   multi_handler.TestProgramEnd(program_summary);
143   multi_handler.RunAllTestsStart();
144   multi_handler.RunAllTestsEnd(run_test_summary);
145   multi_handler.TestCaseStart(test_case);
146   multi_handler.TestCaseEnd(test_case, test_result);
147   multi_handler.TestCaseExpect(test_case, expectation);
148   multi_handler.TestCaseDisabled(test_case);
149 
150   AssertFunctionInvocationCounts(h1, 1);
151   AssertFunctionInvocationCounts(h2, 1);
152 }
153 
154 }  // namespace
155 }  // namespace pw::unit_test