OLD | NEW |
1 // Copyright 2007, Google Inc. | 1 // Copyright 2007, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 // This template class implements a default action spec (i.e. an | 135 // This template class implements a default action spec (i.e. an |
136 // ON_CALL() statement). | 136 // ON_CALL() statement). |
137 template <typename F> | 137 template <typename F> |
138 class DefaultActionSpec { | 138 class DefaultActionSpec { |
139 public: | 139 public: |
140 typedef typename Function<F>::ArgumentTuple ArgumentTuple; | 140 typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
141 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; | 141 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; |
142 | 142 |
143 // Constructs a DefaultActionSpec object from the information inside | 143 // Constructs a DefaultActionSpec object from the information inside |
144 // the parenthesis of an ON_CALL() statement. | 144 // the parenthesis of an ON_CALL() statement. |
145 DefaultActionSpec(const char* file, int line, | 145 DefaultActionSpec(const char* a_file, int a_line, |
146 const ArgumentMatcherTuple& matchers) | 146 const ArgumentMatcherTuple& matchers) |
147 : file_(file), | 147 : file_(a_file), |
148 line_(line), | 148 line_(a_line), |
149 matchers_(matchers), | 149 matchers_(matchers), |
150 // By default, extra_matcher_ should match anything. However, | 150 // By default, extra_matcher_ should match anything. However, |
151 // we cannot initialize it with _ as that triggers a compiler | 151 // we cannot initialize it with _ as that triggers a compiler |
152 // bug in Symbian's C++ compiler (cannot decide between two | 152 // bug in Symbian's C++ compiler (cannot decide between two |
153 // overloaded constructors of Matcher<const ArgumentTuple&>). | 153 // overloaded constructors of Matcher<const ArgumentTuple&>). |
154 extra_matcher_(A<const ArgumentTuple&>()), | 154 extra_matcher_(A<const ArgumentTuple&>()), |
155 last_clause_(kNone) { | 155 last_clause_(kNone) { |
156 } | 156 } |
157 | 157 |
158 // Where in the source file was the default action spec defined? | 158 // Where in the source file was the default action spec defined? |
(...skipping 30 matching lines...) Expand all Loading... |
189 return TupleMatches(matchers_, args) && extra_matcher_.Matches(args); | 189 return TupleMatches(matchers_, args) && extra_matcher_.Matches(args); |
190 } | 190 } |
191 | 191 |
192 // Returns the action specified by the user. | 192 // Returns the action specified by the user. |
193 const Action<F>& GetAction() const { | 193 const Action<F>& GetAction() const { |
194 AssertSpecProperty(last_clause_ == kWillByDefault, | 194 AssertSpecProperty(last_clause_ == kWillByDefault, |
195 ".WillByDefault() must appear exactly " | 195 ".WillByDefault() must appear exactly " |
196 "once in an ON_CALL()."); | 196 "once in an ON_CALL()."); |
197 return action_; | 197 return action_; |
198 } | 198 } |
| 199 |
199 private: | 200 private: |
200 // Gives each clause in the ON_CALL() statement a name. | 201 // Gives each clause in the ON_CALL() statement a name. |
201 enum Clause { | 202 enum Clause { |
202 // Do not change the order of the enum members! The run-time | 203 // Do not change the order of the enum members! The run-time |
203 // syntax checking relies on it. | 204 // syntax checking relies on it. |
204 kNone, | 205 kNone, |
205 kWith, | 206 kWith, |
206 kWillByDefault, | 207 kWillByDefault, |
207 }; | 208 }; |
208 | 209 |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 // types (e.g. all pre-requisites of a particular expectation, all | 555 // types (e.g. all pre-requisites of a particular expectation, all |
555 // expectations in a sequence). Therefore these expectation objects | 556 // expectations in a sequence). Therefore these expectation objects |
556 // must share a common base class. | 557 // must share a common base class. |
557 // | 558 // |
558 // 2. We can avoid binary code bloat by moving methods not depending | 559 // 2. We can avoid binary code bloat by moving methods not depending |
559 // on the template argument of Expectation to the base class. | 560 // on the template argument of Expectation to the base class. |
560 // | 561 // |
561 // This class is internal and mustn't be used by user code directly. | 562 // This class is internal and mustn't be used by user code directly. |
562 class ExpectationBase { | 563 class ExpectationBase { |
563 public: | 564 public: |
564 ExpectationBase(const char* file, int line); | 565 // source_text is the EXPECT_CALL(...) source that created this Expectation. |
| 566 ExpectationBase(const char* file, int line, const string& source_text); |
565 | 567 |
566 virtual ~ExpectationBase(); | 568 virtual ~ExpectationBase(); |
567 | 569 |
568 // Where in the source file was the expectation spec defined? | 570 // Where in the source file was the expectation spec defined? |
569 const char* file() const { return file_; } | 571 const char* file() const { return file_; } |
570 int line() const { return line_; } | 572 int line() const { return line_; } |
571 | 573 const char* source_text() const { return source_text_.c_str(); } |
572 // Returns the cardinality specified in the expectation spec. | 574 // Returns the cardinality specified in the expectation spec. |
573 const Cardinality& cardinality() const { return cardinality_; } | 575 const Cardinality& cardinality() const { return cardinality_; } |
574 | 576 |
575 // Describes the source file location of this expectation. | 577 // Describes the source file location of this expectation. |
576 void DescribeLocationTo(::std::ostream* os) const { | 578 void DescribeLocationTo(::std::ostream* os) const { |
577 *os << file() << ":" << line() << ": "; | 579 *os << file() << ":" << line() << ": "; |
578 } | 580 } |
579 | 581 |
580 // Describes how many times a function call matching this | 582 // Describes how many times a function call matching this |
581 // expectation has occurred. | 583 // expectation has occurred. |
582 // L >= g_gmock_mutex | 584 // L >= g_gmock_mutex |
583 virtual void DescribeCallCountTo(::std::ostream* os) const = 0; | 585 virtual void DescribeCallCountTo(::std::ostream* os) const = 0; |
| 586 |
584 protected: | 587 protected: |
585 friend class ::testing::Expectation; | 588 friend class ::testing::Expectation; |
586 | 589 |
587 enum Clause { | 590 enum Clause { |
588 // Don't change the order of the enum members! | 591 // Don't change the order of the enum members! |
589 kNone, | 592 kNone, |
590 kWith, | 593 kWith, |
591 kTimes, | 594 kTimes, |
592 kInSequence, | 595 kInSequence, |
593 kAfter, | 596 kAfter, |
(...skipping 18 matching lines...) Expand all Loading... |
612 | 615 |
613 // Explicitly specifies the cardinality of this expectation. Used | 616 // Explicitly specifies the cardinality of this expectation. Used |
614 // by the subclasses to implement the .Times() clause. | 617 // by the subclasses to implement the .Times() clause. |
615 void SpecifyCardinality(const Cardinality& cardinality); | 618 void SpecifyCardinality(const Cardinality& cardinality); |
616 | 619 |
617 // Returns true iff the user specified the cardinality explicitly | 620 // Returns true iff the user specified the cardinality explicitly |
618 // using a .Times(). | 621 // using a .Times(). |
619 bool cardinality_specified() const { return cardinality_specified_; } | 622 bool cardinality_specified() const { return cardinality_specified_; } |
620 | 623 |
621 // Sets the cardinality of this expectation spec. | 624 // Sets the cardinality of this expectation spec. |
622 void set_cardinality(const Cardinality& cardinality) { | 625 void set_cardinality(const Cardinality& a_cardinality) { |
623 cardinality_ = cardinality; | 626 cardinality_ = a_cardinality; |
624 } | 627 } |
625 | 628 |
626 // The following group of methods should only be called after the | 629 // The following group of methods should only be called after the |
627 // EXPECT_CALL() statement, and only when g_gmock_mutex is held by | 630 // EXPECT_CALL() statement, and only when g_gmock_mutex is held by |
628 // the current thread. | 631 // the current thread. |
629 | 632 |
630 // Retires all pre-requisites of this expectation. | 633 // Retires all pre-requisites of this expectation. |
631 // L >= g_gmock_mutex | 634 // L >= g_gmock_mutex |
632 void RetireAllPreRequisites(); | 635 void RetireAllPreRequisites(); |
633 | 636 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 | 693 |
691 private: | 694 private: |
692 friend class ::testing::Sequence; | 695 friend class ::testing::Sequence; |
693 friend class ::testing::internal::ExpectationTester; | 696 friend class ::testing::internal::ExpectationTester; |
694 | 697 |
695 template <typename Function> | 698 template <typename Function> |
696 friend class TypedExpectation; | 699 friend class TypedExpectation; |
697 | 700 |
698 // This group of fields are part of the spec and won't change after | 701 // This group of fields are part of the spec and won't change after |
699 // an EXPECT_CALL() statement finishes. | 702 // an EXPECT_CALL() statement finishes. |
700 const char* file_; // The file that contains the expectation. | 703 const char* file_; // The file that contains the expectation. |
701 int line_; // The line number of the expectation. | 704 int line_; // The line number of the expectation. |
| 705 const string source_text_; // The EXPECT_CALL(...) source text. |
702 // True iff the cardinality is specified explicitly. | 706 // True iff the cardinality is specified explicitly. |
703 bool cardinality_specified_; | 707 bool cardinality_specified_; |
704 Cardinality cardinality_; // The cardinality of the expectation. | 708 Cardinality cardinality_; // The cardinality of the expectation. |
705 // The immediate pre-requisites (i.e. expectations that must be | 709 // The immediate pre-requisites (i.e. expectations that must be |
706 // satisfied before this expectation can be matched) of this | 710 // satisfied before this expectation can be matched) of this |
707 // expectation. We use linked_ptr in the set because we want an | 711 // expectation. We use linked_ptr in the set because we want an |
708 // Expectation object to be co-owned by its FunctionMocker and its | 712 // Expectation object to be co-owned by its FunctionMocker and its |
709 // successors. This allows multiple mock objects to be deleted at | 713 // successors. This allows multiple mock objects to be deleted at |
710 // different times. | 714 // different times. |
711 ExpectationSet immediate_prerequisites_; | 715 ExpectationSet immediate_prerequisites_; |
712 | 716 |
713 // This group of fields are the current state of the expectation, | 717 // This group of fields are the current state of the expectation, |
714 // and can change as the mock function is called. | 718 // and can change as the mock function is called. |
715 int call_count_; // How many times this expectation has been invoked. | 719 int call_count_; // How many times this expectation has been invoked. |
716 bool retired_; // True iff this expectation has retired. | 720 bool retired_; // True iff this expectation has retired. |
| 721 |
| 722 GTEST_DISALLOW_ASSIGN_(ExpectationBase); |
717 }; // class ExpectationBase | 723 }; // class ExpectationBase |
718 | 724 |
719 // Impements an expectation for the given function type. | 725 // Impements an expectation for the given function type. |
720 template <typename F> | 726 template <typename F> |
721 class TypedExpectation : public ExpectationBase { | 727 class TypedExpectation : public ExpectationBase { |
722 public: | 728 public: |
723 typedef typename Function<F>::ArgumentTuple ArgumentTuple; | 729 typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
724 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; | 730 typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; |
725 typedef typename Function<F>::Result Result; | 731 typedef typename Function<F>::Result Result; |
726 | 732 |
727 TypedExpectation(FunctionMockerBase<F>* owner, const char* file, int line, | 733 TypedExpectation(FunctionMockerBase<F>* owner, |
| 734 const char* a_file, int a_line, const string& a_source_text, |
728 const ArgumentMatcherTuple& m) | 735 const ArgumentMatcherTuple& m) |
729 : ExpectationBase(file, line), | 736 : ExpectationBase(a_file, a_line, a_source_text), |
730 owner_(owner), | 737 owner_(owner), |
731 matchers_(m), | 738 matchers_(m), |
| 739 extra_matcher_specified_(false), |
732 // By default, extra_matcher_ should match anything. However, | 740 // By default, extra_matcher_ should match anything. However, |
733 // we cannot initialize it with _ as that triggers a compiler | 741 // we cannot initialize it with _ as that triggers a compiler |
734 // bug in Symbian's C++ compiler (cannot decide between two | 742 // bug in Symbian's C++ compiler (cannot decide between two |
735 // overloaded constructors of Matcher<const ArgumentTuple&>). | 743 // overloaded constructors of Matcher<const ArgumentTuple&>). |
736 extra_matcher_(A<const ArgumentTuple&>()), | 744 extra_matcher_(A<const ArgumentTuple&>()), |
737 repeated_action_specified_(false), | 745 repeated_action_specified_(false), |
738 repeated_action_(DoDefault()), | 746 repeated_action_(DoDefault()), |
739 retires_on_saturation_(false), | 747 retires_on_saturation_(false), |
740 last_clause_(kNone), | 748 last_clause_(kNone), |
741 action_count_checked_(false) {} | 749 action_count_checked_(false) {} |
(...skipping 11 matching lines...) Expand all Loading... |
753 ".With() cannot appear " | 761 ".With() cannot appear " |
754 "more than once in an EXPECT_CALL()."); | 762 "more than once in an EXPECT_CALL()."); |
755 } else { | 763 } else { |
756 ExpectSpecProperty(last_clause_ < kWith, | 764 ExpectSpecProperty(last_clause_ < kWith, |
757 ".With() must be the first " | 765 ".With() must be the first " |
758 "clause in an EXPECT_CALL()."); | 766 "clause in an EXPECT_CALL()."); |
759 } | 767 } |
760 last_clause_ = kWith; | 768 last_clause_ = kWith; |
761 | 769 |
762 extra_matcher_ = m; | 770 extra_matcher_ = m; |
| 771 extra_matcher_specified_ = true; |
763 return *this; | 772 return *this; |
764 } | 773 } |
765 | 774 |
766 // Implements the .Times() clause. | 775 // Implements the .Times() clause. |
767 TypedExpectation& Times(const Cardinality& cardinality) { | 776 TypedExpectation& Times(const Cardinality& a_cardinality) { |
768 if (last_clause_ ==kTimes) { | 777 if (last_clause_ ==kTimes) { |
769 ExpectSpecProperty(false, | 778 ExpectSpecProperty(false, |
770 ".Times() cannot appear " | 779 ".Times() cannot appear " |
771 "more than once in an EXPECT_CALL()."); | 780 "more than once in an EXPECT_CALL()."); |
772 } else { | 781 } else { |
773 ExpectSpecProperty(last_clause_ < kTimes, | 782 ExpectSpecProperty(last_clause_ < kTimes, |
774 ".Times() cannot appear after " | 783 ".Times() cannot appear after " |
775 ".InSequence(), .WillOnce(), .WillRepeatedly(), " | 784 ".InSequence(), .WillOnce(), .WillRepeatedly(), " |
776 "or .RetiresOnSaturation()."); | 785 "or .RetiresOnSaturation()."); |
777 } | 786 } |
778 last_clause_ = kTimes; | 787 last_clause_ = kTimes; |
779 | 788 |
780 ExpectationBase::SpecifyCardinality(cardinality); | 789 ExpectationBase::SpecifyCardinality(a_cardinality); |
781 return *this; | 790 return *this; |
782 } | 791 } |
783 | 792 |
784 // Implements the .Times() clause. | 793 // Implements the .Times() clause. |
785 TypedExpectation& Times(int n) { | 794 TypedExpectation& Times(int n) { |
786 return Times(Exactly(n)); | 795 return Times(Exactly(n)); |
787 } | 796 } |
788 | 797 |
789 // Implements the .InSequence() clause. | 798 // Implements the .InSequence() clause. |
790 TypedExpectation& InSequence(const Sequence& s) { | 799 TypedExpectation& InSequence(const Sequence& s) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
931 Cardinality::DescribeActualCallCountTo(call_count(), os); | 940 Cardinality::DescribeActualCallCountTo(call_count(), os); |
932 | 941 |
933 // Describes the state of the expectation (e.g. is it satisfied? | 942 // Describes the state of the expectation (e.g. is it satisfied? |
934 // is it active?). | 943 // is it active?). |
935 *os << " - " << (IsOverSaturated() ? "over-saturated" : | 944 *os << " - " << (IsOverSaturated() ? "over-saturated" : |
936 IsSaturated() ? "saturated" : | 945 IsSaturated() ? "saturated" : |
937 IsSatisfied() ? "satisfied" : "unsatisfied") | 946 IsSatisfied() ? "satisfied" : "unsatisfied") |
938 << " and " | 947 << " and " |
939 << (is_retired() ? "retired" : "active"); | 948 << (is_retired() ? "retired" : "active"); |
940 } | 949 } |
| 950 |
| 951 void MaybeDescribeExtraMatcherTo(::std::ostream* os) { |
| 952 if (extra_matcher_specified_) { |
| 953 *os << " Expected args: "; |
| 954 extra_matcher_.DescribeTo(os); |
| 955 *os << "\n"; |
| 956 } |
| 957 } |
| 958 |
941 private: | 959 private: |
942 template <typename Function> | 960 template <typename Function> |
943 friend class FunctionMockerBase; | 961 friend class FunctionMockerBase; |
944 | 962 |
945 // Returns an Expectation object that references and co-owns this | 963 // Returns an Expectation object that references and co-owns this |
946 // expectation. | 964 // expectation. |
947 virtual Expectation GetHandle() { | 965 virtual Expectation GetHandle() { |
948 return owner_->GetHandleOf(this); | 966 return owner_->GetHandleOf(this); |
949 } | 967 } |
950 | 968 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1027 "call_count() is <= 0 when GetCurrentAction() is " | 1045 "call_count() is <= 0 when GetCurrentAction() is " |
1028 "called - this should never happen."); | 1046 "called - this should never happen."); |
1029 | 1047 |
1030 const int action_count = static_cast<int>(actions().size()); | 1048 const int action_count = static_cast<int>(actions().size()); |
1031 if (action_count > 0 && !repeated_action_specified_ && | 1049 if (action_count > 0 && !repeated_action_specified_ && |
1032 count > action_count) { | 1050 count > action_count) { |
1033 // If there is at least one WillOnce() and no WillRepeatedly(), | 1051 // If there is at least one WillOnce() and no WillRepeatedly(), |
1034 // we warn the user when the WillOnce() clauses ran out. | 1052 // we warn the user when the WillOnce() clauses ran out. |
1035 ::std::stringstream ss; | 1053 ::std::stringstream ss; |
1036 DescribeLocationTo(&ss); | 1054 DescribeLocationTo(&ss); |
1037 ss << "Actions ran out.\n" | 1055 ss << "Actions ran out in " << source_text() << "...\n" |
1038 << "Called " << count << " times, but only " | 1056 << "Called " << count << " times, but only " |
1039 << action_count << " WillOnce()" | 1057 << action_count << " WillOnce()" |
1040 << (action_count == 1 ? " is" : "s are") << " specified - "; | 1058 << (action_count == 1 ? " is" : "s are") << " specified - "; |
1041 mocker->DescribeDefaultActionTo(args, &ss); | 1059 mocker->DescribeDefaultActionTo(args, &ss); |
1042 Log(WARNING, ss.str(), 1); | 1060 Log(WARNING, ss.str(), 1); |
1043 } | 1061 } |
1044 | 1062 |
1045 return count <= action_count ? actions()[count - 1] : repeated_action(); | 1063 return count <= action_count ? actions()[count - 1] : repeated_action(); |
1046 } | 1064 } |
1047 | 1065 |
(...skipping 23 matching lines...) Expand all Loading... |
1071 } | 1089 } |
1072 | 1090 |
1073 IncrementCallCount(); | 1091 IncrementCallCount(); |
1074 RetireAllPreRequisites(); | 1092 RetireAllPreRequisites(); |
1075 | 1093 |
1076 if (retires_on_saturation() && IsSaturated()) { | 1094 if (retires_on_saturation() && IsSaturated()) { |
1077 Retire(); | 1095 Retire(); |
1078 } | 1096 } |
1079 | 1097 |
1080 // Must be done after IncrementCount()! | 1098 // Must be done after IncrementCount()! |
1081 *what << "Expected mock function call.\n"; | 1099 *what << "Mock function call matches " << source_text() <<"...\n"; |
1082 return GetCurrentAction(mocker, args); | 1100 return GetCurrentAction(mocker, args); |
1083 } | 1101 } |
1084 | 1102 |
1085 // Checks the action count (i.e. the number of WillOnce() and | 1103 // Checks the action count (i.e. the number of WillOnce() and |
1086 // WillRepeatedly() clauses) against the cardinality if this hasn't | 1104 // WillRepeatedly() clauses) against the cardinality if this hasn't |
1087 // been done before. Prints a warning if there are too many or too | 1105 // been done before. Prints a warning if there are too many or too |
1088 // few actions. | 1106 // few actions. |
1089 // L < mutex_ | 1107 // L < mutex_ |
1090 void CheckActionCountIfNotDone() const { | 1108 void CheckActionCountIfNotDone() const { |
1091 bool should_check = false; | 1109 bool should_check = false; |
(...skipping 24 matching lines...) Expand all Loading... |
1116 } else if (0 < action_count && action_count < lower_bound && | 1134 } else if (0 < action_count && action_count < lower_bound && |
1117 !repeated_action_specified_) { | 1135 !repeated_action_specified_) { |
1118 too_many = false; | 1136 too_many = false; |
1119 } else { | 1137 } else { |
1120 return; | 1138 return; |
1121 } | 1139 } |
1122 | 1140 |
1123 ::std::stringstream ss; | 1141 ::std::stringstream ss; |
1124 DescribeLocationTo(&ss); | 1142 DescribeLocationTo(&ss); |
1125 ss << "Too " << (too_many ? "many" : "few") | 1143 ss << "Too " << (too_many ? "many" : "few") |
1126 << " actions specified.\n" | 1144 << " actions specified in " << source_text() << "...\n" |
1127 << "Expected to be "; | 1145 << "Expected to be "; |
1128 cardinality().DescribeTo(&ss); | 1146 cardinality().DescribeTo(&ss); |
1129 ss << ", but has " << (too_many ? "" : "only ") | 1147 ss << ", but has " << (too_many ? "" : "only ") |
1130 << action_count << " WillOnce()" | 1148 << action_count << " WillOnce()" |
1131 << (action_count == 1 ? "" : "s"); | 1149 << (action_count == 1 ? "" : "s"); |
1132 if (repeated_action_specified_) { | 1150 if (repeated_action_specified_) { |
1133 ss << " and a WillRepeatedly()"; | 1151 ss << " and a WillRepeatedly()"; |
1134 } | 1152 } |
1135 ss << "."; | 1153 ss << "."; |
1136 Log(WARNING, ss.str(), -1); // -1 means "don't print stack trace". | 1154 Log(WARNING, ss.str(), -1); // -1 means "don't print stack trace". |
1137 } | 1155 } |
1138 } | 1156 } |
1139 | 1157 |
1140 // All the fields below won't change once the EXPECT_CALL() | 1158 // All the fields below won't change once the EXPECT_CALL() |
1141 // statement finishes. | 1159 // statement finishes. |
1142 FunctionMockerBase<F>* const owner_; | 1160 FunctionMockerBase<F>* const owner_; |
1143 ArgumentMatcherTuple matchers_; | 1161 ArgumentMatcherTuple matchers_; |
| 1162 bool extra_matcher_specified_; |
1144 Matcher<const ArgumentTuple&> extra_matcher_; | 1163 Matcher<const ArgumentTuple&> extra_matcher_; |
1145 std::vector<Action<F> > actions_; | 1164 std::vector<Action<F> > actions_; |
1146 bool repeated_action_specified_; // True if a WillRepeatedly() was specified. | 1165 bool repeated_action_specified_; // True if a WillRepeatedly() was specified. |
1147 Action<F> repeated_action_; | 1166 Action<F> repeated_action_; |
1148 bool retires_on_saturation_; | 1167 bool retires_on_saturation_; |
1149 Clause last_clause_; | 1168 Clause last_clause_; |
1150 mutable bool action_count_checked_; // Under mutex_. | 1169 mutable bool action_count_checked_; // Under mutex_. |
1151 mutable Mutex mutex_; // Protects action_count_checked_. | 1170 mutable Mutex mutex_; // Protects action_count_checked_. |
| 1171 |
| 1172 GTEST_DISALLOW_COPY_AND_ASSIGN_(TypedExpectation); |
1152 }; // class TypedExpectation | 1173 }; // class TypedExpectation |
1153 | 1174 |
1154 // A MockSpec object is used by ON_CALL() or EXPECT_CALL() for | 1175 // A MockSpec object is used by ON_CALL() or EXPECT_CALL() for |
1155 // specifying the default behavior of, or expectation on, a mock | 1176 // specifying the default behavior of, or expectation on, a mock |
1156 // function. | 1177 // function. |
1157 | 1178 |
1158 // Note: class MockSpec really belongs to the ::testing namespace. | 1179 // Note: class MockSpec really belongs to the ::testing namespace. |
1159 // However if we define it in ::testing, MSVC will complain when | 1180 // However if we define it in ::testing, MSVC will complain when |
1160 // classes in ::testing::internal declare it as a friend class | 1181 // classes in ::testing::internal declare it as a friend class |
1161 // template. To workaround this compiler bug, we define MockSpec in | 1182 // template. To workaround this compiler bug, we define MockSpec in |
(...skipping 17 matching lines...) Expand all Loading... |
1179 const char* file, int line, const char* obj, const char* call) { | 1200 const char* file, int line, const char* obj, const char* call) { |
1180 LogWithLocation(internal::INFO, file, line, | 1201 LogWithLocation(internal::INFO, file, line, |
1181 string("ON_CALL(") + obj + ", " + call + ") invoked"); | 1202 string("ON_CALL(") + obj + ", " + call + ") invoked"); |
1182 return function_mocker_->AddNewDefaultActionSpec(file, line, matchers_); | 1203 return function_mocker_->AddNewDefaultActionSpec(file, line, matchers_); |
1183 } | 1204 } |
1184 | 1205 |
1185 // Adds a new expectation spec to the function mocker and returns | 1206 // Adds a new expectation spec to the function mocker and returns |
1186 // the newly created spec. | 1207 // the newly created spec. |
1187 internal::TypedExpectation<F>& InternalExpectedAt( | 1208 internal::TypedExpectation<F>& InternalExpectedAt( |
1188 const char* file, int line, const char* obj, const char* call) { | 1209 const char* file, int line, const char* obj, const char* call) { |
1189 LogWithLocation(internal::INFO, file, line, | 1210 const string source_text(string("EXPECT_CALL(") + obj + ", " + call + ")"); |
1190 string("EXPECT_CALL(") + obj + ", " + call + ") invoked"); | 1211 LogWithLocation(internal::INFO, file, line, source_text + " invoked"); |
1191 return function_mocker_->AddNewExpectation(file, line, matchers_); | 1212 return function_mocker_->AddNewExpectation( |
| 1213 file, line, source_text, matchers_); |
1192 } | 1214 } |
1193 | 1215 |
1194 private: | 1216 private: |
1195 template <typename Function> | 1217 template <typename Function> |
1196 friend class internal::FunctionMocker; | 1218 friend class internal::FunctionMocker; |
1197 | 1219 |
1198 void SetMatchers(const ArgumentMatcherTuple& matchers) { | 1220 void SetMatchers(const ArgumentMatcherTuple& matchers) { |
1199 matchers_ = matchers; | 1221 matchers_ = matchers; |
1200 } | 1222 } |
1201 | 1223 |
1202 // Logs a message including file and line number information. | 1224 // Logs a message including file and line number information. |
1203 void LogWithLocation(testing::internal::LogSeverity severity, | 1225 void LogWithLocation(testing::internal::LogSeverity severity, |
1204 const char* file, int line, | 1226 const char* file, int line, |
1205 const string& message) { | 1227 const string& message) { |
1206 ::std::ostringstream s; | 1228 ::std::ostringstream s; |
1207 s << file << ":" << line << ": " << message << ::std::endl; | 1229 s << file << ":" << line << ": " << message << ::std::endl; |
1208 Log(severity, s.str(), 0); | 1230 Log(severity, s.str(), 0); |
1209 } | 1231 } |
1210 | 1232 |
1211 // The function mocker that owns this spec. | 1233 // The function mocker that owns this spec. |
1212 internal::FunctionMockerBase<F>* const function_mocker_; | 1234 internal::FunctionMockerBase<F>* const function_mocker_; |
1213 // The argument matchers specified in the spec. | 1235 // The argument matchers specified in the spec. |
1214 ArgumentMatcherTuple matchers_; | 1236 ArgumentMatcherTuple matchers_; |
| 1237 |
| 1238 GTEST_DISALLOW_ASSIGN_(MockSpec); |
1215 }; // class MockSpec | 1239 }; // class MockSpec |
1216 | 1240 |
1217 // MSVC warns about using 'this' in base member initializer list, so | 1241 // MSVC warns about using 'this' in base member initializer list, so |
1218 // we need to temporarily disable the warning. We have to do it for | 1242 // we need to temporarily disable the warning. We have to do it for |
1219 // the entire class to suppress the warning, even though it's about | 1243 // the entire class to suppress the warning, even though it's about |
1220 // the constructor only. | 1244 // the constructor only. |
1221 | 1245 |
1222 #ifdef _MSC_VER | 1246 #ifdef _MSC_VER |
1223 #pragma warning(push) // Saves the current warning state. | 1247 #pragma warning(push) // Saves the current warning state. |
1224 #pragma warning(disable:4355) // Temporarily disables warning 4355. | 1248 #pragma warning(disable:4355) // Temporarily disables warning 4355. |
1225 #endif // _MSV_VER | 1249 #endif // _MSV_VER |
1226 | 1250 |
1227 // C++ treats the void type specially. For example, you cannot define | 1251 // C++ treats the void type specially. For example, you cannot define |
1228 // a void-typed variable or pass a void value to a function. | 1252 // a void-typed variable or pass a void value to a function. |
1229 // ActionResultHolder<T> holds a value of type T, where T must be a | 1253 // ActionResultHolder<T> holds a value of type T, where T must be a |
1230 // copyable type or void (T doesn't need to be default-constructable). | 1254 // copyable type or void (T doesn't need to be default-constructable). |
1231 // It hides the syntactic difference between void and other types, and | 1255 // It hides the syntactic difference between void and other types, and |
1232 // is used to unify the code for invoking both void-returning and | 1256 // is used to unify the code for invoking both void-returning and |
1233 // non-void-returning mock functions. This generic definition is used | 1257 // non-void-returning mock functions. This generic definition is used |
1234 // when T is not void. | 1258 // when T is not void. |
1235 template <typename T> | 1259 template <typename T> |
1236 class ActionResultHolder { | 1260 class ActionResultHolder { |
1237 public: | 1261 public: |
1238 explicit ActionResultHolder(T value) : value_(value) {} | 1262 explicit ActionResultHolder(T a_value) : value_(a_value) {} |
1239 | 1263 |
1240 // The compiler-generated copy constructor and assignment operator | 1264 // The compiler-generated copy constructor and assignment operator |
1241 // are exactly what we need, so we don't need to define them. | 1265 // are exactly what we need, so we don't need to define them. |
1242 | 1266 |
1243 T value() const { return value_; } | 1267 T value() const { return value_; } |
1244 | 1268 |
1245 // Prints the held value as an action's result to os. | 1269 // Prints the held value as an action's result to os. |
1246 void PrintAsActionResult(::std::ostream* os) const { | 1270 void PrintAsActionResult(::std::ostream* os) const { |
1247 *os << "\n Returns: "; | 1271 *os << "\n Returns: "; |
1248 UniversalPrinter<T>::Print(value_, os); | 1272 UniversalPrinter<T>::Print(value_, os); |
(...skipping 13 matching lines...) Expand all Loading... |
1262 // Performs the given action and returns the result in a | 1286 // Performs the given action and returns the result in a |
1263 // ActionResultHolder. | 1287 // ActionResultHolder. |
1264 template <typename Function, typename Arguments> | 1288 template <typename Function, typename Arguments> |
1265 static ActionResultHolder PerformAction(const Action<Function>& action, | 1289 static ActionResultHolder PerformAction(const Action<Function>& action, |
1266 const Arguments& args) { | 1290 const Arguments& args) { |
1267 return ActionResultHolder(action.Perform(args)); | 1291 return ActionResultHolder(action.Perform(args)); |
1268 } | 1292 } |
1269 | 1293 |
1270 private: | 1294 private: |
1271 T value_; | 1295 T value_; |
| 1296 |
| 1297 // T could be a reference type, so = isn't supported. |
| 1298 GTEST_DISALLOW_ASSIGN_(ActionResultHolder); |
1272 }; | 1299 }; |
1273 | 1300 |
1274 // Specialization for T = void. | 1301 // Specialization for T = void. |
1275 template <> | 1302 template <> |
1276 class ActionResultHolder<void> { | 1303 class ActionResultHolder<void> { |
1277 public: | 1304 public: |
1278 ActionResultHolder() {} | 1305 ActionResultHolder() {} |
1279 void value() const {} | 1306 void value() const {} |
1280 void PrintAsActionResult(::std::ostream* /* os */) const {} | 1307 void PrintAsActionResult(::std::ostream* /* os */) const {} |
1281 | 1308 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1410 const char* Name() const { | 1437 const char* Name() const { |
1411 const char* name; | 1438 const char* name; |
1412 { | 1439 { |
1413 // We protect name_ under g_gmock_mutex in case this mock | 1440 // We protect name_ under g_gmock_mutex in case this mock |
1414 // function is called from two threads concurrently. | 1441 // function is called from two threads concurrently. |
1415 MutexLock l(&g_gmock_mutex); | 1442 MutexLock l(&g_gmock_mutex); |
1416 name = name_; | 1443 name = name_; |
1417 } | 1444 } |
1418 return name; | 1445 return name; |
1419 } | 1446 } |
| 1447 |
1420 protected: | 1448 protected: |
1421 template <typename Function> | 1449 template <typename Function> |
1422 friend class MockSpec; | 1450 friend class MockSpec; |
1423 | 1451 |
1424 // Returns the result of invoking this mock function with the given | 1452 // Returns the result of invoking this mock function with the given |
1425 // arguments. This function can be safely called from multiple | 1453 // arguments. This function can be safely called from multiple |
1426 // threads concurrently. | 1454 // threads concurrently. |
1427 // L < g_gmock_mutex | 1455 // L < g_gmock_mutex |
1428 Result InvokeWith(const ArgumentTuple& args); | 1456 Result InvokeWith(const ArgumentTuple& args); |
1429 | 1457 |
1430 // Adds and returns a default action spec for this mock function. | 1458 // Adds and returns a default action spec for this mock function. |
1431 // L < g_gmock_mutex | 1459 // L < g_gmock_mutex |
1432 DefaultActionSpec<F>& AddNewDefaultActionSpec( | 1460 DefaultActionSpec<F>& AddNewDefaultActionSpec( |
1433 const char* file, int line, | 1461 const char* file, int line, |
1434 const ArgumentMatcherTuple& m) { | 1462 const ArgumentMatcherTuple& m) { |
1435 Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line); | 1463 Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line); |
1436 default_actions_.push_back(DefaultActionSpec<F>(file, line, m)); | 1464 default_actions_.push_back(DefaultActionSpec<F>(file, line, m)); |
1437 return default_actions_.back(); | 1465 return default_actions_.back(); |
1438 } | 1466 } |
1439 | 1467 |
1440 // Adds and returns an expectation spec for this mock function. | 1468 // Adds and returns an expectation spec for this mock function. |
1441 // L < g_gmock_mutex | 1469 // L < g_gmock_mutex |
1442 TypedExpectation<F>& AddNewExpectation( | 1470 TypedExpectation<F>& AddNewExpectation( |
1443 const char* file, int line, | 1471 const char* file, |
| 1472 int line, |
| 1473 const string& source_text, |
1444 const ArgumentMatcherTuple& m) { | 1474 const ArgumentMatcherTuple& m) { |
1445 Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line); | 1475 Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line); |
1446 const linked_ptr<TypedExpectation<F> > expectation( | 1476 const linked_ptr<TypedExpectation<F> > expectation( |
1447 new TypedExpectation<F>(this, file, line, m)); | 1477 new TypedExpectation<F>(this, file, line, source_text, m)); |
1448 expectations_.push_back(expectation); | 1478 expectations_.push_back(expectation); |
1449 | 1479 |
1450 // Adds this expectation into the implicit sequence if there is one. | 1480 // Adds this expectation into the implicit sequence if there is one. |
1451 Sequence* const implicit_sequence = g_gmock_implicit_sequence.get(); | 1481 Sequence* const implicit_sequence = g_gmock_implicit_sequence.get(); |
1452 if (implicit_sequence != NULL) { | 1482 if (implicit_sequence != NULL) { |
1453 implicit_sequence->AddExpectation(Expectation(expectation)); | 1483 implicit_sequence->AddExpectation(Expectation(expectation)); |
1454 } | 1484 } |
1455 | 1485 |
1456 return *expectation; | 1486 return *expectation; |
1457 } | 1487 } |
1458 | 1488 |
1459 // The current spec (either default action spec or expectation spec) | 1489 // The current spec (either default action spec or expectation spec) |
1460 // being described on this function mocker. | 1490 // being described on this function mocker. |
1461 MockSpec<F>& current_spec() { return current_spec_; } | 1491 MockSpec<F>& current_spec() { return current_spec_; } |
| 1492 |
1462 private: | 1493 private: |
1463 template <typename Func> friend class TypedExpectation; | 1494 template <typename Func> friend class TypedExpectation; |
1464 | 1495 |
1465 typedef std::vector<internal::linked_ptr<TypedExpectation<F> > > | 1496 typedef std::vector<internal::linked_ptr<TypedExpectation<F> > > |
1466 TypedExpectations; | 1497 TypedExpectations; |
1467 | 1498 |
1468 // Returns an Expectation object that references and co-owns exp, | 1499 // Returns an Expectation object that references and co-owns exp, |
1469 // which must be an expectation on this mock function. | 1500 // which must be an expectation on this mock function. |
1470 Expectation GetHandleOf(TypedExpectation<F>* exp) { | 1501 Expectation GetHandleOf(TypedExpectation<F>* exp) { |
1471 for (typename TypedExpectations::const_iterator it = expectations_.begin(); | 1502 for (typename TypedExpectations::const_iterator it = expectations_.begin(); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1577 g_gmock_mutex.AssertHeld(); | 1608 g_gmock_mutex.AssertHeld(); |
1578 const int count = static_cast<int>(expectations_.size()); | 1609 const int count = static_cast<int>(expectations_.size()); |
1579 *why << "Google Mock tried the following " << count << " " | 1610 *why << "Google Mock tried the following " << count << " " |
1580 << (count == 1 ? "expectation, but it didn't match" : | 1611 << (count == 1 ? "expectation, but it didn't match" : |
1581 "expectations, but none matched") | 1612 "expectations, but none matched") |
1582 << ":\n"; | 1613 << ":\n"; |
1583 for (int i = 0; i < count; i++) { | 1614 for (int i = 0; i < count; i++) { |
1584 *why << "\n"; | 1615 *why << "\n"; |
1585 expectations_[i]->DescribeLocationTo(why); | 1616 expectations_[i]->DescribeLocationTo(why); |
1586 if (count > 1) { | 1617 if (count > 1) { |
1587 *why << "tried expectation #" << i; | 1618 *why << "tried expectation #" << i << ": "; |
1588 } | 1619 } |
1589 *why << "\n"; | 1620 *why << expectations_[i]->source_text() << "...\n"; |
1590 expectations_[i]->DescribeMatchResultTo(args, why); | 1621 expectations_[i]->DescribeMatchResultTo(args, why); |
1591 expectations_[i]->DescribeCallCountTo(why); | 1622 expectations_[i]->DescribeCallCountTo(why); |
1592 } | 1623 } |
1593 } | 1624 } |
1594 | 1625 |
1595 // Address of the mock object this mock method belongs to. Only | 1626 // Address of the mock object this mock method belongs to. Only |
1596 // valid after this mock method has been called or | 1627 // valid after this mock method has been called or |
1597 // ON_CALL/EXPECT_CALL has been invoked on it. | 1628 // ON_CALL/EXPECT_CALL has been invoked on it. |
1598 const void* mock_obj_; // Protected by g_gmock_mutex. | 1629 const void* mock_obj_; // Protected by g_gmock_mutex. |
1599 | 1630 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1644 TypedExpectation<F>* const exp = it->get(); | 1675 TypedExpectation<F>* const exp = it->get(); |
1645 | 1676 |
1646 if (exp->IsOverSaturated()) { | 1677 if (exp->IsOverSaturated()) { |
1647 // There was an upper-bound violation. Since the error was | 1678 // There was an upper-bound violation. Since the error was |
1648 // already reported when it occurred, there is no need to do | 1679 // already reported when it occurred, there is no need to do |
1649 // anything here. | 1680 // anything here. |
1650 expectations_met = false; | 1681 expectations_met = false; |
1651 } else if (!exp->IsSatisfied()) { | 1682 } else if (!exp->IsSatisfied()) { |
1652 expectations_met = false; | 1683 expectations_met = false; |
1653 ::std::stringstream ss; | 1684 ::std::stringstream ss; |
1654 ss << "Actual function call count doesn't match this expectation.\n"; | 1685 ss << "Actual function call count doesn't match " |
| 1686 << exp->source_text() << "...\n"; |
1655 // No need to show the source file location of the expectation | 1687 // No need to show the source file location of the expectation |
1656 // in the description, as the Expect() call that follows already | 1688 // in the description, as the Expect() call that follows already |
1657 // takes care of it. | 1689 // takes care of it. |
| 1690 exp->MaybeDescribeExtraMatcherTo(&ss); |
1658 exp->DescribeCallCountTo(&ss); | 1691 exp->DescribeCallCountTo(&ss); |
1659 Expect(false, exp->file(), exp->line(), ss.str()); | 1692 Expect(false, exp->file(), exp->line(), ss.str()); |
1660 } | 1693 } |
1661 } | 1694 } |
1662 expectations_.clear(); | 1695 expectations_.clear(); |
1663 return expectations_met; | 1696 return expectations_met; |
1664 } | 1697 } |
1665 | 1698 |
1666 // Reports an uninteresting call (whose description is in msg) in the | 1699 // Reports an uninteresting call (whose description is in msg) in the |
1667 // manner specified by 'reaction'. | 1700 // manner specified by 'reaction'. |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1811 #define GMOCK_ON_CALL_IMPL_(obj, call) \ | 1844 #define GMOCK_ON_CALL_IMPL_(obj, call) \ |
1812 ((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \ | 1845 ((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \ |
1813 #obj, #call) | 1846 #obj, #call) |
1814 #define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call) | 1847 #define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call) |
1815 | 1848 |
1816 #define GMOCK_EXPECT_CALL_IMPL_(obj, call) \ | 1849 #define GMOCK_EXPECT_CALL_IMPL_(obj, call) \ |
1817 ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call) | 1850 ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call) |
1818 #define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call) | 1851 #define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call) |
1819 | 1852 |
1820 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ | 1853 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ |
OLD | NEW |