| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 #if GTEST_OS_CYGWIN | 47 #if GTEST_OS_CYGWIN |
| 48 #include <sys/types.h> // For ssize_t. NOLINT | 48 #include <sys/types.h> // For ssize_t. NOLINT |
| 49 #endif | 49 #endif |
| 50 | 50 |
| 51 namespace testing { | 51 namespace testing { |
| 52 namespace internal { | 52 namespace internal { |
| 53 | 53 |
| 54 namespace { | 54 namespace { |
| 55 | 55 |
| 56 using ::std::tr1::make_tuple; |
| 56 using ::std::tr1::tuple; | 57 using ::std::tr1::tuple; |
| 57 | 58 |
| 58 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) { | 59 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) { |
| 59 EXPECT_EQ("", ConvertIdentifierNameToWords("")); | 60 EXPECT_EQ("", ConvertIdentifierNameToWords("")); |
| 60 EXPECT_EQ("", ConvertIdentifierNameToWords("_")); | 61 EXPECT_EQ("", ConvertIdentifierNameToWords("_")); |
| 61 EXPECT_EQ("", ConvertIdentifierNameToWords("__")); | 62 EXPECT_EQ("", ConvertIdentifierNameToWords("__")); |
| 62 } | 63 } |
| 63 | 64 |
| 64 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) { | 65 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) { |
| 65 EXPECT_EQ("1", ConvertIdentifierNameToWords("_1")); | 66 EXPECT_EQ("1", ConvertIdentifierNameToWords("_1")); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 124 |
| 124 // Tests that RemoveConst does not affect non-const types. | 125 // Tests that RemoveConst does not affect non-const types. |
| 125 TEST(RemoveConstTest, DoesNotAffectNonConstType) { | 126 TEST(RemoveConstTest, DoesNotAffectNonConstType) { |
| 126 CompileAssertTypesEqual<int, RemoveConst<int>::type>(); | 127 CompileAssertTypesEqual<int, RemoveConst<int>::type>(); |
| 127 CompileAssertTypesEqual<char&, RemoveConst<char&>::type>(); | 128 CompileAssertTypesEqual<char&, RemoveConst<char&>::type>(); |
| 128 } | 129 } |
| 129 | 130 |
| 130 // Tests that RemoveConst removes const from const types. | 131 // Tests that RemoveConst removes const from const types. |
| 131 TEST(RemoveConstTest, RemovesConst) { | 132 TEST(RemoveConstTest, RemovesConst) { |
| 132 CompileAssertTypesEqual<int, RemoveConst<const int>::type>(); | 133 CompileAssertTypesEqual<int, RemoveConst<const int>::type>(); |
| 134 CompileAssertTypesEqual<char[2], RemoveConst<const char[2]>::type>(); |
| 135 CompileAssertTypesEqual<char[2][3], RemoveConst<const char[2][3]>::type>(); |
| 133 } | 136 } |
| 134 | 137 |
| 135 // Tests GMOCK_REMOVE_CONST_. | 138 // Tests GMOCK_REMOVE_CONST_. |
| 136 | 139 |
| 137 template <typename T1, typename T2> | 140 template <typename T1, typename T2> |
| 138 void TestGMockRemoveConst() { | 141 void TestGMockRemoveConst() { |
| 139 CompileAssertTypesEqual<T1, GMOCK_REMOVE_CONST_(T2)>(); | 142 CompileAssertTypesEqual<T1, GMOCK_REMOVE_CONST_(T2)>(); |
| 140 } | 143 } |
| 141 | 144 |
| 142 TEST(RemoveConstTest, MacroVersion) { | 145 TEST(RemoveConstTest, MacroVersion) { |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 TEST(ExpectTest, FailsNonfatallyOnFalse) { | 490 TEST(ExpectTest, FailsNonfatallyOnFalse) { |
| 488 EXPECT_NONFATAL_FAILURE({ // NOLINT | 491 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 489 Expect(false, __FILE__, __LINE__, "This should fail."); | 492 Expect(false, __FILE__, __LINE__, "This should fail."); |
| 490 }, "This should fail"); | 493 }, "This should fail"); |
| 491 | 494 |
| 492 EXPECT_NONFATAL_FAILURE({ // NOLINT | 495 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 493 Expect(false, __FILE__, __LINE__); | 496 Expect(false, __FILE__, __LINE__); |
| 494 }, "Expectation failed"); | 497 }, "Expectation failed"); |
| 495 } | 498 } |
| 496 | 499 |
| 500 // Tests LogIsVisible(). |
| 501 |
| 502 class LogIsVisibleTest : public ::testing::Test { |
| 503 protected: |
| 504 virtual void SetUp() { original_verbose_ = GMOCK_FLAG(verbose); } |
| 505 virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } |
| 506 |
| 507 string original_verbose_; |
| 508 }; |
| 509 |
| 510 TEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) { |
| 511 GMOCK_FLAG(verbose) = kInfoVerbosity; |
| 512 EXPECT_TRUE(LogIsVisible(INFO)); |
| 513 EXPECT_TRUE(LogIsVisible(WARNING)); |
| 514 } |
| 515 |
| 516 TEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) { |
| 517 GMOCK_FLAG(verbose) = kErrorVerbosity; |
| 518 EXPECT_FALSE(LogIsVisible(INFO)); |
| 519 EXPECT_FALSE(LogIsVisible(WARNING)); |
| 520 } |
| 521 |
| 522 TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) { |
| 523 GMOCK_FLAG(verbose) = kWarningVerbosity; |
| 524 EXPECT_FALSE(LogIsVisible(INFO)); |
| 525 EXPECT_TRUE(LogIsVisible(WARNING)); |
| 526 } |
| 527 |
| 497 // TODO(wan@google.com): find a way to re-enable these tests. | 528 // TODO(wan@google.com): find a way to re-enable these tests. |
| 498 #if 0 | 529 #if 0 |
| 499 | 530 |
| 500 // Tests the Log() function. | 531 // Tests the Log() function. |
| 501 | 532 |
| 502 // Verifies that Log() behaves correctly for the given verbosity level | 533 // Verifies that Log() behaves correctly for the given verbosity level |
| 503 // and log severity. | 534 // and log severity. |
| 504 void TestLogWithSeverity(const string& verbosity, LogSeverity severity, | 535 void TestLogWithSeverity(const string& verbosity, LogSeverity severity, |
| 505 bool should_print) { | 536 bool should_print) { |
| 506 const string old_flag = GMOCK_FLAG(verbose); | 537 const string old_flag = GMOCK_FLAG(verbose); |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 } | 717 } |
| 687 | 718 |
| 688 // Verifies that ON_CALL prints provided _ argument. | 719 // Verifies that ON_CALL prints provided _ argument. |
| 689 TEST(OnCallTest, LogsAnythingArgument) { | 720 TEST(OnCallTest, LogsAnythingArgument) { |
| 690 EXPECT_THAT(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity), | 721 EXPECT_THAT(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity), |
| 691 HasSubstr("ON_CALL(mock, TestMethodArg(_)")); | 722 HasSubstr("ON_CALL(mock, TestMethodArg(_)")); |
| 692 } | 723 } |
| 693 | 724 |
| 694 #endif // 0 | 725 #endif // 0 |
| 695 | 726 |
| 727 // Tests ArrayEq(). |
| 728 |
| 729 TEST(ArrayEqTest, WorksForDegeneratedArrays) { |
| 730 EXPECT_TRUE(ArrayEq(5, 5L)); |
| 731 EXPECT_FALSE(ArrayEq('a', 0)); |
| 732 } |
| 733 |
| 734 TEST(ArrayEqTest, WorksForOneDimensionalArrays) { |
| 735 const int a[] = { 0, 1 }; |
| 736 long b[] = { 0, 1 }; |
| 737 EXPECT_TRUE(ArrayEq(a, b)); |
| 738 EXPECT_TRUE(ArrayEq(a, 2, b)); |
| 739 |
| 740 b[0] = 2; |
| 741 EXPECT_FALSE(ArrayEq(a, b)); |
| 742 EXPECT_FALSE(ArrayEq(a, 1, b)); |
| 743 } |
| 744 |
| 745 TEST(ArrayEqTest, WorksForTwoDimensionalArrays) { |
| 746 const char a[][3] = { "hi", "lo" }; |
| 747 const char b[][3] = { "hi", "lo" }; |
| 748 const char c[][3] = { "hi", "li" }; |
| 749 |
| 750 EXPECT_TRUE(ArrayEq(a, b)); |
| 751 EXPECT_TRUE(ArrayEq(a, 2, b)); |
| 752 |
| 753 EXPECT_FALSE(ArrayEq(a, c)); |
| 754 EXPECT_FALSE(ArrayEq(a, 2, c)); |
| 755 } |
| 756 |
| 757 // Tests ArrayAwareFind(). |
| 758 |
| 759 TEST(ArrayAwareFindTest, WorksForOneDimensionalArray) { |
| 760 const char a[] = "hello"; |
| 761 EXPECT_EQ(a + 4, ArrayAwareFind(a, a + 5, 'o')); |
| 762 EXPECT_EQ(a + 5, ArrayAwareFind(a, a + 5, 'x')); |
| 763 } |
| 764 |
| 765 TEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) { |
| 766 int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; |
| 767 const int b[2] = { 2, 3 }; |
| 768 EXPECT_EQ(a + 1, ArrayAwareFind(a, a + 3, b)); |
| 769 |
| 770 const int c[2] = { 6, 7 }; |
| 771 EXPECT_EQ(a + 3, ArrayAwareFind(a, a + 3, c)); |
| 772 } |
| 773 |
| 774 // Tests CopyArray(). |
| 775 |
| 776 TEST(CopyArrayTest, WorksForDegeneratedArrays) { |
| 777 int n = 0; |
| 778 CopyArray('a', &n); |
| 779 EXPECT_EQ('a', n); |
| 780 } |
| 781 |
| 782 TEST(CopyArrayTest, WorksForOneDimensionalArrays) { |
| 783 const char a[3] = "hi"; |
| 784 int b[3]; |
| 785 CopyArray(a, &b); |
| 786 EXPECT_TRUE(ArrayEq(a, b)); |
| 787 |
| 788 int c[3]; |
| 789 CopyArray(a, 3, c); |
| 790 EXPECT_TRUE(ArrayEq(a, c)); |
| 791 } |
| 792 |
| 793 TEST(CopyArrayTest, WorksForTwoDimensionalArrays) { |
| 794 const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } }; |
| 795 int b[2][3]; |
| 796 CopyArray(a, &b); |
| 797 EXPECT_TRUE(ArrayEq(a, b)); |
| 798 |
| 799 int c[2][3]; |
| 800 CopyArray(a, 2, c); |
| 801 EXPECT_TRUE(ArrayEq(a, c)); |
| 802 } |
| 803 |
| 804 // Tests NativeArray. |
| 805 |
| 806 TEST(NativeArrayTest, ConstructorFromArrayReferenceWorks) { |
| 807 const int a[3] = { 0, 1, 2 }; |
| 808 NativeArray<int> na(a, kReference); |
| 809 EXPECT_EQ(3, na.size()); |
| 810 EXPECT_EQ(a, na.begin()); |
| 811 } |
| 812 |
| 813 TEST(NativeArrayTest, ConstructorFromTupleWorks) { |
| 814 int a[3] = { 0, 1, 2 }; |
| 815 int* const p = a; |
| 816 // Tests with a plain pointer. |
| 817 NativeArray<int> na(make_tuple(p, 3U), kReference); |
| 818 EXPECT_EQ(a, na.begin()); |
| 819 |
| 820 const linked_ptr<char> b(new char); |
| 821 *b = 'a'; |
| 822 // Tests with a smart pointer. |
| 823 NativeArray<char> nb(make_tuple(b, 1), kCopy); |
| 824 EXPECT_NE(b.get(), nb.begin()); |
| 825 EXPECT_EQ('a', nb.begin()[0]); |
| 826 } |
| 827 |
| 828 TEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) { |
| 829 typedef int Array[2]; |
| 830 Array* a = new Array[1]; |
| 831 (*a)[0] = 0; |
| 832 (*a)[1] = 1; |
| 833 NativeArray<int> na(*a, kCopy); |
| 834 EXPECT_NE(*a, na.begin()); |
| 835 delete[] a; |
| 836 EXPECT_EQ(0, na.begin()[0]); |
| 837 EXPECT_EQ(1, na.begin()[1]); |
| 838 |
| 839 // We rely on the heap checker to verify that na deletes the copy of |
| 840 // array. |
| 841 } |
| 842 |
| 843 TEST(NativeArrayTest, TypeMembersAreCorrect) { |
| 844 StaticAssertTypeEq<char, NativeArray<char>::value_type>(); |
| 845 StaticAssertTypeEq<int[2], NativeArray<int[2]>::value_type>(); |
| 846 |
| 847 StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>(); |
| 848 StaticAssertTypeEq<const bool(*)[2], NativeArray<bool[2]>::const_iterator>(); |
| 849 } |
| 850 |
| 851 TEST(NativeArrayTest, MethodsWork) { |
| 852 const int a[] = { 0, 1, 2 }; |
| 853 NativeArray<int> na(a, kCopy); |
| 854 ASSERT_EQ(3, na.size()); |
| 855 EXPECT_EQ(3, na.end() - na.begin()); |
| 856 |
| 857 NativeArray<int>::const_iterator it = na.begin(); |
| 858 EXPECT_EQ(0, *it); |
| 859 ++it; |
| 860 EXPECT_EQ(1, *it); |
| 861 it++; |
| 862 EXPECT_EQ(2, *it); |
| 863 ++it; |
| 864 EXPECT_EQ(na.end(), it); |
| 865 |
| 866 EXPECT_THAT(na, Eq(na)); |
| 867 |
| 868 NativeArray<int> na2(a, kReference); |
| 869 EXPECT_THAT(na, Eq(na2)); |
| 870 |
| 871 const int b1[] = { 0, 1, 1 }; |
| 872 const int b2[] = { 0, 1, 2, 3 }; |
| 873 EXPECT_THAT(na, Not(Eq(NativeArray<int>(b1, kReference)))); |
| 874 EXPECT_THAT(na, Not(Eq(NativeArray<int>(b2, kCopy)))); |
| 875 } |
| 876 |
| 877 TEST(NativeArrayTest, WorksForTwoDimensionalArray) { |
| 878 const char a[2][3] = { "hi", "lo" }; |
| 879 NativeArray<char[3]> na(a, kReference); |
| 880 ASSERT_EQ(2, na.size()); |
| 881 EXPECT_EQ(a, na.begin()); |
| 882 } |
| 883 |
| 884 // Tests StlContainerView. |
| 885 |
| 886 TEST(StlContainerViewTest, WorksForStlContainer) { |
| 887 StaticAssertTypeEq<std::vector<int>, |
| 888 StlContainerView<std::vector<int> >::type>(); |
| 889 StaticAssertTypeEq<const std::vector<double>&, |
| 890 StlContainerView<std::vector<double> >::const_reference>(); |
| 891 |
| 892 typedef std::vector<char> Chars; |
| 893 Chars v1; |
| 894 const Chars& v2(StlContainerView<Chars>::ConstReference(v1)); |
| 895 EXPECT_EQ(&v1, &v2); |
| 896 |
| 897 v1.push_back('a'); |
| 898 Chars v3 = StlContainerView<Chars>::Copy(v1); |
| 899 EXPECT_THAT(v3, Eq(v3)); |
| 900 } |
| 901 |
| 902 TEST(StlContainerViewTest, WorksForStaticNativeArray) { |
| 903 StaticAssertTypeEq<NativeArray<int>, |
| 904 StlContainerView<int[3]>::type>(); |
| 905 StaticAssertTypeEq<NativeArray<double>, |
| 906 StlContainerView<const double[4]>::type>(); |
| 907 StaticAssertTypeEq<NativeArray<char[3]>, |
| 908 StlContainerView<const char[2][3]>::type>(); |
| 909 |
| 910 StaticAssertTypeEq<const NativeArray<int>, |
| 911 StlContainerView<int[2]>::const_reference>(); |
| 912 |
| 913 int a1[3] = { 0, 1, 2 }; |
| 914 NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1); |
| 915 EXPECT_EQ(3, a2.size()); |
| 916 EXPECT_EQ(a1, a2.begin()); |
| 917 |
| 918 const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1); |
| 919 ASSERT_EQ(3, a3.size()); |
| 920 EXPECT_EQ(0, a3.begin()[0]); |
| 921 EXPECT_EQ(1, a3.begin()[1]); |
| 922 EXPECT_EQ(2, a3.begin()[2]); |
| 923 |
| 924 // Makes sure a1 and a3 aren't aliases. |
| 925 a1[0] = 3; |
| 926 EXPECT_EQ(0, a3.begin()[0]); |
| 927 } |
| 928 |
| 929 TEST(StlContainerViewTest, WorksForDynamicNativeArray) { |
| 930 StaticAssertTypeEq<NativeArray<int>, |
| 931 StlContainerView<tuple<const int*, size_t> >::type>(); |
| 932 StaticAssertTypeEq<NativeArray<double>, |
| 933 StlContainerView<tuple<linked_ptr<double>, int> >::type>(); |
| 934 |
| 935 StaticAssertTypeEq<const NativeArray<int>, |
| 936 StlContainerView<tuple<const int*, int> >::const_reference>(); |
| 937 |
| 938 int a1[3] = { 0, 1, 2 }; |
| 939 const int* const p1 = a1; |
| 940 NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >:: |
| 941 ConstReference(make_tuple(p1, 3)); |
| 942 EXPECT_EQ(3, a2.size()); |
| 943 EXPECT_EQ(a1, a2.begin()); |
| 944 |
| 945 const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >:: |
| 946 Copy(make_tuple(static_cast<int*>(a1), 3)); |
| 947 ASSERT_EQ(3, a3.size()); |
| 948 EXPECT_EQ(0, a3.begin()[0]); |
| 949 EXPECT_EQ(1, a3.begin()[1]); |
| 950 EXPECT_EQ(2, a3.begin()[2]); |
| 951 |
| 952 // Makes sure a1 and a3 aren't aliases. |
| 953 a1[0] = 3; |
| 954 EXPECT_EQ(0, a3.begin()[0]); |
| 955 } |
| 956 |
| 696 } // namespace | 957 } // namespace |
| 697 } // namespace internal | 958 } // namespace internal |
| 698 } // namespace testing | 959 } // namespace testing |
| OLD | NEW |