From da6a36a205d6f45b180d3e79b2567119714c336b Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Mon, 17 May 2021 03:19:47 +0200 Subject: [PATCH] start creating tests for MainSpaceParser --- tests/main.cpp | 3 +- tests/mainargsparser.cpp | 143 +++++++++++++++++++++++++++++++++++++++ tests/mainargsparser.h | 62 +++++++++++++++++ 3 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 tests/mainargsparser.cpp create mode 100644 tests/mainargsparser.h diff --git a/tests/main.cpp b/tests/main.cpp index 3d5cc58..f0db839 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -36,6 +36,7 @@ */ #include "convert.h" +#include "mainargsparser.h" #include @@ -50,7 +51,7 @@ const char * test_msg = nullptr; int main() { pt::pt_convert_tests::make_tests(); - + pt::pt_mainargsparser_tests::make_tests(); if( pt::was_error ) { diff --git a/tests/mainargsparser.cpp b/tests/mainargsparser.cpp new file mode 100644 index 0000000..2876422 --- /dev/null +++ b/tests/mainargsparser.cpp @@ -0,0 +1,143 @@ +/* + * This file is a part of PikoTools + * and is distributed under the (new) BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2021, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name Tomasz Sowa nor the names of contributors to this + * project may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "mainargsparser.h" +#include "test.h" +#include "mainspaceparser/mainspaceparser.h" +#include "utf8/utf8.h" + + +namespace pt +{ + +// remove me in the future (when PT will be changed to pt) +using namespace PT; + + +namespace pt_mainargsparser_tests +{ + +void test_text1() +{ + reset_test_counter("mainargsparser"); + + MainSpaceParser parser; + Space space; + + const char * argv[] = { + "program_name", + "-a", + "-", + "-b", + "-c", + "-d", + "param for d", + "-b", + "--long", + "--foo", + "foo-one", + "foo-two", + "--long-param", + "--bar", + "bar1", + "bar2", + "bar3", + "-x", + "--piggy2=swinka2", + "--swinka3", + "--bar", + "xbar1", + "xbar2", + "xbar3", + }; + + + + + MainSpaceParser::Status status; + + + Space params; + params.add(L"d", 1); + params.add(L"foo", 2); + params.add(L"bar", 3); + params.add(L"piggy", 1); + params.add(L"piggy2", 1); + + size_t len = sizeof(argv) / sizeof(const char *); + + status = parser.parse(len, argv, space, params); + + + if( status != MainSpaceParser::status_ok ) + { + std::wstring & err = parser.get_wrong_option(); + std::string err_str; + WideToUTF8(err, err_str); + std::cout << "blad: " << (int)status << ": " << err_str << std::endl; + } + + test(status, MainSpaceParser::status_ok); + std::cout << space.serialize_to_json_str() << std::endl; + +} + + + + + + + + + + +void make_tests() +{ + test_text1(); +// test_text2(); +// test_text3(); +// test_text4(); +// test_text5(); +} + + +} + + +} + diff --git a/tests/mainargsparser.h b/tests/mainargsparser.h new file mode 100644 index 0000000..c2b20a0 --- /dev/null +++ b/tests/mainargsparser.h @@ -0,0 +1,62 @@ +/* + * This file is a part of PikoTools + * and is distributed under the (new) BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2021, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name Tomasz Sowa nor the names of contributors to this + * project may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef headerfile_picotools_tests_mainargsparser +#define headerfile_picotools_tests_mainargsparser + + +namespace pt +{ + +namespace pt_mainargsparser_tests +{ + + + + + +void make_tests(); + + + + + +} + +} + +#endif