Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
aleph_exceptions_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
37#include <gtest/gtest.h>
38
39#include <stdexcept>
40#include <string>
41
42// aleph-exceptions.H is deprecated, but supported until 8.0.0; this test is
43// its coverage. Tests/aleph_exceptions_deprecation_check.cmake checks where
44// it warns.
45#if defined(_MSC_VER) && !defined(__clang__)
46# pragma warning(push)
47# pragma warning(disable : 4996)
48#else
49# pragma GCC diagnostic push
50# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
51#endif
52
53#include <aleph-exceptions.H>
54
55namespace
56{
57 DEFINE_ALEPH_EXCEPTION(MyAlephError, "my category")
58
59 template <class E>
60 void expect_the_exception(const char * name, const std::string & category)
61 {
62 const size_t line = __LINE__ + 3;
63 try
64 {
65 ALEPHTHROW(E, "the message");
66 }
67 catch (const AlephException & e)
68 {
69 EXPECT_NE(dynamic_cast<const E *>(&e), nullptr) << name;
70 EXPECT_EQ(e.line_number, line) << name;
71 EXPECT_EQ(e.file_name, __FILE__) << name;
72 EXPECT_EQ(e.type, "E") << name;
73 EXPECT_EQ(std::string(e.what()),
74 category + "|" + __FILE__ + "|" + std::to_string(line)
75 + "|E|the message") << name;
76 return;
77 }
78 ADD_FAILURE() << name << " was not caught as an AlephException";
79 }
80}
81
82// The category, as DEFINE_ALEPH_EXCEPTION takes it: the text of its argument
83#define EXPECT_THE_EXCEPTION(E, category) \
84 expect_the_exception<E>(#E, #category)
85
86// Each exception of the header is an AlephException, and so a
87// std::runtime_error, whose what() joins its category, where it was thrown,
88// the name that ALEPHTHROW gives it and the message
89TEST(AlephExceptions, EachIsAnAlephExceptionWithItsCategory)
90{
92 "minimum value is greater than maximum value");
93 EXPECT_THE_EXCEPTION(InvalidCsvHeader, "Invalid csv header");
94 EXPECT_THE_EXCEPTION(InvalidCsvRow, "Invalid csv row");
95 EXPECT_THE_EXCEPTION(InvalidValue, "Invalid value");
97 EXPECT_THE_EXCEPTION(OutOfRange, "Value is out of allowed range");
98 EXPECT_THE_EXCEPTION(ValueNotFound, "Value not found");
99 EXPECT_THE_EXCEPTION(CommandLineError, "error parsing command line");
100 EXPECT_THE_EXCEPTION(InvariantError, "A bug has been detected. "
101 "Please send a bug report to leandro.r.leon@gmail.com");
102 EXPECT_THE_EXCEPTION(SizeMismatch, "Sequences have different sizes");
103 EXPECT_THE_EXCEPTION(InvalidConversion, "failure in numeric conversion");
104 EXPECT_THE_EXCEPTION(NameNotFound, "name not found");
105 EXPECT_THE_EXCEPTION(InvalidSize, "invalid size");
106 EXPECT_THE_EXCEPTION(InvalidRange, "invalid range");
107 EXPECT_THE_EXCEPTION(InvalidUnit, "invalid unit");
108 EXPECT_THE_EXCEPTION(DuplicatedOperation, "duplicated operation");
109 EXPECT_THE_EXCEPTION(DuplicatedName, "Duplicated name");
110 EXPECT_THE_EXCEPTION(InvalidRead, "Error while reading input stream");
111 EXPECT_THE_EXCEPTION(FileNotFound, "file not found");
112}
113
114// DEFINE_ALEPH_EXCEPTION still defines an AlephException, which takes as
115// its category the text of its second argument
116TEST(AlephExceptions, DefineAlephExceptionDefinesOne)
117{
118 EXPECT_THE_EXCEPTION(MyAlephError, "my category");
119
120 EXPECT_THROW(ALEPHTHROW(MyAlephError, "m"), std::runtime_error);
121}
122
123#undef EXPECT_THE_EXCEPTION
124
125#if defined(_MSC_VER) && !defined(__clang__)
126# pragma warning(pop)
127#else
128# pragma GCC diagnostic pop
129#endif
Custom exception classes with source location tracking.
#define DEFINE_ALEPH_EXCEPTION(name, category_msg)
Define a new exception.
#define ALEPHTHROW(type, msg)
Macro for throwing an aleph exception.
#define EXPECT_THE_EXCEPTION(E, category)
#define TEST(name)
STL namespace.
Aleph exception class.
const std::string file_name
file name where the exception was thrown
const size_t line_number
Line number of source file where the exception was thrown.
const std::string type