xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/fuchsia/lib/inspect_testing/inspect.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 
15 #ifdef PW_BLUETOOTH_SAPPHIRE_INSPECT_ENABLED
16 
17 #include "lib/inspect/testing/cpp/inspect.h"
18 
19 using inspect::NodeValue;
20 using inspect::PropertyValue;
21 
22 namespace {}  // namespace
23 
24 namespace inspect {
25 
PrintTo(const PropertyValue & property,std::ostream * os)26 void PrintTo(const PropertyValue& property, std::ostream* os) {
27   if (property.format() == PropertyFormat::kInt)
28     *os << "Int";
29   if (property.format() == PropertyFormat::kUint)
30     *os << "Uint";
31   if (property.format() == PropertyFormat::kDouble)
32     *os << "Double";
33   if (property.format() == PropertyFormat::kBool)
34     *os << "Bool";
35   if (property.format() == PropertyFormat::kIntArray)
36     *os << "IntArray";
37   if (property.format() == PropertyFormat::kUintArray)
38     *os << "UintArray";
39   if (property.format() == PropertyFormat::kDoubleArray)
40     *os << "DoubleArray";
41   if (property.format() == PropertyFormat::kString)
42     *os << "String";
43   if (property.format() == PropertyFormat::kBytes)
44     *os << "ByteVector";
45   *os << "Property(" << ::testing::PrintToString(property.name()) << ", ";
46   if (property.format() == PropertyFormat::kInt)
47     *os << ::testing::PrintToString(property.Get<IntPropertyValue>().value());
48   if (property.format() == PropertyFormat::kUint)
49     *os << ::testing::PrintToString(property.Get<UintPropertyValue>().value());
50   if (property.format() == PropertyFormat::kDouble)
51     *os << ::testing::PrintToString(
52         property.Get<DoublePropertyValue>().value());
53   if (property.format() == PropertyFormat::kBool)
54     *os << ::testing::PrintToString(property.Get<BoolPropertyValue>().value());
55   if (property.format() == PropertyFormat::kIntArray)
56     *os << ::testing::PrintToString(property.Get<IntArrayValue>().value());
57   if (property.format() == PropertyFormat::kUintArray)
58     *os << ::testing::PrintToString(property.Get<UintArrayValue>().value());
59   if (property.format() == PropertyFormat::kDoubleArray)
60     *os << ::testing::PrintToString(property.Get<DoubleArrayValue>().value());
61   if (property.format() == PropertyFormat::kString)
62     *os << ::testing::PrintToString(
63         property.Get<StringPropertyValue>().value());
64   if (property.format() == PropertyFormat::kBytes)
65     *os << ::testing::PrintToString(
66         property.Get<ByteVectorPropertyValue>().value());
67   *os << ")";
68 }
69 
PrintTo(const NodeValue & node,std::ostream * os)70 void PrintTo(const NodeValue& node, std::ostream* os) {
71   *os << "Node(" << ::testing::PrintToString(node.name()) << ", "
72       << ::testing::PrintToString(node.properties().size()) << " properties)";
73 }
74 
PrintTo(const::inspect::Hierarchy & hierarchy,std::ostream * os)75 void PrintTo(const ::inspect::Hierarchy& hierarchy, std::ostream* os) {
76   *os << "Hierarchy(" << ::testing::PrintToString(hierarchy.node()) << ", "
77       << ::testing::PrintToString(hierarchy.children().size()) << " children)";
78 }
79 
80 namespace testing {
81 
NameMatchesMatcher(std::string name)82 internal::NameMatchesMatcher::NameMatchesMatcher(std::string name)
83     : name_(std::move(name)) {}
84 
MatchAndExplain(const NodeValue & node,::testing::MatchResultListener * listener) const85 bool internal::NameMatchesMatcher::MatchAndExplain(
86     const NodeValue& node, ::testing::MatchResultListener* listener) const {
87   if (node.name() != name_) {
88     *listener << "expected name \"" << name_ << "\" but found \"" << node.name()
89               << "\"";
90     return false;
91   }
92   return true;
93 }
94 
DescribeTo(::std::ostream * os) const95 void internal::NameMatchesMatcher::DescribeTo(::std::ostream* os) const {
96   *os << "name matches \"" << name_ << "\"";
97 }
98 
DescribeNegationTo(::std::ostream * os) const99 void internal::NameMatchesMatcher::DescribeNegationTo(
100     ::std::ostream* os) const {
101   *os << "name does not match \"" << name_ << "\"";
102 }
103 
PropertyListMatcher(PropertiesMatcher matcher)104 internal::PropertyListMatcher::PropertyListMatcher(PropertiesMatcher matcher)
105     : matcher_(std::move(matcher)) {}
106 
MatchAndExplain(const NodeValue & node,::testing::MatchResultListener * listener) const107 bool internal::PropertyListMatcher::MatchAndExplain(
108     const NodeValue& node, ::testing::MatchResultListener* listener) const {
109   return ::testing::ExplainMatchResult(matcher_, node.properties(), listener);
110 }
111 
DescribeTo(::std::ostream * os) const112 void internal::PropertyListMatcher::DescribeTo(::std::ostream* os) const {
113   *os << "property list ";
114   matcher_.DescribeTo(os);
115 }
116 
DescribeNegationTo(::std::ostream * os) const117 void internal::PropertyListMatcher::DescribeNegationTo(
118     ::std::ostream* os) const {
119   *os << "property list ";
120   matcher_.DescribeNegationTo(os);
121 }
122 
NameMatches(std::string name)123 ::testing::Matcher<const NodeValue&> NameMatches(std::string name) {
124   return ::testing::MakeMatcher(
125       new internal::NameMatchesMatcher(std::move(name)));
126 }
127 
PropertyList(PropertiesMatcher matcher)128 ::testing::Matcher<const NodeValue&> PropertyList(PropertiesMatcher matcher) {
129   return ::testing::MakeMatcher(
130       new internal::PropertyListMatcher(std::move(matcher)));
131 }
132 
StringIs(const std::string & name,::testing::Matcher<std::string> matcher)133 ::testing::Matcher<const PropertyValue&> StringIs(
134     const std::string& name, ::testing::Matcher<std::string> matcher) {
135   return ::testing::AllOf(
136       ::testing::Property(&PropertyValue::name, ::testing::StrEq(name)),
137       ::testing::Property(&PropertyValue::format, PropertyFormat::kString),
138       ::testing::Property(&PropertyValue::Get<StringPropertyValue>,
139                           ::testing::Property(&StringPropertyValue::value,
140                                               std::move(matcher))));
141 }
142 
ByteVectorIs(const std::string & name,::testing::Matcher<std::vector<uint8_t>> matcher)143 ::testing::Matcher<const PropertyValue&> ByteVectorIs(
144     const std::string& name, ::testing::Matcher<std::vector<uint8_t>> matcher) {
145   return ::testing::AllOf(
146       ::testing::Property(&PropertyValue::name, ::testing::StrEq(name)),
147       ::testing::Property(&PropertyValue::format, PropertyFormat::kBytes),
148       ::testing::Property(&PropertyValue::Get<ByteVectorPropertyValue>,
149                           ::testing::Property(&ByteVectorPropertyValue::value,
150                                               std::move(matcher))));
151 }
152 
IntIs(const std::string & name,::testing::Matcher<int64_t> matcher)153 ::testing::Matcher<const PropertyValue&> IntIs(
154     const std::string& name, ::testing::Matcher<int64_t> matcher) {
155   return ::testing::AllOf(
156       ::testing::Property(&PropertyValue::name, ::testing::StrEq(name)),
157       ::testing::Property(&PropertyValue::format, PropertyFormat::kInt),
158       ::testing::Property(
159           &PropertyValue::Get<IntPropertyValue>,
160           ::testing::Property(&IntPropertyValue::value, std::move(matcher))));
161 }
162 
UintIs(const std::string & name,::testing::Matcher<uint64_t> matcher)163 ::testing::Matcher<const PropertyValue&> UintIs(
164     const std::string& name, ::testing::Matcher<uint64_t> matcher) {
165   return ::testing::AllOf(
166       ::testing::Property(&PropertyValue::name, ::testing::StrEq(name)),
167       ::testing::Property(&PropertyValue::format, PropertyFormat::kUint),
168       ::testing::Property(
169           &PropertyValue::Get<UintPropertyValue>,
170           ::testing::Property(&UintPropertyValue::value, std::move(matcher))));
171 }
172 
DoubleIs(const std::string & name,::testing::Matcher<double> matcher)173 ::testing::Matcher<const PropertyValue&> DoubleIs(
174     const std::string& name, ::testing::Matcher<double> matcher) {
175   return ::testing::AllOf(
176       ::testing::Property(&PropertyValue::name, ::testing::StrEq(name)),
177       ::testing::Property(&PropertyValue::format, PropertyFormat::kDouble),
178       ::testing::Property(&PropertyValue::Get<DoublePropertyValue>,
179                           ::testing::Property(&DoublePropertyValue::value,
180                                               std::move(matcher))));
181 }
182 
BoolIs(const std::string & name,::testing::Matcher<bool> matcher)183 ::testing::Matcher<const PropertyValue&> BoolIs(
184     const std::string& name, ::testing::Matcher<bool> matcher) {
185   return ::testing::AllOf(
186       ::testing::Property(&PropertyValue::name, ::testing::StrEq(name)),
187       ::testing::Property(&PropertyValue::format, PropertyFormat::kBool),
188       ::testing::Property(
189           &PropertyValue::Get<BoolPropertyValue>,
190           ::testing::Property(&BoolPropertyValue::value, std::move(matcher))));
191 }
192 
IntArrayIs(const std::string & name,::testing::Matcher<std::vector<int64_t>> matcher)193 ::testing::Matcher<const PropertyValue&> IntArrayIs(
194     const std::string& name, ::testing::Matcher<std::vector<int64_t>> matcher) {
195   return ::testing::AllOf(
196       ::testing::Property(&PropertyValue::name, ::testing::StrEq(name)),
197       ::testing::Property(&PropertyValue::format, PropertyFormat::kIntArray),
198       ::testing::Property(
199           &PropertyValue::Get<IntArrayValue>,
200           ::testing::Property(&IntArrayValue::value, std::move(matcher))));
201 }
202 
UintArrayIs(const std::string & name,::testing::Matcher<std::vector<uint64_t>> matcher)203 ::testing::Matcher<const PropertyValue&> UintArrayIs(
204     const std::string& name,
205     ::testing::Matcher<std::vector<uint64_t>> matcher) {
206   return ::testing::AllOf(
207       ::testing::Property(&PropertyValue::name, ::testing::StrEq(name)),
208       ::testing::Property(&PropertyValue::format, PropertyFormat::kUintArray),
209       ::testing::Property(
210           &PropertyValue::Get<UintArrayValue>,
211           ::testing::Property(&UintArrayValue::value, std::move(matcher))));
212 }
213 
DoubleArrayIs(const std::string & name,::testing::Matcher<std::vector<double>> matcher)214 ::testing::Matcher<const PropertyValue&> DoubleArrayIs(
215     const std::string& name, ::testing::Matcher<std::vector<double>> matcher) {
216   return ::testing::AllOf(
217       ::testing::Property(&PropertyValue::name, ::testing::StrEq(name)),
218       ::testing::Property(&PropertyValue::format, PropertyFormat::kDoubleArray),
219       ::testing::Property(
220           &PropertyValue::Get<DoubleArrayValue>,
221           ::testing::Property(&DoubleArrayValue::value, std::move(matcher))));
222 }
223 
ArrayDisplayFormatIs(ArrayDisplayFormat format)224 ::testing::Matcher<const PropertyValue&> ArrayDisplayFormatIs(
225     ArrayDisplayFormat format) {
226   return ::testing::AnyOf(
227       ::testing::AllOf(
228           ::testing::Property(&PropertyValue::format,
229                               PropertyFormat::kIntArray),
230           ::testing::Property(
231               &PropertyValue::Get<IntArrayValue>,
232               ::testing::Property(&IntArrayValue::GetDisplayFormat, format))),
233       ::testing::AllOf(
234           ::testing::Property(&PropertyValue::format,
235                               PropertyFormat::kUintArray),
236           ::testing::Property(
237               &PropertyValue::Get<UintArrayValue>,
238               ::testing::Property(&UintArrayValue::GetDisplayFormat, format))),
239       ::testing::AllOf(::testing::Property(&PropertyValue::format,
240                                            PropertyFormat::kDoubleArray),
241                        ::testing::Property(
242                            &PropertyValue::Get<DoubleArrayValue>,
243                            ::testing::Property(
244                                &DoubleArrayValue::GetDisplayFormat, format))));
245 }
246 
NodeMatches(NodeMatcher matcher)247 ::testing::Matcher<const Hierarchy&> NodeMatches(NodeMatcher matcher) {
248   return ::testing::Property(&Hierarchy::node, std::move(matcher));
249 }
250 
ChildrenMatch(ChildrenMatcher matcher)251 ::testing::Matcher<const Hierarchy&> ChildrenMatch(ChildrenMatcher matcher) {
252   return ::testing::Property(&Hierarchy::children, std::move(matcher));
253 }
254 
255 }  // namespace testing
256 }  // namespace inspect
257 #endif  // PW_BLUETOOTH_SAPPHIRE_INSPECT_ENABLED
258