Skip to content

Commit 29a7bd3

Browse files
author
Rodrigo Fernandes
committed
Support for building with code coverage (also used internally for CDash reporting)
Removed some compilation warnings (unused paramters) Added Service test
1 parent 02018fb commit 29a7bd3

10 files changed

Lines changed: 195 additions & 33 deletions

File tree

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,15 @@ add_subdirectory(examples)
9090

9191
enable_testing()
9292
include(CTest)
93+
94+
option(CodeCoverage "Build support for code coverage (requires -DCMAKE_BUILD_TYPE=Debug)" OFF)
95+
96+
if (CodeCoverage)
97+
message (STATUS "Building with code coverage support")
98+
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -Wall -W -Wshadow -Wunused-variable -Wunused-parameter -Wunused-function -Wunused -Wno-system-headers -Wno-deprecated -Woverloaded-virtual -Wwrite-strings -fprofile-arcs -ftest-coverage")
99+
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0 -Wall -W -fprofile-arcs -ftest-coverage")
100+
set (CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fprofile-arcs -ftest-coverage")
101+
set (CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fprofile-arcs -ftest-coverage")
102+
endif()
103+
93104
add_subdirectory(tests)

examples/include/Instantiation.hh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef _CALCULATOR_HH_
2+
#define _CALCULATOR_HH_
3+
#include <answer/Operation.hh>
4+
#include <string>
5+
#include <set>
6+
7+
namespace WebServices{
8+
9+
///Define request and response types
10+
struct Data{
11+
double result;
12+
13+
template<class Archive>
14+
void serialize(Archive &ar, const unsigned int /*version*/){
15+
ar & BOOST_SERIALIZATION_NVP(result);
16+
}
17+
};
18+
19+
///Complex type service testing
20+
class Instantiation: public answer::instantiation::Singleton{
21+
public:
22+
Data calculator(const Data &request);
23+
};
24+
25+
///Complex type service testing
26+
class Instantiation{
27+
public:
28+
Data calculator(const Data &request);
29+
};
30+
31+
ANSWER_REGISTER_OPERATION(MyService::calculator);
32+
33+
}
34+
35+
#endif // _CALCULATOR_HH_

examples/src/Instantiation.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include ".hh"
2+
3+
using namespace std;
4+
using namespace answer;
5+
6+
namespace WebServices{
7+
8+
CalculatorResponse MyService::calculator(const CalculatorRequest &request){
9+
CalculatorResponse response;
10+
switch(request.operation){
11+
case 'x':
12+
case '*':
13+
response.result = request.operand1 * request.operand2;
14+
break;
15+
case '-':
16+
response.result = request.operand1 - request.operand2;
17+
break;
18+
case 'd':
19+
case '/':
20+
response.result = request.operand1 / request.operand2;
21+
break;
22+
case '+':
23+
response.result = request.operand1 + request.operand2;
24+
break;
25+
default:
26+
throw WebMethodInvalidInput("Invalid operation requested");
27+
}
28+
29+
return response;
30+
}
31+
32+
}

include/answer/Codec.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace answer{
3434
}
3535

3636
template<typename T>
37-
bool ResponseWrapper( std::ostream& out, const std::string& response, const std::string& mimeType, const answer::WebMethodException*)
37+
bool ResponseWrapper( std::ostream& out, const std::string& response, const std::string& /*mimeType*/, const answer::WebMethodException*)
3838
{
3939
out << response;
4040
return true;

include/answer/Operation.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public:
2727
Operation(const std::string& name):_name(name){}
2828
virtual ~Operation() {};
2929
//The invocation wrapper
30-
virtual Response invoke(const std::string&, const std::string & ="")=0;
30+
virtual Response invoke(const std::string& params, const std::string & prefix="")=0;
3131
};
3232

3333
template <typename RequestT>

tests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_subdirectory(serialization)
2+
add_subdirectory(service)
3+
add_subdirectory(module)

tests/registration/RegistrationTest.cpp

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/serialization/SerializationTest.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ struct testInput{
2424
}
2525
};
2626

27-
28-
2927
BOOST_AUTO_TEST_CASE( serialization )
3028
{
3129
testInput testIntSet1;
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
find_package(Boost COMPONENTS unit_test_framework REQUIRED QUIET)
22

3-
add_executable(registrationTest
4-
RegistrationTest.cpp
3+
add_executable(serviceTest
4+
ServiceTest.cpp
55
)
66

77
include_directories(
88
${Boost_INCLUDE_DIRS}
99
)
1010

11-
target_link_libraries(registrationTest
11+
target_link_libraries(serviceTest
1212
${Boost_LIBRARIES}
1313
answer
1414
)
1515

16-
add_test(registration registrationTest)
16+
add_test(service serviceTest)
1717

tests/service/ServiceTest.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#define BOOST_TEST_MODULE RegistrationTest
2+
#include <boost/test/included/unit_test.hpp>
3+
4+
//Normally this is defined in the build file, it's usually the project name
5+
#define ANSWER_SERVICE_NAME "RegistrationTest"
6+
7+
#include "answer/Operation.hh"
8+
#include <boost/algorithm/string.hpp>
9+
10+
using namespace std;
11+
12+
class Operation{
13+
public:
14+
int test(string X){
15+
return X.size();
16+
}
17+
};
18+
19+
ANSWER_REGISTER_OPERATION(Operation::test);
20+
21+
22+
class TestContext: public
23+
answer::Context,
24+
answer::TransportInfo,
25+
answer::OperationInfo{
26+
27+
string _service;
28+
string _operation;
29+
string _accept;
30+
list<string> _accepts;
31+
32+
public:
33+
TestContext(const string &serviceName, const string &operationName, const string& acceptsType):
34+
_service(serviceName), _operation(operationName), _accepts({acceptsType}){
35+
}
36+
37+
//Context
38+
virtual answer::CookieJar& cookieJar(){
39+
throw std::runtime_error("CookieJar not implemented");
40+
}
41+
virtual answer::Environment& environment(){
42+
throw std::runtime_error("Environment not implemented");
43+
}
44+
virtual answer::OperationInfo& operationInfo(){
45+
return *this;
46+
}
47+
virtual answer::ProviderStore& providerStore(){
48+
throw std::runtime_error("ProviderStore not implemented");
49+
}
50+
virtual answer::TransportInfo& transportInfo(){
51+
return *this;
52+
}
53+
54+
//TransportInfo
55+
virtual const list<string>& accepts() const{
56+
return _accepts;
57+
}
58+
virtual void addHeader ( const string& /*key*/, const string& /*value*/ = "", bool /*replace*/ = true ){
59+
throw std::runtime_error("addHeader not implemented");
60+
}
61+
virtual const string& redirect() const{
62+
throw std::runtime_error("redirect not implemented");
63+
}
64+
virtual const string& redirect ( const string& /*uri*/ ){
65+
throw std::runtime_error("redirect (set) not implemented");
66+
}
67+
virtual bool redirectSet() const{
68+
return false;
69+
}
70+
71+
//OperationInfo
72+
virtual const string& operation() const{
73+
return _operation;
74+
}
75+
virtual const string& service() const{
76+
return _service;
77+
}
78+
virtual const string& url() const{
79+
throw std::runtime_error("url not implemented");
80+
}
81+
82+
};
83+
84+
85+
BOOST_AUTO_TEST_CASE( registration )
86+
{
87+
list<string> operations = answer::OperationStore::Instance().operationList();
88+
BOOST_CHECK(operations.front() == string("RegistrationTest/test"));
89+
}
90+
91+
BOOST_AUTO_TEST_CASE( request_response ){
92+
TestContext context("RegistrationTest", "test", "application/xml");
93+
answer::Operation &operation = answer::OperationStore::Instance().operation("RegistrationTest", "test");
94+
answer::Response response = operation.invoke("<test>foobar</test>");
95+
string body = response.body();
96+
boost::trim(body);
97+
BOOST_CHECK(body == "<test>6</test>");
98+
}
99+
100+
BOOST_AUTO_TEST_CASE( request_response_codec ){
101+
TestContext context("RegistrationTest", "test", "application/json");
102+
answer::Operation &operation = answer::OperationStore::Instance().operation("RegistrationTest", "test");
103+
answer::Response response = operation.invoke("<test>foobar</test>");
104+
string body = response.body();
105+
boost::trim(body);
106+
BOOST_CHECK(body == "\"6\"");
107+
}
108+

0 commit comments

Comments
 (0)