Skip to content

Initial migration from Boost.Test to googletest #576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "deps/googletest"]
path = deps/googletest
url = https://github.com/google/googletest.git
[submodule "deps/googlemock"]
path = deps/googlemock
url = https://github.com/google/googlemock.git
4 changes: 4 additions & 0 deletions .ycm_extra_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1',
'-I',
os.environ['BOOST_ROOT'],
# add dependency to googletest and googlemock
'-I', 'deps/googletest/googletest/include',
'-I', 'deps/googletest/googlemock/include',

# Always enable debugging for the project when building for semantic
# completion.
'-DBOOST_NETWORK_DEBUG',
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ if (Boost_FOUND)
enable_testing()
add_subdirectory(libs/network/src)
if (CPP-NETLIB_BUILD_TESTS)
add_subdirectory(deps/googletest)
add_subdirectory(libs/network/test)
endif (CPP-NETLIB_BUILD_TESTS)
if (CPP-NETLIB_BUILD_EXPERIMENTS)
Expand Down
12 changes: 12 additions & 0 deletions boost/network/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ struct basic_message {
friend struct detail::directive_base<Tag>;
friend struct detail::wrapper_base<Tag, basic_message<Tag> >;

// Define an equality operator that's only available via ADL.
friend bool operator==(basic_message const& l, basic_message const& r) {
return l.headers_ == r.headers_ && l.source_ == r.source_ &&
l.destination_ == r.destination_ && l.body_ == r.body_;
}

// Define an inequality operator that's only available via ADL in terms of
// equality defined above.
friend bool operator!=(basic_message const& l, basic_message const& r) {
return !(l == r);
}

mutable headers_container_type headers_;
mutable string_type body_;
mutable string_type source_;
Expand Down
1 change: 1 addition & 0 deletions deps/googlemock
Submodule googlemock added at f7d03d
1 change: 1 addition & 0 deletions deps/googletest
Submodule googletest added at ddb801
51 changes: 27 additions & 24 deletions libs/network/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Copyright (c) Dean Michael Berris 2010.
# Copyright (c) Dean Michael Berris 2010.
# Copyright 2015 Google, Inc.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

include_directories(${CPP-NETLIB_SOURCE_DIR})
include_directories(${CPP-NETLIB_SOURCE_DIR} ${gtest_SOURCE_DIR}/include)

add_subdirectory(uri)
add_subdirectory(http)
Expand All @@ -17,26 +18,28 @@ if (Boost_FOUND)
# utils_base64_test -- turn on when ready.
)
foreach (test ${TESTS})
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
set_source_files_properties(${test}.cpp
PROPERTIES COMPILE_FLAGS "-Wall")
endif()
add_executable(cpp-netlib-${test} ${test}.cpp)
add_dependencies(cpp-netlib-${test} cppnetlib-uri)
target_link_libraries(cpp-netlib-${test} ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} cppnetlib-uri)
if (OPENSSL_FOUND)
target_link_libraries(cpp-netlib-${test} ${OPENSSL_LIBRARIES})
endif()
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU AND ${CMAKE_SYSTEM_NAME} MATCHES "Windows")
target_link_libraries(cpp-netlib-${test} ws2_32 wsock32)
endif()
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
target_link_libraries(cpp-netlib-${test} rt)
endif()
set_target_properties(cpp-netlib-${test}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
add_test(cpp-netlib-${test}
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-${test})
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
set_source_files_properties(${test}.cpp
PROPERTIES COMPILE_FLAGS "-Wall")
endif()
add_executable(cpp-netlib-${test} ${test}.cpp)
add_dependencies(cpp-netlib-${test} cppnetlib-uri)
target_link_libraries(cpp-netlib-${test}
${Boost_THREAD_LIBRARY} ${Boost_SYSTEM_LIBRARY}
${CMAKE_THREAD_LIBS_INIT} cppnetlib-uri gtest_main)
if (OPENSSL_FOUND)
target_link_libraries(cpp-netlib-${test} ${OPENSSL_LIBRARIES})
endif()
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU AND ${CMAKE_SYSTEM_NAME} MATCHES "Windows")
target_link_libraries(cpp-netlib-${test} ws2_32 wsock32)
endif()
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
target_link_libraries(cpp-netlib-${test} rt)
endif()
set_target_properties(cpp-netlib-${test}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
add_test(cpp-netlib-${test}
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-${test})
endforeach (test)

# Also copy the server directory to the root of the build directory.
Expand Down
204 changes: 46 additions & 158 deletions libs/network/test/message_test.cpp
Original file line number Diff line number Diff line change
@@ -1,177 +1,65 @@

// Copyright Dean Michael Berris 2007.
// Copyright Dean Michael Berris 2007.
// Copyright 2015 Google, Inc.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#define BOOST_TEST_MODULE message test
#include <boost/config/warning_disable.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/network.hpp>
// Migrated from using Boost.Test to using googletest instead.
#include <gtest/gtest.h>
#include <algorithm>
#include <boost/mpl/list.hpp>
#include <boost/network/include/message.hpp>
#include <boost/network/message/directives.hpp>

using namespace boost::network;
namespace {

typedef boost::mpl::list<http::tags::http_default_8bit_tcp_resolve,
http::tags::http_default_8bit_udp_resolve,
http::tags::http_keepalive_8bit_tcp_resolve,
http::tags::http_keepalive_8bit_udp_resolve,
tags::default_string, tags::default_wstring> tag_types;
namespace http = boost::network::http;
namespace network = boost::network;

struct string_header_name {
static std::string string;
template <class T>
class MessageTest : public ::testing::Test {
};

std::string string_header_name::string = "Header";

struct wstring_header_name {
static std::wstring string;
};

std::wstring wstring_header_name::string = L"Header";

struct string_header_value {
static std::string string;
};

std::string string_header_value::string = "Value";

struct wstring_header_value {
static std::wstring string;
};

std::wstring wstring_header_value::string = L"Value";

template <class Tag>
struct header_name : string_header_name {};

template <>
struct header_name<tags::default_wstring> : wstring_header_name {};

template <class Tag>
struct header_value : string_header_value {};

template <>
struct header_value<tags::default_wstring> : wstring_header_value {};

struct string_body_data {
static std::string string;
};

std::string string_body_data::string =
"The quick brown fox jumps over the lazy dog.";

struct wstring_body_data {
static std::wstring string;
};

std::wstring wstring_body_data::string =
L"The quick brown fox jumps over the lazy dog.";

template <class Tag>
struct body_data : string_body_data {};

template <>
struct body_data<tags::default_wstring> : wstring_body_data {};

struct string_source_data {
static std::string string;
};

std::string string_source_data::string = "Source";

struct wstring_source_data {
static std::wstring string;
};

std::wstring wstring_source_data::string = L"Source";

template <class Tag>
struct source_data : string_source_data {};

template <>
struct source_data<tags::default_wstring> : wstring_body_data {};

struct string_destination_data {
static std::string string;
};

std::string string_destination_data::string = "Destination";

struct wstring_destination_data {
static std::wstring string;
};

std::wstring wstring_destination_data::string = L"Destination";

template <class Tag>
struct destination_data : string_destination_data {};

template <>
struct destination_data<tags::default_wstring> : wstring_destination_data {};
using TagTypes = ::testing::Types<http::tags::http_default_8bit_tcp_resolve,
http::tags::http_default_8bit_udp_resolve,
http::tags::http_keepalive_8bit_tcp_resolve,
http::tags::http_keepalive_8bit_udp_resolve,
network::tags::default_string>;
TYPED_TEST_CASE(MessageTest, TagTypes);

TYPED_TEST(MessageTest, Constructors){
network::basic_message<TypeParam> message; // default construction
auto copy = message; // copy construction
ASSERT_TRUE(copy == message);
}

/**
* Defines a set of template functions that can be used to test
* generic code.
*/
TYPED_TEST(MessageTest, Equality) {
// Create an original message.
network::basic_message<TypeParam> message;
message << network::source("source") << network::destination("destination")
<< network::header("key", "value") << network::body("body");

BOOST_AUTO_TEST_CASE_TEMPLATE(copy_constructor_test, T, tag_types) {
basic_message<T> instance;
instance << header(header_name<T>::string, header_value<T>::string);
basic_message<T> copy(instance);
BOOST_CHECK_EQUAL(headers(copy).count(header_name<T>::string),
static_cast<std::size_t>(1));
typename headers_range<basic_message<T> >::type range =
headers(copy)[header_name<T>::string];
BOOST_CHECK(boost::begin(range) != boost::end(range));
}
// Create the identical message.
network::basic_message<TypeParam> other;
other << network::source("source") << network::destination("destination")
<< network::header("key", "value") << network::body("body");

BOOST_AUTO_TEST_CASE_TEMPLATE(swap_test, T, tag_types) {
basic_message<T> instance;
instance << header(header_name<T>::string, header_value<T>::string);
basic_message<T> other;
swap(instance, other);
BOOST_CHECK_EQUAL(headers(instance).count(header_name<T>::string),
static_cast<std::size_t>(0));
BOOST_CHECK_EQUAL(headers(other).count(header_name<T>::string),
static_cast<std::size_t>(1));
ASSERT_TRUE(message == other);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(headers_directive_test, T, tag_types) {
basic_message<T> instance;
instance << header(header_name<T>::string, header_value<T>::string);
BOOST_CHECK_EQUAL(headers(instance).count(header_name<T>::string),
static_cast<std::size_t>(1));
typename headers_range<basic_message<T> >::type range =
headers(instance)[header_name<T>::string];
BOOST_CHECK(boost::begin(range) != boost::end(range));
}
TYPED_TEST(MessageTest, Inequality) {
// Create an original message.
network::basic_message<TypeParam> message;
message << network::source("source") << network::destination("destination")
<< network::header("key", "value") << network::body("body");

BOOST_AUTO_TEST_CASE_TEMPLATE(body_directive_test, T, tag_types) {
basic_message<T> instance;
instance << ::boost::network::body(body_data<T>::string);
typename string<T>::type body_string = body(instance);
BOOST_CHECK(body_string == body_data<T>::string);
}
// Create a different message.
network::basic_message<TypeParam> other;
other << network::source("source") << network::destination("destination")
<< network::header("key", "value") << network::body("different body!");

BOOST_AUTO_TEST_CASE_TEMPLATE(source_directive_test, T, tag_types) {
basic_message<T> instance;
instance << ::boost::network::source(source_data<T>::string);
typename string<T>::type source_string = source(instance);
BOOST_CHECK(source_string == source_data<T>::string);
ASSERT_FALSE(message == other);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(destination_directive_test, T, tag_types) {
basic_message<T> instance;
instance << destination(destination_data<T>::string);
BOOST_CHECK(destination(instance) == destination_data<T>::string);
}
} // namespace

BOOST_AUTO_TEST_CASE_TEMPLATE(remove_header_directive_test, T, tag_types) {
basic_message<T> instance;
instance << header(header_name<T>::string, header_value<T>::string)
<< remove_header(header_name<T>::string);
typename headers_range<basic_message<T> >::type range = headers(instance);
BOOST_CHECK(boost::begin(range) == boost::end(range));
}
33 changes: 16 additions & 17 deletions libs/network/test/message_transform_test.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@

// Copyright Dean Michael Berris 2007.
// Copyright Dean Michael Berris 2007.
// Copyright 2015, Google, Inc.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#define BOOST_TEST_MODULE message test
#include <boost/config/warning_disable.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/network/include/message.hpp>
#include <gtest/gtest.h>
#include <algorithm>
#include <boost/network/include/message.hpp>

BOOST_AUTO_TEST_CASE(message_transform_toupper) {
TEST(MessageTransformTest, TransformToUpper) {
using namespace boost::network;

message msg;
msg << source("me");
BOOST_CHECK_EQUAL(source(msg), "me");
ASSERT_EQ("me",source(msg));
msg << transform(to_upper_, source_);
BOOST_CHECK_EQUAL(source(msg), "ME");
ASSERT_EQ("ME",source(msg));
msg << destination("you");
BOOST_CHECK_EQUAL(destination(msg), "you");
ASSERT_EQ("you",destination(msg));
msg << transform(to_upper_, destination_);
BOOST_CHECK_EQUAL(destination(msg), "YOU");
ASSERT_EQ("YOU",destination(msg));
}

BOOST_AUTO_TEST_CASE(message_transform_tolower) {
TEST(MessageTransformTest, TransformToLower) {
using namespace boost::network;

message msg;
msg << source("ME");
BOOST_CHECK_EQUAL(source(msg), "ME");
ASSERT_EQ("ME",source(msg));
msg << transform(to_lower_, source_);
BOOST_CHECK_EQUAL(source(msg), "me");
ASSERT_EQ("me",source(msg));
msg << destination("YOU");
BOOST_CHECK_EQUAL(destination(msg), "YOU");
ASSERT_EQ("YOU",destination(msg));
msg << transform(to_lower_, destination_);
BOOST_CHECK_EQUAL(destination(msg), "you");
ASSERT_EQ("you",destination(msg));
}
Loading