Exemplo n.º 1
0
int CommandOption_T::testCommandOption()
{
    TestUtil  tester( "CommandOption", "Initialization", __FILE__, __LINE__ );

    CommandOptionVec  testCmdOptVec;
    int  expectedCount = 0;

    try   // No arg, trailing, no flags
    {
        CommandOption  cmdOpt(CommandOption::noArgument, CommandOption::trailingType, 0, "", "", false, testCmdOptVec);
        tester.assert( true, "CommandOption was created successfully.", __LINE__ );
        ++expectedCount;
        tester.assert( (cmdOpt.getArgString().compare("ARG") == 0), "CommandOption getArgString() should return ARG.", __LINE__ );
        tester.assert( (cmdOpt.getCount() == 0), "CommandOption count should be 0.", __LINE__ );
        tester.assert( (cmdOpt.getValue().size() == 0), "CommandOption value size should be 0.", __LINE__ );
        tester.assert( (cmdOpt.getOrder() == 0), "CommandOption order should be 0.", __LINE__ );
        tester.assert( (cmdOpt.checkArguments().size() == 0), "CommandOption checkArguments() should return nothing.", __LINE__ );
        tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ );
    }

    try   // No arg, standard, no flags
    {
        CommandOption  cmdOpt(CommandOption::noArgument, CommandOption::stdType, 0, "", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( false, "CommandOption creation should have failed due to missing short and long options.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ );
    }

    try   // No arg, standard, short flag (valid)
    {
        CommandOption  cmdOpt(CommandOption::noArgument, CommandOption::stdType, 'f', "", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( true, "CommandOption was created successfully.", __LINE__ );
        tester.assert( (cmdOpt.getOptionString().compare("-f") == 0),
                       "CommandOption getOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '-f'",
                       __LINE__ );
        tester.assert( (cmdOpt.getFullOptionString().compare("  -f") == 0),
                       "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '  -f'",
                       __LINE__ );
        tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ );
    }

    try   // No arg, standard, short flag (bogus)
    {
        CommandOption  cmdOpt(CommandOption::noArgument, CommandOption::stdType, ' ', "", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( false, "CommandOption creation should have failed due to invalid short option.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ );
    }

    try   // No arg, standard, long flag (valid)
    {
        CommandOption  cmdOpt(CommandOption::noArgument, CommandOption::stdType, 0, "foo", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( true, "CommandOption was created successfully.", __LINE__ );
        tester.assert( (cmdOpt.getOptionString().compare("--foo") == 0),
                       "CommandOption getOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '--foo'",
                       __LINE__ );
        tester.assert( (cmdOpt.getFullOptionString().compare("      --foo") == 0),
                       "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '      --foo'",
                       __LINE__ );
        tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ );
    }

    try   // No arg, standard, long flag (bogus)
    {
        CommandOption  cmdOpt(CommandOption::noArgument, CommandOption::stdType, 0, "foo bar", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( false, "CommandOption creation should have failed due to invalid long option.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ );
    }

    try   // No arg, standard, both flags (valid)
    {
        CommandOption  cmdOpt(CommandOption::noArgument, CommandOption::stdType, 'f', "foo", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( true, "CommandOption was created successfully.", __LINE__ );
        tester.assert( (cmdOpt.getOptionString().compare("-f | --foo") == 0),
                       "CommandOption getOptionString() returned '" + cmdOpt.getOptionString() + "', expected '-f | --foo'",
                       __LINE__ );
        tester.assert( (cmdOpt.getFullOptionString().compare("  -f, --foo") == 0),
                       "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '  -f, --foo'",
                       __LINE__ );
        tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ );
    }

    try   // No arg, meta, no flags
    {
        CommandOption  cmdOpt(CommandOption::noArgument, CommandOption::metaType, 0, "", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( true, "CommandOption was created successfully.", __LINE__ );
        tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ );
    }

    try   // Arg, trailing, no flags
    {
        CommandOption  cmdOpt(CommandOption::hasArgument, CommandOption::trailingType, 0, "", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( true, "CommandOption was created successfully.", __LINE__ );
        tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ );
    }

    try   // Arg, standard, no flags
    {
        CommandOption  cmdOpt(CommandOption::hasArgument, CommandOption::stdType, 0, "", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( false, "CommandOption creation should have failed due to missing short and long options.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ );
    }

    try   // Arg, standard, short flag (valid)
    {
        CommandOption  cmdOpt(CommandOption::hasArgument, CommandOption::stdType, 'f', "", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( true, "CommandOption was created successfully.", __LINE__ );
        tester.assert( (cmdOpt.getOptionString().compare("-f") == 0),
                       "CommandOption getOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '-f'",
                       __LINE__ );
        tester.assert( (cmdOpt.getFullOptionString().compare("  -f  ARG") == 0),
                       "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '  -f  ARG'",
                       __LINE__ );
        tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ );
    }

    try   // Arg, standard, short flag (bogus)
    {
        CommandOption  cmdOpt(CommandOption::hasArgument, CommandOption::stdType, ' ', "", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( false, "CommandOption creation should have failed due to invalid short option.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ );
    }

    try   // Arg, standard, long flag (valid)
    {
        CommandOption  cmdOpt(CommandOption::hasArgument, CommandOption::stdType, 0, "foo", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( true, "CommandOption was created successfully.", __LINE__ );
        tester.assert( (cmdOpt.getOptionString().compare("--foo") == 0),
                       "CommandOption getOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '--foo'",
                       __LINE__ );
        tester.assert( (cmdOpt.getFullOptionString().compare("      --foo=ARG") == 0),
                       "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '      --foo=ARG'",
                       __LINE__ );
        tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ );
    }

    try   // Arg, standard, long flag (bogus)
    {
        CommandOption  cmdOpt(CommandOption::hasArgument, CommandOption::stdType, 0, "foo bar", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( false, "CommandOption creation should have failed due to invalid long option.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ );
    }

    try   // Arg, standard, both flags (valid)
    {
        CommandOption  cmdOpt(CommandOption::hasArgument, CommandOption::stdType, 'f', "foo", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( true, "CommandOption was created successfully.", __LINE__ );
        tester.assert( (cmdOpt.getOptionString().compare("-f | --foo") == 0),
                       "CommandOption getOptionString() returned '" + cmdOpt.getOptionString() + "', expected '-f | --foo'",
                       __LINE__ );
        tester.assert( (cmdOpt.getFullOptionString().compare("  -f, --foo=ARG") == 0),
                       "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '  -f, --foo=ARG'",
                       __LINE__ );
        tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ );
    }

    // @todo - Test getDescription()

    try   // Arg, meta, no flags
    {
        CommandOption  cmdOpt(CommandOption::hasArgument, CommandOption::metaType, 0, "", "", false, testCmdOptVec);
        ++expectedCount;
        tester.assert( true, "CommandOption was created successfully.", __LINE__ );
        tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ );
    }
    catch ( ... )
    {
        tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ );
    }

    return tester.countFails();
}