@@ -271,90 +271,106 @@ template <class T> OnMatch<T> stringOnMatch(absl::string_view value, bool keep_m
271
271
return OnMatch<T>{[s = std::string (value)]() { return stringValue (s); }, nullptr , keep_matching};
272
272
}
273
273
274
- // Verifies the match tree completes the matching with an not match result.
275
- void verifyNoMatch (const MatchTree<TestData>::MatchResult& result) {
276
- EXPECT_EQ (MatchState::MatchComplete, result.match_state_ );
277
- EXPECT_FALSE (result.on_match_ .has_value ());
274
+ inline void PrintTo (const Action& action, std::ostream* os) {
275
+ if (action.typeUrl () == " google.protobuf.StringValue" ) {
276
+ *os << " {string_value=\" " << action.getTyped <StringAction>().string_ << " \" }" ;
277
+ return ;
278
+ }
279
+ *os << " {type=" << action.typeUrl () << " }" ;
278
280
}
279
281
280
- void verifyOnMatch (const OnMatch<TestData>& on_match, absl::string_view expected_value) {
281
- EXPECT_NE (on_match.action_cb_ , nullptr );
282
- EXPECT_EQ (on_match.action_cb_ ().get ()->getTyped <StringAction>(), *stringValue (expected_value))
283
- << " Actual: " << on_match.action_cb_ ().get ()->getTyped <StringAction>().string_
284
- << " . Expected: " << expected_value;
282
+ inline void PrintTo (const ActionFactoryCb& action_cb, std::ostream* os) {
283
+ if (action_cb == nullptr ) {
284
+ *os << " nullptr" ;
285
+ return ;
286
+ }
287
+ ActionPtr action = action_cb ();
288
+ PrintTo (*action, os);
285
289
}
286
290
287
- // Verifies the match tree completes the matching with the expected value.
288
- void verifyImmediateMatch (const MatchTree<TestData>::MatchResult& result,
289
- absl::string_view expected_value) {
290
- EXPECT_EQ (MatchState::MatchComplete, result.match_state_ );
291
- EXPECT_TRUE (result.on_match_ .has_value ());
291
+ inline void PrintTo (const MatchState state, std::ostream* os) {
292
+ switch (state) {
293
+ case MatchState::UnableToMatch:
294
+ *os << " UnableToMatch" ;
295
+ break ;
296
+ case MatchState::MatchComplete:
297
+ *os << " MatchComplete" ;
298
+ break ;
299
+ }
300
+ }
292
301
293
- EXPECT_EQ ( nullptr , result. on_match_ -> matcher_ );
294
- verifyOnMatch (*result. on_match_ , expected_value) ;
302
+ inline void PrintTo ( const MatchTree<TestData>& matcher, std::ostream* os) {
303
+ *os << " {type= " << typeid (matcher). name () << " } " ;
295
304
}
296
305
297
- // Verifies the match tree fails to match since the data are not enough.
298
- void verifyNotEnoughDataForMatch (const MatchTree<TestData>::MatchResult& result) {
299
- EXPECT_EQ (MatchState::UnableToMatch, result.match_state_ );
300
- EXPECT_FALSE (result.on_match_ .has_value ());
306
+ inline void PrintTo (const OnMatch<TestData>& on_match, std::ostream* os) {
307
+ if (on_match.action_cb_ ) {
308
+ *os << " {action_cb_=" ;
309
+ PrintTo (on_match.action_cb_ , os);
310
+ *os << " }" ;
311
+ } else if (on_match.matcher_ ) {
312
+ *os << " {matcher_=" ;
313
+ PrintTo (*on_match.matcher_ , os);
314
+ *os << " }" ;
315
+ } else {
316
+ *os << " {invalid, no value set}" ;
317
+ }
301
318
}
302
319
303
- MATCHER_P (IsStringAction, m, " " ) {
304
- // Accepts an ActionFactoryCb argument.
305
- if (arg == nullptr ) {
306
- *result_listener << " action callback is nullptr" ;
307
- return false ;
320
+ inline void PrintTo (const MatchTree<TestData>::MatchResult& result, std::ostream* os) {
321
+ *os << " {match_state_=" ;
322
+ PrintTo (result.match_state_ , os);
323
+ *os << " , on_match_=" ;
324
+ if (result.on_match_ .has_value ()) {
325
+ PrintTo (result.on_match_ .value (), os);
326
+ } else {
327
+ *os << " nullopt" ;
308
328
}
309
- ActionPtr action = arg ();
310
- StringAction string_action = action->getTyped <StringAction>();
311
- return ::testing::ExplainMatchResult (m, string_action.string_ , result_listener);
329
+ *os << " }" ;
312
330
}
313
331
314
- MATCHER_P (HasStringAction, m, " " ) {
315
- // Accepts a MatchResult argument.
316
- if (arg.match_state_ != MatchState::MatchComplete) {
317
- *result_listener << " match_state_ is not MatchComplete" ;
332
+ MATCHER (HasNotEnoughData, " " ) {
333
+ // Takes a MatchTree<TestData>::MatchResult& and validates that it
334
+ // is in the UnableToMatch state.
335
+ return arg.match_state_ == MatchState::UnableToMatch && !arg.on_match_ .has_value ();
336
+ }
337
+
338
+ MATCHER_P (IsStringAction, matcher, " " ) {
339
+ // Takes an ActionFactoryCb argument, and compares its StringAction's string against matcher.
340
+ if (arg == nullptr ) {
318
341
return false ;
319
342
}
320
- if (arg. on_match_ == absl:: nullopt ) {
321
- *result_listener << " on_match_ is nullopt " ;
343
+ ActionPtr action = arg ();
344
+ if (action-> typeUrl () != " google.protobuf.StringValue " ) {
322
345
return false ;
323
346
}
324
- return ExplainMatchResult (IsStringAction (m), arg.on_match_ ->action_cb_ , result_listener);
347
+ return ::testing::ExplainMatchResult (testing::Matcher<std::string>(matcher),
348
+ action->template getTyped <StringAction>().string_ ,
349
+ result_listener);
325
350
}
326
351
327
- MATCHER (HasNoMatch, " " ) {
328
- // Accepts a MatchResult argument.
329
- if (arg.match_state_ != MatchState::MatchComplete) {
330
- *result_listener << " match_state_ is not MatchComplete" ;
352
+ MATCHER_P (HasStringAction, matcher, " " ) {
353
+ // Takes a MatchTree<TestData>::MatchResult& and validates that it
354
+ // is a StringAction with contents matching matcher.
355
+ if (arg.match_state_ != MatchState::MatchComplete || !arg.on_match_ .has_value () ||
356
+ arg.on_match_ ->matcher_ != nullptr ) {
331
357
return false ;
332
358
}
333
- if (arg.on_match_ != absl::nullopt ) {
334
- *result_listener << " on_match_ was not nullopt" ;
335
- return false ;
336
- }
337
- return true ;
359
+ return ::testing::ExplainMatchResult (IsStringAction (matcher), arg.on_match_ ->action_cb_ ,
360
+ result_listener);
361
+ }
362
+
363
+ MATCHER (HasNoMatch, " " ) {
364
+ // Takes a MatchTree<TestData>::MatchResult& and validates that it
365
+ // is MatchComplete with no on_match_.
366
+ return arg.match_state_ == MatchState::MatchComplete && !arg.on_match_ .has_value ();
338
367
}
339
368
340
369
MATCHER (HasSubMatcher, " " ) {
341
- // Accepts a MatchResult argument.
342
- if (arg.match_state_ != MatchState::MatchComplete) {
343
- *result_listener << " match_state_ is not MatchComplete" ;
344
- return false ;
345
- }
346
- if (arg.on_match_ == absl::nullopt ) {
347
- *result_listener << " on_match_ is nullopt" ;
348
- return false ;
349
- }
350
- if (arg.on_match_ ->matcher_ == nullptr ) {
351
- *result_listener << " on_match_->matcher_ is nullptr, expected it to not be." ;
352
- if (arg.on_match_ ->action_cb_ != nullptr ) {
353
- *result_listener << " \n on_match_->action_cb_ is not nullptr." ;
354
- }
355
- return false ;
356
- }
357
- return true ;
370
+ // Takes a MatchTree<TestData>::MatchResult& and validates that it
371
+ // has a matcher_ value in on_match_.
372
+ return arg.match_state_ == MatchState::MatchComplete && arg.on_match_ .has_value () &&
373
+ arg.on_match_ ->matcher_ != nullptr ;
358
374
}
359
375
360
376
MATCHER_P (HasResult, m, " " ) {
0 commit comments