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
21 #include "common/RawspeedException.h" // for RawspeedException (ptr...
22 #include "decoders/RawDecoderException.h" // for RawDecoderException (p...
23 #include "io/FileIOException.h" // for FileIOException (ptr o...
24 #include "io/IOException.h" // for IOException (ptr only)
25 #include "metadata/CameraMetadataException.h" // for CameraMetadataExceptio...
26 #include "parsers/CiffParserException.h" // for CiffParserException (p...
27 #include "parsers/FiffParserException.h" // for FiffParserException (p...
28 #include "parsers/RawParserException.h" // for RawParserException (pt...
29 #include "parsers/TiffParserException.h" // for TiffParserException (p...
30 #include <exception> // for exception
31 #include <gmock/gmock.h> // for HasSubstr, ASSERT_THAT
32 #include <gtest/gtest.h> // for Message, TestPartResult
33 #include <memory> // for unique_ptr
34 #include <stdexcept> // for runtime_error
35
36 using rawspeed::CameraMetadataException;
37 using rawspeed::CiffParserException;
38 using rawspeed::FiffParserException;
39 using rawspeed::FileIOException;
40 using rawspeed::IOException;
41 using rawspeed::RawDecoderException;
42 using rawspeed::RawParserException;
43 using rawspeed::RawspeedException;
44 using rawspeed::TiffParserException;
45 using std::unique_ptr;
46
47 namespace rawspeed_test {
48
49 static const char* msg = "my very Smart error Message #1 !";
50
51 #define FMT "%s"
52
MetaHelper(const char * str)53 template <typename T> static void* MetaHelper(const char* str) {
54 ADD_FAILURE() << "non-specialzer was called";
55 return nullptr;
56 }
57
MetaHelper(const char * str)58 template <> void* MetaHelper<RawspeedException>(const char* str) {
59 ThrowRSE(FMT, str);
60 }
61
MetaHelper(const char * str)62 template <> void* MetaHelper<CameraMetadataException>(const char* str) {
63 ThrowCME(FMT, str);
64 }
65
MetaHelper(const char * str)66 template <> void* MetaHelper<CiffParserException>(const char* str) {
67 ThrowCPE(FMT, str);
68 }
69
MetaHelper(const char * str)70 template <> void* MetaHelper<FileIOException>(const char* str) {
71 ThrowFIE(FMT, str);
72 }
73
MetaHelper(const char * str)74 template <> void* MetaHelper<IOException>(const char* str) {
75 ThrowIOE(FMT, str);
76 }
77
MetaHelper(const char * str)78 template <> void* MetaHelper<RawDecoderException>(const char* str) {
79 ThrowRDE(FMT, str);
80 }
81
MetaHelper(const char * str)82 template <> void* MetaHelper<RawParserException>(const char* str) {
83 ThrowRPE(FMT, str);
84 }
85
MetaHelper(const char * str)86 template <> void* MetaHelper<TiffParserException>(const char* str) {
87 ThrowTPE(FMT, str);
88 }
89
MetaHelper(const char * str)90 template <> void* MetaHelper<FiffParserException>(const char* str) {
91 ThrowFPE(FMT, str);
92 }
93
94 template <class T> class ExceptionsTest : public testing::Test {};
95
96 using Classes =
97 testing::Types<RawspeedException, CameraMetadataException,
98 CiffParserException, FileIOException, IOException,
99 RawDecoderException, TiffParserException,
100 FiffParserException, RawParserException>;
101
102 TYPED_TEST_CASE(ExceptionsTest, Classes);
103
TYPED_TEST(ExceptionsTest,Constructor)104 TYPED_TEST(ExceptionsTest, Constructor) {
105 ASSERT_NO_THROW({ TypeParam Exception(msg); });
106 ASSERT_NO_THROW({ unique_ptr<TypeParam> Exception(new TypeParam(msg)); });
107 }
108
TYPED_TEST(ExceptionsTest,AssignmentConstructor)109 TYPED_TEST(ExceptionsTest, AssignmentConstructor) {
110 ASSERT_NO_THROW({
111 const TypeParam ExceptionOne(msg);
112 TypeParam ExceptionTwo(ExceptionOne); // NOLINT trying to test the copy
113 });
114
115 ASSERT_NO_THROW({
116 const unique_ptr<const TypeParam> ExceptionOne(new TypeParam(msg));
117 unique_ptr<TypeParam> ExceptionTwo(new TypeParam(*ExceptionOne));
118 });
119
120 ASSERT_NO_THROW({
121 const TypeParam ExceptionOne(msg);
122 unique_ptr<TypeParam> ExceptionTwo(new TypeParam(ExceptionOne));
123 });
124
125 ASSERT_NO_THROW({
126 const unique_ptr<const TypeParam> ExceptionOne(new TypeParam(msg));
127 TypeParam ExceptionTwo(*ExceptionOne);
128 });
129 }
130
TYPED_TEST(ExceptionsTest,Throw)131 TYPED_TEST(ExceptionsTest, Throw) {
132 ASSERT_ANY_THROW(throw TypeParam(msg));
133 EXPECT_THROW(throw TypeParam(msg), TypeParam);
134 EXPECT_THROW(throw TypeParam(msg), RawspeedException);
135 EXPECT_THROW(throw TypeParam(msg), std::runtime_error);
136
137 ASSERT_ANY_THROW({
138 std::unique_ptr<TypeParam> Exception(new TypeParam(msg));
139 throw *Exception.get();
140 });
141 EXPECT_THROW(
142 {
143 std::unique_ptr<TypeParam> Exception(new TypeParam(msg));
144 throw *Exception.get();
145 },
146 std::runtime_error);
147 EXPECT_THROW(
148 {
149 std::unique_ptr<TypeParam> Exception(new TypeParam(msg));
150 throw *Exception.get();
151 },
152 RawspeedException);
153 EXPECT_THROW(
154 {
155 std::unique_ptr<TypeParam> Exception(new TypeParam(msg));
156 throw *Exception.get();
157 },
158 TypeParam);
159 }
160
TYPED_TEST(ExceptionsTest,ThrowMessage)161 TYPED_TEST(ExceptionsTest, ThrowMessage) {
162 try {
163 throw TypeParam(msg);
164 } catch (std::exception& ex) {
165 ASSERT_THAT(ex.what(), testing::HasSubstr(msg));
166 }
167
168 try {
169 std::unique_ptr<TypeParam> Exception(new TypeParam(msg));
170 throw *Exception.get();
171 } catch (std::exception& ex) {
172 ASSERT_THAT(ex.what(), testing::HasSubstr(msg));
173 }
174
175 try {
176 std::unique_ptr<TypeParam> ExceptionOne(new TypeParam(msg));
177 const std::unique_ptr<const TypeParam> ExceptionTwo(new TypeParam(msg));
178 throw *ExceptionTwo.get();
179 } catch (std::exception& ex) {
180 ASSERT_THAT(ex.what(), testing::HasSubstr(msg));
181 }
182
183 try {
184 const TypeParam ExceptionOne(msg);
185 std::unique_ptr<TypeParam> ExceptionTwo(new TypeParam(msg));
186 throw *ExceptionTwo.get();
187 } catch (std::exception& ex) {
188 ASSERT_THAT(ex.what(), testing::HasSubstr(msg));
189 }
190 }
191
TYPED_TEST(ExceptionsTest,ThrowHelperTest)192 TYPED_TEST(ExceptionsTest, ThrowHelperTest) {
193 ASSERT_ANY_THROW(MetaHelper<TypeParam>(msg));
194 EXPECT_THROW(MetaHelper<TypeParam>(msg), std::runtime_error);
195 EXPECT_THROW(MetaHelper<TypeParam>(msg), RawspeedException);
196 EXPECT_THROW(MetaHelper<TypeParam>(msg), TypeParam);
197 }
198
TYPED_TEST(ExceptionsTest,ThrowHelperTestMessage)199 TYPED_TEST(ExceptionsTest, ThrowHelperTestMessage) {
200 try {
201 MetaHelper<TypeParam>(msg);
202 } catch (std::exception& ex) {
203 ASSERT_THAT(ex.what(), testing::HasSubstr(msg));
204 }
205 }
206
207 } // namespace rawspeed_test
208