xref: /aosp_15_r20/system/libhidl/gtest_helper/hidl/GtestPrinter.h (revision 8222fbe171c3d6fadfe95119c180cf3010c392a8)
1*8222fbe1SAndroid Build Coastguard Worker /*
2*8222fbe1SAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*8222fbe1SAndroid Build Coastguard Worker  *
4*8222fbe1SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8222fbe1SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8222fbe1SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8222fbe1SAndroid Build Coastguard Worker  *
8*8222fbe1SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8222fbe1SAndroid Build Coastguard Worker  *
10*8222fbe1SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8222fbe1SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8222fbe1SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8222fbe1SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8222fbe1SAndroid Build Coastguard Worker  * limitations under the License.
15*8222fbe1SAndroid Build Coastguard Worker  */
16*8222fbe1SAndroid Build Coastguard Worker 
17*8222fbe1SAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*8222fbe1SAndroid Build Coastguard Worker 
19*8222fbe1SAndroid Build Coastguard Worker namespace android {
20*8222fbe1SAndroid Build Coastguard Worker namespace hardware {
21*8222fbe1SAndroid Build Coastguard Worker 
Sanitize(std::string name)22*8222fbe1SAndroid Build Coastguard Worker static inline std::string Sanitize(std::string name) {
23*8222fbe1SAndroid Build Coastguard Worker     for (size_t i = 0; i < name.size(); i++) {
24*8222fbe1SAndroid Build Coastguard Worker         // gtest test names must only contain alphanumeric characters
25*8222fbe1SAndroid Build Coastguard Worker         if (!std::isalnum(name[i])) name[i] = '_';
26*8222fbe1SAndroid Build Coastguard Worker     }
27*8222fbe1SAndroid Build Coastguard Worker     return name;
28*8222fbe1SAndroid Build Coastguard Worker }
29*8222fbe1SAndroid Build Coastguard Worker 
PrintInstanceNameToString(const testing::TestParamInfo<std::string> & info)30*8222fbe1SAndroid Build Coastguard Worker static inline std::string PrintInstanceNameToString(
31*8222fbe1SAndroid Build Coastguard Worker         const testing::TestParamInfo<std::string>& info) {
32*8222fbe1SAndroid Build Coastguard Worker     // test names need to be unique -> index prefix
33*8222fbe1SAndroid Build Coastguard Worker     std::string name = std::to_string(info.index) + "/" + info.param;
34*8222fbe1SAndroid Build Coastguard Worker     return Sanitize(name);
35*8222fbe1SAndroid Build Coastguard Worker }
36*8222fbe1SAndroid Build Coastguard Worker 
37*8222fbe1SAndroid Build Coastguard Worker template <typename... T>
PrintInstanceTupleNameToString(const testing::TestParamInfo<std::tuple<T...>> & info)38*8222fbe1SAndroid Build Coastguard Worker static inline std::string PrintInstanceTupleNameToString(
39*8222fbe1SAndroid Build Coastguard Worker         const testing::TestParamInfo<std::tuple<T...>>& info) {
40*8222fbe1SAndroid Build Coastguard Worker     std::vector<std::string> instances = std::apply(
41*8222fbe1SAndroid Build Coastguard Worker             [](auto&&... elems) {
42*8222fbe1SAndroid Build Coastguard Worker                 std::vector<std::string> instances;
43*8222fbe1SAndroid Build Coastguard Worker                 instances.reserve(sizeof...(elems));
44*8222fbe1SAndroid Build Coastguard Worker                 (instances.push_back(std::forward<decltype(elems)>(elems)), ...);
45*8222fbe1SAndroid Build Coastguard Worker                 return instances;
46*8222fbe1SAndroid Build Coastguard Worker             },
47*8222fbe1SAndroid Build Coastguard Worker             info.param);
48*8222fbe1SAndroid Build Coastguard Worker     std::string param_string;
49*8222fbe1SAndroid Build Coastguard Worker     for (const std::string& instance : instances) {
50*8222fbe1SAndroid Build Coastguard Worker         param_string += instance + "_";
51*8222fbe1SAndroid Build Coastguard Worker     }
52*8222fbe1SAndroid Build Coastguard Worker     param_string += std::to_string(info.index);
53*8222fbe1SAndroid Build Coastguard Worker 
54*8222fbe1SAndroid Build Coastguard Worker     return Sanitize(param_string);
55*8222fbe1SAndroid Build Coastguard Worker }
56*8222fbe1SAndroid Build Coastguard Worker 
57*8222fbe1SAndroid Build Coastguard Worker }  // namespace hardware
58*8222fbe1SAndroid Build Coastguard Worker }  // namespace android
59