File: test_except.cpp

package info (click to toggle)
hippomocks 5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 672 kB
  • sloc: cpp: 7,791; ansic: 31; makefile: 28
file content (87 lines) | stat: -rw-r--r-- 1,599 bytes parent folder | download | duplicates (2)
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
#include "hippomocks.h"
#include "Framework.h"

// For obvious reasons, the Throw is not present when you disable exceptions.
#ifndef HM_NO_EXCEPTIONS
class IE {
public:
	virtual ~IE() {}
	virtual int f();
	virtual std::string g() = 0;
};

TEST (checkPrimitiveExceptionAcceptedAndThrown)
{
	MockRepository mocks;
	IE *iamock = mocks.Mock<IE>();
	mocks.ExpectCall(iamock, IE::f).Throw(42);
	bool exceptionCaught = false;
	try 
	{
		iamock->f();
	}
	catch(int a)
	{
		CHECK(a == 42);
		exceptionCaught = true;
	}
	CHECK(exceptionCaught);
}

class SomeException : public std::exception {
private:
	const char *text;
public:
	SomeException(const char *txt) : text(txt) {}
	const char *what() const throw() { return text; }
};

TEST (checkClassTypeExceptionWithContent)
{
	const char *sText = "someText";
	MockRepository mocks;
	IE *iamock = mocks.Mock<IE>();
	mocks.ExpectCall(iamock, IE::f).Throw(SomeException(sText));
	bool exceptionCaught = false;
	try 
	{
		iamock->f();
	}
	catch(SomeException &a)
	{
		CHECK(a.what() == sText);
		exceptionCaught = true;
	}
	CHECK(exceptionCaught);
}

TEST(checkMockRepoVerifyDoesNotThrowDuringException)
{
	bool exceptionCaught = false;
	try
	{
		MockRepository mocks;
		IE *iamock = mocks.Mock<IE>();
		mocks.ExpectCall(iamock, IE::f);
	}
	catch (HippoMocks::CallMissingException &)
	{
		exceptionCaught = true;
	}
	CHECK(exceptionCaught);
	exceptionCaught = false;
	try
	{
		MockRepository mocks;
		IE *iamock = mocks.Mock<IE>();
		mocks.ExpectCall(iamock, IE::f);
		throw 42;
	}
	catch (int)
	{
		exceptionCaught = true;
	}
	CHECK(exceptionCaught);
}
#endif