diff --git a/src/ProgramOptionsTest.cpp b/src/ProgramOptionsTest.cpp index 67032fc..1bcddfe 100644 --- a/src/ProgramOptionsTest.cpp +++ b/src/ProgramOptionsTest.cpp @@ -1,89 +1,87 @@ -#include "external/catch.hpp" -#include "ProgramOptions.h" - -class Args -{ -public: - explicit Args(const std::vector& args) : arg0("foo"), arguments(args) - { - pointers.push_back(const_cast(arg0.data())); - for (auto& s : arguments) - pointers.push_back(const_cast(s.data())); - } - - int argc() const - { - return static_cast(pointers.size()); - } - - char** argv() - { - return &pointers[0]; - } - -private: - std::string arg0; - std::vector arguments; - std::vector 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{})); - REQUIRE((po.parseCharsString("0") == std::set{0})); - REQUIRE((po.parseCharsString("42") == std::set{42})); - REQUIRE((po.parseCharsString("0-1") == std::set{0,1})); - REQUIRE((po.parseCharsString("1-3") == std::set{1,2,3})); - REQUIRE((po.parseCharsString(" 1 - 3 ") == std::set{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& args) : arg0("foo"), arguments(args) + { + pointers.push_back(const_cast(arg0.data())); + for (auto& s : arguments) + pointers.push_back(const_cast(s.data())); + } + + int argc() const + { + return static_cast(pointers.size()); + } + + char** argv() + { + return &pointers[0]; + } + +private: + std::string arg0; + std::vector arguments; + std::vector 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{})); + REQUIRE((po.parseCharsString("0") == std::set{0})); + REQUIRE((po.parseCharsString("42") == std::set{42})); + REQUIRE((po.parseCharsString("0-1") == std::set{0,1})); + REQUIRE((po.parseCharsString("1-3") == std::set{1,2,3})); + REQUIRE((po.parseCharsString(" 1 - 3 ") == std::set{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); +}