commit 659815054145cf8ad077ef7260f5a41daeef1788
parent 3453b2b616f23155a2fc23480e13bc066374087b
Author: Chris Bracken <chris@bracken.jp>
Date: Fri, 8 May 2020 17:19:05 -0700
Add absl, update googletest dependencies
absl generally makes C++ less bad, so may as well include in the default
template.
Diffstat:
9 files changed, 73 insertions(+), 33 deletions(-)
diff --git a/WORKSPACE b/WORKSPACE
@@ -1,9 +1,19 @@
+workspace(name = "cc_template")
+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+# abseil-cpp
+http_archive(
+ name = "com_google_absl",
+ urls = ["https://github.com/abseil/abseil-cpp/archive/b832dce8489ef7b6231384909fd9b68d5a5ff2b7.zip"],
+ strip_prefix = "abseil-cpp-b832dce8489ef7b6231384909fd9b68d5a5ff2b7",
+ sha256 = "6b54129c89707e66d1c33705501a87cb1659223e7e0d4e27d923956065fac6d9",
+)
+
+# Google Test
http_archive(
- name = "googletest",
- url = "https://github.com/google/googletest/archive/release-1.8.1.zip",
- sha256 = "927827c183d01734cc5cfef85e0ff3f5a92ffe6188e0d18e909c5efebf28a0c7",
- build_file = "googletest.BUILD",
- strip_prefix = "googletest-release-1.8.1",
+ name = "com_google_googletest",
+ urls = ["https://github.com/google/googletest/archive/703bd9caab50b139428cea1aaff9974ebee5742e.zip"],
+ strip_prefix = "googletest-703bd9caab50b139428cea1aaff9974ebee5742e",
+ sha256 = "2db427be8b258ad401177c411c2a7c2f6bc78548a04f1a23576cc62616d9cd38",
)
diff --git a/foobar/BUILD b/foobar/BUILD
@@ -1,3 +1,5 @@
+package(default_visibility = ["//visibility:public"])
+
cc_library(
name = "foolib",
srcs = [
@@ -8,12 +10,16 @@ cc_library(
"bar.h",
"foo.h",
],
+ deps = [
+ "@com_google_absl//absl/strings",
+ ],
visibility = ["//foobar:__pkg__"],
)
cc_binary(
name = "foobar",
srcs = ["main.cc"],
+ linkopts = ["-lm"],
deps = [":foolib"],
)
@@ -23,8 +29,9 @@ cc_test(
"bar_test.cc",
"foo_test.cc",
],
+ linkopts = ["-lm"],
deps = [
- "@googletest//:gtest_main",
+ "@com_google_googletest//:gtest_main",
":foolib",
],
)
diff --git a/foobar/bar.cc b/foobar/bar.cc
@@ -1,6 +1,18 @@
#include "bar.h"
-int Bar(int x) {
- return x - 1;
+#include <string>
+
+#include "absl/strings/str_cat.h"
+#include "absl/strings/string_view.h"
+
+namespace foobar {
+
+std::string Bar(absl::string_view name) {
+ std::string greeting = "Hello";
+ if (name.size() >= 1) {
+ absl::StrAppend(&greeting, ", ", name);
+ }
+ return greeting;
}
+} // namespace foobar
diff --git a/foobar/bar.h b/foobar/bar.h
@@ -1,6 +1,14 @@
#ifndef PROJNAME_BAR_H_
#define PROJNAME_BAR_H_
-int Bar(int x);
+#include <string>
+
+#include "absl/strings/string_view.h"
+
+namespace foobar {
+
+std::string Bar(absl::string_view name);
+
+} // namespace foobar
#endif // PROJNAME_BAR_H_
diff --git a/foobar/bar_test.cc b/foobar/bar_test.cc
@@ -2,15 +2,12 @@
#include "gtest/gtest.h"
-TEST(Bar, DecrementsZero) {
- EXPECT_EQ(-1, Bar(0));
-}
+namespace foobar {
+namespace {
-TEST(Bar, DecrementsPositive) {
- EXPECT_EQ(1, Bar(2));
-}
+TEST(Bar, HandlesEmptyString) { EXPECT_EQ("Hello", Bar("")); }
-TEST(Bar, DecrementsNegative) {
- EXPECT_EQ(-2, Bar(-1));
-}
+TEST(Bar, HandlesName) { EXPECT_EQ("Hello, Alice", Bar("Alice")); }
+} // namespace
+} // namespace foobar
diff --git a/foobar/foo.cc b/foobar/foo.cc
@@ -1,5 +1,7 @@
#include "foo.h"
-int Foo(int x) {
- return x + 1;
-}
+namespace foobar {
+
+int Foo(int x) { return x + 1; }
+
+} // namespace foobar
diff --git a/foobar/foo.h b/foobar/foo.h
@@ -1,6 +1,10 @@
#ifndef PROJNAME_FOO_H_
#define PROJNAME_FOO_H_
+namespace foobar {
+
int Foo(int x);
+} // namespace foobar
+
#endif // PROJNAME_FOO_H_
diff --git a/foobar/foo_test.cc b/foobar/foo_test.cc
@@ -2,14 +2,14 @@
#include "gtest/gtest.h"
-TEST(Foo, IncrementsZero) {
- EXPECT_EQ(1, Foo(0));
-}
+namespace foobar {
+namespace {
-TEST(Foo, IncrementsPositive) {
- EXPECT_EQ(2, Foo(1));
-}
+TEST(Foo, IncrementsZero) { EXPECT_EQ(1, Foo(0)); }
-TEST(Foo, IncrementsNegative) {
- EXPECT_EQ(-1, Foo(-2));
-}
+TEST(Foo, IncrementsPositive) { EXPECT_EQ(2, Foo(1)); }
+
+TEST(Foo, IncrementsNegative) { EXPECT_EQ(-1, Foo(-2)); }
+
+} // namespace
+} // namespace foobar
diff --git a/foobar/main.cc b/foobar/main.cc
@@ -1,13 +1,13 @@
#include <iostream>
-#include "foo.h"
#include "bar.h"
+#include "foo.h"
int main(int argc, char** argv) {
for (auto i = 0; i < argc; ++i)
std::cout << "argv[" << i << "]: " << argv[i] << std::endl;
- auto x = Foo(3);
- auto y = Bar(x);
+ auto x = foobar::Foo(3);
+ auto y = foobar::Bar("Fred");
std::cout << "x=" << x << ",y=" << y << std::endl;
}