RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
ExceptionsTest.cpp
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2016-2017 Roman Lebedev
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
23#include "io/FileIOException.h"
24#include "io/IOException.h"
30#include <exception>
31#include <memory>
32#include <stdexcept>
33#include <gmock/gmock.h>
34#include <gtest/gtest.h>
35
45using std::unique_ptr;
46
47namespace rawspeed_test {
48
49namespace {
50
51const char* msg = "my very Smart error Message #1 !";
52
53#define FMT "%s"
54
55template <typename T> void* MetaHelper(const char* str) {
56 ADD_FAILURE() << "non-specialzer was called";
57 return nullptr;
58}
59
60template <> void* MetaHelper<RawspeedException>(const char* str) {
61 ThrowRSE(FMT, str);
62}
63
64template <> void* MetaHelper<CameraMetadataException>(const char* str) {
65 ThrowCME(FMT, str);
66}
67
68template <> void* MetaHelper<CiffParserException>(const char* str) {
69 ThrowCPE(FMT, str);
70}
71
72template <> void* MetaHelper<FileIOException>(const char* str) {
73 ThrowFIE(FMT, str);
74}
75
76template <> void* MetaHelper<IOException>(const char* str) {
77 ThrowIOE(FMT, str);
78}
79
80template <> void* MetaHelper<RawDecoderException>(const char* str) {
81 ThrowRDE(FMT, str);
82}
83
84template <> void* MetaHelper<RawParserException>(const char* str) {
85 ThrowRPE(FMT, str);
86}
87
88template <> void* MetaHelper<TiffParserException>(const char* str) {
89 ThrowTPE(FMT, str);
90}
91
92template <> void* MetaHelper<FiffParserException>(const char* str) {
93 ThrowFPE(FMT, str);
94}
95
96template <class T> class ExceptionsTest : public testing::Test {};
97
98using Classes =
103
105
107 ASSERT_NO_THROW({ TypeParam Exception(msg); });
108 ASSERT_NO_THROW({ unique_ptr<TypeParam> Exception(new TypeParam(msg)); });
109}
110
111TYPED_TEST(ExceptionsTest, AssignmentConstructor) {
112 ASSERT_NO_THROW({
113 const TypeParam ExceptionOne(msg);
114 TypeParam ExceptionTwo(ExceptionOne);
115 });
116
117 ASSERT_NO_THROW({
118 const unique_ptr<const TypeParam> ExceptionOne(new TypeParam(msg));
119 unique_ptr<TypeParam> ExceptionTwo(new TypeParam(*ExceptionOne));
120 });
121
122 ASSERT_NO_THROW({
123 const TypeParam ExceptionOne(msg);
124 unique_ptr<TypeParam> ExceptionTwo(new TypeParam(ExceptionOne));
125 });
126
127 ASSERT_NO_THROW({
128 const unique_ptr<const TypeParam> ExceptionOne(new TypeParam(msg));
129 TypeParam ExceptionTwo(*ExceptionOne);
130 });
131}
132
134 ASSERT_ANY_THROW(throw TypeParam(msg));
135 EXPECT_THROW(throw TypeParam(msg), TypeParam);
136 EXPECT_THROW(throw TypeParam(msg), RawspeedException);
137 EXPECT_THROW(throw TypeParam(msg), std::runtime_error);
138
139 ASSERT_ANY_THROW({
140 std::unique_ptr<TypeParam> Exception(new TypeParam(msg));
141 throw *Exception.get();
142 });
143 EXPECT_THROW(
144 {
145 std::unique_ptr<TypeParam> Exception(new TypeParam(msg));
146 throw *Exception.get();
147 },
148 std::runtime_error);
149 EXPECT_THROW(
150 {
151 std::unique_ptr<TypeParam> Exception(new TypeParam(msg));
152 throw *Exception.get();
153 },
155 EXPECT_THROW(
156 {
157 std::unique_ptr<TypeParam> Exception(new TypeParam(msg));
158 throw *Exception.get();
159 },
160 TypeParam);
161}
162
164 try {
165 throw TypeParam(msg);
166 } catch (std::exception& ex) {
167 ASSERT_THAT(ex.what(), testing::HasSubstr(msg));
168 }
169
170 try {
171 std::unique_ptr<TypeParam> Exception(new TypeParam(msg));
172 throw *Exception.get();
173 } catch (std::exception& ex) {
174 ASSERT_THAT(ex.what(), testing::HasSubstr(msg));
175 }
176
177 try {
178 std::unique_ptr<TypeParam> ExceptionOne(new TypeParam(msg));
179 const std::unique_ptr<const TypeParam> ExceptionTwo(new TypeParam(msg));
180 throw *ExceptionTwo.get();
181 } catch (std::exception& ex) {
182 ASSERT_THAT(ex.what(), testing::HasSubstr(msg));
183 }
184
185 try {
186 const TypeParam ExceptionOne(msg);
187 std::unique_ptr<TypeParam> ExceptionTwo(new TypeParam(msg));
188 throw *ExceptionTwo.get();
189 } catch (std::exception& ex) {
190 ASSERT_THAT(ex.what(), testing::HasSubstr(msg));
191 }
192}
193
194TYPED_TEST(ExceptionsTest, ThrowHelperTest) {
195 ASSERT_ANY_THROW(MetaHelper<TypeParam>(msg));
196 EXPECT_THROW(MetaHelper<TypeParam>(msg), std::runtime_error);
198 EXPECT_THROW(MetaHelper<TypeParam>(msg), TypeParam);
199}
200
201TYPED_TEST(ExceptionsTest, ThrowHelperTestMessage) {
202 try {
204 } catch (std::exception& ex) {
205 ASSERT_THAT(ex.what(), testing::HasSubstr(msg));
206 }
207}
208
209} // namespace
210
211} // namespace rawspeed_test
#define ThrowCME(...)
#define ThrowCPE(...)
#define FMT
#define ThrowFPE(...)
#define ThrowFIE(...)
#define ThrowIOE(...)
Definition IOException.h:37
#define ThrowRDE(...)
#define ThrowRPE(...)
#define ThrowRSE(...)
#define ThrowTPE(...)
testing::Types< RawspeedException, CameraMetadataException, CiffParserException, FileIOException, IOException, RawDecoderException, TiffParserException, FiffParserException, RawParserException > Classes