Helper library for tests.
Tests parameterized on test names need to have a way to print their
instance names (to make the test output nicer/easily see which instance
name is problematic).
Unfortunately, HIDL allows more strings as instance names than is
allowed by gtests. In order to work around this limitation, providing a
helper function here that can easily be used to provide a meaningful
gtest test name without risking duplicates or a runtime error.
Bug: 139437880
Test: converted lights VTS test
Change-Id: I952b6b6f075b7edf1c3bf3d980579c89d6eaa5fb
diff --git a/Android.bp b/Android.bp
index 4bd5eb3..608ad1a 100644
--- a/Android.bp
+++ b/Android.bp
@@ -30,6 +30,11 @@
],
}
+cc_library_headers {
+ name: "libhidl_gtest_helpers",
+ export_include_dirs: ["gtest_helpers"],
+}
+
cc_test {
name: "libhidl_test",
defaults: ["libhidl-defaults"],
diff --git a/gtest_helpers/hidl/GtestPrinter.h b/gtest_helpers/hidl/GtestPrinter.h
new file mode 100644
index 0000000..c9f5dd3
--- /dev/null
+++ b/gtest_helpers/hidl/GtestPrinter.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+namespace android {
+namespace hardware {
+
+static inline std::string PrintInstanceNameToString(
+ const testing::TestParamInfo<std::string>& info) {
+ // test names need to be unique -> index prefix
+ std::string name = std::to_string(info.index) + "/" + info.param;
+
+ for (size_t i = 0; i < name.size(); i++) {
+ // gtest test names must only contain alphanumeric characters
+ if (!std::isalnum(name[i])) name[i] = '_';
+ }
+
+ return name;
+}
+
+} // namespace hardware
+} // namespace android