Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirgamalyan committed Dec 4, 2021
2 parents 72422cf + 54c7673 commit d8b07bf
Showing 1 changed file with 87 additions and 89 deletions.
176 changes: 87 additions & 89 deletions src/ProgramOptionsTest.cpp
Original file line number Diff line number Diff line change
@@ -1,89 +1,87 @@
#include "external/catch.hpp"
#include "ProgramOptions.h"

class Args
{
public:
explicit Args(const std::vector<std::string>& args) : arg0("foo"), arguments(args)
{
pointers.push_back(const_cast<char*>(arg0.data()));
for (auto& s : arguments)
pointers.push_back(const_cast<char*>(s.data()));
}

int argc() const
{
return static_cast<int>(pointers.size());
}

char** argv()
{
return &pointers[0];
}

private:
std::string arg0;
std::vector<std::string> arguments;
std::vector<char*> pointers;
};

TEST_CASE( "parseCmdLine")
{
{
Args args({"--font-file", "vera.ttf", "--output", "vera"});
ProgramOptions po;
Config config = po.parseCommandLine(args.argc(), args.argv());
REQUIRE(config.fontFile.size() == 1);
REQUIRE(config.fontFile[0] == "vera.ttf");
REQUIRE(config.output == "vera");
REQUIRE(config.textureSize.w == 256);
REQUIRE(config.textureSize.h == 256);
}

{
Args args({"--font-file", "vera.ttf", "--output", "vera", "--font-file", "default.ttf"});
ProgramOptions po;
Config config = po.parseCommandLine(args.argc(), args.argv());
REQUIRE(config.fontFile.size() == 2);
REQUIRE(config.fontFile[0] == "vera.ttf");
REQUIRE(config.fontFile[1] == "default.ttf");
REQUIRE(config.output == "vera");
}

{
Args args({"--font-file", "vera.ttf"});
ProgramOptions po;
REQUIRE_THROWS_AS(po.parseCommandLine(args.argc(), args.argv()), std::runtime_error);
}
}

TEST_CASE("parseColor")
{
ProgramOptions po;
REQUIRE((po.parseColor("0,0,0") == Config::Color{0, 0, 0}));
REQUIRE((po.parseColor("255,255,255") == Config::Color{255, 255, 255}));
REQUIRE((po.parseColor(" 255 , 255 , 255 ") == Config::Color{255, 255, 255}));

REQUIRE_THROWS_AS(po.parseColor(""), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("foo"), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("0,1"), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("0,1,2,3"), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("0,a,1"), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("0,1,256"), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("0,1,-1"), std::logic_error);
}

TEST_CASE("parseCharsString")
{
ProgramOptions po;
REQUIRE((po.parseCharsString("") == std::set<std::uint32_t>{}));
REQUIRE((po.parseCharsString("0") == std::set<std::uint32_t>{0}));
REQUIRE((po.parseCharsString("42") == std::set<std::uint32_t>{42}));
REQUIRE((po.parseCharsString("0-1") == std::set<std::uint32_t>{0,1}));
REQUIRE((po.parseCharsString("1-3") == std::set<std::uint32_t>{1,2,3}));
REQUIRE((po.parseCharsString(" 1 - 3 ") == std::set<std::uint32_t>{1,2,3}));

REQUIRE_THROWS_AS(po.parseCharsString("foo"), std::logic_error);
REQUIRE_THROWS_AS(po.parseCharsString("-1"), std::logic_error);
REQUIRE_THROWS_AS(po.parseCharsString("-1-2"), std::logic_error);
}
#include "external/catch.hpp"
#include "ProgramOptions.h"

class Args
{
public:
explicit Args(const std::vector<std::string>& args) : arg0("foo"), arguments(args)
{
pointers.push_back(const_cast<char*>(arg0.data()));
for (auto& s : arguments)
pointers.push_back(const_cast<char*>(s.data()));
}

int argc() const
{
return static_cast<int>(pointers.size());
}

char** argv()
{
return &pointers[0];
}

private:
std::string arg0;
std::vector<std::string> arguments;
std::vector<char*> pointers;
};

TEST_CASE( "parseCmdLine")
{
{
Args args({"--font-file", "vera.ttf", "--output", "vera"});
ProgramOptions po;
Config config = po.parseCommandLine(args.argc(), args.argv());
REQUIRE(config.fontFile.size() == 1);
REQUIRE(config.fontFile[0] == "vera.ttf");
REQUIRE(config.output == "vera");
}

{
Args args({"--font-file", "vera.ttf", "--output", "vera", "--font-file", "default.ttf"});
ProgramOptions po;
Config config = po.parseCommandLine(args.argc(), args.argv());
REQUIRE(config.fontFile.size() == 2);
REQUIRE(config.fontFile[0] == "vera.ttf");
REQUIRE(config.fontFile[1] == "default.ttf");
REQUIRE(config.output == "vera");
}

{
Args args({"--font-file", "vera.ttf"});
ProgramOptions po;
REQUIRE_THROWS_AS(po.parseCommandLine(args.argc(), args.argv()), std::runtime_error);
}
}

TEST_CASE("parseColor")
{
ProgramOptions po;
REQUIRE((po.parseColor("0,0,0") == Config::Color{0, 0, 0}));
REQUIRE((po.parseColor("255,255,255") == Config::Color{255, 255, 255}));
REQUIRE((po.parseColor(" 255 , 255 , 255 ") == Config::Color{255, 255, 255}));

REQUIRE_THROWS_AS(po.parseColor(""), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("foo"), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("0,1"), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("0,1,2,3"), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("0,a,1"), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("0,1,256"), std::logic_error);
REQUIRE_THROWS_AS(po.parseColor("0,1,-1"), std::logic_error);
}

TEST_CASE("parseCharsString")
{
ProgramOptions po;
REQUIRE((po.parseCharsString("") == std::set<std::uint32_t>{}));
REQUIRE((po.parseCharsString("0") == std::set<std::uint32_t>{0}));
REQUIRE((po.parseCharsString("42") == std::set<std::uint32_t>{42}));
REQUIRE((po.parseCharsString("0-1") == std::set<std::uint32_t>{0,1}));
REQUIRE((po.parseCharsString("1-3") == std::set<std::uint32_t>{1,2,3}));
REQUIRE((po.parseCharsString(" 1 - 3 ") == std::set<std::uint32_t>{1,2,3}));

REQUIRE_THROWS_AS(po.parseCharsString("foo"), std::logic_error);
REQUIRE_THROWS_AS(po.parseCharsString("-1"), std::logic_error);
REQUIRE_THROWS_AS(po.parseCharsString("-1-2"), std::logic_error);
}

0 comments on commit d8b07bf

Please sign in to comment.