| // Copyright 2025 Google Inc. All Rights Reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include "output_stream.h" |
| |
| #include "test.h" |
| |
| /// A class that wraps an integer token and provides a ToString() method. |
| /// Used to verify that OutputStream::operator<< correctly supports it. |
| struct TestToken { |
| TestToken(int x) : x_(x) {} |
| std::string ToString() const { return StringFormat("Token(%d)", x_); } |
| int x_; |
| }; |
| |
| TEST(OutputStreamTest, StringOutputStream) { |
| StringOutputStream s; |
| |
| EXPECT_TRUE(s.str().empty()); |
| |
| s.Write("foobar", 6); |
| EXPECT_EQ(s.str(), "foobar"); |
| EXPECT_EQ(s.Take(), "foobar"); |
| EXPECT_TRUE(s.str().empty()); |
| |
| s.Write("Hello World!", 10); |
| EXPECT_EQ(s.str(), "Hello Worl"); |
| EXPECT_EQ(s.Take(), "Hello Worl"); |
| |
| s.Format("Hello %s", "World!"); |
| EXPECT_EQ(s.Take(), "Hello World!"); |
| |
| s << int(10); |
| EXPECT_EQ(s.Take(), "10"); |
| |
| s << int(-42); |
| EXPECT_EQ(s.Take(), "-42"); |
| |
| s << char('c'); |
| EXPECT_EQ(s.Take(), "c"); |
| |
| s << static_cast<unsigned char>('c'); |
| EXPECT_EQ(s.Take(), "99"); |
| |
| s << true; |
| EXPECT_EQ(s.Take(), "true"); |
| |
| s << false; |
| EXPECT_EQ(s.Take(), "false"); |
| |
| s << "This is " << std::string("a") << " concatenation of " << 5 << " parts"; |
| EXPECT_EQ(s.Take(), "This is a concatenation of 5 parts"); |
| |
| s << TestToken(10) << ", " << TestToken(20); |
| EXPECT_EQ(s.Take(), "Token(10), Token(20)"); |
| } |