From 43d4552e59f8870279de2b6b3a62ce16c72bb872 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Sun, 3 May 2015 12:44:03 +0200 Subject: [PATCH] Added Option tests. --- tests/Makefile.am | 7 +++- tests/option/handles_boolean_value.cpp | 56 ++++++++++++++++++++++++++ tests/option/handles_string_value.cpp | 43 ++++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 tests/option/handles_boolean_value.cpp create mode 100644 tests/option/handles_string_value.cpp diff --git a/tests/Makefile.am b/tests/Makefile.am index c08c2cc..e17ac34 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -10,7 +10,9 @@ TESTS = \ argument_handles_negative_int_value.test \ argument_handles_float_value.test \ argument_handles_negative_float_value.test \ - argument_handles_boolean_value.test + argument_handles_boolean_value.test \ + option_handles_string_value.test \ + option_handles_boolean_value.test noinst_PROGRAMS = $(TESTS) @@ -29,3 +31,6 @@ argument_handles_negative_int_value_test_SOURCES = argument/handles_negative_in argument_handles_float_value_test_SOURCES = argument/handles_float_value.cpp argument_handles_negative_float_value_test_SOURCES = argument/handles_negative_float_value.cpp argument_handles_boolean_value_test_SOURCES = argument/handles_boolean_value.cpp + +option_handles_string_value_test_SOURCES = option/handles_string_value.cpp +option_handles_boolean_value_test_SOURCES = option/handles_boolean_value.cpp diff --git a/tests/option/handles_boolean_value.cpp b/tests/option/handles_boolean_value.cpp new file mode 100644 index 0000000..473a6b7 --- /dev/null +++ b/tests/option/handles_boolean_value.cpp @@ -0,0 +1,56 @@ +#include + +#include "option.h" + +using namespace std; +using namespace command; + +#define NAME "test" +#define FALSE "0" +#define TRUE "1" + +#define OPTION1 NAME "=" FALSE +#define OPTION2 NAME "=" TRUE + +typedef bool OptionType; + +OptionType test; + +void function(OptionType value) { + test = value; +} + +int main() { + Option option(NAME, "Option with boolean value", function); + + if (option.understand(OPTION1)) { + option.handle(); + } + else { + cout << option.describe() << " do not understand " << FALSE << " values\n"; + return 1; + } + + if (test == (bool)std::stoi(FALSE)) { + cout << option.describe() << " handles " << FALSE << " values\n"; + } + + Option option2(NAME, "Option with boolean value", function); + + if (option2.understand(OPTION2)) { + option2.handle(); + } + else { + cout << option.describe() << " do not understand " << TRUE << " values\n"; + return 1; + } + + if (test == (bool)std::stoi(TRUE)) { + cout << option.describe() << " handles " << TRUE << " values\n"; + return 0; + } + + cout << "Option class do not handle boolean values\n" << test; + + return 1; +} diff --git a/tests/option/handles_string_value.cpp b/tests/option/handles_string_value.cpp new file mode 100644 index 0000000..574b5c5 --- /dev/null +++ b/tests/option/handles_string_value.cpp @@ -0,0 +1,43 @@ +#include +#include + +#include "option.h" + +using namespace std; +using namespace command; + +#define NAME "test" +#define VALUE "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + +#define OPTION NAME"="VALUE + +typedef std::string OptionType; + +OptionType test; + +void function(OptionType value) { + test = value; +} + +int main() { + Option option(NAME, "Option with string value", function); + + if (option.understand(OPTION)) { + option.handle(); + } + else { + cout << "Option class do not understand string values\n"; + return 1; + } + + int cmp = strcmp(test.c_str(), VALUE); + + if (cmp == 0) { + cout << "Option class handles string values\n"; + return 0; + } + + cout << "Option class do not handle string values\n"; + + return 1; +} -- 2.30.2