start creating tests for MainSpaceParser

This commit is contained in:
Tomasz Sowa 2021-05-17 03:19:47 +02:00
parent ac691bccb7
commit da6a36a205
3 changed files with 207 additions and 1 deletions

View File

@ -36,6 +36,7 @@
*/
#include "convert.h"
#include "mainargsparser.h"
#include <iostream>
@ -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 )
{

143
tests/mainargsparser.cpp Normal file
View File

@ -0,0 +1,143 @@
/*
* This file is a part of PikoTools
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* 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 <iostream>
#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();
}
}
}

62
tests/mainargsparser.h Normal file
View File

@ -0,0 +1,62 @@
/*
* This file is a part of PikoTools
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* 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