From: Rafał Długołęcki Date: Tue, 5 May 2015 22:34:22 +0000 (+0200) Subject: Add Option tests. X-Git-Tag: v0.2~16 X-Git-Url: https://git.dlugolecki.net.pl/?p=command.git;a=commitdiff_plain;h=e8dc8567d9b42bc67298e8b9c3249848a74926a2 Add Option tests. --- 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; +}