2019-05-12 21:40:26 +02:00
|
|
|
/* This file is part of remwharead.
|
|
|
|
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <catch.hpp>
|
|
|
|
#include "parse_options.hpp"
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
SCENARIO ("The option parser works correctly")
|
|
|
|
{
|
|
|
|
bool exception = false;
|
|
|
|
options opts;
|
2019-05-16 08:36:35 +02:00
|
|
|
const string uri = "https://example.com/article.html";
|
2019-05-12 21:40:26 +02:00
|
|
|
|
|
|
|
WHEN ("The options are --help --file test")
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const char *argv[]
|
|
|
|
= { "remwharead", "--help", "--file", "test" };
|
|
|
|
opts = parse_options(4, argv);
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
exception = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
THEN ("No exception is thrown")
|
|
|
|
AND_THEN ("status code is 0")
|
|
|
|
AND_THEN ("options.file is empty")
|
|
|
|
{
|
|
|
|
REQUIRE_FALSE(exception);
|
|
|
|
REQUIRE(opts.status_code == 0);
|
|
|
|
REQUIRE(opts.file == "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WHEN ("The options are --version --file test")
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const char *argv[]
|
|
|
|
= { "remwharead", "--version", "--file", "test" };
|
|
|
|
opts = parse_options(4, argv);
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
exception = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
THEN ("No exception is thrown")
|
|
|
|
AND_THEN ("status code is 0")
|
|
|
|
AND_THEN ("options.file is empty")
|
|
|
|
{
|
|
|
|
REQUIRE_FALSE(exception);
|
|
|
|
REQUIRE(opts.status_code == 0);
|
|
|
|
REQUIRE(opts.file == "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:36:35 +02:00
|
|
|
WHEN ("The options are -t 💩 " + uri)
|
2019-05-12 21:40:26 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const char *argv[]
|
2019-05-16 08:36:35 +02:00
|
|
|
= { "remwharead", "-t", "💩", uri.c_str() };
|
2019-05-13 02:33:43 +02:00
|
|
|
opts = parse_options(4, argv);
|
2019-05-12 21:40:26 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
exception = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
THEN ("No exception is thrown")
|
|
|
|
AND_THEN ("status code is 0")
|
|
|
|
AND_THEN ("options.file is empty")
|
|
|
|
{
|
|
|
|
REQUIRE_FALSE(exception);
|
|
|
|
REQUIRE(opts.status_code == 0);
|
|
|
|
REQUIRE(opts.tags == vector<string>{ "💩" });
|
2019-05-16 08:36:35 +02:00
|
|
|
REQUIRE(opts.uri == uri);
|
2019-05-12 21:40:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WHEN ("A very long string is passed as a tag")
|
|
|
|
{
|
|
|
|
const string longstring = // 5 · 60 = 300
|
|
|
|
"aw6hui6chieRo9aihai9un1aeke6oushoo2oRo4aeD6eiDeiSheek4ahGiel"
|
|
|
|
"othaemeeyo4ievieV8kae9xiriejohD0aelah6oophaQueilohyaix3joo7O"
|
|
|
|
"laiceeFePuetaeBe0aip3eemuaheer0aj8aij8ahchisi4eiperiechoopoo"
|
|
|
|
"ohmie5doog5ohbahDoodah7daurah6haebeife2tah5Pheeweeb0eishooc4"
|
|
|
|
"phohKu5Ha3HiCeedeoph1ocaingaHeedeepeesohmee6Equ4meirahk3aihe";
|
|
|
|
const string tags = "tag1," + longstring + ",tag3";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const char *argv[] =
|
|
|
|
{
|
|
|
|
"remwharead",
|
|
|
|
"-t",
|
2019-05-13 02:33:43 +02:00
|
|
|
tags.c_str(),
|
2019-05-16 08:36:35 +02:00
|
|
|
uri.c_str()
|
2019-05-12 21:40:26 +02:00
|
|
|
};
|
2019-05-13 02:33:43 +02:00
|
|
|
opts = parse_options(4, argv);
|
2019-05-12 21:40:26 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
exception = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
THEN ("No exception is thrown")
|
|
|
|
AND_THEN ("status code is 0")
|
|
|
|
AND_THEN ("options.file is empty")
|
|
|
|
{
|
|
|
|
REQUIRE_FALSE(exception);
|
|
|
|
REQUIRE(opts.status_code == 0);
|
2019-05-17 00:40:55 +02:00
|
|
|
REQUIRE((opts.tags == vector<string>{ "tag1", longstring, "tag3" }));
|
2019-05-16 08:36:35 +02:00
|
|
|
REQUIRE(opts.uri == uri);
|
2019-05-12 21:40:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|