forked from PhysicsX/ExampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_example.cpp
More file actions
96 lines (73 loc) · 2.46 KB
/
test_example.cpp
File metadata and controls
96 lines (73 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <algorithm>
#include <gtest/gtest.h>
#include "dummy.hpp"
#include "foo_if.hpp"
#include "foo.hpp"
#include "mock_foo.hpp"
class myTestFixture1: public ::testing::Test {
public:
myTestFixture1( ) {
// initialization code here
std::cout<<"testDummy is null "<<(testDummy == nullptr)<<std::endl;
//foo fooTest;
//mockFoo fooTest;
testDummy = std::make_unique<dummy>(fooTest);
}
void SetUp( ) {
// code here will execute just before the test ensues
}
void TearDown( ) {
// code here will be called just after the test completes
// ok to through exceptions from here if need be
}
~myTestFixture1( ) {
// cleanup any pending stuff, but no exceptions allowed
}
mockFoo fooTest;
std::unique_ptr<dummy> testDummy;
// put in any custom data members that you need
};
TEST_F (myTestFixture1, TestDummyStr) {
std::string testString;
std::string returnStr {"testString"};
EXPECT_CALL(fooTest, fooStr(::testing::_)).WillOnce(::testing::SetArgReferee<0>(returnStr));
testDummy->dummyStr(testString);
ASSERT_EQ(testString,returnStr);
}
TEST_F (myTestFixture1, UnitTest2) {
ASSERT_EQ(1,1);
}
ACTION(throwError)
{
std::cout << "ERROR!\n";
throw std::runtime_error("ERROR_TEST");
}
TEST_F (myTestFixture1, ThrowTest) {
EXPECT_CALL(fooTest, fooThrow()).WillOnce(throwError());
ASSERT_THROW(testDummy->dummyThrow(), std::runtime_error);
}
void throwFunction()
{
std::cout << "ERROR!\n";
throw std::runtime_error("ERROR_TEST");
}
TEST_F (myTestFixture1, ThrowTest2) {
EXPECT_CALL(fooTest, fooThrow()).WillOnce(testing::Throw(std::runtime_error("ERROR_TEST")));
ASSERT_THROW(testDummy->dummyThrow(), std::runtime_error);
EXPECT_CALL(fooTest, fooThrow()).WillOnce(testing::Invoke([](){
std::cout << "ERROR!\n";
throw std::runtime_error("ERROR_TEST");
}));
ASSERT_THROW(testDummy->dummyThrow(), std::runtime_error);
EXPECT_CALL(fooTest, fooThrow()).WillOnce(testing::Invoke(throwFunction));
ASSERT_THROW(testDummy->dummyThrow(), std::runtime_error);
}
TEST_F (myTestFixture1, TestCallback) {
testing::MockFunction<void(void)> testFunc;
// std::function is not ==, that is why _ is used. Call can be checked.
EXPECT_CALL(fooTest, callbackMethod(::testing::_)).Times(1)
.WillOnce(testing::InvokeArgument<0>());;
EXPECT_CALL(testFunc, Call());
testDummy->dummyCallback(testFunc.AsStdFunction());
}