├── src ├── WORKSPACE ├── util │ ├── gtl │ │ ├── dummy.cc │ │ └── BUILD │ └── utf │ │ └── BUILD ├── base │ ├── logging.h │ ├── testing.cc │ ├── testing.h │ ├── testing_main.cc │ ├── base.cc │ ├── base.h │ └── BUILD ├── test │ ├── gtest_test.cc │ ├── glog_main.cc │ ├── BUILD │ ├── strings_test.cc │ └── gmock_test.cc ├── third_party │ ├── glog │ │ ├── BUILD │ │ └── config.h │ ├── gflags │ │ └── BUILD │ ├── gtest │ │ └── BUILD │ └── gmock │ │ └── BUILD └── strings │ └── BUILD ├── README.md ├── example ├── example.proto ├── gtest_test.cc ├── glog_main.cc ├── proto_test.cc ├── WORKSPACE ├── BUILD └── strings_test.cc ├── .gitignore ├── archive.sh ├── fake_compiler.sh ├── Makefile ├── repository.sh ├── LICENSE └── generate.sh /src/WORKSPACE: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/util/gtl/dummy.cc: -------------------------------------------------------------------------------- 1 | void __util_gtl_dummy() {} 2 | -------------------------------------------------------------------------------- /src/base/logging.h: -------------------------------------------------------------------------------- 1 | #include "third_party/glog/logging.h" 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gbase 2 | Package of gflags, glog, gmock and gtest for Bazel. 3 | -------------------------------------------------------------------------------- /example/example.proto: -------------------------------------------------------------------------------- 1 | message Example { 2 | optional int32 id = 1; 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | example/bazel-* 3 | gbase.zip 4 | generated/* 5 | output/* 6 | repository/* 7 | -------------------------------------------------------------------------------- /example/gtest_test.cc: -------------------------------------------------------------------------------- 1 | #include "base/testing.h" 2 | 3 | TEST(GTest, Test) { 4 | ASSERT_TRUE(true); 5 | } 6 | -------------------------------------------------------------------------------- /src/test/gtest_test.cc: -------------------------------------------------------------------------------- 1 | #include "third_party/gtest/gtest.h" 2 | 3 | TEST(GTest, Test) { 4 | ASSERT_TRUE(true); 5 | } 6 | -------------------------------------------------------------------------------- /src/base/testing.cc: -------------------------------------------------------------------------------- 1 | #include "base/base.h" 2 | #include "base/testing.h" 3 | 4 | void InitTest(int* argc, char*** argv) { 5 | testing::InitGoogleMock(argc, *argv); 6 | ParseCommandLineFlags(argc, argv); 7 | } 8 | -------------------------------------------------------------------------------- /archive.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eu 4 | 5 | cd output 6 | 7 | if [ -f ../gbase.zip ]; then 8 | rm ../gbase.zip 9 | fi 10 | 11 | zip -r ../gbase.zip * --exclude \*\*/.DS_Store --exclude test --exclude bazel-\* 12 | -------------------------------------------------------------------------------- /fake_compiler.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e -u 4 | 5 | args=() 6 | for arg in "$@"; do 7 | case "${arg}" in 8 | '-lgflags'|'-lgtest') :;; 9 | *) args+=("${arg}");; 10 | esac 11 | done 12 | 13 | "${args[@]}" 14 | -------------------------------------------------------------------------------- /src/base/testing.h: -------------------------------------------------------------------------------- 1 | #ifndef BASE_TESTING_H_ 2 | #define BASE_TESTING_H_ 3 | 4 | #include "third_party/gmock/gmock.h" 5 | #include "third_party/gtest/gtest.h" 6 | 7 | void InitTest(int* argc, char*** argv); 8 | 9 | #endif // BASE_TESTING_H_ 10 | -------------------------------------------------------------------------------- /src/base/testing_main.cc: -------------------------------------------------------------------------------- 1 | #include "base/testing.h" 2 | 3 | #include 4 | 5 | GTEST_API_ int main(int argc, char** argv) { 6 | std::cout << "Running main() from testing_main.cc\n"; 7 | InitTest(&argc, &argv); 8 | return RUN_ALL_TESTS(); 9 | } 10 | -------------------------------------------------------------------------------- /src/util/utf/BUILD: -------------------------------------------------------------------------------- 1 | licenses(["notice"]) 2 | 3 | package(default_visibility = ["//visibility:private"]) 4 | 5 | cc_library( 6 | name = "utf", 7 | srcs = glob(["*.cc"]), 8 | hdrs = glob(["*.h"]), 9 | visibility = ["//visibility:public"], 10 | ) 11 | -------------------------------------------------------------------------------- /example/glog_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "base/base.h" 4 | 5 | DEFINE_string(message, "", "Message."); 6 | 7 | int main(int argc, char **argv) { 8 | ParseCommandLineFlags(&argc, &argv); 9 | LOG(INFO) << FLAGS_message; 10 | puts(FLAGS_message.c_str()); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /src/test/glog_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "base/base.h" 4 | 5 | DEFINE_string(message, "", "Message."); 6 | 7 | int main(int argc, char **argv) { 8 | ParseCommandLineFlags(&argc, &argv); 9 | LOG(INFO) << FLAGS_message; 10 | puts(FLAGS_message.c_str()); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /src/util/gtl/BUILD: -------------------------------------------------------------------------------- 1 | licenses(["notice"]) 2 | 3 | package(default_visibility = ["//visibility:private"]) 4 | 5 | cc_library( 6 | name = "gtl", 7 | srcs = glob(["*.cc"]), 8 | hdrs = glob(["*.h"]), 9 | visibility = ["//visibility:public"], 10 | deps = ["//base:base_internal"], 11 | ) 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | gbase.zip: output 2 | bash archive.sh 3 | 4 | test: output 5 | cd output && bazel test -c dbg //... 6 | 7 | output: repository 8 | bash generate.sh 9 | 10 | repository: 11 | bash repository.sh 12 | 13 | clean: 14 | -rm gbase.zip 15 | -rm -rf output 16 | -rm -rf generated 17 | .PHONY: clean 18 | -------------------------------------------------------------------------------- /src/base/base.cc: -------------------------------------------------------------------------------- 1 | #include "base/macros.h" 2 | #include "third_party/gflags/gflags.h" 3 | #include "third_party/glog/logging.h" 4 | 5 | void ParseCommandLineFlags(int* argc, char*** argv) { 6 | google::ParseCommandLineFlags(argc, argv, true); 7 | google::InitGoogleLogging((*argv)[0]); 8 | google::InstallFailureSignalHandler(); 9 | } 10 | -------------------------------------------------------------------------------- /example/proto_test.cc: -------------------------------------------------------------------------------- 1 | #include "base/base.h" 2 | #include "base/testing.h" 3 | #include "example.pb.h" 4 | #include "src/google/protobuf/text_format.h" 5 | 6 | using google::protobuf::TextFormat; 7 | 8 | TEST(GTest, Test) { 9 | Example example; 10 | TextFormat::ParseFromString("id: 12345", &example); 11 | EXPECT_EQ(12345, example.id()); 12 | } 13 | -------------------------------------------------------------------------------- /src/third_party/glog/BUILD: -------------------------------------------------------------------------------- 1 | licenses(["notice"]) 2 | 3 | package(default_visibility = ["//visibility:private"]) 4 | 5 | cc_library( 6 | name = "glog", 7 | srcs = glob(["*.cc"]), 8 | hdrs = glob(["*.h", "base/*.h"]), 9 | visibility = ["//visibility:public"], 10 | deps = ["//third_party/gflags"], 11 | linkopts = ["-pthread"], 12 | alwayslink = 1, 13 | ) 14 | -------------------------------------------------------------------------------- /src/third_party/gflags/BUILD: -------------------------------------------------------------------------------- 1 | licenses(["notice"]) 2 | 3 | package(default_visibility = ["//visibility:private"]) 4 | 5 | cc_library( 6 | name = "gflags", 7 | srcs = glob(["*.cc"], exclude = ["windows_*.cc"]), 8 | hdrs = glob(["*.h"], exclude = ["windows_*.h"]), 9 | visibility = ["//visibility:public"], 10 | linkopts = ["-pthread"], 11 | alwayslink = 1, 12 | ) 13 | -------------------------------------------------------------------------------- /repository.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ -d repository ]; then 4 | rm -rf repository 5 | fi 6 | 7 | mkdir -p repository 8 | pushd repository 9 | git clone -b v2.1.2 'https://github.com/gflags/gflags' gflags 10 | git clone -b v0.3.4 'https://github.com/google/glog' glog 11 | git clone -b release-1.7.0 'https://github.com/google/googletest' gtest 12 | git clone -b release-1.7.0 'https://github.com/google/googlemock' gmock 13 | git clone -b 0.5.0 'https://github.com/google/lmctfy' lmctfy 14 | git clone -b v3.0.0 'https://github.com/google/protobuf' protobuf 15 | -------------------------------------------------------------------------------- /src/strings/BUILD: -------------------------------------------------------------------------------- 1 | licenses(["notice"]) 2 | 3 | package(default_visibility = ["//visibility:private"]) 4 | 5 | cc_library( 6 | name = "strings", 7 | srcs = glob(["*.cc"]), 8 | hdrs = glob(["*.h"]), 9 | visibility = [ 10 | "//base:__subpackages__", 11 | ], 12 | deps = [ 13 | "//base:base_internal", 14 | "//util/gtl", 15 | "//util/utf", 16 | ], 17 | # Disable warnings because this is a common library. 18 | copts = [ 19 | "-Wno-c++11-narrowing", 20 | "-w", 21 | ], 22 | ) 23 | -------------------------------------------------------------------------------- /src/third_party/gtest/BUILD: -------------------------------------------------------------------------------- 1 | licenses(["notice"]) 2 | 3 | package(default_visibility = ["//visibility:private"]) 4 | 5 | cc_library( 6 | name = "gtest", 7 | srcs = glob(["*.cc"], exclude = ["gtest_main.cc", "gtest-all.cc"]), 8 | hdrs = glob(["*.h", "internal/*.h"]), 9 | visibility = ["//visibility:public"], 10 | ) 11 | 12 | cc_library( 13 | name = "gtest_main", 14 | srcs = ["gtest_main.cc"], 15 | hdrs = glob(["*.h", "internal/*.h"]), 16 | visibility = ["//visibility:public"], 17 | deps = [":gtest"], 18 | alwayslink = 1, 19 | ) 20 | -------------------------------------------------------------------------------- /src/third_party/gmock/BUILD: -------------------------------------------------------------------------------- 1 | licenses(["notice"]) 2 | 3 | package(default_visibility = ["//visibility:private"]) 4 | 5 | cc_library( 6 | name = "gmock", 7 | srcs = glob(["*.cc"], exclude = ["gmock_main.cc", "gmock-all.cc"]), 8 | hdrs = glob(["*.h", "internal/*.h"]), 9 | deps = ["//third_party/gtest"], 10 | visibility = ["//visibility:public"], 11 | ) 12 | 13 | cc_library( 14 | name = "gmock_main", 15 | srcs = ["gmock_main.cc"], 16 | hdrs = glob(["*.h", "internal/*.h"]), 17 | visibility = ["//visibility:public"], 18 | deps = [":gmock"], 19 | alwayslink = 1, 20 | ) 21 | -------------------------------------------------------------------------------- /example/WORKSPACE: -------------------------------------------------------------------------------- 1 | http_archive( 2 | name = "gbase", 3 | url = "https://github.com/imos/gbase/releases/download/v0.8.5/gbase.zip", 4 | sha256 = "0aee114ec1ef15555b2022b8594b70ff3cdf22e3fd42f8e101a98e4c461c9555", 5 | ) 6 | 7 | bind( 8 | name = "base", 9 | actual = "@gbase//base" 10 | ) 11 | 12 | bind( 13 | name = "testing", 14 | actual = "@gbase//base:testing" 15 | ) 16 | 17 | bind( 18 | name = "testing_main", 19 | actual = "@gbase//base:testing_main" 20 | ) 21 | 22 | git_repository( 23 | name = "protobuf", 24 | tag = "v3.0.0-beta-3.3", 25 | remote = "https://github.com/google/protobuf.git", 26 | ) 27 | -------------------------------------------------------------------------------- /src/base/base.h: -------------------------------------------------------------------------------- 1 | #ifndef BASE_BASE_H_ 2 | #define BASE_BASE_H_ 3 | 4 | #include "base/integral_types.h" 5 | #include "base/macros.h" 6 | #include "base/stringprintf.h" 7 | #include "base/strtoint.h" 8 | #include "base/thread_annotations.h" 9 | #include "strings/join.h" 10 | #include "strings/split.h" 11 | #include "strings/strcat.h" 12 | #include "strings/strip.h" 13 | #include "strings/substitute.h" 14 | #include "strings/util.h" 15 | #include "third_party/gflags/gflags.h" 16 | #include "third_party/glog/logging.h" 17 | #include "util/gtl/algorithm.h" 18 | #include "util/gtl/map_util.h" 19 | #include "util/gtl/stl_util.h" 20 | 21 | void ParseCommandLineFlags(int* argc, char*** argv); 22 | 23 | #endif // BASE_BASE_H_ 24 | -------------------------------------------------------------------------------- /src/test/BUILD: -------------------------------------------------------------------------------- 1 | licenses(["notice"]) 2 | 3 | package(default_visibility = ["//visibility:private"]) 4 | 5 | cc_binary( 6 | name = "glog_main", 7 | srcs = ["glog_main.cc"], 8 | deps = [ 9 | "//base", 10 | ], 11 | ) 12 | 13 | cc_test( 14 | name = "gmock_test", 15 | srcs = ["gmock_test.cc"], 16 | deps = [ 17 | "//base:testing_main", 18 | ], 19 | ) 20 | 21 | cc_test( 22 | name = "gtest_test", 23 | srcs = ["gtest_test.cc"], 24 | deps = [ 25 | "//base:testing_main", 26 | ], 27 | ) 28 | 29 | cc_test( 30 | name = "strings_test", 31 | srcs = ["strings_test.cc"], 32 | deps = [ 33 | "//base", 34 | "//base:testing_main", 35 | ], 36 | ) 37 | -------------------------------------------------------------------------------- /example/BUILD: -------------------------------------------------------------------------------- 1 | licenses(["notice"]) 2 | 3 | package(default_visibility = ["//visibility:private"]) 4 | 5 | load("@protobuf//:protobuf.bzl", "cc_proto_library") 6 | 7 | cc_proto_library( 8 | name = "example_proto", 9 | srcs = ["example.proto"], 10 | default_runtime = "@protobuf//:protobuf", 11 | protoc = "@protobuf//:protoc", 12 | # include = "external/protobuf/src", 13 | ) 14 | 15 | cc_test( 16 | name = "proto_test", 17 | srcs = ["proto_test.cc"], 18 | deps = [ 19 | ":example_proto", 20 | "@protobuf//:protobuf", 21 | "//external:base", 22 | "//external:testing_main", 23 | ], 24 | ) 25 | 26 | cc_binary( 27 | name = "glog_main", 28 | srcs = ["glog_main.cc"], 29 | deps = [ 30 | "//external:base", 31 | ], 32 | ) 33 | 34 | cc_test( 35 | name = "gtest_test", 36 | srcs = ["gtest_test.cc"], 37 | deps = [ 38 | "//external:testing_main", 39 | ], 40 | ) 41 | 42 | cc_test( 43 | name = "strings_test", 44 | srcs = ["strings_test.cc"], 45 | deps = [ 46 | "//external:testing_main", 47 | ], 48 | ) 49 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Kentaro IMAJO 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/base/BUILD: -------------------------------------------------------------------------------- 1 | licenses(["notice"]) 2 | 3 | package(default_visibility = ["//visibility:private"]) 4 | 5 | cc_library( 6 | name = "base", 7 | srcs = ["base.cc"], 8 | hdrs = ["base.h"], 9 | visibility = ["//visibility:public"], 10 | deps = [ 11 | ":base_internal", 12 | "//strings", 13 | "//util/gtl", 14 | ], 15 | ) 16 | 17 | cc_library( 18 | name = "testing", 19 | srcs = ["testing.cc"], 20 | hdrs = ["testing.h"], 21 | testonly = 1, 22 | visibility = ["//visibility:public"], 23 | deps = [ 24 | ":base", 25 | "//third_party/gmock", 26 | ], 27 | ) 28 | 29 | cc_library( 30 | name = "testing_main", 31 | srcs = ["testing_main.cc"], 32 | testonly = 1, 33 | visibility = ["//visibility:public"], 34 | deps = [":testing"], 35 | alwayslink = 1, 36 | ) 37 | 38 | cc_library( 39 | name = "base_internal", 40 | srcs = glob( 41 | ["*.cc"], 42 | exclude =["base.cc", "testing.cc", "testing_main.cc"]), 43 | hdrs = glob(["*.h"], exclude =["base.h", "testing.h"]), 44 | visibility = [ 45 | "//strings:__subpackages__", 46 | "//util:__subpackages__", 47 | ], 48 | deps = [ 49 | "//third_party/glog", 50 | "//third_party/gflags", 51 | ], 52 | ) 53 | -------------------------------------------------------------------------------- /example/strings_test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "base/base.h" 4 | #include "base/testing.h" 5 | 6 | // NOTE: StringPrintf is implemented under base/, but this is placed here 7 | // because this is often used with strings. 8 | TEST(BaseTest, StringPrintf) { 9 | EXPECT_EQ("01234", StringPrintf("%05d", 1234)); 10 | } 11 | 12 | TEST(StringsTest, StrCat) { 13 | EXPECT_EQ("FooBar", StrCat("Foo", "Bar")); 14 | EXPECT_EQ("Foo12345Bar", StrCat("Foo", 12345, "Bar")); 15 | } 16 | 17 | TEST(StringsTest, Split) { 18 | vector result; 19 | 20 | result = strings::Split("a,bb,ccc", ","); 21 | EXPECT_THAT(result, testing::ElementsAre("a", "bb", "ccc")); 22 | 23 | result = strings::Split("a,bb,ccc", strings::delimiter::Limit(",", 1)); 24 | EXPECT_THAT(result, testing::ElementsAre("a", "bb,ccc")); 25 | 26 | result = strings::Split("a,bb,,ccc", ","); 27 | EXPECT_THAT(result, testing::ElementsAre("a", "bb", "", "ccc")); 28 | 29 | result = strings::Split("a,bb,,ccc", ",", strings::SkipEmpty()); 30 | EXPECT_THAT(result, testing::ElementsAre("a", "bb", "ccc")); 31 | } 32 | 33 | TEST(StringsTest, Join) { 34 | EXPECT_EQ("a,bb,ccc", 35 | strings::Join(vector({"a", "bb", "ccc"}), ",")); 36 | } 37 | 38 | TEST(StringsTest, JoinElements) { 39 | EXPECT_EQ("1,10,100", 40 | strings::JoinElements(vector({1, 10, 100}), ",")); 41 | } 42 | 43 | TEST(StringsTest, StringReplace) { 44 | EXPECT_EQ("aaaxbbccc", StringReplace("aaabbbccc", "b", "x", false)); 45 | EXPECT_EQ("aaaxxxccc", StringReplace("aaabbbccc", "b", "x", true)); 46 | } 47 | 48 | TEST(StringsTest, HasPrefixString) { 49 | EXPECT_TRUE(HasPrefixString("foobar", "foo")); 50 | EXPECT_FALSE(HasPrefixString("foobar", "bar")); 51 | } 52 | 53 | TEST(StringsTest, StripPrefixString) { 54 | EXPECT_EQ("bar", StripPrefixString("foobar", "foo")); 55 | } 56 | 57 | TEST(StringsTest, Substitute) { 58 | EXPECT_EQ("this is an apple", 59 | strings::Substitute("$1 is $0", "an apple", "this")); 60 | } 61 | -------------------------------------------------------------------------------- /src/test/strings_test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "base/base.h" 4 | #include "base/testing.h" 5 | 6 | // NOTE: StringPrintf is implemented under base/, but this is placed here 7 | // because this is often used with strings. 8 | TEST(BaseTest, StringPrintf) { 9 | EXPECT_EQ("01234", StringPrintf("%05d", 1234)); 10 | } 11 | 12 | TEST(StringsTest, StrCat) { 13 | EXPECT_EQ("FooBar", StrCat("Foo", "Bar")); 14 | EXPECT_EQ("Foo12345Bar", StrCat("Foo", 12345, "Bar")); 15 | } 16 | 17 | TEST(StringsTest, Split) { 18 | vector result; 19 | 20 | result = strings::Split("a,bb,ccc", ","); 21 | EXPECT_THAT(result, testing::ElementsAre("a", "bb", "ccc")); 22 | 23 | result = strings::Split("a,bb,ccc", strings::delimiter::Limit(",", 1)); 24 | EXPECT_THAT(result, testing::ElementsAre("a", "bb,ccc")); 25 | 26 | result = strings::Split("a,bb,,ccc", ","); 27 | EXPECT_THAT(result, testing::ElementsAre("a", "bb", "", "ccc")); 28 | 29 | result = strings::Split("a,bb,,ccc", ",", strings::SkipEmpty()); 30 | EXPECT_THAT(result, testing::ElementsAre("a", "bb", "ccc")); 31 | } 32 | 33 | TEST(StringsTest, Join) { 34 | EXPECT_EQ("a,bb,ccc", 35 | strings::Join(vector({"a", "bb", "ccc"}), ",")); 36 | } 37 | 38 | TEST(StringsTest, JoinElements) { 39 | EXPECT_EQ("1,10,100", 40 | strings::JoinElements(vector({1, 10, 100}), ",")); 41 | } 42 | 43 | TEST(StringsTest, StringReplace) { 44 | EXPECT_EQ("aaaxbbccc", StringReplace("aaabbbccc", "b", "x", false)); 45 | EXPECT_EQ("aaaxxxccc", StringReplace("aaabbbccc", "b", "x", true)); 46 | } 47 | 48 | TEST(StringsTest, HasPrefixString) { 49 | EXPECT_TRUE(HasPrefixString("foobar", "foo")); 50 | EXPECT_FALSE(HasPrefixString("foobar", "bar")); 51 | } 52 | 53 | TEST(StringsTest, StripPrefixString) { 54 | EXPECT_EQ("bar", StripPrefixString("foobar", "foo")); 55 | } 56 | 57 | TEST(StringsTest, Substitute) { 58 | EXPECT_EQ("this is an apple", 59 | strings::Substitute("$1 is $0", "an apple", "this")); 60 | } 61 | -------------------------------------------------------------------------------- /generate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eu 4 | 5 | ROOT_DIRECTORY="$(pwd)" 6 | CXX="${CXX:=g++}" 7 | 8 | if [ -d generated ]; then 9 | rm -rf generated 10 | fi 11 | cp -a repository generated 12 | 13 | replace() { 14 | sed -i.bak -e "s/${1//\//\\/}/${2//\//\\/}/g" "$3" 15 | rm "$3.bak" 16 | } 17 | 18 | generate_gflags() { 19 | pushd generated/gflags 20 | cmake . 21 | popd 22 | mkdir -p output/third_party/gflags 23 | cp generated/gflags/include/gflags/* output/third_party/gflags/ 24 | cp generated/gflags/src/*.cc generated/gflags/src/*.h \ 25 | output/third_party/gflags/ 26 | for file in output/third_party/gflags/*.h \ 27 | output/third_party/gflags/*.cc; do 28 | replace '#[[:blank:]]*include[[:blank:]]*"\([^/]*\)"' \ 29 | '#include "third_party/gflags/\1"' \ 30 | "${file}" 31 | replace '#[[:blank:]]*include[[:blank:]]*' \ 32 | '#include "third_party/gflags/config.h"' \ 33 | "${file}" 34 | done 35 | } 36 | 37 | generate_glog() { 38 | pushd generated/glog 39 | ./configure CXX="${ROOT_DIRECTORY}/fake_compiler.sh ${CXX}" 40 | popd 41 | mkdir -p output/third_party/glog/ 42 | cp generated/glog/src/glog/*.h output/third_party/glog/ 43 | cp -f generated/glog/src/*.cc generated/glog/src/*.h \ 44 | output/third_party/glog/ 45 | cp -a generated/glog/src/base output/third_party/glog/base 46 | rm output/third_party/glog/*test*.cc 47 | for file in output/third_party/glog/*.h \ 48 | output/third_party/glog/*.cc \ 49 | output/third_party/glog/base/*.h; do 50 | replace '#[[:blank:]]*include[[:blank:]]*' \ 51 | '#include "third_party/gflags/\1"' \ 52 | "${file}" 53 | replace '#[[:blank:]]*include[[:blank:]]*"\([^/]*\)"' \ 54 | '#include "third_party/glog/\1"' \ 55 | "${file}" 56 | replace '#[[:blank:]]*include[[:blank:]]*"glog/\([^/]*\)"' \ 57 | '#include "third_party/glog/\1"' \ 58 | "${file}" 59 | replace '#[[:blank:]]*include[[:blank:]]*"base/\([^/]*\)"' \ 60 | '#include "third_party/glog/base/\1"' \ 61 | "${file}" 62 | done 63 | cp -f src/third_party/glog/config.h output/third_party/glog/config.h 64 | } 65 | 66 | generate_gtest() { 67 | pushd generated/gtest 68 | cmake . 69 | popd 70 | mkdir -p output/third_party/gtest/internal/ 71 | cp generated/gtest/src/*.{cc,h} output/third_party/gtest/ 72 | cp generated/gtest/include/gtest/*.h output/third_party/gtest/ 73 | cp generated/gtest/include/gtest/internal/*.h \ 74 | output/third_party/gtest/internal/ 75 | for file in output/third_party/gtest/*.h \ 76 | output/third_party/gtest/*.cc \ 77 | output/third_party/gtest/internal/*.h; do 78 | replace '#[[:blank:]]*include[[:blank:]]*"gtest/\([^/]*\)"' \ 79 | '#include "third_party/gtest/\1"' \ 80 | "${file}" 81 | replace '#[[:blank:]]*include[[:blank:]]*"gtest/internal/\([^/]*\)"' \ 82 | '#include "third_party/gtest/internal/\1"' \ 83 | "${file}" 84 | replace '#[[:blank:]]*include[[:blank:]]*"src/\([^/]*\)"' \ 85 | '#include "third_party/gtest/\1"' \ 86 | "${file}" 87 | done 88 | replace '.*kPathSeparatorString.*' '' \ 89 | 'output/third_party/gtest/gtest-filepath.cc' 90 | } 91 | 92 | generate_gmock() { 93 | pushd generated/gmock 94 | cmake . 95 | popd 96 | mkdir -p output/third_party/gmock/internal/ 97 | cp generated/gmock/src/*.cc output/third_party/gmock/ 98 | cp generated/gmock/include/gmock/*.h output/third_party/gmock/ 99 | cp generated/gmock/include/gmock/internal/*.h \ 100 | output/third_party/gmock/internal/ 101 | for file in output/third_party/gmock/*.h \ 102 | output/third_party/gmock/*.cc \ 103 | output/third_party/gmock/internal/*.h; do 104 | replace '#[[:blank:]]*include[[:blank:]]*"gmock/\([^/]*\)"' \ 105 | '#include "third_party/gmock/\1"' \ 106 | "${file}" 107 | replace '#[[:blank:]]*include[[:blank:]]*"gmock/internal/\([^/]*\)"' \ 108 | '#include "third_party/gmock/internal/\1"' \ 109 | "${file}" 110 | replace '#[[:blank:]]*include[[:blank:]]*"src/\([^/]*\)"' \ 111 | '#include "third_party/gmock/\1"' \ 112 | "${file}" 113 | replace '#[[:blank:]]*include[[:blank:]]*"gtest/\([^"]*\)"' \ 114 | '#include "third_party/gtest/\1"' \ 115 | "${file}" 116 | done 117 | } 118 | 119 | generate_base() { 120 | # Remove to override with glog. 121 | rm generated/lmctfy/base/logging.* 122 | # Remove files not supported by Mac OSX. 123 | rm generated/lmctfy/base/sysinfo.* 124 | rm generated/lmctfy/base/walltime.* 125 | # Duplicated functions exist. 126 | rm generated/lmctfy/strings/strutil.* 127 | 128 | cp generated/lmctfy/base/*.{cc,h} output/base/ 129 | cp generated/lmctfy/strings/*.{cc,h} output/strings/ 130 | cp generated/lmctfy/util/gtl/*.h output/util/gtl/ 131 | cp generated/lmctfy/util/utf/*.{cc,h} output/util/utf/ 132 | } 133 | 134 | generate_map_util() { 135 | cp generated/protobuf/src/google/protobuf/stubs/map_util.h \ 136 | output/util/gtl/ 137 | local file='output/util/gtl/map_util.h' 138 | replace 'namespace google {' '' "${file}" 139 | replace 'namespace protobuf {' '' "${file}" 140 | replace '} // namespace google' '' "${file}" 141 | replace '} // namespace protobuf' '' "${file}" 142 | replace '#include ' \ 143 | '#include "third_party/glog/logging.h"' "${file}" 144 | } 145 | 146 | if [ -d output ]; then 147 | rm -rf output 148 | fi 149 | cp -a src output 150 | generate_gflags 151 | generate_glog 152 | generate_gtest 153 | generate_gmock 154 | generate_base 155 | generate_map_util 156 | -------------------------------------------------------------------------------- /src/third_party/glog/config.h: -------------------------------------------------------------------------------- 1 | /* src/config.h. Generated from config.h.in by configure. */ 2 | /* src/config.h.in. Generated from configure.ac by autoheader. */ 3 | 4 | /* define if glog doesn't use RTTI */ 5 | /* #undef DISABLE_RTTI */ 6 | 7 | /* Namespace for Google classes */ 8 | #define GOOGLE_NAMESPACE google 9 | 10 | /* Define if you have the `dladdr' function */ 11 | #ifdef __APPLE__ 12 | #define HAVE_DLADDR 1 13 | #endif 14 | 15 | /* Define to 1 if you have the header file. */ 16 | #define HAVE_DLFCN_H 1 17 | 18 | /* Define to 1 if you have the header file. */ 19 | #define HAVE_EXECINFO_H 1 20 | 21 | /* Define if you have the `fcntl' function */ 22 | #define HAVE_FCNTL 1 23 | 24 | /* Define to 1 if you have the header file. */ 25 | #define HAVE_GLOB_H 1 26 | 27 | /* Define to 1 if you have the header file. */ 28 | #define HAVE_INTTYPES_H 1 29 | 30 | /* Define to 1 if you have the `pthread' library (-lpthread). */ 31 | #define HAVE_LIBPTHREAD 1 32 | 33 | /* Define to 1 if you have the header file. */ 34 | #ifdef __APPLE__ 35 | #define HAVE_LIBUNWIND_H 1 36 | #endif 37 | 38 | /* define if you have google gflags library */ 39 | #define HAVE_LIB_GFLAGS 1 40 | 41 | /* define if you have google gmock library */ 42 | /* #undef HAVE_LIB_GMOCK */ 43 | 44 | /* define if you have google gtest library */ 45 | /* #undef HAVE_LIB_GTEST */ 46 | 47 | /* define if you have libunwind */ 48 | /* #undef HAVE_LIB_UNWIND */ 49 | 50 | /* Define to 1 if you have the header file. */ 51 | #define HAVE_MEMORY_H 1 52 | 53 | /* define if the compiler implements namespaces */ 54 | #define HAVE_NAMESPACES 1 55 | 56 | /* Define if you have the 'pread' function */ 57 | #define HAVE_PREAD 1 58 | 59 | /* Define if you have POSIX threads libraries and header files. */ 60 | #define HAVE_PTHREAD 1 61 | 62 | /* Define to 1 if you have the header file. */ 63 | #define HAVE_PWD_H 1 64 | 65 | /* Define if you have the 'pwrite' function */ 66 | #define HAVE_PWRITE 1 67 | 68 | /* define if the compiler implements pthread_rwlock_* */ 69 | #define HAVE_RWLOCK 1 70 | 71 | /* Define if you have the 'sigaction' function */ 72 | #define HAVE_SIGACTION 1 73 | 74 | /* Define if you have the `sigaltstack' function */ 75 | #define HAVE_SIGALTSTACK 1 76 | 77 | /* Define to 1 if you have the header file. */ 78 | #define HAVE_STDINT_H 1 79 | 80 | /* Define to 1 if you have the header file. */ 81 | #define HAVE_STDLIB_H 1 82 | 83 | /* Define to 1 if you have the header file. */ 84 | #define HAVE_STRINGS_H 1 85 | 86 | /* Define to 1 if you have the header file. */ 87 | #define HAVE_STRING_H 1 88 | 89 | /* Define to 1 if you have the header file. */ 90 | #ifdef __linux__ 91 | #define HAVE_SYSCALL_H 92 | #endif 93 | 94 | /* Define to 1 if you have the header file. */ 95 | #define HAVE_SYSLOG_H 1 96 | 97 | /* Define to 1 if you have the header file. */ 98 | #define HAVE_SYS_STAT_H 1 99 | 100 | /* Define to 1 if you have the header file. */ 101 | #define HAVE_SYS_SYSCALL_H 1 102 | 103 | /* Define to 1 if you have the header file. */ 104 | #define HAVE_SYS_TIME_H 1 105 | 106 | /* Define to 1 if you have the header file. */ 107 | #define HAVE_SYS_TYPES_H 1 108 | 109 | /* Define to 1 if you have the header file. */ 110 | #define HAVE_SYS_UCONTEXT_H 1 111 | 112 | /* Define to 1 if you have the header file. */ 113 | #define HAVE_SYS_UTSNAME_H 1 114 | 115 | /* Define to 1 if you have the header file. */ 116 | #ifdef __linux__ 117 | #define HAVE_UCONTEXT_H 118 | #endif 119 | 120 | /* Define to 1 if you have the header file. */ 121 | #define HAVE_UNISTD_H 1 122 | 123 | /* Define to 1 if you have the header file. */ 124 | #define HAVE_UNWIND_H 1 125 | 126 | /* define if the compiler supports using expression for operator */ 127 | #define HAVE_USING_OPERATOR 1 128 | 129 | /* define if your compiler has __attribute__ */ 130 | #define HAVE___ATTRIBUTE__ 1 131 | 132 | /* define if your compiler has __builtin_expect */ 133 | #define HAVE___BUILTIN_EXPECT 1 134 | 135 | /* define if your compiler has __sync_val_compare_and_swap */ 136 | #define HAVE___SYNC_VAL_COMPARE_AND_SWAP 1 137 | 138 | /* Define to the sub-directory in which libtool stores uninstalled libraries. 139 | */ 140 | #define LT_OBJDIR ".libs/" 141 | 142 | /* Name of package */ 143 | #define PACKAGE "glog" 144 | 145 | /* Define to the address where bug reports for this package should be sent. */ 146 | #define PACKAGE_BUGREPORT "opensource@google.com" 147 | 148 | /* Define to the full name of this package. */ 149 | #define PACKAGE_NAME "glog" 150 | 151 | /* Define to the full name and version of this package. */ 152 | #define PACKAGE_STRING "glog 0.3.4" 153 | 154 | /* Define to the one symbol short name of this package. */ 155 | #define PACKAGE_TARNAME "glog" 156 | 157 | /* Define to the home page for this package. */ 158 | #define PACKAGE_URL "" 159 | 160 | /* Define to the version of this package. */ 161 | #define PACKAGE_VERSION "0.3.4" 162 | 163 | /* How to access the PC from a struct ucontext */ 164 | #ifdef __APPLE__ 165 | #define PC_FROM_UCONTEXT uc_mcontext->__ss.__rip 166 | #endif 167 | #ifdef __linux__ 168 | #define PC_FROM_UCONTEXT uc_mcontext.gregs[REG_RIP] 169 | #endif 170 | 171 | /* Define to necessary symbol if this constant uses a non-standard name on 172 | your system. */ 173 | /* #undef PTHREAD_CREATE_JOINABLE */ 174 | 175 | /* The size of `void *', as computed by sizeof. */ 176 | #ifdef __x86_64__ 177 | #define SIZEOF_VOID_P 8 178 | #endif 179 | 180 | /* Define to 1 if you have the ANSI C header files. */ 181 | /* #undef STDC_HEADERS */ 182 | 183 | /* the namespace where STL code like vector<> is defined */ 184 | #define STL_NAMESPACE std 185 | 186 | /* location of source code */ 187 | #define TEST_SRC_DIR "." 188 | 189 | /* Version number of package */ 190 | #define VERSION "0.3.4" 191 | 192 | /* Stops putting the code inside the Google namespace */ 193 | #define _END_GOOGLE_NAMESPACE_ } 194 | 195 | /* Puts following code inside the Google namespace */ 196 | #define _START_GOOGLE_NAMESPACE_ namespace google { 197 | -------------------------------------------------------------------------------- /src/test/gmock_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2008, Google Inc. 2 | // All rights reserved. 3 | // 4 | // Redistribution and use in source and binary forms, with or without 5 | // modification, are permitted provided that the following conditions are 6 | // met: 7 | // 8 | // * Redistributions of source code must retain the above copyright 9 | // notice, this list of conditions and the following disclaimer. 10 | // * Redistributions in binary form must reproduce the above 11 | // copyright notice, this list of conditions and the following disclaimer 12 | // in the documentation and/or other materials provided with the 13 | // distribution. 14 | // * Neither the name of Google Inc. nor the names of its 15 | // contributors may be used to endorse or promote products derived from 16 | // this software without specific prior written permission. 17 | // 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | // 30 | // Author: wan@google.com (Zhanyong Wan) 31 | 32 | // Google Mock - a framework for writing C++ mock classes. 33 | // 34 | // This file tests code in gmock.cc. 35 | 36 | #include "third_party/gmock/gmock.h" 37 | 38 | #include 39 | #include "third_party/gtest/gtest.h" 40 | 41 | using testing::GMOCK_FLAG(verbose); 42 | using testing::InitGoogleMock; 43 | using testing::internal::g_init_gtest_count; 44 | 45 | // Verifies that calling InitGoogleMock() on argv results in new_argv, 46 | // and the gmock_verbose flag's value is set to expected_gmock_verbose. 47 | template 48 | void TestInitGoogleMock(const Char* (&argv)[M], const Char* (&new_argv)[N], 49 | const ::std::string& expected_gmock_verbose) { 50 | const ::std::string old_verbose = GMOCK_FLAG(verbose); 51 | 52 | int argc = M; 53 | InitGoogleMock(&argc, const_cast(argv)); 54 | ASSERT_EQ(N, argc) << "The new argv has wrong number of elements."; 55 | 56 | for (int i = 0; i < N; i++) { 57 | EXPECT_STREQ(new_argv[i], argv[i]); 58 | } 59 | 60 | EXPECT_EQ(expected_gmock_verbose, GMOCK_FLAG(verbose).c_str()); 61 | GMOCK_FLAG(verbose) = old_verbose; // Restores the gmock_verbose flag. 62 | } 63 | 64 | TEST(InitGoogleMockTest, ParsesInvalidCommandLine) { 65 | const char* argv[] = { 66 | NULL 67 | }; 68 | 69 | const char* new_argv[] = { 70 | NULL 71 | }; 72 | 73 | TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 74 | } 75 | 76 | TEST(InitGoogleMockTest, ParsesEmptyCommandLine) { 77 | const char* argv[] = { 78 | "foo.exe", 79 | NULL 80 | }; 81 | 82 | const char* new_argv[] = { 83 | "foo.exe", 84 | NULL 85 | }; 86 | 87 | TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 88 | } 89 | 90 | TEST(InitGoogleMockTest, ParsesSingleFlag) { 91 | const char* argv[] = { 92 | "foo.exe", 93 | "--gmock_verbose=info", 94 | NULL 95 | }; 96 | 97 | const char* new_argv[] = { 98 | "foo.exe", 99 | NULL 100 | }; 101 | 102 | TestInitGoogleMock(argv, new_argv, "info"); 103 | } 104 | 105 | TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) { 106 | const char* argv[] = { 107 | "foo.exe", 108 | "--non_gmock_flag=blah", 109 | NULL 110 | }; 111 | 112 | const char* new_argv[] = { 113 | "foo.exe", 114 | "--non_gmock_flag=blah", 115 | NULL 116 | }; 117 | 118 | TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 119 | } 120 | 121 | TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) { 122 | const char* argv[] = { 123 | "foo.exe", 124 | "--non_gmock_flag=blah", 125 | "--gmock_verbose=error", 126 | NULL 127 | }; 128 | 129 | const char* new_argv[] = { 130 | "foo.exe", 131 | "--non_gmock_flag=blah", 132 | NULL 133 | }; 134 | 135 | TestInitGoogleMock(argv, new_argv, "error"); 136 | } 137 | 138 | TEST(InitGoogleMockTest, CallsInitGoogleTest) { 139 | const int old_init_gtest_count = g_init_gtest_count; 140 | const char* argv[] = { 141 | "foo.exe", 142 | "--non_gmock_flag=blah", 143 | "--gmock_verbose=error", 144 | NULL 145 | }; 146 | 147 | const char* new_argv[] = { 148 | "foo.exe", 149 | "--non_gmock_flag=blah", 150 | NULL 151 | }; 152 | 153 | TestInitGoogleMock(argv, new_argv, "error"); 154 | EXPECT_EQ(old_init_gtest_count + 1, g_init_gtest_count); 155 | } 156 | 157 | TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) { 158 | const wchar_t* argv[] = { 159 | NULL 160 | }; 161 | 162 | const wchar_t* new_argv[] = { 163 | NULL 164 | }; 165 | 166 | TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 167 | } 168 | 169 | TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) { 170 | const wchar_t* argv[] = { 171 | L"foo.exe", 172 | NULL 173 | }; 174 | 175 | const wchar_t* new_argv[] = { 176 | L"foo.exe", 177 | NULL 178 | }; 179 | 180 | TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 181 | } 182 | 183 | TEST(WideInitGoogleMockTest, ParsesSingleFlag) { 184 | const wchar_t* argv[] = { 185 | L"foo.exe", 186 | L"--gmock_verbose=info", 187 | NULL 188 | }; 189 | 190 | const wchar_t* new_argv[] = { 191 | L"foo.exe", 192 | NULL 193 | }; 194 | 195 | TestInitGoogleMock(argv, new_argv, "info"); 196 | } 197 | 198 | TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) { 199 | const wchar_t* argv[] = { 200 | L"foo.exe", 201 | L"--non_gmock_flag=blah", 202 | NULL 203 | }; 204 | 205 | const wchar_t* new_argv[] = { 206 | L"foo.exe", 207 | L"--non_gmock_flag=blah", 208 | NULL 209 | }; 210 | 211 | TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 212 | } 213 | 214 | TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) { 215 | const wchar_t* argv[] = { 216 | L"foo.exe", 217 | L"--non_gmock_flag=blah", 218 | L"--gmock_verbose=error", 219 | NULL 220 | }; 221 | 222 | const wchar_t* new_argv[] = { 223 | L"foo.exe", 224 | L"--non_gmock_flag=blah", 225 | NULL 226 | }; 227 | 228 | TestInitGoogleMock(argv, new_argv, "error"); 229 | } 230 | 231 | TEST(WideInitGoogleMockTest, CallsInitGoogleTest) { 232 | const int old_init_gtest_count = g_init_gtest_count; 233 | const wchar_t* argv[] = { 234 | L"foo.exe", 235 | L"--non_gmock_flag=blah", 236 | L"--gmock_verbose=error", 237 | NULL 238 | }; 239 | 240 | const wchar_t* new_argv[] = { 241 | L"foo.exe", 242 | L"--non_gmock_flag=blah", 243 | NULL 244 | }; 245 | 246 | TestInitGoogleMock(argv, new_argv, "error"); 247 | EXPECT_EQ(old_init_gtest_count + 1, g_init_gtest_count); 248 | } 249 | 250 | // Makes sure Google Mock flags can be accessed in code. 251 | TEST(FlagTest, IsAccessibleInCode) { 252 | bool dummy = testing::GMOCK_FLAG(catch_leaked_mocks) && 253 | testing::GMOCK_FLAG(verbose) == ""; 254 | (void)dummy; // Avoids the "unused local variable" warning. 255 | } 256 | --------------------------------------------------------------------------------