Add Option tests.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Tue, 5 May 2015 22:34:22 +0000 (00:34 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Tue, 5 May 2015 22:34:22 +0000 (00:34 +0200)
include/option.h
tests/Makefile.am
tests/option/should_throw_exception_on_missing_value.cpp [new file with mode: 0644]

index 115676665388a3fb88f42ce065bde8ae85bf4b39..426942bf41cb7cd4616af64ab655a99cee41a8d3 100644 (file)
@@ -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
 
index 85a8a466e5270d2c4f76fd5371731c51adf99972..11c3c1800278de23e0a90b806be2c9374ba6770f 100644 (file)
@@ -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 (file)
index 0000000..f872ed1
--- /dev/null
@@ -0,0 +1,29 @@
+#include <iostream>
+#include <vector>
+
+#include "option.h"
+
+#define NAME "test"
+#define BAD_OPTION "test="
+
+using namespace std;
+using namespace command;
+
+void function(int) { }
+
+int main() {
+    Option<int> 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;
+}