📄 optiontest.cpp
字号:
//
// OptionTest.cpp
//
// $Id: //poco/Main/Util/testsuite/src/OptionTest.cpp#1 $
//
// Copyright (c) 2004-2005, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. 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.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// 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 "OptionTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Util/Option.h"
#include "Util/OptionException.h"
using Util::Option;
OptionTest::OptionTest(const std::string& name): CppUnit::TestCase(name)
{
}
OptionTest::~OptionTest()
{
}
void OptionTest::testOption()
{
Option incOpt = Option("include-dir", "I", "specify an include search path")
.required(false)
.repeatable(true)
.argument("path");
Option libOpt = Option("library-dir", "L", "specify a library search path", false)
.repeatable(true)
.argument("path");
Option outOpt = Option("output", "o", "specify the output file", true)
.argument("file", true);
Option vrbOpt = Option("verbose", "v")
.description("enable verbose mode")
.required(false)
.repeatable(false);
Option optOpt = Option("optimize", "O")
.description("enable optimization")
.required(false)
.repeatable(false)
.argument("level", false);
assert (incOpt.shortName() == "I");
assert (incOpt.fullName() == "include-dir");
assert (incOpt.repeatable());
assert (!incOpt.required());
assert (incOpt.argumentName() == "path");
assert (incOpt.argumentRequired());
assert (incOpt.takesArgument());
assert (libOpt.shortName() == "L");
assert (libOpt.fullName() == "library-dir");
assert (libOpt.repeatable());
assert (!libOpt.required());
assert (libOpt.argumentName() == "path");
assert (libOpt.argumentRequired());
assert (incOpt.takesArgument());
assert (outOpt.shortName() == "o");
assert (outOpt.fullName() == "output");
assert (!outOpt.repeatable());
assert (outOpt.required());
assert (outOpt.argumentName() == "file");
assert (outOpt.argumentRequired());
assert (incOpt.takesArgument());
assert (vrbOpt.shortName() == "v");
assert (vrbOpt.fullName() == "verbose");
assert (!vrbOpt.repeatable());
assert (!vrbOpt.required());
assert (!vrbOpt.argumentRequired());
assert (!vrbOpt.takesArgument());
assert (optOpt.shortName() == "O");
assert (optOpt.fullName() == "optimize");
assert (!optOpt.repeatable());
assert (!optOpt.required());
assert (optOpt.argumentName() == "level");
assert (optOpt.takesArgument());
assert (!optOpt.argumentRequired());
}
void OptionTest::testMatches1()
{
Option incOpt = Option("include-dir", "I", "specify an include search path")
.required(false)
.repeatable(true)
.argument("path");
assert (incOpt.matchesShort("Iinclude"));
assert (incOpt.matchesFull("include:include"));
assert (incOpt.matchesFull("include-dir:include"));
assert (incOpt.matchesFull("inc=include"));
assert (incOpt.matchesFull("INCLUDE=include"));
assert (incOpt.matchesFull("include"));
assert (incOpt.matchesShort("I"));
assert (incOpt.matchesFull("i"));
assert (!incOpt.matchesFull("include-dir2=include"));
assert (!incOpt.matchesShort("linclude"));
}
void OptionTest::testMatches2()
{
Option incOpt = Option("include-dir", "", "specify an include search path")
.required(false)
.repeatable(true)
.argument("path");
assert (!incOpt.matchesShort("Iinclude"));
assert (incOpt.matchesFull("include:include"));
assert (incOpt.matchesFull("include-dir:include"));
assert (incOpt.matchesFull("inc=include"));
assert (incOpt.matchesFull("INCLUDE=include"));
assert (incOpt.matchesFull("I"));
assert (incOpt.matchesFull("i"));
assert (!incOpt.matchesFull("include-dir2=include"));
assert (!incOpt.matchesShort("linclude"));
}
void OptionTest::testProcess1()
{
Option incOpt = Option("include-dir", "I", "specify an include search path")
.required(false)
.repeatable(true)
.argument("path");
std::string arg;
incOpt.process("Iinclude", arg);
assert (arg == "include");
incOpt.process("I/usr/include", arg);
assert (arg == "/usr/include");
incOpt.process("include:/usr/local/include", arg);
assert (arg == "/usr/local/include");
incOpt.process("include=/proj/include", arg);
assert (arg == "/proj/include");
incOpt.process("include-dir=/usr/include", arg);
assert (arg == "/usr/include");
incOpt.process("Include-dir:/proj/include", arg);
assert (arg == "/proj/include");
try
{
incOpt.process("I", arg);
fail("argument required - must throw");
}
catch (Util::MissingArgumentException&)
{
}
try
{
incOpt.process("Include", arg);
fail("argument required - must throw");
}
catch (Util::MissingArgumentException&)
{
}
try
{
incOpt.process("Llib", arg);
fail("wrong option - must throw");
}
catch (Util::UnknownOptionException&)
{
}
Option vrbOpt = Option("verbose", "v")
.description("enable verbose mode")
.required(false)
.repeatable(false);
vrbOpt.process("v", arg);
assert (arg.empty());
vrbOpt.process("verbose", arg);
assert (arg.empty());
try
{
vrbOpt.process("v2", arg);
fail("no argument expected - must throw");
}
catch (Util::UnexpectedArgumentException&)
{
}
try
{
vrbOpt.process("verbose:2", arg);
fail("no argument expected - must throw");
}
catch (Util::UnexpectedArgumentException&)
{
}
Option optOpt = Option("optimize", "O")
.description("enable optimization")
.required(false)
.repeatable(false)
.argument("level", false);
optOpt.process("O", arg);
assert (arg.empty());
optOpt.process("O2", arg);
assert (arg == "2");
optOpt.process("optimize:1", arg);
assert (arg == "1");
optOpt.process("opt", arg);
assert (arg.empty());
optOpt.process("opt=3", arg);
assert (arg == "3");
optOpt.process("opt=", arg);
assert (arg.empty());
}
void OptionTest::testProcess2()
{
Option incOpt = Option("include-dir", "", "specify an include search path")
.required(false)
.repeatable(true)
.argument("path");
std::string arg;
incOpt.process("include:/usr/local/include", arg);
assert (arg == "/usr/local/include");
incOpt.process("include=/proj/include", arg);
assert (arg == "/proj/include");
incOpt.process("include-dir=/usr/include", arg);
assert (arg == "/usr/include");
incOpt.process("Include-dir:/proj/include", arg);
assert (arg == "/proj/include");
try
{
incOpt.process("Iinclude", arg);
fail("unknown option - must throw");
}
catch (Util::UnknownOptionException&)
{
}
try
{
incOpt.process("I", arg);
fail("argument required - must throw");
}
catch (Util::MissingArgumentException&)
{
}
try
{
incOpt.process("Include", arg);
fail("argument required - must throw");
}
catch (Util::MissingArgumentException&)
{
}
try
{
incOpt.process("Llib", arg);
fail("wrong option - must throw");
}
catch (Util::UnknownOptionException&)
{
}
Option vrbOpt = Option("verbose", "")
.description("enable verbose mode")
.required(false)
.repeatable(false);
vrbOpt.process("v", arg);
assert (arg.empty());
vrbOpt.process("verbose", arg);
assert (arg.empty());
try
{
vrbOpt.process("v2", arg);
fail("no argument expected - must throw");
}
catch (Util::UnknownOptionException&)
{
}
try
{
vrbOpt.process("verbose:2", arg);
fail("no argument expected - must throw");
}
catch (Util::UnexpectedArgumentException&)
{
}
}
void OptionTest::setUp()
{
}
void OptionTest::tearDown()
{
}
CppUnit::Test* OptionTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("OptionTest");
CppUnit_addTest(pSuite, OptionTest, testOption);
CppUnit_addTest(pSuite, OptionTest, testMatches1);
CppUnit_addTest(pSuite, OptionTest, testMatches2);
CppUnit_addTest(pSuite, OptionTest, testProcess1);
CppUnit_addTest(pSuite, OptionTest, testProcess2);
return pSuite;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -