• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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/unit_test_service.h"
16 
17 #include "gtest/gtest.h"
18 #include "pw_containers/vector.h"
19 #include "pw_log/log.h"
20 #include "pw_protobuf/decoder.h"
21 
22 namespace pw::unit_test {
23 
Run(ConstByteSpan request,RawServerWriter & writer)24 void UnitTestService::Run(ConstByteSpan request, RawServerWriter& writer) {
25   writer_ = std::move(writer);
26   verbose_ = false;
27 
28   // List of test suite names to run. The string views in this vector point to
29   // data in the raw protobuf request message, so it is only valid for the
30   // duration of this function.
31   pw::Vector<std::string_view, 16> suites_to_run;
32 
33   protobuf::Decoder decoder(request);
34 
35   Status status;
36   while ((status = decoder.Next()).ok()) {
37     switch (static_cast<TestRunRequest::Fields>(decoder.FieldNumber())) {
38       case TestRunRequest::Fields::kReportPassedExpectations:
39         decoder.ReadBool(&verbose_)
40             .IgnoreError();  // TODO(b/242598609): Handle Status properly
41         break;
42 
43       case TestRunRequest::Fields::kTestSuite: {
44         std::string_view suite_name;
45         if (!decoder.ReadString(&suite_name).ok()) {
46           break;
47         }
48 
49         if (!suites_to_run.full()) {
50           suites_to_run.push_back(suite_name);
51         } else {
52           PW_LOG_ERROR("Maximum of %u test suite filters supported",
53                        static_cast<unsigned>(suites_to_run.max_size()));
54           writer_.Finish(Status::InvalidArgument())
55               .IgnoreError();  // TODO(b/242598609): Handle Status properly
56           return;
57         }
58 
59         break;
60       }
61     }
62   }
63 
64   if (status != Status::OutOfRange()) {
65     writer_.Finish(status)
66         .IgnoreError();  // TODO(b/242598609): Handle Status properly
67     return;
68   }
69 
70   PW_LOG_INFO("Starting unit test run");
71   handler_.ExecuteTests(suites_to_run);
72   PW_LOG_INFO("Unit test run complete");
73 
74   writer_.Finish().IgnoreError();  // TODO(b/242598609): Handle Status properly
75 }
76 
WriteTestRunStart()77 void UnitTestService::WriteTestRunStart() {
78   // Write out the key for the start field (even though the message is empty).
79   WriteEvent(
80       [&](Event::StreamEncoder& event) { event.GetTestRunStartEncoder(); });
81 }
82 
WriteTestRunEnd(const RunTestsSummary & summary)83 void UnitTestService::WriteTestRunEnd(const RunTestsSummary& summary) {
84   WriteEvent([&](Event::StreamEncoder& event) {
85     TestRunEnd::StreamEncoder test_run_end = event.GetTestRunEndEncoder();
86     test_run_end.WritePassed(summary.passed_tests)
87         .IgnoreError();  // TODO(b/242598609): Handle Status properly
88     test_run_end.WriteFailed(summary.failed_tests)
89         .IgnoreError();  // TODO(b/242598609): Handle Status properly
90     test_run_end.WriteSkipped(summary.skipped_tests)
91         .IgnoreError();  // TODO(b/242598609): Handle Status properly
92     test_run_end.WriteDisabled(summary.disabled_tests)
93         .IgnoreError();  // TODO(b/242598609): Handle Status properly
94   });
95 }
96 
WriteTestCaseStart(const TestCase & test_case)97 void UnitTestService::WriteTestCaseStart(const TestCase& test_case) {
98   WriteEvent([&](Event::StreamEncoder& event) {
99     TestCaseDescriptor::StreamEncoder descriptor =
100         event.GetTestCaseStartEncoder();
101     descriptor.WriteSuiteName(test_case.suite_name)
102         .IgnoreError();  // TODO(b/242598609): Handle Status properly
103     descriptor.WriteTestName(test_case.test_name)
104         .IgnoreError();  // TODO(b/242598609): Handle Status properly
105     descriptor.WriteFileName(test_case.file_name)
106         .IgnoreError();  // TODO(b/242598609): Handle Status properly
107   });
108 }
109 
WriteTestCaseEnd(TestResult result)110 void UnitTestService::WriteTestCaseEnd(TestResult result) {
111   WriteEvent([&](Event::StreamEncoder& event) {
112     event.WriteTestCaseEnd(static_cast<TestCaseResult>(result))
113         .IgnoreError();  // TODO(b/242598609): Handle Status properly
114   });
115 }
116 
WriteTestCaseDisabled(const TestCase & test_case)117 void UnitTestService::WriteTestCaseDisabled(const TestCase& test_case) {
118   WriteEvent([&](Event::StreamEncoder& event) {
119     TestCaseDescriptor::StreamEncoder descriptor =
120         event.GetTestCaseDisabledEncoder();
121     descriptor.WriteSuiteName(test_case.suite_name)
122         .IgnoreError();  // TODO(b/242598609): Handle Status properly
123     descriptor.WriteTestName(test_case.test_name)
124         .IgnoreError();  // TODO(b/242598609): Handle Status properly
125     descriptor.WriteFileName(test_case.file_name)
126         .IgnoreError();  // TODO(b/242598609): Handle Status properly
127   });
128 }
129 
WriteTestCaseExpectation(const TestExpectation & expectation)130 void UnitTestService::WriteTestCaseExpectation(
131     const TestExpectation& expectation) {
132   if (!verbose_ && expectation.success) {
133     return;
134   }
135 
136   WriteEvent([&](Event::StreamEncoder& event) {
137     TestCaseExpectation::StreamEncoder test_case_expectation =
138         event.GetTestCaseExpectationEncoder();
139     test_case_expectation.WriteExpression(expectation.expression)
140         .IgnoreError();  // TODO(b/242598609): Handle Status properly
141     test_case_expectation
142         .WriteEvaluatedExpression(expectation.evaluated_expression)
143         .IgnoreError();  // TODO(b/242598609): Handle Status properly
144     test_case_expectation.WriteLineNumber(expectation.line_number)
145         .IgnoreError();  // TODO(b/242598609): Handle Status properly
146     test_case_expectation.WriteSuccess(expectation.success)
147         .IgnoreError();  // TODO(b/242598609): Handle Status properly
148   });
149 }
150 
151 }  // namespace pw::unit_test
152