From 9b94773a7f546f5d1a825c3a6f07420a5ced6404 Mon Sep 17 00:00:00 2001 From: Yi Wang Date: Sun, 11 Dec 2016 12:39:02 -0800 Subject: [PATCH] Add gtest --- WORKSPACE | 14 ++++++++++++++ .../build_and_install/docker_install_en.rst | 5 +++-- third_party/gtest.BUILD | 14 ++++++++++++++ third_party/protobuf_test/BUILD | 11 +++++++++++ third_party/protobuf_test/example.proto | 2 +- third_party/protobuf_test/example_lib.cc | 9 +++++++-- third_party/protobuf_test/example_lib.h | 8 +++++++- third_party/protobuf_test/example_lib_test.cc | 15 +++++++++++++++ 8 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 third_party/gtest.BUILD create mode 100644 third_party/protobuf_test/example_lib_test.cc diff --git a/WORKSPACE b/WORKSPACE index 06495690ab9..38e1628d11c 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,8 +1,22 @@ +# External dependency to grpc-enabled Google-styleprotobuf bulding +# rules. This method comes from +# https://github.com/pubref/rules_protobuf#usage. git_repository( name = "org_pubref_rules_protobuf", remote = "https://github.com/pubref/rules_protobuf", tag = "v0.7.1", ) +# External dependency to gtest 1.7.0. This method comes from +# https://www.bazel.io/versions/master/docs/tutorial/cpp.html. +new_http_archive( + name = "gtest", + url = "https://github.com/google/googletest/archive/release-1.7.0.zip", + sha256 = "b58cb7547a28b2c718d1e38aee18a3659c9e3ff52440297e965f5edffe34b6d0", + build_file = "third_party/gtest.BUILD", + strip_prefix = "googletest-release-1.7.0", +) + + load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cpp_proto_repositories") cpp_proto_repositories() diff --git a/doc/getstarted/build_and_install/docker_install_en.rst b/doc/getstarted/build_and_install/docker_install_en.rst index f6f1bbab428..feb027ccbbc 100644 --- a/doc/getstarted/build_and_install/docker_install_en.rst +++ b/doc/getstarted/build_and_install/docker_install_en.rst @@ -161,12 +161,13 @@ The general development workflow with Docker and Bazel is as follows: cd /paddle # where paddle source code has been mounted into the container mkdir -p build cd build - cmake .. + cmake -DWITH_TESTING=ON .. make -j `nproc` + CTEST_OUTPUT_ON_FAILURE=1 ctest or Bazel in the container: .. code-block:: bash cd /paddle - bazel build ... + bazel test ... diff --git a/third_party/gtest.BUILD b/third_party/gtest.BUILD new file mode 100644 index 00000000000..3e68a1d8793 --- /dev/null +++ b/third_party/gtest.BUILD @@ -0,0 +1,14 @@ +cc_library( + name = "main", + srcs = glob( + ["src/*.cc"], + exclude = ["src/gtest-all.cc"] + ), + hdrs = glob([ + "include/**/*.h", + "src/*.h" + ]), + copts = ["-Iexternal/gtest/include"], + linkopts = ["-pthread"], + visibility = ["//visibility:public"], +) diff --git a/third_party/protobuf_test/BUILD b/third_party/protobuf_test/BUILD index 7c5b1c69947..6208b870829 100644 --- a/third_party/protobuf_test/BUILD +++ b/third_party/protobuf_test/BUILD @@ -7,6 +7,7 @@ cpp_proto_library( protos = [ "example.proto" ], + with_grpc = True, ) cc_library( @@ -15,3 +16,13 @@ cc_library( hdrs = ["example_lib.h"], deps = [":example_proto"], ) + +cc_test( + name = "example_lib_test", + srcs = ["example_lib_test.cc"], + copts = ["-Iexternal/gtest/include"], + deps =[ + "@gtest//:main", + ":example_lib", + ], +) diff --git a/third_party/protobuf_test/example.proto b/third_party/protobuf_test/example.proto index 57c52d3521f..6a7eada9c14 100644 --- a/third_party/protobuf_test/example.proto +++ b/third_party/protobuf_test/example.proto @@ -1,6 +1,6 @@ syntax = "proto3"; -package protos; +package third_party.protobuf_test; message Greeting { string name = 1; diff --git a/third_party/protobuf_test/example_lib.cc b/third_party/protobuf_test/example_lib.cc index 8d55ed66dea..56341a0124c 100644 --- a/third_party/protobuf_test/example_lib.cc +++ b/third_party/protobuf_test/example_lib.cc @@ -1,6 +1,11 @@ #include "third_party/protobuf_test/example_lib.h" -#include -std::string get_greet(const ::protos::Greeting& who) { +namespace third_party { +namespace protobuf_test { + +std::string get_greet(const Greeting& who) { return "Hello " + who.name(); } + +} // namespace protobuf_test +} // namespace thrid_party diff --git a/third_party/protobuf_test/example_lib.h b/third_party/protobuf_test/example_lib.h index eaf4dd4cea7..516326e812e 100644 --- a/third_party/protobuf_test/example_lib.h +++ b/third_party/protobuf_test/example_lib.h @@ -4,4 +4,10 @@ #include -std::string get_greet(const ::protos::Greeting &who); +namespace third_party { +namespace protobuf_test { + +std::string get_greet(const Greeting &who); + +} // namespace protobuf_test +} // namespace third_party diff --git a/third_party/protobuf_test/example_lib_test.cc b/third_party/protobuf_test/example_lib_test.cc new file mode 100644 index 00000000000..6229f56e602 --- /dev/null +++ b/third_party/protobuf_test/example_lib_test.cc @@ -0,0 +1,15 @@ +#include "third_party/protobuf_test/example_lib.h" + +#include "gtest/gtest.h" + +namespace third_party { +namespace protobuf_test { + +TEST(ProtobufTest, GetGreet) { + Greeting g; + g.set_name("Paddle"); + EXPECT_EQ("Hello Paddle", get_greet(g)); +} + +} // namespace protobuf_test +} // namespace third_party -- GitLab