1 // Copyright 2023 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 #pragma once 16 #include <gmock/gmock.h> 17 18 #include "pw_bluetooth_sapphire/internal/host/common/assert.h" 19 #include "pw_bluetooth_sapphire/internal/host/testing/inspect.h" 20 21 #ifndef NINSPECT 22 23 namespace bt::testing { 24 25 // Read the hierarchy of |inspector|. 26 inspect::Hierarchy ReadInspect(const inspect::Inspector& inspector); 27 28 // Get the value of the property at |path|. The last item in |path| 29 // should be the property name. 30 // Example: 31 // EXPECT_THAT(GetInspectValue<inspect::IntPropertyValue>(inspector, {"node", 32 // "property"}), 33 // Optional(42)); 34 template <class PropertyValue> GetInspectValue(const inspect::Inspector & inspector,std::vector<std::string> path)35std::optional<typename PropertyValue::value_type> GetInspectValue( 36 const inspect::Inspector& inspector, std::vector<std::string> path) { 37 if (path.empty()) { 38 return std::nullopt; 39 } 40 41 // The last path item is the property name. 42 std::string property = path.back(); 43 path.pop_back(); 44 45 inspect::Hierarchy hierarchy = ReadInspect(inspector); 46 auto node = hierarchy.GetByPath(path); 47 if (!node) { 48 return std::nullopt; 49 } 50 const PropertyValue* prop_value = 51 node->node().get_property<PropertyValue>(property); 52 if (!prop_value) { 53 return std::nullopt; 54 } 55 return prop_value->value(); 56 } 57 58 } // namespace bt::testing 59 60 #endif // NINSPECT 61