1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc.
3
// http://code.google.com/p/protobuf/
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
17
// Author: kenton@google.com (Kenton Varda)
20
#include <google/protobuf/stubs/common.h>
21
#include <google/protobuf/stubs/strutil.h>
22
#include <google/protobuf/stubs/substitute.h>
24
#include <google/protobuf/testing/googletest.h>
25
#include <gtest/gtest.h>
33
// TODO(kenton): More tests.
35
#ifdef PACKAGE_VERSION // only defined when using automake, not MSVC
37
TEST(VersionTest, VersionMatchesConfig) {
38
// Verify that the version string specified in config.h matches the one
39
// in common.h. The config.h version is a string which may have a suffix
40
// like "beta", so we remove that.
41
string version = PACKAGE_VERSION;
42
int pos = version.size();
43
while (pos > 0 && !ascii_isdigit(version[pos-1])) {
48
EXPECT_EQ(version, internal::VersionString(GOOGLE_PROTOBUF_VERSION));
51
#endif // PACKAGE_VERSION
53
vector<string> captured_messages_;
55
void CaptureLog(LogLevel level, const char* filename, int line,
56
const string& message) {
57
captured_messages_.push_back(
58
strings::Substitute("$0 $1:$2: $3",
59
implicit_cast<int>(level), filename, line, message));
62
TEST(LoggingTest, DefaultLogging) {
65
GOOGLE_LOG(INFO ) << "A message.";
66
GOOGLE_LOG(WARNING) << "A warning.";
67
GOOGLE_LOG(ERROR ) << "An error.";
69
string text = GetCapturedTestStderr();
71
"libprotobuf INFO "__FILE__":" + SimpleItoa(line + 1) + "] A message.\n"
72
"libprotobuf WARNING "__FILE__":" + SimpleItoa(line + 2) + "] A warning.\n"
73
"libprotobuf ERROR "__FILE__":" + SimpleItoa(line + 3) + "] An error.\n",
77
TEST(LoggingTest, NullLogging) {
78
LogHandler* old_handler = SetLogHandler(NULL);
81
GOOGLE_LOG(INFO ) << "A message.";
82
GOOGLE_LOG(WARNING) << "A warning.";
83
GOOGLE_LOG(ERROR ) << "An error.";
85
EXPECT_TRUE(SetLogHandler(old_handler) == NULL);
87
string text = GetCapturedTestStderr();
91
TEST(LoggingTest, CaptureLogging) {
92
captured_messages_.clear();
94
LogHandler* old_handler = SetLogHandler(&CaptureLog);
96
int start_line = __LINE__;
97
GOOGLE_LOG(ERROR) << "An error.";
98
GOOGLE_LOG(WARNING) << "A warning.";
100
EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog);
102
ASSERT_EQ(2, captured_messages_.size());
104
"2 "__FILE__":" + SimpleItoa(start_line + 1) + ": An error.",
105
captured_messages_[0]);
107
"1 "__FILE__":" + SimpleItoa(start_line + 2) + ": A warning.",
108
captured_messages_[1]);
111
TEST(LoggingTest, SilenceLogging) {
112
captured_messages_.clear();
114
LogHandler* old_handler = SetLogHandler(&CaptureLog);
116
int line1 = __LINE__; GOOGLE_LOG(INFO) << "Visible1";
117
LogSilencer* silencer1 = new LogSilencer;
118
GOOGLE_LOG(INFO) << "Not visible.";
119
LogSilencer* silencer2 = new LogSilencer;
120
GOOGLE_LOG(INFO) << "Not visible.";
122
GOOGLE_LOG(INFO) << "Not visible.";
124
int line2 = __LINE__; GOOGLE_LOG(INFO) << "Visible2";
126
EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog);
128
ASSERT_EQ(2, captured_messages_.size());
130
"0 "__FILE__":" + SimpleItoa(line1) + ": Visible1",
131
captured_messages_[0]);
133
"0 "__FILE__":" + SimpleItoa(line2) + ": Visible2",
134
captured_messages_[1]);
137
class ClosureTest : public testing::Test {
139
void SetA123Method() { a_ = 123; }
140
static void SetA123Function() { current_instance_->a_ = 123; }
142
void SetAMethod(int a) { a_ = a; }
143
void SetCMethod(string c) { c_ = c; }
145
static void SetAFunction(int a) { current_instance_->a_ = a; }
146
static void SetCFunction(string c) { current_instance_->c_ = c; }
148
void SetABMethod(int a, const char* b) { a_ = a; b_ = b; }
149
static void SetABFunction(int a, const char* b) {
150
current_instance_->a_ = a;
151
current_instance_->b_ = b;
154
virtual void SetUp() {
155
current_instance_ = this;
165
static ClosureTest* current_instance_;
168
ClosureTest* ClosureTest::current_instance_ = NULL;
170
TEST_F(ClosureTest, TestClosureFunction0) {
171
Closure* closure = NewCallback(&SetA123Function);
177
TEST_F(ClosureTest, TestClosureMethod0) {
178
Closure* closure = NewCallback(current_instance_,
179
&ClosureTest::SetA123Method);
185
TEST_F(ClosureTest, TestClosureFunction1) {
186
Closure* closure = NewCallback(&SetAFunction, 456);
192
TEST_F(ClosureTest, TestClosureMethod1) {
193
Closure* closure = NewCallback(current_instance_,
194
&ClosureTest::SetAMethod, 456);
200
TEST_F(ClosureTest, TestClosureFunction1String) {
201
Closure* closure = NewCallback(&SetCFunction, string("test"));
202
EXPECT_NE("test", c_);
204
EXPECT_EQ("test", c_);
207
TEST_F(ClosureTest, TestClosureMethod1String) {
208
Closure* closure = NewCallback(current_instance_,
209
&ClosureTest::SetCMethod, string("test"));
210
EXPECT_NE("test", c_);
212
EXPECT_EQ("test", c_);
215
TEST_F(ClosureTest, TestClosureFunction2) {
216
const char* cstr = "hello";
217
Closure* closure = NewCallback(&SetABFunction, 789, cstr);
225
TEST_F(ClosureTest, TestClosureMethod2) {
226
const char* cstr = "hello";
227
Closure* closure = NewCallback(current_instance_,
228
&ClosureTest::SetABMethod, 789, cstr);
236
// Repeat all of the above with NewPermanentCallback()
238
TEST_F(ClosureTest, TestPermanentClosureFunction0) {
239
Closure* closure = NewPermanentCallback(&SetA123Function);
249
TEST_F(ClosureTest, TestPermanentClosureMethod0) {
250
Closure* closure = NewPermanentCallback(current_instance_,
251
&ClosureTest::SetA123Method);
261
TEST_F(ClosureTest, TestPermanentClosureFunction1) {
262
Closure* closure = NewPermanentCallback(&SetAFunction, 456);
272
TEST_F(ClosureTest, TestPermanentClosureMethod1) {
273
Closure* closure = NewPermanentCallback(current_instance_,
274
&ClosureTest::SetAMethod, 456);
284
TEST_F(ClosureTest, TestPermanentClosureFunction2) {
285
const char* cstr = "hello";
286
Closure* closure = NewPermanentCallback(&SetABFunction, 789, cstr);
300
TEST_F(ClosureTest, TestPermanentClosureMethod2) {
301
const char* cstr = "hello";
302
Closure* closure = NewPermanentCallback(current_instance_,
303
&ClosureTest::SetABMethod, 789, cstr);
317
} // anonymous namespace
318
} // namespace protobuf
319
} // namespace google