Commit f4b97bbb authored by gejun's avatar gejun

Remove UT's deps on gmock & fix UT

parent 5b817802
......@@ -9,6 +9,7 @@ include config.mk
CPPFLAGS+=-DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DBRPC_REVISION=\"$(shell git rev-parse --short HEAD)\"
CXXFLAGS+=$(CPPFLAGS) -g -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -std=c++0x
CFLAGS+=$(CPPFLAGS) -g -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-unused-parameter -fno-omit-frame-pointer
DEBUG_CXXFLAGS = $(CXXFLAGS) -DUNIT_TEST -DBVAR_NOT_LINK_DEFAULT_VARIABLES
HDRPATHS=-I./src $(addprefix -I, $(HDRS))
LIBPATHS = $(addprefix -L, $(LIBS))
COMMA = ,
......@@ -168,13 +169,15 @@ MCPACK2PB_SOURCES = \
MCPACK2PB_OBJS = src/idl_options.pb.o $(addsuffix .o, $(basename $(MCPACK2PB_SOURCES)))
OBJS=$(BUTIL_OBJS) $(BVAR_OBJS) $(BTHREAD_OBJS) $(JSON2PB_OBJS) $(MCPACK2PB_OBJS) $(BRPC_OBJS)
BVAR_DEBUG_OBJS=$(BUTIL_OBJS:.o=.dbg.o) $(BVAR_OBJS:.o=.dbg.o)
DEBUG_OBJS = $(OBJS:.o=.dbg.o)
.PHONY:all
all: protoc-gen-mcpack libbrpc.a libbrpc.so output/include output/lib output/bin
.PHONY:debug
debug: libbrpc.dbg.a
debug: libbrpc.dbg.a libbvar.dbg.a
.PHONY:clean
clean:clean_debug
......@@ -183,7 +186,9 @@ clean:clean_debug
.PHONY:clean_debug
clean_debug:
@rm -rf libbrpc.dbg.a $(DEBUG_OBJS)
@rm -rf libbrpc.dbg.a libbvar.dbg.a $(DEBUG_OBJS)
.PRECIOUS: %.o
protoc-gen-mcpack: src/idl_options.pb.cc src/mcpack2pb/generator.o libbrpc.a
@echo "Linking $@"
......@@ -192,15 +197,19 @@ protoc-gen-mcpack: src/idl_options.pb.cc src/mcpack2pb/generator.o libbrpc.a
# force generation of pb headers before compiling to avoid fail-to-import issues in compiling pb.cc
libbrpc.a:$(BRPC_PROTOS:.proto=.pb.h) $(OBJS)
@echo "Packing $@"
@ar crs $@ $(OBJS)
@ar crs $@ $(filter %.o,$^)
libbrpc.so:$(BRPC_PROTOS:.proto=.pb.h) $(OBJS)
@echo "Linking $@"
$(CXX) -shared -o $@ $(HDRPATHS) $(LIBPATHS) $(SOPATHS) -Xlinker "-(" $(OBJS) -Xlinker "-)" $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS)
$(CXX) -shared -o $@ $(LIBPATHS) $(SOPATHS) -Xlinker "-(" $(filter %.o,$^) -Xlinker "-)" $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS)
libbvar.dbg.a:$(BVAR_DEBUG_OBJS)
@echo "Packing $@"
@ar crs $@ $^
libbrpc.dbg.a:$(BRPC_PROTOS:.proto=.pb.h) $(DEBUG_OBJS)
@echo "Packing $@"
@ar crs $@ $(DEBUG_OBJS)
@ar crs $@ $(filter %.o,$^)
.PHONY:output/include
output/include:
......@@ -231,7 +240,7 @@ output/bin:protoc-gen-mcpack
%.dbg.o:%.cpp
@echo "Compiling $@"
@$(CXX) -c $(HDRPATHS) $(CXXFLAGS) -DBVAR_NOT_LINK_DEFAULT_VARIABLES $< -o $@
@$(CXX) -c $(HDRPATHS) $(DEBUG_CXXFLAGS) $< -o $@
%.o:%.cc
@echo "Compiling $@"
......@@ -239,7 +248,7 @@ output/bin:protoc-gen-mcpack
%.dbg.o:%.cc
@echo "Compiling $@"
@$(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@
@$(CXX) -c $(HDRPATHS) $(DEBUG_CXXFLAGS) $< -o $@
%.o:%.c
@echo "Compiling $@"
......
......@@ -324,22 +324,6 @@ else
fi
append_to_output "endif"
#gmock
GMOCK_LIB=$(find_dir_of_lib gmock)
HDRS=$OLD_HDRS
LIBS=$OLD_LIBS
append_to_output "ifeq (\$(NEED_GMOCK), 1)"
if [ -z "$GMOCK_LIB" ]; then
append_to_output " \$(error \"Fail to find gmock\")"
else
GMOCK_HDR=$(find_dir_of_header_or_die gmock/gmock.h)
append_to_output_libs $GMOCK_LIB " "
append_to_output_headers $GMOCK_HDR " "
append_to_output_linkings $GMOCK_LIB gmock " "
append_to_output_linkings $GMOCK_LIB gmock_main " "
fi
append_to_output "endif"
# generate src/butil/config.h
cat << EOF > src/butil/config.h
// This file is auto-generated by $(basename "$0"). DON'T edit it!
......
......@@ -36,15 +36,8 @@ Examples link brpc statically, if you need to link the shared version, `make cle
To run examples with cpu/heap profilers, install `libgoogle-perftools-dev` and re-run `config_brpc.sh` before compiling
### compile tests
Install gmock and gtest, use the gtest embedded in gmock and don't install libgtest-dev
```
$ sudo apt-get install google-mock
$ cd /usr/src
$ sudo cmake .
$ sudo make
$ sudo mv lib*.a gtest/lib*.a /usr/lib
$ sudo mv gtest/include/gtest /usr/include/
```
Install libgtest-dev
Rerun config_brpc.sh and run make in test/
## Fedora/CentOS
......
......@@ -25,8 +25,6 @@
namespace bvar {
namespace detail {
// set to true in UT. Not using gflags since users hardly need to change it.
bool FLAGS_show_sampler_usage = true;
const int WARN_NOSLEEP_THRESHOLD = 2;
// Combine two circular linked list into one.
......@@ -94,11 +92,11 @@ private:
void SamplerCollector::run() {
butil::LinkNode<Sampler> root;
int consecutive_nosleep = 0;
#ifndef UNIT_TEST
PassiveStatus<double> cumulated_time(get_cumulated_time, this);
bvar::PerSecond<bvar::PassiveStatus<double> > usage(&cumulated_time, 10);
if (FLAGS_show_sampler_usage) {
usage.expose("bvar_sampler_collector_usage");
}
bvar::PerSecond<bvar::PassiveStatus<double> > usage(
"bvar_sampler_collector_usage", &cumulated_time, 10);
#endif
while (!_stop) {
int64_t abstime = butil::gettimeofday_us();
Sampler* s = this->reset();
......
NEED_GPERFTOOLS=1
NEED_GTEST=1
NEED_GMOCK=1
include ../config.mk
CPPFLAGS+=-DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS
CPPFLAGS+=-DUNIT_TEST -Dprivate=public -Dprotected=public -DBVAR_NOT_LINK_DEFAULT_VARIABLES --include sstream_workaround.h
......@@ -15,8 +14,8 @@ ifeq ($(shell test $(GCC_VERSION) -ge 70000; echo $$?),0)
CXXFLAGS+=-Wno-aligned-new
endif
HDRPATHS=-I. -I.. -I../src $(addprefix -I, $(HDRS))
LIBPATHS=$(addprefix -L, $(LIBS))
HDRPATHS=-I. -I../src $(addprefix -I, $(HDRS))
LIBPATHS=-L.. $(addprefix -L, $(LIBS))
TEST_BUTIL_SOURCES = \
at_exit_unittest.cc \
......@@ -137,7 +136,7 @@ TEST_BINS = test_butil test_bvar $(TEST_BTHREAD_SOURCES:.cpp=) $(TEST_BRPC_SOURC
all: $(TEST_BINS)
.PHONY:clean
clean:clean_bins clean_dbg
clean:clean_bins clean_debug
@echo "Cleaning"
@rm -rf $(TEST_BUTIL_OBJS) $(TEST_BVAR_OBJS) $(TEST_BTHREAD_OBJS) \
$(TEST_BRPC_OBJS) $(TEST_PROTO_OBJS)
......@@ -146,12 +145,12 @@ clean:clean_bins clean_dbg
clean_bins:
@rm -rf $(TEST_BINS)
.PHONY:clean_dbg
clean_dbg:
.PHONY:clean_debug
clean_debug:
@$(MAKE) -C.. clean_debug
../libbrpc.dbg.a: FORCE
@$(MAKE) -C.. libbrpc.dbg.a
../libbrpc.dbg.a ../libbvar.dbg.a: FORCE
@$(MAKE) -C.. debug
FORCE:
......@@ -161,7 +160,7 @@ test_butil:$(TEST_BUTIL_OBJS) ../libbrpc.dbg.a
@echo "Linking $@"
@$(CXX) -o $@ $(LIBPATHS) -Xlinker "-(" $^ -Wl,-Bstatic $(STATIC_LINKINGS) -Wl,-Bdynamic -Xlinker "-)" $(DYNAMIC_LINKINGS)
test_bvar:$(TEST_BVAR_OBJS) ../libbrpc.dbg.a
test_bvar:$(TEST_BVAR_OBJS) ../libbvar.dbg.a
@echo "Linking $@"
@$(CXX) -o $@ $(LIBPATHS) -Xlinker "-(" $^ -Wl,-Bstatic $(STATIC_LINKINGS) -Wl,-Bdynamic -Xlinker "-)" $(DYNAMIC_LINKINGS)
......
This diff is collapsed.
......@@ -37,7 +37,7 @@
#include "brpc/builtin/sockets_service.h" // SocketsService
#include "brpc/builtin/common.h"
#include "brpc/builtin/bad_method_service.h"
#include "test/echo.pb.h"
#include "echo.pb.h"
DEFINE_bool(foo, false, "Flags for UT");
BRPC_VALIDATE_GFLAG(foo, brpc::PassValidate);
......@@ -194,7 +194,7 @@ protected:
service.default_method(&cntl, &req, &res, &done);
EXPECT_FALSE(cntl.Failed());
EXPECT_EQ(expect_type, cntl.http_response().content_type());
CheckContent(cntl, "test_builtin_service");
CheckContent(cntl, "brpc_builtin_service_unittest");
#endif
}
......@@ -655,8 +655,8 @@ TEST_F(BuiltinServiceTest, pprof) {
ClosureChecker done;
brpc::Controller cntl;
service.growth(&cntl, NULL, NULL, &done);
// MUST fail since tcmalloc hasn't been linked in
EXPECT_EQ(brpc::ENOMETHOD, cntl.ErrorCode());
// linked tcmalloc in UT
EXPECT_EQ(0, cntl.ErrorCode());
}
{
ClosureChecker done;
......@@ -670,7 +670,7 @@ TEST_F(BuiltinServiceTest, pprof) {
brpc::Controller cntl;
service.cmdline(&cntl, NULL, NULL, &done);
EXPECT_FALSE(cntl.Failed());
CheckContent(cntl, "test_builtin_service");
CheckContent(cntl, "brpc_builtin_service_unittest");
}
}
......
......@@ -26,7 +26,7 @@
#include "brpc/selective_channel.h"
#include "brpc/socket_map.h"
#include "brpc/controller.h"
#include "test/echo.pb.h"
#include "echo.pb.h"
#include "brpc/options.pb.h"
namespace brpc {
......
......@@ -9,7 +9,7 @@
#include "brpc/server.h"
#include "brpc/details/http_message.h"
#include "brpc/policy/http_rpc_protocol.h"
#include "test/echo.pb.h"
#include "echo.pb.h"
namespace brpc {
namespace policy {
......
......@@ -19,7 +19,7 @@
#include "brpc/channel.h"
#include "brpc/policy/most_common_message.h"
#include "brpc/controller.h"
#include "test/echo.pb.h"
#include "echo.pb.h"
#include "brpc/policy/http_rpc_protocol.h"
#include "json2pb/pb_to_json.h"
#include "json2pb/json_to_pb.h"
......
......@@ -19,7 +19,7 @@
#include "brpc/policy/hulu_pbrpc_protocol.h"
#include "brpc/policy/most_common_message.h"
#include "brpc/controller.h"
#include "test/echo.pb.h"
#include "echo.pb.h"
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
......
// Copyright (c) 2014 Baidu, Inc.
// Date: Thu Jun 11 14:30:07 CST 2015
#include <iostream>
#ifdef BAIDU_INTERNAL
#include <iostream>
#include "butil/time.h"
#include "butil/logging.h"
#include <brpc/memcache.h>
......@@ -164,3 +165,5 @@ TEST_F(MemcacheTest, version) {
std::cout << "version=" << version << std::endl;
}
} //namespace
#endif // BAIDU_INTERNAL
......@@ -13,7 +13,7 @@
#include "brpc/policy/file_naming_service.h"
#include "brpc/policy/list_naming_service.h"
#include "brpc/policy/remote_file_naming_service.h"
#include "test/echo.pb.h"
#include "echo.pb.h"
#include "brpc/server.h"
namespace {
......
......@@ -18,7 +18,7 @@
#include "brpc/policy/nova_pbrpc_protocol.h"
#include "brpc/policy/most_common_message.h"
#include "brpc/controller.h"
#include "test/echo.pb.h"
#include "echo.pb.h"
namespace {
void* RunClosure(void* arg) {
......
......@@ -8,7 +8,7 @@
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/dynamic_message.h>
#include "brpc/policy/baidu_rpc_meta.pb.h"
#include "test/echo.pb.h"
#include "echo.pb.h"
namespace {
using namespace google::protobuf;
......
......@@ -19,7 +19,7 @@
#include "brpc/policy/public_pbrpc_protocol.h"
#include "brpc/policy/most_common_message.h"
#include "brpc/controller.h"
#include "test/echo.pb.h"
#include "echo.pb.h"
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
......
// Copyright (c) 2014 Baidu, Inc.
// Date: Thu Jun 11 14:30:07 CST 2015
#include <iostream>
#ifdef BAIDU_INTERNAL
#include <iostream>
#include "butil/time.h"
#include "butil/logging.h"
#include <brpc/redis.h>
......@@ -234,3 +235,4 @@ TEST_F(RedisTest, by_components) {
ASSERT_EQ(-10, response.reply(3).integer());
}
} //namespace
#endif // BAIDU_INTERNAL
......@@ -37,9 +37,9 @@
#include "brpc/channel.h"
#include "brpc/socket_map.h"
#include "brpc/controller.h"
#include "test/echo.pb.h"
#include "test/v1.pb.h"
#include "test/v2.pb.h"
#include "echo.pb.h"
#include "v1.pb.h"
#include "v2.pb.h"
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
......
......@@ -9,7 +9,7 @@
#include "butil/macros.h"
#include "butil/iobuf.h"
#include "butil/time.h"
#include "test/snappy_message.pb.h"
#include "snappy_message.pb.h"
#include "brpc/policy/snappy_compress.h"
#include "brpc/policy/gzip_compress.h"
......
......@@ -19,7 +19,7 @@
#include "brpc/policy/sofa_pbrpc_protocol.h"
#include "brpc/policy/most_common_message.h"
#include "brpc/controller.h"
#include "test/echo.pb.h"
#include "echo.pb.h"
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
......
......@@ -9,7 +9,7 @@
#include "brpc/controller.h"
#include "brpc/channel.h"
#include "brpc/stream_impl.h"
#include "test/echo.pb.h"
#include "echo.pb.h"
class AfterAcceptStream {
public:
......
......@@ -6,7 +6,7 @@
#include "butil/base_switches.h"
#include "butil/at_exit.h"
#include "butil/logging.h"
#include "test/multiprocess_func_list.h"
#include "multiprocess_func_list.h"
// Disable coredumps by default to avoid generating a lot of coredumps
// after running death tests.
......
......@@ -14,17 +14,10 @@
#include "bvar/bvar.h"
#include <gtest/gtest.h>
namespace bvar {
namespace detail {
extern bool FLAGS_show_sampler_usage;
}
}
namespace {
class StatusTest : public testing::Test {
protected:
void SetUp() {
bvar::detail::FLAGS_show_sampler_usage = false;
}
void TearDown() {
ASSERT_EQ(0UL, bvar::Variable::count_exposed());
......
......@@ -19,9 +19,6 @@
namespace bvar {
DECLARE_bool(bvar_log_dumpped);
namespace detail {
extern bool FLAGS_show_sampler_usage;
}
}
template <typename T>
......@@ -41,7 +38,6 @@ namespace {
class VariableTest : public testing::Test {
protected:
void SetUp() {
bvar::detail::FLAGS_show_sampler_usage = false;
}
void TearDown() {
ASSERT_EQ(0UL, bvar::Variable::count_exposed());
......
......@@ -5,7 +5,6 @@
#include "butil/basictypes.h"
#include "butil/logging.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <gflags/gflags.h>
......@@ -17,8 +16,6 @@ DECLARE_int32(v);
namespace {
using ::testing::Return;
// Needs to be global since log assert handlers can't maintain state.
int log_sink_call_count = 0;
......@@ -65,44 +62,6 @@ private:
LogStateSaver log_state_saver_;
};
class MockLogSource {
public:
MOCK_METHOD0(Log, const char*());
};
TEST_F(LoggingTest, BasicLogging) {
MockLogSource mock_log_source;
EXPECT_CALL(mock_log_source, Log()).Times(DEBUG_MODE ? 16 : 8).
WillRepeatedly(Return("log message"));
SetMinLogLevel(BLOG_INFO);
EXPECT_TRUE(LOG_IS_ON(INFO));
// As of g++-4.5, the first argument to EXPECT_EQ cannot be a
// constant expression.
const bool kIsDebugMode = (DEBUG_MODE != 0);
EXPECT_TRUE(kIsDebugMode == DLOG_IS_ON(INFO));
EXPECT_TRUE(VLOG_IS_ON(0));
LOG(INFO) << mock_log_source.Log();
LOG_IF(INFO, true) << mock_log_source.Log();
PLOG(INFO) << mock_log_source.Log();
PLOG_IF(INFO, true) << mock_log_source.Log();
VLOG(0) << mock_log_source.Log();
VLOG_IF(0, true) << mock_log_source.Log();
VPLOG(0) << mock_log_source.Log();
VPLOG_IF(0, true) << mock_log_source.Log();
DLOG(INFO) << mock_log_source.Log();
DLOG_IF(INFO, true) << mock_log_source.Log();
DPLOG(INFO) << mock_log_source.Log();
DPLOG_IF(INFO, true) << mock_log_source.Log();
DVLOG(0) << mock_log_source.Log();
DVLOG_IF(0, true) << mock_log_source.Log();
DVPLOG(0) << mock_log_source.Log();
DVPLOG_IF(0, true) << mock_log_source.Log();
}
TEST_F(LoggingTest, LogIsOn) {
#if defined(NDEBUG)
const bool kDfatalIsFatal = false;
......@@ -140,56 +99,6 @@ TEST_F(LoggingTest, LogIsOn) {
EXPECT_TRUE(kDfatalIsFatal == LOG_IS_ON(DFATAL));
}
TEST_F(LoggingTest, LoggingIsLazy) {
MockLogSource mock_log_source;
EXPECT_CALL(mock_log_source, Log()).Times(0);
SetMinLogLevel(BLOG_WARNING);
EXPECT_FALSE(LOG_IS_ON(INFO));
EXPECT_FALSE(DLOG_IS_ON(INFO));
EXPECT_FALSE(VLOG_IS_ON(1));
LOG(INFO) << mock_log_source.Log();
LOG_IF(INFO, false) << mock_log_source.Log();
PLOG(INFO) << mock_log_source.Log();
PLOG_IF(INFO, false) << mock_log_source.Log();
VLOG(1) << mock_log_source.Log();
VLOG_IF(1, true) << mock_log_source.Log();
VPLOG(1) << mock_log_source.Log();
VPLOG_IF(1, true) << mock_log_source.Log();
DLOG(INFO) << mock_log_source.Log();
DLOG_IF(INFO, true) << mock_log_source.Log();
DPLOG(INFO) << mock_log_source.Log();
DPLOG_IF(INFO, true) << mock_log_source.Log();
DVLOG(1) << mock_log_source.Log();
DVLOG_IF(1, true) << mock_log_source.Log();
DVPLOG(1) << mock_log_source.Log();
DVPLOG_IF(1, true) << mock_log_source.Log();
}
// Official builds have CHECKs directly call BreakDebugger.
#if !defined(OFFICIAL_BUILD)
TEST_F(LoggingTest, CheckStreamsAreLazy) {
MockLogSource mock_log_source, uncalled_mock_log_source;
EXPECT_CALL(mock_log_source, Log()).Times(8).
WillRepeatedly(Return("check message"));
EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0);
SetLogAssertHandler(&LogSink);
CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log();
PCHECK(!mock_log_source.Log()) << mock_log_source.Log();
CHECK_EQ(mock_log_source.Log(), mock_log_source.Log())
<< uncalled_mock_log_source.Log();
CHECK_NE(mock_log_source.Log(), mock_log_source.Log())
<< mock_log_source.Log();
}
#endif
TEST_F(LoggingTest, DebugLoggingReleaseBehavior) {
#if !defined(NDEBUG)
int debug_only_variable = 1;
......@@ -202,21 +111,6 @@ TEST_F(LoggingTest, DebugLoggingReleaseBehavior) {
DVLOG_IF(1, debug_only_variable) << "test";
}
TEST_F(LoggingTest, DcheckStreamsAreLazy) {
MockLogSource mock_log_source;
EXPECT_CALL(mock_log_source, Log()).Times(0);
#if DCHECK_IS_ON()
DCHECK(true) << mock_log_source.Log();
DCHECK_EQ(0, 0) << mock_log_source.Log();
#else
DCHECK(mock_log_source.Log()) << mock_log_source.Log();
DPCHECK(mock_log_source.Log()) << mock_log_source.Log();
DCHECK_EQ(0, 0) << mock_log_source.Log();
DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL))
<< mock_log_source.Log();
#endif
}
TEST_F(LoggingTest, Dcheck) {
#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
// Release build.
......
#/bin/bash -f
test_num=0
failed_test=""
rc=0
make -j8
test_bins="test_butil test_bvar bthread*unittest brpc*unittest"
for test_bin in $test_bins; do
test_num=$((test_num + 1))
>&2 echo "[runtest] $test_bin"
./$test_bin
rc=$?
if [[ $rc -ne 0 ]]; then
failed_test="$test_bin"
break;
fi
done
if [[ $test_num -eq 0 ]]; then
>&2 echo "[runtest] Cannot find any tests"
exit 1
fi
if [[ -z "$failed_test" ]]; then
>&2 echo "[runtest] $test_num succeeded"
elif [[ $test_num -gt 1 ]]; then
>&2 echo "[runtest] '$failed_test' failed, $((test_num-1)) succeeded"
else
>&2 echo "[runtest] '$failed_test' failed"
fi
exit $rc
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "test/scoped_locale.h"
#include "scoped_locale.h"
#include <locale.h>
......
......@@ -9,11 +9,11 @@
#include "butil/rand_util.h"
#include "butil/strings/string_number_conversions.h"
#include "butil/sys_info.h"
#include "test/multiprocess_test.h"
#include "multiprocess_test.h"
#include "butil/threading/platform_thread.h"
#include "butil/time/time.h"
#include <gtest/gtest.h>
#include "test/multiprocess_func_list.h"
#include "multiprocess_func_list.h"
#if defined(OS_MACOSX)
#include "butil/mac/scoped_nsautorelease_pool.h"
......
......@@ -5,15 +5,21 @@
#include "butil/strings/string_split.h"
#include "butil/strings/utf_string_conversions.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::ElementsAre;
namespace butil {
namespace {
void AssertElements(std::vector<std::string>& result,
const char* const expected_data[],
size_t data_size) {
ASSERT_EQ(data_size, result.size());
for (size_t i = 0; i < data_size; ++i) {
ASSERT_STREQ(expected_data[i], result[i].c_str());
}
}
#if !defined(WCHAR_T_IS_UTF16)
// Overload SplitString with a wide-char version to make it easier to
// test the string16 version with wide character literals.
......@@ -165,8 +171,8 @@ TEST_F(SplitStringIntoKeyValuePairsTest, DelimiterInValue) {
TEST(SplitStringUsingSubstrTest, EmptyString) {
std::vector<std::string> results;
SplitStringUsingSubstr(std::string(), "DELIMITER", &results);
ASSERT_EQ(1u, results.size());
EXPECT_THAT(results, ElementsAre(""));
const char* const expected[] = { "" };
AssertElements(results, expected, arraysize(expected));
}
TEST(StringUtilTest, SplitString) {
......@@ -236,8 +242,8 @@ TEST(StringUtilTest, SplitString) {
TEST(SplitStringUsingSubstrTest, StringWithNoDelimiter) {
std::vector<std::string> results;
SplitStringUsingSubstr("alongwordwithnodelimiter", "DELIMITER", &results);
ASSERT_EQ(1u, results.size());
EXPECT_THAT(results, ElementsAre("alongwordwithnodelimiter"));
const char* const expected[] = { "alongwordwithnodelimiter" };
AssertElements(results, expected, arraysize(expected));
}
TEST(SplitStringUsingSubstrTest, LeadingDelimitersSkipped) {
......@@ -246,8 +252,8 @@ TEST(SplitStringUsingSubstrTest, LeadingDelimitersSkipped) {
"DELIMITERDELIMITERDELIMITERoneDELIMITERtwoDELIMITERthree",
"DELIMITER",
&results);
ASSERT_EQ(6u, results.size());
EXPECT_THAT(results, ElementsAre("", "", "", "one", "two", "three"));
const char* const expected[] = { "", "", "", "one", "two", "three" };
AssertElements(results, expected, arraysize(expected));
}
TEST(SplitStringUsingSubstrTest, ConsecutiveDelimitersSkipped) {
......@@ -256,8 +262,9 @@ TEST(SplitStringUsingSubstrTest, ConsecutiveDelimitersSkipped) {
"unoDELIMITERDELIMITERDELIMITERdosDELIMITERtresDELIMITERDELIMITERcuatro",
"DELIMITER",
&results);
ASSERT_EQ(7u, results.size());
EXPECT_THAT(results, ElementsAre("uno", "", "", "dos", "tres", "", "cuatro"));
const char* const expected[] = { "uno", "", "", "dos", "tres", "", "cuatro" };
AssertElements(results, expected, arraysize(expected));
}
TEST(SplitStringUsingSubstrTest, TrailingDelimitersSkipped) {
......@@ -266,9 +273,8 @@ TEST(SplitStringUsingSubstrTest, TrailingDelimitersSkipped) {
"unDELIMITERdeuxDELIMITERtroisDELIMITERquatreDELIMITERDELIMITERDELIMITER",
"DELIMITER",
&results);
ASSERT_EQ(7u, results.size());
EXPECT_THAT(
results, ElementsAre("un", "deux", "trois", "quatre", "", "", ""));
const char* const expected[] = { "un", "deux", "trois", "quatre", "", "", "" };
AssertElements(results, expected, arraysize(expected));
}
TEST(StringSplitTest, StringSplitDontTrim) {
......
......@@ -12,11 +12,8 @@
#include "butil/basictypes.h"
#include "butil/strings/string16.h"
#include "butil/strings/utf_string_conversions.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::ElementsAre;
namespace butil {
static const struct trim_case {
......
......@@ -8,7 +8,7 @@
#include "butil/strings/string_piece.h"
#include "butil/strings/sys_string_conversions.h"
#include "butil/strings/utf_string_conversions.h"
#include "test/scoped_locale.h"
#include "scoped_locale.h"
#include <gtest/gtest.h>
#ifdef WCHAR_T_IS_UTF32
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "test/test_switches.h"
#include "test_switches.h"
// Time (in milliseconds) that the tests should wait before timing out.
// TODO(phajdan.jr): Clean up the switch names.
......
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "butil/nix/xdg_util.h"
#include "butil/environment.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::_;
using ::testing::Return;
using ::testing::SetArgumentPointee;
using ::testing::StrEq;
namespace butil {
namespace nix {
namespace {
class MockEnvironment : public Environment {
public:
MOCK_METHOD2(GetVar, bool(const char*, std::string* result));
MOCK_METHOD2(SetVar, bool(const char*, const std::string& new_value));
MOCK_METHOD1(UnSetVar, bool(const char*));
};
const char* kGnome = "gnome";
const char* kKDE4 = "kde4";
const char* kKDE = "kde";
const char* kXFCE = "xfce";
} // namespace
TEST(XDGUtilTest, GetDesktopEnvironmentGnome) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_, _)).WillRepeatedly(Return(false));
EXPECT_CALL(getter, GetVar(StrEq("DESKTOP_SESSION"), _))
.WillOnce(DoAll(SetArgumentPointee<1>(kGnome), Return(true)));
EXPECT_EQ(DESKTOP_ENVIRONMENT_GNOME,
GetDesktopEnvironment(&getter));
}
TEST(XDGUtilTest, GetDesktopEnvironmentKDE4) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_, _)).WillRepeatedly(Return(false));
EXPECT_CALL(getter, GetVar(StrEq("DESKTOP_SESSION"), _))
.WillOnce(DoAll(SetArgumentPointee<1>(kKDE4), Return(true)));
EXPECT_EQ(DESKTOP_ENVIRONMENT_KDE4,
GetDesktopEnvironment(&getter));
}
TEST(XDGUtilTest, GetDesktopEnvironmentKDE3) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_, _)).WillRepeatedly(Return(false));
EXPECT_CALL(getter, GetVar(StrEq("DESKTOP_SESSION"), _))
.WillOnce(DoAll(SetArgumentPointee<1>(kKDE), Return(true)));
EXPECT_EQ(DESKTOP_ENVIRONMENT_KDE3,
GetDesktopEnvironment(&getter));
}
TEST(XDGUtilTest, GetDesktopEnvironmentXFCE) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_, _)).WillRepeatedly(Return(false));
EXPECT_CALL(getter, GetVar(StrEq("DESKTOP_SESSION"), _))
.WillOnce(DoAll(SetArgumentPointee<1>(kXFCE), Return(true)));
EXPECT_EQ(DESKTOP_ENVIRONMENT_XFCE,
GetDesktopEnvironment(&getter));
}
} // namespace nix
} // namespace butil
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment