-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathexceptions_tests.cpp
More file actions
105 lines (84 loc) · 2.98 KB
/
Copy pathexceptions_tests.cpp
File metadata and controls
105 lines (84 loc) · 2.98 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
97
98
99
100
101
102
103
104
105
// Copyright Joakim Karlsson & Kim Gräsman 2010-2013.
// 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)
#include <stdexcept>
#include "tests.h"
using namespace snowhouse;
struct ClassWithExceptions
{
int LogicError()
{
throw std::logic_error("not logical!");
}
double RangeError()
{
throw std::range_error("range error!");
}
void NoError()
{
}
};
struct ExpectedException : public std::exception
{
const char* what() const noexcept override
{
return "Description of the exception we expected";
}
};
void ExceptionTests()
{
ClassWithExceptions objectUnderTest;
describe("Exceptions");
it("detects exceptions");
{
AssertThrows(std::exception, objectUnderTest.LogicError());
}
it("asserts on LastException()");
{
AssertThrows(std::logic_error, objectUnderTest.LogicError());
AssertThat(LastException<std::logic_error>().what(), Contains("not logical!"));
}
it("detects when wrong exception is thrown");
{
AssertTestFails(AssertThrows(std::logic_error, objectUnderTest.RangeError()), "Wrong exception");
}
it("prints expected exception type when wrong exception is thrown");
{
AssertTestFails(AssertThrows(std::logic_error, objectUnderTest.RangeError()), "Expected std::logic_error");
}
it("has several exception assertions in same spec");
{
AssertThrows(std::logic_error, objectUnderTest.LogicError());
AssertThat(LastException<std::logic_error>().what(), Contains("not logical!"));
AssertThrows(std::range_error, objectUnderTest.RangeError());
AssertThat(LastException<std::range_error>().what(), Contains("range error!"));
}
it("has several exception assertion for the same exception in same spec");
{
AssertThrows(std::logic_error, objectUnderTest.LogicError());
AssertThat(LastException<std::logic_error>().what(), Contains("not logical!"));
AssertThrows(std::logic_error, objectUnderTest.LogicError());
AssertThat(LastException<std::logic_error>().what(), Contains("not logical!"));
}
it("detects when no exception is thrown");
{
AssertTestFails(AssertThrows(std::logic_error, objectUnderTest.NoError()), "No exception");
}
it("prints expected exception when no exception is thrown");
{
AssertTestFails(AssertThrows(std::logic_error, objectUnderTest.NoError()), "Expected std::logic_error");
}
it("destroys exceptions when out-of-scope");
{
{
AssertThrows(std::logic_error, objectUnderTest.LogicError());
}
AssertThrows(AssertionException, LastException<std::logic_error>());
AssertThat(LastException<AssertionException>().what(), Contains("No exception was stored"));
}
it("prints description of unwanted exception");
{
AssertTestFails(AssertThrows(ExpectedException, objectUnderTest.LogicError()), "Expected ExpectedException. Wrong exception was thrown. Description of unwanted exception: not logical!");
}
}