Skip to content

Commit

Permalink
Add a testing framework to KJ and rip out gtest.
Browse files Browse the repository at this point in the history
A compatibility layer is provided to ease migration.
  • Loading branch information
kentonv committed Dec 30, 2014
1 parent 46b2a37 commit 6cd2526
Show file tree
Hide file tree
Showing 69 changed files with 858 additions and 387 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
# setup-ekam.sh
/c++/.ekam

# setup-autotools.sh
/c++/gtest

# Jekyll-generated site
/doc/_site

Expand Down
50 changes: 14 additions & 36 deletions c++/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,6 @@ ACLOCAL_AMFLAGS = -I m4

AUTOMAKE_OPTIONS = foreign subdir-objects

# Always include gtest in distributions.
DIST_SUBDIRS = $(subdirs)

# Build gtest before we build Cap'n Proto tests. We don't add gtest to SUBDIRS
# because then "make check" would also build and run all of gtest's own tests,
# which takes a lot of time and is generally not useful to us. Also, we don't
# want "make install" to recurse into gtest since we don't want to overwrite
# the installed version of gtest if there is one (and, actually, gtest doesn't
# even support "make install" anyway). So we define a rule such that it will
# only be built when needed.
gtest/lib/libgtest.la:
@echo "Making lib/libgtest.a lib/libgtest_main.a in gtest"
@cd gtest && $(MAKE) $(AM_MAKEFLAGS) lib/libgtest.la lib/libgtest_main.la

gtest/lib/libgtest_main.la: gtest/lib/libgtest.la
@:

# We would like to clean gtest when "make clean" is invoked. But we have to
# be careful because clean-local is also invoked during "make distclean", but
# "make distclean" already recurses into gtest because it's listed among the
# DIST_SUBDIRS. distclean will delete gtest/Makefile, so if we then try to
# cd to the directory again and "make clean" it will fail. So, check that the
# Makefile exists before recursing.
clean-local:
@if test -e gtest/Makefile; then \
echo "Making clean in gtest"; \
cd gtest && $(MAKE) $(AM_MAKEFLAGS) clean; \
fi

# When running distcheck, verify that we've included all the files needed by
# the cmake build.
distcheck-hook:
Expand Down Expand Up @@ -130,6 +101,7 @@ includecapnpdir = $(includedir)/capnp
includekjdir = $(includedir)/kj
includekjparsedir = $(includekjdir)/parse
includekjstddir = $(includekjdir)/std
includekjcompatdir = $(includekjdir)/compat

dist_includecapnp_DATA = $(public_capnpc_inputs)

Expand Down Expand Up @@ -163,6 +135,7 @@ includekj_HEADERS = \
src/kj/async-unix.h \
src/kj/async-io.h \
src/kj/main.h \
src/kj/test.h \
src/kj/windows-sanity.h

includekjparse_HEADERS = \
Expand All @@ -172,6 +145,9 @@ includekjparse_HEADERS = \
includekjstd_HEADERS = \
src/kj/std/iostream.h

includekjcompat_HEADERS = \
src/kj/compat/gtest.h

includecapnp_HEADERS = \
src/capnp/c++.capnp.h \
src/capnp/common.h \
Expand Down Expand Up @@ -204,9 +180,9 @@ includecapnp_HEADERS = \
src/capnp/ez-rpc.h

if LITE_MODE
lib_LTLIBRARIES = libkj.la libcapnp.la
lib_LTLIBRARIES = libkj.la libkj-test.la libcapnp.la
else
lib_LTLIBRARIES = libkj.la libkj-async.la libcapnp.la libcapnp-rpc.la libcapnpc.la
lib_LTLIBRARIES = libkj.la libkj-test.la libkj-async.la libcapnp.la libcapnp-rpc.la libcapnpc.la
endif

libkj_la_LIBADD = $(PTHREAD_LIBS)
Expand All @@ -228,6 +204,10 @@ libkj_la_SOURCES= \
src/kj/main.c++ \
src/kj/parse/char.c++

libkj_test_la_LIBADD = libkj.la $(PTHREAD_LIBS)
libkj_test_la_LDFLAGS = -release $(VERSION) -no-undefined
libkj_test_la_SOURCES = src/kj/test.c++

if !LITE_MODE
libkj_async_la_LIBADD = libkj.la $(PTHREAD_LIBS)
libkj_async_la_LDFLAGS = -release $(VERSION) -no-undefined
Expand Down Expand Up @@ -374,8 +354,7 @@ if LITE_MODE

check_PROGRAMS = capnp-test
compiler_tests =
capnp_test_LDADD = gtest/lib/libgtest.la gtest/lib/libgtest_main.la \
libcapnp.la libkj.la
capnp_test_LDADD = libcapnp.la libkj-test.la libkj.la

else !LITE_MODE

Expand All @@ -398,12 +377,11 @@ heavy_tests = \
src/capnp/ez-rpc-test.c++ \
src/capnp/compiler/lexer-test.c++ \
src/capnp/compiler/md5-test.c++
capnp_test_LDADD = gtest/lib/libgtest.la gtest/lib/libgtest_main.la \
libcapnpc.la libcapnp-rpc.la libcapnp.la libkj-async.la libkj.la
capnp_test_LDADD = libcapnpc.la libcapnp-rpc.la libcapnp.la libkj-async.la libkj-test.la libkj.la

endif !LITE_MODE

capnp_test_CPPFLAGS = -Wno-deprecated-declarations -Igtest/include -I$(srcdir)/gtest/include
capnp_test_CPPFLAGS = -Wno-deprecated-declarations
capnp_test_SOURCES = \
src/kj/common-test.c++ \
src/kj/memory-test.c++ \
Expand Down
9 changes: 0 additions & 9 deletions c++/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ AS_CASE("${host_os}", *mingw*, [
AC_SUBST(PTHREAD_LIBS)
AC_SUBST(PTHREAD_CFLAGS)
AC_SUBST(PTHREAD_CC)
# Disable pthreads when configuring gtest.
ac_configure_args="$ac_configure_args --without-pthreads"
], *, [
ACX_PTHREAD
])
Expand Down Expand Up @@ -104,11 +101,5 @@ AC_SUBST([STDLIB_FLAG])
LIBS="$PTHREAD_LIBS $LIBS"
CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"

# AX_CXX_COMPILE_STDCXX_11 may have modified CXX, and we need gtest to use these modifications as
# well (especially -stdlib). Note that this may cause ac_configure_args to contain CXX= twice, but
# the latter one should override the former.
ac_configure_args="$ac_configure_args 'CXX=$CXX'"
AC_CONFIG_SUBDIRS([gtest])

AC_CONFIG_FILES([Makefile capnp.pc capnp-rpc.pc])
AC_OUTPUT
13 changes: 1 addition & 12 deletions c++/setup-autotools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,5 @@

set -euo pipefail

if [ ! -e gtest ]; then
echo "================================================================================"
echo "Fetching Google Test code..."
echo "================================================================================"
svn checkout http://googletest.googlecode.com/svn/tags/release-1.7.0 gtest
fi

echo "================================================================================"
echo "Done"
echo "================================================================================"
echo
echo "Ready to run autoreconf. For example:"
echo "This script is no longer needed. Go ahead and run autoreconf. For example:"
echo " autoreconf -i && ./configure && make -j6 check && sudo make install"
42 changes: 0 additions & 42 deletions c++/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,6 @@ if(BUILD_TESTING)
find_package(CapnProto QUIET)
set(CAPNP_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}")

# Setup googletest build and library targets (gtest and gtest_main)
include(ExternalProject)
ExternalProject_Add(gtest_build
URL http://googletest.googlecode.com/files/gtest-1.7.0.zip
URL_MD5 2d6ec8ccdf5c46b05ba54a9fd1d130d7
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -Dgtest_force_shared_crt=1
INSTALL_COMMAND "" # Disable install
)

ExternalProject_Get_Property(gtest_build binary_dir)

# Set platform-specific library prefix/extensions.
if(MSVC)
set(gtest_prefix)
set(gtest_suffix ".lib")
else()
set(gtest_prefix "lib")
set(gtest_suffix ".a")
endif()

add_library(gtest UNKNOWN IMPORTED)
if(CMAKE_GENERATOR MATCHES "Visual Studio.*" OR CMAKE_GENERATOR STREQUAL Xcode)
set_target_properties(gtest PROPERTIES IMPORTED_LOCATION_DEBUG "${binary_dir}/Debug/${gtest_prefix}gtest${gtest_suffix}")
set_target_properties(gtest PROPERTIES IMPORTED_LOCATION_RELEASE "${binary_dir}/Release/${gtest_prefix}gtest${gtest_suffix}")
else()
set_target_properties(gtest PROPERTIES IMPORTED_LOCATION "${binary_dir}/${gtest_prefix}gtest${gtest_suffix}")
endif()

add_dependencies(gtest gtest_build)

add_library(gtest_main UNKNOWN IMPORTED)
if(CMAKE_GENERATOR MATCHES "Visual Studio.*" OR CMAKE_GENERATOR STREQUAL Xcode)
set_target_properties(gtest_main PROPERTIES IMPORTED_LOCATION_DEBUG "${binary_dir}/Debug/${gtest_prefix}gtest_main${gtest_suffix}")
set_target_properties(gtest_main PROPERTIES IMPORTED_LOCATION_RELEASE "${binary_dir}/Release/${gtest_prefix}gtest_main${gtest_suffix}")
else()
set_target_properties(gtest_main PROPERTIES IMPORTED_LOCATION "${binary_dir}/${gtest_prefix}gtest_main${gtest_suffix}")
endif()
add_dependencies(gtest_main gtest)

ExternalProject_Get_Property(gtest_build source_dir)
include_directories("${source_dir}/include")

# Sadly, we can't use the 'test' target, as that's coopted by ctest
add_custom_target(check "${CMAKE_CTEST_COMMAND}" -V)
endif() # BUILD_TESTING
Expand Down
4 changes: 2 additions & 2 deletions c++/src/capnp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ if(BUILD_TESTING)
capnp_generate_cpp(test_capnp_cpp_files test_capnp_h_files ${test_capnp_files})

if(CAPNP_LITE)
set(test_libraries capnp kj gtest gtest_main)
set(test_libraries capnp kj-test kj)
else()
set(test_libraries capnp capnp-rpc capnpc kj kj-async gtest gtest_main)
set(test_libraries capnp-rpc capnp capnpc kj-async kj-test kj)
endif()

add_executable(capnp-tests
Expand Down
2 changes: 1 addition & 1 deletion c++/src/capnp/any-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "any.h"
#include "message.h"
#include <gtest/gtest.h>
#include <kj/compat/gtest.h>
#include "test-util.h"

namespace capnp {
Expand Down
2 changes: 1 addition & 1 deletion c++/src/capnp/blob-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// THE SOFTWARE.

#include "blob.h"
#include <gtest/gtest.h>
#include <kj/compat/gtest.h>
#include <iostream>
#include <string>
#include "test-util.h"
Expand Down
2 changes: 1 addition & 1 deletion c++/src/capnp/capability-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "capability.h"
#include "test-util.h"
#include <kj/debug.h>
#include <gtest/gtest.h>
#include <kj/compat/gtest.h>

namespace capnp {
namespace _ {
Expand Down
2 changes: 1 addition & 1 deletion c++/src/capnp/common-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// THE SOFTWARE.

#include "common.h"
#include <gtest/gtest.h>
#include <kj/compat/gtest.h>
#include <kj/string.h>
#include <kj/debug.h>

Expand Down
4 changes: 2 additions & 2 deletions c++/src/capnp/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <kj/units.h>
#include <inttypes.h>
#include <kj/string.h>

namespace capnp {

Expand Down Expand Up @@ -64,8 +65,7 @@ struct Void {
static KJ_CONSTEXPR(const) Void VOID = Void();
// Constant value for `Void`, which is an empty struct.

template <typename T>
inline T& operator<<(T& os, Void) { return os << "void"; }
inline kj::StringPtr KJ_STRINGIFY(Void) { return "void"; }

struct Text;
struct Data;
Expand Down
4 changes: 2 additions & 2 deletions c++/src/capnp/compiler/lexer-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "lexer.h"
#include "../message.h"
#include <gtest/gtest.h>
#include <kj/compat/gtest.h>

namespace capnp {
namespace compiler {
Expand All @@ -30,7 +30,7 @@ namespace {
class TestFailingErrorReporter: public ErrorReporter {
public:
void addError(uint32_t startByte, uint32_t endByte, kj::StringPtr message) override {
ADD_FAILURE() << "Parse failed: (" << startByte << "-" << endByte << ") " << message.cStr();
KJ_FAIL_EXPECT("Parse failed.", startByte, endByte, message);
}

bool hadErrors() override {
Expand Down
2 changes: 1 addition & 1 deletion c++/src/capnp/compiler/md5-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// THE SOFTWARE.

#include "md5.h"
#include <gtest/gtest.h>
#include <kj/compat/gtest.h>

namespace capnp {
namespace compiler {
Expand Down
4 changes: 2 additions & 2 deletions c++/src/capnp/dynamic-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "dynamic.h"
#include "message.h"
#include <kj/debug.h>
#include <gtest/gtest.h>
#include <kj/compat/gtest.h>
#include "test-util.h"

namespace capnp {
Expand Down Expand Up @@ -226,7 +226,7 @@ TEST(DynamicApi, DynamicAnyPointers) {
KJ_IF_MAYBE(name, exp) { \
EXPECT_EQ(expected, actual); \
} else { \
FAIL() << "Maybe was empty."; \
KJ_FAIL_EXPECT("Maybe was empty."); \
}

TEST(DynamicApi, UnionsRead) {
Expand Down
19 changes: 6 additions & 13 deletions c++/src/capnp/encoding-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <capnp/test-import2.capnp.h>
#include "message.h"
#include <kj/debug.h>
#include <gtest/gtest.h>
#include <kj/compat/gtest.h>
#include "test-util.h"
#include "schema-lite.h"

Expand Down Expand Up @@ -152,15 +152,8 @@ struct UnionState {
}
};

std::ostream& operator<<(std::ostream& os, const UnionState& us) {
os << "UnionState({";

for (uint i = 0; i < 4; i++) {
if (i > 0) os << ", ";
os << us.discriminants[i];
}

return os << "}, " << us.dataOffset << ")";
kj::String KJ_STRINGIFY(const UnionState& us) {
return kj::str("UnionState({", kj::strArray(us.discriminants, ", "), "}, ", us.dataOffset, ")");
}

template <typename StructType, typename Func>
Expand Down Expand Up @@ -565,7 +558,7 @@ TEST(Encoding, ListUpgrade) {
#endif
});

EXPECT_TRUE(e != nullptr) << "Should have thrown an exception.";
KJ_EXPECT(e != nullptr, "Should have thrown an exception.");
}

{
Expand Down Expand Up @@ -673,7 +666,7 @@ TEST(Encoding, BitListUpgrade) {
#endif
});

EXPECT_TRUE(e != nullptr) << "Should have thrown an exception.";
KJ_EXPECT(e != nullptr, "Should have thrown an exception.");
}

auto reader = root.asReader();
Expand All @@ -686,7 +679,7 @@ TEST(Encoding, BitListUpgrade) {
#endif
});

EXPECT_TRUE(e != nullptr) << "Should have thrown an exception.";
KJ_EXPECT(e != nullptr, "Should have thrown an exception.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion c++/src/capnp/endian-reverse-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// the bswap-based code.
#define CAPNP_REVERSE_ENDIAN 1
#include "endian.h"
#include <gtest/gtest.h>
#include <kj/compat/gtest.h>

namespace capnp {
namespace _ { // private
Expand Down
2 changes: 1 addition & 1 deletion c++/src/capnp/endian-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// THE SOFTWARE.

#include "endian.h"
#include <gtest/gtest.h>
#include <kj/compat/gtest.h>

namespace capnp {
namespace _ { // private
Expand Down
Loading

0 comments on commit 6cd2526

Please sign in to comment.