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