Skip to content

Commit

Permalink
Merge pull request #130 from cschreib/location
Browse files Browse the repository at this point in the history
Fix narrowing cast when creating source location
  • Loading branch information
cschreib authored Oct 5, 2023
2 parents 87260ed + 4423069 commit c0a910c
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 89 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14)

project(snitch LANGUAGES CXX VERSION 1.2.2)
project(snitch LANGUAGES CXX VERSION 1.2.3)

# Maximum lengths.
set(SNITCH_MAX_TEST_CASES 5000 CACHE STRING "Maximum number of test cases in a test application.")
Expand Down
10 changes: 5 additions & 5 deletions include/snitch/snitch_macros_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,37 @@
do { \
auto& SNITCH_CURRENT_TEST = snitch::impl::get_current_test(); \
SNITCH_CURRENT_TEST.reg.report_assertion( \
true, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, (MESSAGE)); \
true, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, (MESSAGE)); \
} while (0)

#define SNITCH_FAIL(MESSAGE) \
do { \
auto& SNITCH_CURRENT_TEST = snitch::impl::get_current_test(); \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, (MESSAGE)); \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, (MESSAGE)); \
SNITCH_TESTING_ABORT; \
} while (0)

#define SNITCH_FAIL_CHECK(MESSAGE) \
do { \
auto& SNITCH_CURRENT_TEST = snitch::impl::get_current_test(); \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, (MESSAGE)); \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, (MESSAGE)); \
} while (0)

#define SNITCH_SKIP(MESSAGE) \
do { \
auto& SNITCH_CURRENT_TEST = snitch::impl::get_current_test(); \
SNITCH_CURRENT_TEST.reg.report_skipped( \
SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, (MESSAGE)); \
SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, (MESSAGE)); \
SNITCH_TESTING_ABORT; \
} while (0)

#define SNITCH_SKIP_CHECK(MESSAGE) \
do { \
auto& SNITCH_CURRENT_TEST = snitch::impl::get_current_test(); \
SNITCH_CURRENT_TEST.reg.report_skipped( \
SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, (MESSAGE)); \
SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, (MESSAGE)); \
} while (0)

#define SNITCH_REQUIRE_THAT_IMPL(CHECK, MAYBE_ABORT, EXPR, ...) \
Expand Down
2 changes: 1 addition & 1 deletion include/snitch/snitch_macros_check_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#define SNITCH_REPORT_EXPRESSION(MAYBE_ABORT) \
SNITCH_CURRENT_TEST.reg.report_assertion( \
SNITCH_CURRENT_EXPRESSION.success, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
SNITCH_CURRENT_EXPRESSION.success, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
SNITCH_CURRENT_EXPRESSION); \
if (!SNITCH_CURRENT_EXPRESSION.success) { \
MAYBE_ABORT; \
Expand Down
24 changes: 12 additions & 12 deletions include/snitch/snitch_macros_exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@
try { \
static_cast<void>(EXPRESSION); \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#__VA_ARGS__ " expected but no exception thrown"); \
MAYBE_ABORT; \
} catch (const __VA_ARGS__&) { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
true, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
true, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#__VA_ARGS__ " was thrown as expected"); \
} catch (...) { \
try { \
throw; \
} catch (const std::exception& e) { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#__VA_ARGS__ " expected but other std::exception thrown; message: ", \
e.what()); \
MAYBE_ABORT; \
} catch (...) { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#__VA_ARGS__ " expected but other unknown exception thrown"); \
MAYBE_ABORT; \
} \
Expand All @@ -50,21 +50,21 @@
try { \
static_cast<void>(EXPRESSION); \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#EXCEPTION " expected but no exception thrown"); \
MAYBE_ABORT; \
} catch (const EXCEPTION& e) { \
auto&& SNITCH_TEMP_MATCHER = __VA_ARGS__; \
if (!SNITCH_TEMP_MATCHER.match(e)) { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
"could not match caught " #EXCEPTION " with expected content: ", \
SNITCH_TEMP_MATCHER.describe_match( \
e, snitch::matchers::match_status::failed)); \
MAYBE_ABORT; \
} else { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
true, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
true, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
"caught " #EXCEPTION " matched expected content: ", \
SNITCH_TEMP_MATCHER.describe_match( \
e, snitch::matchers::match_status::matched)); \
Expand All @@ -74,13 +74,13 @@
throw; \
} catch (const std::exception& e) { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#EXCEPTION " expected but other std::exception thrown; message: ", \
e.what()); \
MAYBE_ABORT; \
} catch (...) { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#EXCEPTION " expected but other unknown exception thrown"); \
MAYBE_ABORT; \
} \
Expand All @@ -98,20 +98,20 @@
try { \
static_cast<void>(__VA_ARGS__); \
SNITCH_CURRENT_TEST.reg.report_assertion( \
true, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
true, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#__VA_ARGS__ " did not throw"); \
} catch (...) { \
try { \
throw; \
} catch (const std::exception& e) { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
"expected " #__VA_ARGS__ \
" not to throw but it threw a std::exception; message: ", \
e.what()); \
} catch (...) { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, {__FILE__, __LINE__}, \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
"expected " #__VA_ARGS__ \
" not to throw but it threw an unknown exception"); \
} \
Expand Down
2 changes: 1 addition & 1 deletion include/snitch/snitch_macros_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#define SNITCH_SECTION(...) \
if (snitch::impl::section_entry_checker SNITCH_MACRO_CONCAT(section_id_, __COUNTER__){ \
{{__VA_ARGS__}, {__FILE__, __LINE__}}, snitch::impl::get_current_test()})
{{__VA_ARGS__}, SNITCH_CURRENT_LOCATION}, snitch::impl::get_current_test()})

#define SNITCH_CAPTURE(...) \
auto SNITCH_MACRO_CONCAT(capture_id_, __COUNTER__) = \
Expand Down
12 changes: 6 additions & 6 deletions include/snitch/snitch_macros_test_case.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define SNITCH_TEST_CASE_IMPL(ID, ...) \
static void ID(); \
static const char* SNITCH_MACRO_CONCAT(test_id_, __COUNTER__) [[maybe_unused]] = \
snitch::tests.add({__VA_ARGS__}, {__FILE__, __LINE__}, &ID); \
snitch::tests.add({__VA_ARGS__}, SNITCH_CURRENT_LOCATION, &ID); \
void ID()

#define SNITCH_TEST_CASE(...) \
Expand All @@ -19,7 +19,7 @@
static void ID(); \
static const char* SNITCH_MACRO_CONCAT(test_id_, __COUNTER__) [[maybe_unused]] = \
snitch::tests.add_with_type_list<TYPES>( \
{NAME, TAGS}, {__FILE__, __LINE__}, []<typename TestType>() { ID<TestType>(); }); \
{NAME, TAGS}, SNITCH_CURRENT_LOCATION, []<typename TestType>() { ID<TestType>(); }); \
template<typename TestType> \
void ID()

Expand All @@ -32,7 +32,7 @@
static void ID(); \
static const char* SNITCH_MACRO_CONCAT(test_id_, __COUNTER__) [[maybe_unused]] = \
snitch::tests.add_with_types<__VA_ARGS__>( \
{NAME, TAGS}, {__FILE__, __LINE__}, []<typename TestType>() { ID<TestType>(); }); \
{NAME, TAGS}, SNITCH_CURRENT_LOCATION, []<typename TestType>() { ID<TestType>(); }); \
template<typename TestType> \
void ID()

Expand All @@ -48,7 +48,7 @@
} \
static const char* SNITCH_MACRO_CONCAT(test_id_, __COUNTER__) [[maybe_unused]] = \
snitch::tests.add_fixture( \
{#FIXTURE, __VA_ARGS__}, {__FILE__, __LINE__}, []() { ID{}.test_fun(); }); \
{#FIXTURE, __VA_ARGS__}, SNITCH_CURRENT_LOCATION, []() { ID{}.test_fun(); }); \
void ID::test_fun()

#define SNITCH_TEST_CASE_METHOD(FIXTURE, ...) \
Expand All @@ -64,7 +64,7 @@
} \
static const char* SNITCH_MACRO_CONCAT(test_id_, __COUNTER__) [[maybe_unused]] = \
snitch::tests.add_fixture_with_type_list<TYPES>( \
{#FIXTURE, NAME, TAGS}, {__FILE__, __LINE__}, \
{#FIXTURE, NAME, TAGS}, SNITCH_CURRENT_LOCATION, \
[]() < typename TestType > { ID<TestType>{}.test_fun(); }); \
template<typename TestType> \
void ID<TestType>::test_fun()
Expand All @@ -82,7 +82,7 @@
} \
static const char* SNITCH_MACRO_CONCAT(test_id_, __COUNTER__) [[maybe_unused]] = \
snitch::tests.add_fixture_with_types<__VA_ARGS__>( \
{#FIXTURE, NAME, TAGS}, {__FILE__, __LINE__}, \
{#FIXTURE, NAME, TAGS}, SNITCH_CURRENT_LOCATION, \
[]() < typename TestType > { ID<TestType>{}.test_fun(); }); \
template<typename TestType> \
void ID<TestType>::test_fun()
Expand Down
3 changes: 3 additions & 0 deletions include/snitch/snitch_macros_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
#define SNITCH_CONCAT_IMPL(x, y) x##y
#define SNITCH_MACRO_CONCAT(x, y) SNITCH_CONCAT_IMPL(x, y)

#define SNITCH_CURRENT_LOCATION \
snitch::source_location { std::string_view{__FILE__}, static_cast<std::size_t>(__LINE__) }

#endif
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project('snitch', 'cpp',
default_options: ['cpp_std=c++20', 'default_library=static'],
version: '1.2.2'
version: '1.2.3'
)

include_dirs = include_directories('.', 'include')
Expand Down
4 changes: 2 additions & 2 deletions tests/approval_tests/reporter_catch2_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ using snitch::matchers::contains_substring;
TEST_CASE("teamcity reporter", "[reporters]") {
mock_framework framework;
register_tests_for_reporters(framework.registry);
framework.registry.add({"test escape <>&\"'"}, {__FILE__, __LINE__}, [] {
framework.registry.add({"test escape <>&\"'"}, SNITCH_CURRENT_LOCATION, [] {
SNITCH_FAIL("escape <>&\"' in messages");
});
framework.registry.add({"test escape very long"}, {__FILE__, __LINE__}, [] {
framework.registry.add({"test escape very long"}, SNITCH_CURRENT_LOCATION, [] {
SNITCH_FAIL(std::string(2 * snitch::max_message_length, '&'));
});

Expand Down
4 changes: 2 additions & 2 deletions tests/approval_tests/reporter_teamcity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ using snitch::matchers::contains_substring;
TEST_CASE("teamcity reporter", "[reporters]") {
mock_framework framework;
register_tests_for_reporters(framework.registry);
framework.registry.add({"test escape |'\n\r[]"}, {__FILE__, __LINE__}, [] {
framework.registry.add({"test escape |'\n\r[]"}, SNITCH_CURRENT_LOCATION, [] {
SNITCH_FAIL("escape | message || | '\n\r[]");
});
framework.registry.add({"test escape very long"}, {__FILE__, __LINE__}, [] {
framework.registry.add({"test escape very long"}, SNITCH_CURRENT_LOCATION, [] {
SNITCH_FAIL(std::string(2 * snitch::max_message_length, '|'));
});

Expand Down
Loading

0 comments on commit c0a910c

Please sign in to comment.