// This file is part of OpenCV project. // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. #include "test_precomp.hpp" using namespace cv; namespace { static const char * const keys = "{ h help | | print help }" "{ i info | false | print info }" "{ t true | true | true value }" "{ n unused | | dummy }" ; TEST(CommandLineParser, testFailure) { const char* argv[] = {"", "-q"}; const int argc = 2; cv::CommandLineParser parser(argc, argv, keys); EXPECT_ANY_THROW(parser.has("q")); EXPECT_ANY_THROW(parser.get("q")); EXPECT_ANY_THROW(parser.get(0)); parser.get("h"); EXPECT_FALSE(parser.check()); } TEST(CommandLineParser, testHas_noValues) { const char* argv[] = {"", "-h", "--info"}; const int argc = 3; cv::CommandLineParser parser(argc, argv, keys); EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("h")); EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("i")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); } TEST(CommandLineParser, testHas_TrueValues) { const char* argv[] = {"", "-h=TRUE", "--info=true"}; const int argc = 3; cv::CommandLineParser parser(argc, argv, keys); EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("h")); EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("i")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); } TEST(CommandLineParser, testHas_TrueValues1) { const char* argv[] = {"", "-h=1", "--info=1"}; const int argc = 3; cv::CommandLineParser parser(argc, argv, keys); EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("h")); EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("i")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); } TEST(CommandLineParser, testHas_FalseValues0) { const char* argv[] = {"", "-h=0", "--info=0"}; const int argc = 3; cv::CommandLineParser parser(argc, argv, keys); EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("h")); EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("i")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); } TEST(CommandLineParser, testBoolOption_noArgs) { const char* argv[] = {""}; const int argc = 1; cv::CommandLineParser parser(argc, argv, keys); EXPECT_FALSE(parser.get("help")); EXPECT_FALSE(parser.get("h")); EXPECT_FALSE(parser.get("info")); EXPECT_FALSE(parser.get("i")); EXPECT_TRUE(parser.get("true")); // default is true EXPECT_TRUE(parser.get("t")); } TEST(CommandLineParser, testBoolOption_noValues) { const char* argv[] = {"", "-h", "--info"}; const int argc = 3; cv::CommandLineParser parser(argc, argv, keys); EXPECT_TRUE(parser.get("help")); EXPECT_TRUE(parser.get("h")); EXPECT_TRUE(parser.get("info")); EXPECT_TRUE(parser.get("i")); } TEST(CommandLineParser, testBoolOption_TrueValues) { const char* argv[] = {"", "-h=TRUE", "--info=true"}; const int argc = 3; cv::CommandLineParser parser(argc, argv, keys); //EXPECT_TRUE(parser.get("help")); //EXPECT_TRUE(parser.get("h")); EXPECT_TRUE(parser.get("info")); EXPECT_TRUE(parser.get("i")); EXPECT_FALSE(parser.get("unused")); EXPECT_FALSE(parser.get("n")); } TEST(CommandLineParser, testBoolOption_FalseValues) { const char* argv[] = {"", "--help=FALSE", "-i=false"}; const int argc = 3; cv::CommandLineParser parser(argc, argv, keys); EXPECT_FALSE(parser.get("help")); EXPECT_FALSE(parser.get("h")); EXPECT_FALSE(parser.get("info")); EXPECT_FALSE(parser.get("i")); } static const char * const keys2 = "{ h help | | print help }" "{ @arg1 | default1 | param1 }" "{ @arg2 | | param2 }" "{ n unused | | dummy }" ; TEST(CommandLineParser, testPositional_noArgs) { const char* argv[] = {""}; const int argc = 1; cv::CommandLineParser parser(argc, argv, keys2); EXPECT_TRUE(parser.has("@arg1")); EXPECT_FALSE(parser.has("@arg2")); EXPECT_EQ("default1", parser.get("@arg1")); EXPECT_EQ("default1", parser.get(0)); EXPECT_EQ("", parser.get("@arg2")); EXPECT_EQ("", parser.get(1)); } TEST(CommandLineParser, testPositional_default) { const char* argv[] = {"", "test1", "test2"}; const int argc = 3; cv::CommandLineParser parser(argc, argv, keys2); EXPECT_TRUE(parser.has("@arg1")); EXPECT_TRUE(parser.has("@arg2")); EXPECT_EQ("test1", parser.get("@arg1")); EXPECT_EQ("test2", parser.get("@arg2")); EXPECT_EQ("test1", parser.get(0)); EXPECT_EQ("test2", parser.get(1)); } TEST(CommandLineParser, testPositional_withFlagsBefore) { const char* argv[] = {"", "-h", "test1", "test2"}; const int argc = 4; cv::CommandLineParser parser(argc, argv, keys2); EXPECT_TRUE(parser.has("@arg1")); EXPECT_TRUE(parser.has("@arg2")); EXPECT_EQ("test1", parser.get("@arg1")); EXPECT_EQ("test2", parser.get("@arg2")); EXPECT_EQ("test1", parser.get(0)); EXPECT_EQ("test2", parser.get(1)); } TEST(CommandLineParser, testPositional_withFlagsAfter) { const char* argv[] = {"", "test1", "test2", "-h"}; const int argc = 4; cv::CommandLineParser parser(argc, argv, keys2); EXPECT_TRUE(parser.has("@arg1")); EXPECT_TRUE(parser.has("@arg2")); EXPECT_EQ("test1", parser.get("@arg1")); EXPECT_EQ("test2", parser.get("@arg2")); EXPECT_EQ("test1", parser.get(0)); EXPECT_EQ("test2", parser.get(1)); } TEST(CommandLineParser, testEmptyStringValue) { static const char * const keys3 = "{ @pos0 | | empty default value }" "{ @pos1 | | forbid empty default value }"; const char* argv[] = {""}; const int argc = 1; cv::CommandLineParser parser(argc, argv, keys3); // EXPECT_TRUE(parser.has("@pos0")); EXPECT_EQ("", parser.get("@pos0")); EXPECT_TRUE(parser.check()); EXPECT_FALSE(parser.has("@pos1")); parser.get(1); EXPECT_FALSE(parser.check()); } TEST(CommandLineParser, positional_regression_5074_equal_sign) { static const char * const keys3 = "{ @eq0 | | }" "{ eq1 | | }"; const char* argv[] = {"", "1=0", "--eq1=1=0"}; const int argc = 3; cv::CommandLineParser parser(argc, argv, keys3); EXPECT_EQ("1=0", parser.get("@eq0")); EXPECT_EQ("1=0", parser.get(0)); EXPECT_EQ("1=0", parser.get("eq1")); EXPECT_TRUE(parser.check()); } } // namespace