cc_project_template_bazel

A Bazel, ninja, and googletest-based C++ project template
git clone https://git.bracken.jp/cc_project_template_bazel.git
Log | Files | Refs | README | LICENSE

commit c7afad5ac28a223ed0b668e744f114a7633570dc
parent 6bfd092460fd736cb534ac9ebbbbe29fddf15b7d
Author: Chris Bracken <chris@bracken.jp>
Date:   Thu, 25 Apr 2019 17:53:52 -0700

Add foobar example project

Adds a C++ example project.

Diffstat:
Afoobar/BUILD | 44++++++++++++++++++++++++++++++++++++++++++++
Afoobar/bar.cc | 6++++++
Afoobar/bar.h | 6++++++
Afoobar/bar_test.cc | 16++++++++++++++++
Afoobar/foo.cc | 5+++++
Afoobar/foo.h | 6++++++
Afoobar/foo_test.cc | 15+++++++++++++++
Afoobar/main.cc | 13+++++++++++++
8 files changed, 111 insertions(+), 0 deletions(-)

diff --git a/foobar/BUILD b/foobar/BUILD @@ -0,0 +1,44 @@ +cc_library( + name = "foolib", + srcs = [ + "bar.cc", + "foo.cc", + ], + hdrs = [ + "bar.h", + "foo.h", + ], + visibility = ["//foobar:__pkg__"], +) + +cc_binary( + name = "foobar", + srcs = ["main.cc"], + deps = [":foolib"], +) + +cc_test( + name = "foobar_test", + srcs = [ + "bar_test.cc", + "foo_test.cc", + ], + deps = [ + "@googletest//:gtest_main", + ":foolib", + ], +) + +#executable("foo_tests") { +# testonly = true +# +# sources = [ +# "bar_test.cc", +# "foo_test.cc", +# ] +# deps = [ +# ":foo_srcs", +# "//third_party/googletest:gtest", +# "//third_party/googletest:gtest_main", +# ] +#} diff --git a/foobar/bar.cc b/foobar/bar.cc @@ -0,0 +1,6 @@ +#include "bar.h" + +int Bar(int x) { + return x - 1; +} + diff --git a/foobar/bar.h b/foobar/bar.h @@ -0,0 +1,6 @@ +#ifndef PROJNAME_BAR_H_ +#define PROJNAME_BAR_H_ + +int Bar(int x); + +#endif // PROJNAME_BAR_H_ diff --git a/foobar/bar_test.cc b/foobar/bar_test.cc @@ -0,0 +1,16 @@ +#include "bar.h" + +#include "gtest/gtest.h" + +TEST(Bar, DecrementsZero) { + EXPECT_EQ(-1, Bar(0)); +} + +TEST(Bar, DecrementsPositive) { + EXPECT_EQ(1, Bar(2)); +} + +TEST(Bar, DecrementsNegative) { + EXPECT_EQ(-2, Bar(-1)); +} + diff --git a/foobar/foo.cc b/foobar/foo.cc @@ -0,0 +1,5 @@ +#include "foo.h" + +int Foo(int x) { + return x + 1; +} diff --git a/foobar/foo.h b/foobar/foo.h @@ -0,0 +1,6 @@ +#ifndef PROJNAME_FOO_H_ +#define PROJNAME_FOO_H_ + +int Foo(int x); + +#endif // PROJNAME_FOO_H_ diff --git a/foobar/foo_test.cc b/foobar/foo_test.cc @@ -0,0 +1,15 @@ +#include "foo.h" + +#include "gtest/gtest.h" + +TEST(Foo, IncrementsZero) { + EXPECT_EQ(1, Foo(0)); +} + +TEST(Foo, IncrementsPositive) { + EXPECT_EQ(2, Foo(1)); +} + +TEST(Foo, IncrementsNegative) { + EXPECT_EQ(-1, Foo(-2)); +} diff --git a/foobar/main.cc b/foobar/main.cc @@ -0,0 +1,13 @@ +#include <iostream> + +#include "foo.h" +#include "bar.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); + std::cout << "x=" << x << ",y=" << y << std::endl; +}