@@ -411,9 +411,27 @@ class IntValue {
411
411
int value_;
412
412
};
413
413
414
+ // For testing casting matchers between compatible types. This is similar to
415
+ // IntValue, but takes a non-const reference to the value, showing MatcherCast
416
+ // works with such types (and doesn't, for example, use a const ref internally).
417
+ class MutableIntView {
418
+ public:
419
+ // An int& can be statically (although not implicitly) cast to a
420
+ // MutableIntView.
421
+ explicit MutableIntView (int & a_value) : value_(a_value) {}
422
+
423
+ int & value () const { return value_; }
424
+
425
+ private:
426
+ int & value_;
427
+ };
428
+
414
429
// For testing casting matchers between compatible types.
415
430
bool IsPositiveIntValue (const IntValue& foo) { return foo.value () > 0 ; }
416
431
432
+ // For testing casting matchers between compatible types.
433
+ bool IsPositiveMutableIntView (MutableIntView foo) { return foo.value () > 0 ; }
434
+
417
435
// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
418
436
// can be statically converted to U.
419
437
TEST (MatcherCastTest, FromCompatibleType) {
@@ -429,14 +447,34 @@ TEST(MatcherCastTest, FromCompatibleType) {
429
447
// predicate.
430
448
EXPECT_TRUE (m4.Matches (1 ));
431
449
EXPECT_FALSE (m4.Matches (0 ));
450
+
451
+ Matcher<MutableIntView> m5 = Truly (IsPositiveMutableIntView);
452
+ Matcher<int > m6 = MatcherCast<int >(m5);
453
+ // In the following, the arguments 1 and 0 are statically converted to
454
+ // MutableIntView objects, and then tested by the IsPositiveMutableIntView()
455
+ // predicate.
456
+ EXPECT_TRUE (m6.Matches (1 ));
457
+ EXPECT_FALSE (m6.Matches (0 ));
432
458
}
433
459
434
460
// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
435
461
TEST (MatcherCastTest, FromConstReferenceToNonReference) {
436
- Matcher<const int &> m1 = Eq (0 );
462
+ int n = 0 ;
463
+ Matcher<const int &> m1 = Ref (n);
437
464
Matcher<int > m2 = MatcherCast<int >(m1);
438
- EXPECT_TRUE (m2.Matches (0 ));
439
- EXPECT_FALSE (m2.Matches (1 ));
465
+ int n1 = 0 ;
466
+ EXPECT_TRUE (m2.Matches (n));
467
+ EXPECT_FALSE (m2.Matches (n1));
468
+ }
469
+
470
+ // Tests that MatcherCast<T&>(m) works when m is a Matcher<const T&>.
471
+ TEST (MatcherCastTest, FromConstReferenceToReference) {
472
+ int n = 0 ;
473
+ Matcher<const int &> m1 = Ref (n);
474
+ Matcher<int &> m2 = MatcherCast<int &>(m1);
475
+ int n1 = 0 ;
476
+ EXPECT_TRUE (m2.Matches (n));
477
+ EXPECT_FALSE (m2.Matches (n1));
440
478
}
441
479
442
480
// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
@@ -445,6 +483,12 @@ TEST(MatcherCastTest, FromReferenceToNonReference) {
445
483
Matcher<int > m2 = MatcherCast<int >(m1);
446
484
EXPECT_TRUE (m2.Matches (0 ));
447
485
EXPECT_FALSE (m2.Matches (1 ));
486
+
487
+ // Of course, reference identity isn't preserved since a copy is required.
488
+ int n = 0 ;
489
+ Matcher<int &> m3 = Ref (n);
490
+ Matcher<int > m4 = MatcherCast<int >(m3);
491
+ EXPECT_FALSE (m4.Matches (n));
448
492
}
449
493
450
494
// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
@@ -649,6 +693,16 @@ TEST(SafeMatcherCastTest, FromBaseClass) {
649
693
EXPECT_FALSE (m4.Matches (d2));
650
694
}
651
695
696
+ // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<const T&>.
697
+ TEST (SafeMatcherCastTest, FromConstReferenceToNonReference) {
698
+ int n = 0 ;
699
+ Matcher<const int &> m1 = Ref (n);
700
+ Matcher<int > m2 = SafeMatcherCast<int >(m1);
701
+ int n1 = 0 ;
702
+ EXPECT_TRUE (m2.Matches (n));
703
+ EXPECT_FALSE (m2.Matches (n1));
704
+ }
705
+
652
706
// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
653
707
TEST (SafeMatcherCastTest, FromConstReferenceToReference) {
654
708
int n = 0 ;
0 commit comments