|
5 | 5 | #include <gtest/gtest.h> |
6 | 6 |
|
7 | 7 | #include <sharg/parser.hpp> |
| 8 | +#include <sharg/test/expect_throw_msg.hpp> |
8 | 9 | #include <sharg/test/test_fixture.hpp> |
9 | 10 |
|
10 | 11 | class design_error_test : public sharg::test::test_fixture |
@@ -295,30 +296,23 @@ TEST_F(design_error_test, not_allowed_after_parse) |
295 | 296 | parser.add_option(value, sharg::config{.short_id = 'i'}); |
296 | 297 | EXPECT_NO_THROW(parser.parse()); |
297 | 298 |
|
298 | | - auto check_error = [](auto call_fn, std::string const function_name) |
299 | | - { |
300 | | - try |
301 | | - { |
302 | | - call_fn(); |
303 | | - FAIL(); |
304 | | - } |
305 | | - catch (sharg::design_error const & exception) |
306 | | - { |
307 | | - EXPECT_EQ(function_name + " may only be used before calling parse().", exception.what()); |
308 | | - } |
309 | | - catch (...) |
310 | | - { |
311 | | - FAIL(); |
312 | | - } |
313 | | - }; |
314 | | - |
315 | | - // clang-format off |
316 | | - check_error([&parser, &value]() { parser.add_option(value, sharg::config{.short_id = 'i'}); }, "add_option"); |
317 | | - check_error([&parser, &flag]() { parser.add_flag(flag, sharg::config{.short_id = 'i'}); }, "add_flag"); |
318 | | - check_error([&parser, &value]() { parser.add_positional_option(value, sharg::config{}); }, "add_positional_option"); |
319 | | - check_error([&parser]() { parser.add_section(""); }, "add_section"); |
320 | | - check_error([&parser]() { parser.add_subsection(""); }, "add_subsection"); |
321 | | - check_error([&parser]() { parser.add_line(""); }, "add_line"); |
322 | | - check_error([&parser]() { parser.add_list_item("", ""); }, "add_list_item"); |
323 | | - // clang-format on |
| 299 | + EXPECT_THROW_MSG(parser.add_option(value, sharg::config{.short_id = 'i'}), |
| 300 | + sharg::design_error, |
| 301 | + "add_option may only be used before calling parse()."); |
| 302 | + EXPECT_THROW_MSG(parser.add_flag(flag, sharg::config{.short_id = 'i'}), |
| 303 | + sharg::design_error, |
| 304 | + "add_flag may only be used before calling parse()."); |
| 305 | + EXPECT_THROW_MSG(parser.add_positional_option(value, sharg::config{}), |
| 306 | + sharg::design_error, |
| 307 | + "add_positional_option may only be used before calling parse()."); |
| 308 | + EXPECT_THROW_MSG(parser.add_section(""), |
| 309 | + sharg::design_error, |
| 310 | + "add_section may only be used before calling parse()."); |
| 311 | + EXPECT_THROW_MSG(parser.add_subsection(""), |
| 312 | + sharg::design_error, |
| 313 | + "add_subsection may only be used before calling parse()."); |
| 314 | + EXPECT_THROW_MSG(parser.add_line(""), sharg::design_error, "add_line may only be used before calling parse()."); |
| 315 | + EXPECT_THROW_MSG(parser.add_list_item("", ""), |
| 316 | + sharg::design_error, |
| 317 | + "add_list_item may only be used before calling parse()."); |
324 | 318 | } |
0 commit comments