From e8dc8567d9b42bc67298e8b9c3249848a74926a2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Wed, 6 May 2015 00:34:22 +0200 Subject: [PATCH] Add Option tests. --- include/option.h | 2 +- tests/Makefile.am | 4 ++- ...hould_throw_exception_on_missing_value.cpp | 29 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/option/should_throw_exception_on_missing_value.cpp diff --git a/include/option.h b/include/option.h index 1156766..426942b 100644 --- a/include/option.h +++ b/include/option.h @@ -99,7 +99,7 @@ namespace command { std::stringstream ss; ss << argv.substr(pos + 1); - ss >> value;// memory leak? when is uncommented, and exception is + ss >> value;// memory leak? when uncommented and exception is // thrown, valgrind shows e.g.: // possibly lost: 380 bytes in 7 blocks diff --git a/tests/Makefile.am b/tests/Makefile.am index 85a8a46..11c3c18 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -18,7 +18,8 @@ TESTS = \ option_handles_negative_float_value.test \ option_handles_boolean_value.test \ option_handles_void_value.test \ - option_should_match_exact_name.test + option_should_match_exact_name.test \ + option_should_throw_exception_on_missing_value.test noinst_PROGRAMS = $(TESTS) @@ -46,3 +47,4 @@ option_handles_negative_float_value_test_SOURCES = option/handles_negative_floa option_handles_boolean_value_test_SOURCES = option/handles_boolean_value.cpp option_handles_void_value_test_SOURCES = option/handles_void_value.cpp option_should_match_exact_name_test_SOURCES = option/should_match_exact_name.cpp +option_should_throw_exception_on_missing_value_test_SOURCES = option/should_throw_exception_on_missing_value.cpp diff --git a/tests/option/should_throw_exception_on_missing_value.cpp b/tests/option/should_throw_exception_on_missing_value.cpp new file mode 100644 index 0000000..f872ed1 --- /dev/null +++ b/tests/option/should_throw_exception_on_missing_value.cpp @@ -0,0 +1,29 @@ +#include +#include + +#include "option.h" + +#define NAME "test" +#define BAD_OPTION "test=" + +using namespace std; +using namespace command; + +void function(int) { } + +int main() { + Option option(NAME, "Option should throw exception on missing value", function); + + try { + if (option.understand(BAD_OPTION)) { + std::cout << option.describe() << " but instead it understanded it\n"; + } + } + catch(std::invalid_argument e) { + std::cout << option.describe() << " and it was thrown correctly\n"; + return 0; + } + + std::cout << option.describe() << " but nothing happened\n"; + return 1; +} -- 2.30.2