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 #pragma once 15 16 #ifdef PW_BLUETOOTH_SAPPHIRE_INSPECT_ENABLED 17 18 #include <gmock/gmock.h> 19 #include <gtest/gtest.h> 20 #include <lib/inspect/cpp/reader.h> 21 22 namespace inspect { 23 24 // Printers for inspect types. 25 void PrintTo(const PropertyValue& property, std::ostream* os); 26 void PrintTo(const NodeValue& node, std::ostream* os); 27 28 // Printer for Hierarchy wrapper. 29 void PrintTo(const Hierarchy& hierarchy, std::ostream* os); 30 31 namespace testing { 32 33 // Type for a matcher matching a Node. 34 using NodeMatcher = ::testing::Matcher<const NodeValue&>; 35 36 // Type for a matcher matching a vector of properties. 37 using PropertiesMatcher = ::testing::Matcher<const std::vector<PropertyValue>&>; 38 39 // Type for a matcher that matches a base path on an |Hierarchy|. 40 using PrefixPathMatcher = ::testing::Matcher<const std::vector<std::string>&>; 41 42 // Type for a matcher that matches a vector of |Hierarchy| children. 43 using ChildrenMatcher = ::testing::Matcher<const std::vector<Hierarchy>&>; 44 45 namespace internal { 46 47 // Matcher interface to check the name of an inspect Nodes. 48 class NameMatchesMatcher 49 : public ::testing::MatcherInterface<const NodeValue&> { 50 public: 51 NameMatchesMatcher(std::string name); 52 53 bool MatchAndExplain(const NodeValue& obj, 54 ::testing::MatchResultListener* listener) const override; 55 56 void DescribeTo(::std::ostream* os) const override; 57 58 void DescribeNegationTo(::std::ostream* os) const override; 59 60 private: 61 std::string name_; 62 }; 63 64 // Matcher interface to check the list of Node properties. 65 class PropertyListMatcher 66 : public ::testing::MatcherInterface<const NodeValue&> { 67 public: 68 PropertyListMatcher(PropertiesMatcher matcher); 69 70 bool MatchAndExplain(const NodeValue& obj, 71 ::testing::MatchResultListener* listener) const override; 72 73 void DescribeTo(::std::ostream* os) const override; 74 75 void DescribeNegationTo(::std::ostream* os) const override; 76 77 private: 78 PropertiesMatcher matcher_; 79 }; 80 81 } // namespace internal 82 83 // Matches against the name of an Inspect Node. 84 // Example: 85 // EXPECT_THAT(node, NameMatches("objects")); 86 ::testing::Matcher<const NodeValue&> NameMatches(std::string name); 87 88 // Matches against the property list of an Inspect Node. 89 // Example: 90 // EXPECT_THAT(node, AllOf(PropertyList(::testing::IsEmpty()))); 91 ::testing::Matcher<const NodeValue&> PropertyList(PropertiesMatcher matcher); 92 93 // Matches a particular StringProperty with the given name using the given 94 // matcher. 95 ::testing::Matcher<const PropertyValue&> StringIs( 96 const std::string& name, ::testing::Matcher<std::string> matcher); 97 98 // Matches a particular ByteVectorProperty with the given name using the given 99 // matcher. 100 ::testing::Matcher<const PropertyValue&> ByteVectorIs( 101 const std::string& name, ::testing::Matcher<std::vector<uint8_t>> matcher); 102 103 // Matches a particular IntProperty with the given name using the given matcher. 104 ::testing::Matcher<const PropertyValue&> IntIs( 105 const std::string& name, ::testing::Matcher<int64_t> matcher); 106 107 // Matches a particular UintProperty with the given name using the given 108 // matcher. 109 ::testing::Matcher<const PropertyValue&> UintIs( 110 const std::string& name, ::testing::Matcher<uint64_t> matcher); 111 112 // Matches a particular DoubleProperty with the given name using the given 113 // matcher. 114 ::testing::Matcher<const PropertyValue&> DoubleIs( 115 const std::string& name, ::testing::Matcher<double> matcher); 116 117 // Matches a particular BoolProperty with the given name using the given 118 // matcher. 119 ::testing::Matcher<const PropertyValue&> BoolIs( 120 const std::string& name, ::testing::Matcher<bool> matcher); 121 122 // Matches the values of an integer array. 123 ::testing::Matcher<const PropertyValue&> IntArrayIs( 124 const std::string& name, ::testing::Matcher<std::vector<int64_t>>); 125 126 // Matches the values of an unsigned integer array. 127 ::testing::Matcher<const PropertyValue&> UintArrayIs( 128 const std::string& name, ::testing::Matcher<std::vector<uint64_t>>); 129 130 // Matches the values of a double width floating point number array. 131 ::testing::Matcher<const PropertyValue&> DoubleArrayIs( 132 const std::string& name, ::testing::Matcher<std::vector<double>>); 133 134 // Matches the display format of a numeric array value. 135 ::testing::Matcher<const PropertyValue&> ArrayDisplayFormatIs( 136 ArrayDisplayFormat format); 137 138 // Matcher for the object inside an Hierarchy. 139 ::testing::Matcher<const Hierarchy&> NodeMatches(NodeMatcher matcher); 140 141 // Matcher for the base path inside an Hierarchy. 142 ::testing::Matcher<const Hierarchy&> PrefixPathMatches( 143 PrefixPathMatcher matcher); 144 145 // Matcher for the children of the object in an Hierarchy. 146 ::testing::Matcher<const Hierarchy&> ChildrenMatch(ChildrenMatcher matcher); 147 148 } // namespace testing 149 } // namespace inspect 150 151 #endif // PW_BLUETOOTH_SAPPHIRE_INSPECT_ENABLED 152