Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | // Copyright 2005, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | // |
michael@0 | 30 | // Author: wan@google.com (Zhanyong Wan) |
michael@0 | 31 | // |
michael@0 | 32 | // Tests for the Message class. |
michael@0 | 33 | |
michael@0 | 34 | #include "gtest/gtest-message.h" |
michael@0 | 35 | |
michael@0 | 36 | #include "gtest/gtest.h" |
michael@0 | 37 | |
michael@0 | 38 | namespace { |
michael@0 | 39 | |
michael@0 | 40 | using ::testing::Message; |
michael@0 | 41 | |
michael@0 | 42 | // A helper function that turns a Message into a C string. |
michael@0 | 43 | const char* ToCString(const Message& msg) { |
michael@0 | 44 | static testing::internal::String result; |
michael@0 | 45 | result = msg.GetString(); |
michael@0 | 46 | return result.c_str(); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | // Tests the testing::Message class |
michael@0 | 50 | |
michael@0 | 51 | // Tests the default constructor. |
michael@0 | 52 | TEST(MessageTest, DefaultConstructor) { |
michael@0 | 53 | const Message msg; |
michael@0 | 54 | EXPECT_STREQ("", ToCString(msg)); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | // Tests the copy constructor. |
michael@0 | 58 | TEST(MessageTest, CopyConstructor) { |
michael@0 | 59 | const Message msg1("Hello"); |
michael@0 | 60 | const Message msg2(msg1); |
michael@0 | 61 | EXPECT_STREQ("Hello", ToCString(msg2)); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | // Tests constructing a Message from a C-string. |
michael@0 | 65 | TEST(MessageTest, ConstructsFromCString) { |
michael@0 | 66 | Message msg("Hello"); |
michael@0 | 67 | EXPECT_STREQ("Hello", ToCString(msg)); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | // Tests streaming a float. |
michael@0 | 71 | TEST(MessageTest, StreamsFloat) { |
michael@0 | 72 | const char* const s = ToCString(Message() << 1.23456F << " " << 2.34567F); |
michael@0 | 73 | // Both numbers should be printed with enough precision. |
michael@0 | 74 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "1.234560", s); |
michael@0 | 75 | EXPECT_PRED_FORMAT2(testing::IsSubstring, " 2.345669", s); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | // Tests streaming a double. |
michael@0 | 79 | TEST(MessageTest, StreamsDouble) { |
michael@0 | 80 | const char* const s = ToCString(Message() << 1260570880.4555497 << " " |
michael@0 | 81 | << 1260572265.1954534); |
michael@0 | 82 | // Both numbers should be printed with enough precision. |
michael@0 | 83 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s); |
michael@0 | 84 | EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | // Tests streaming a non-char pointer. |
michael@0 | 88 | TEST(MessageTest, StreamsPointer) { |
michael@0 | 89 | int n = 0; |
michael@0 | 90 | int* p = &n; |
michael@0 | 91 | EXPECT_STRNE("(null)", ToCString(Message() << p)); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | // Tests streaming a NULL non-char pointer. |
michael@0 | 95 | TEST(MessageTest, StreamsNullPointer) { |
michael@0 | 96 | int* p = NULL; |
michael@0 | 97 | EXPECT_STREQ("(null)", ToCString(Message() << p)); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | // Tests streaming a C string. |
michael@0 | 101 | TEST(MessageTest, StreamsCString) { |
michael@0 | 102 | EXPECT_STREQ("Foo", ToCString(Message() << "Foo")); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | // Tests streaming a NULL C string. |
michael@0 | 106 | TEST(MessageTest, StreamsNullCString) { |
michael@0 | 107 | char* p = NULL; |
michael@0 | 108 | EXPECT_STREQ("(null)", ToCString(Message() << p)); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | // Tests streaming std::string. |
michael@0 | 112 | TEST(MessageTest, StreamsString) { |
michael@0 | 113 | const ::std::string str("Hello"); |
michael@0 | 114 | EXPECT_STREQ("Hello", ToCString(Message() << str)); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | // Tests that we can output strings containing embedded NULs. |
michael@0 | 118 | TEST(MessageTest, StreamsStringWithEmbeddedNUL) { |
michael@0 | 119 | const char char_array_with_nul[] = |
michael@0 | 120 | "Here's a NUL\0 and some more string"; |
michael@0 | 121 | const ::std::string string_with_nul(char_array_with_nul, |
michael@0 | 122 | sizeof(char_array_with_nul) - 1); |
michael@0 | 123 | EXPECT_STREQ("Here's a NUL\\0 and some more string", |
michael@0 | 124 | ToCString(Message() << string_with_nul)); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | // Tests streaming a NUL char. |
michael@0 | 128 | TEST(MessageTest, StreamsNULChar) { |
michael@0 | 129 | EXPECT_STREQ("\\0", ToCString(Message() << '\0')); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | // Tests streaming int. |
michael@0 | 133 | TEST(MessageTest, StreamsInt) { |
michael@0 | 134 | EXPECT_STREQ("123", ToCString(Message() << 123)); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | // Tests that basic IO manipulators (endl, ends, and flush) can be |
michael@0 | 138 | // streamed to Message. |
michael@0 | 139 | TEST(MessageTest, StreamsBasicIoManip) { |
michael@0 | 140 | EXPECT_STREQ("Line 1.\nA NUL char \\0 in line 2.", |
michael@0 | 141 | ToCString(Message() << "Line 1." << std::endl |
michael@0 | 142 | << "A NUL char " << std::ends << std::flush |
michael@0 | 143 | << " in line 2.")); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | // Tests Message::GetString() |
michael@0 | 147 | TEST(MessageTest, GetString) { |
michael@0 | 148 | Message msg; |
michael@0 | 149 | msg << 1 << " lamb"; |
michael@0 | 150 | EXPECT_STREQ("1 lamb", msg.GetString().c_str()); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | // Tests streaming a Message object to an ostream. |
michael@0 | 154 | TEST(MessageTest, StreamsToOStream) { |
michael@0 | 155 | Message msg("Hello"); |
michael@0 | 156 | ::std::stringstream ss; |
michael@0 | 157 | ss << msg; |
michael@0 | 158 | EXPECT_STREQ("Hello", testing::internal::StringStreamToString(&ss).c_str()); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | // Tests that a Message object doesn't take up too much stack space. |
michael@0 | 162 | TEST(MessageTest, DoesNotTakeUpMuchStackSpace) { |
michael@0 | 163 | EXPECT_LE(sizeof(Message), 16U); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | } // namespace |