X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Console%2FParse.test.cc;h=46ad9865e05a0626959516800fd1df38c91cd0d7;hb=456ee576285b76aa46240f8001f426757810dcc1;hp=491866af55a9f2811b93bbee39f7aa39d701a2c4;hpb=2d5a1fd2cef2d84e16226a7336948f524fbb71c6;p=senf.git diff --git a/Console/Parse.test.cc b/Console/Parse.test.cc index 491866a..46ad986 100644 --- a/Console/Parse.test.cc +++ b/Console/Parse.test.cc @@ -59,7 +59,8 @@ namespace void endCommand() { os_ << "endCommand()\n"; } - void pushArgument(std::string const & argument) + void pushArgument(senf::console::ArgumentToken::TokenType type, + std::string const & argument) { os_ << "pushArgument( " << argument << " )\n"; } void openGroup() { os_ << "openGroup()\n"; } @@ -67,7 +68,8 @@ namespace { os_ << "closeGroup()\n"; } void pushPunctuation(std::string const & token) { os_ << "pushPunctuation( " << token << " )\n"; } - void pushWord(std::string const & token) + void pushWord(senf::console::ArgumentToken::TokenType type, + std::string const & token) { os_ << "pushWord( " << token << " )\n"; } void builtin_cd(std::vector const & path) @@ -95,7 +97,7 @@ BOOST_AUTO_UNIT_TEST(commandGrammar) "doo / bii / doo arg" " flab::blub" " 123.434>a" - " (a,b,c (huhu))" + " (a,b;c (huhu/{haha}))" " \"foo\\\"bar\" #\n" " x\"01 02 # Inner comment\n" " 0304\";" @@ -118,10 +120,14 @@ BOOST_AUTO_UNIT_TEST(commandGrammar) "pushWord( a )\n" "pushPunctuation( , )\n" "pushWord( b )\n" - "pushPunctuation( , )\n" + "pushPunctuation( ; )\n" "pushWord( c )\n" "pushPunctuation( ( )\n" "pushWord( huhu )\n" + "pushPunctuation( / )\n" + "pushPunctuation( { )\n" + "pushWord( haha )\n" + "pushPunctuation( } )\n" "pushPunctuation( ) )\n" "closeGroup()\n" "pushArgument( foo\"bar )\n" @@ -162,34 +168,83 @@ BOOST_AUTO_UNIT_TEST(commandParser) BOOST_CHECK_EQUAL_COLLECTIONS( info.commandPath().begin(), info.commandPath().end(), path, path + sizeof(path)/sizeof(path[0]) ); - BOOST_REQUIRE_EQUAL( info.arguments().size(), 6u ); - BOOST_REQUIRE_EQUAL( info.tokens().size(), 13u ); + BOOST_CHECK_EQUAL( info.tokens().size(), 15u ); char const * tokens[] = { "arg", "flab::blub", "123.434>a", - "a", ",", "b", ",", "c", "(", "huhu", ")", + "(", "a", ",", "b", ",", "c", "(", "huhu", ")", ")", "foo\"bar", "\x01\x02\x03\x04" }; - BOOST_REQUIRE_EQUAL( info.arguments().begin()[0].size(), 1u ); - BOOST_CHECK_EQUAL( info.arguments().begin()[0].begin()->value(), tokens[0] ); - - BOOST_REQUIRE_EQUAL( info.arguments().begin()[1].size(), 1u ); - BOOST_CHECK_EQUAL( info.arguments().begin()[1].begin()->value(), tokens[1] ); + senf::console::ParseCommandInfo::argument_iterator args (info.arguments().begin()); + BOOST_REQUIRE( args != info.arguments().end() ); + BOOST_REQUIRE_EQUAL( args->size(), 1u ); + BOOST_CHECK_EQUAL( args->begin()->value(), tokens[0] ); - BOOST_REQUIRE_EQUAL( info.arguments().begin()[2].size(), 1u ); - BOOST_CHECK_EQUAL( info.arguments().begin()[2].begin()->value(), tokens[2] ); + ++ args; + BOOST_REQUIRE( args != info.arguments().end() ); + BOOST_REQUIRE_EQUAL( args->size(), 1u ); + BOOST_CHECK_EQUAL( args->begin()->value(), tokens[1] ); + + ++ args; + BOOST_REQUIRE( args != info.arguments().end() ); + BOOST_REQUIRE_EQUAL( args->size(), 1u ); + BOOST_CHECK_EQUAL( args->begin()->value(), tokens[2] ); - BOOST_REQUIRE_EQUAL( info.arguments().begin()[3].size(), 8u ); + ++ args; + BOOST_REQUIRE( args != info.arguments().end() ); + BOOST_REQUIRE_EQUAL( args->size(), 8u ); for (unsigned i (0); i<8; ++i) - BOOST_CHECK_EQUAL( info.arguments().begin()[3].begin()[i].value(), tokens[3+i] ); + BOOST_CHECK_EQUAL( args->begin()[i].value(), tokens[4+i] ); + + ++ args; + BOOST_REQUIRE( args != info.arguments().end() ); + BOOST_REQUIRE_EQUAL( args->size(), 1u ); + BOOST_CHECK_EQUAL( args->begin()->value(), tokens[13] ); + + ++ args; + BOOST_REQUIRE( args != info.arguments().end() ); + BOOST_REQUIRE_EQUAL( args->size(), 1u ); + BOOST_CHECK_EQUAL( args->begin()->value(), tokens[14] ); - BOOST_REQUIRE_EQUAL( info.arguments().begin()[4].size(), 1u ); - BOOST_CHECK_EQUAL( info.arguments().begin()[4].begin()->value(), tokens[11] ); + ++ args; + BOOST_CHECK( args == info.arguments().end() ); +} + +namespace { + void parseArgs(senf::console::ParseCommandInfo::ArgumentsRange const & args) + { + senf::console::CheckedArgumentIteratorWrapper arg (args); + senf::console::ParseCommandInfo::TokensRange arg1 (*(arg++)); + senf::console::ParseCommandInfo::TokensRange arg2 (*(arg++)); + } +} + +BOOST_AUTO_UNIT_TEST(checkedArgumentIterator) +{ + senf::console::CommandParser parser; + + BOOST_CHECK( parser.parse("foo a", &setInfo) ); + BOOST_CHECK_THROW( parseArgs(info.arguments()), senf::console::SyntaxErrorException ); + + BOOST_CHECK( parser.parse("foo a b", &setInfo) ); + BOOST_CHECK_NO_THROW( parseArgs(info.arguments()) ); + + BOOST_CHECK( parser.parse("foo a b c", &setInfo) ); + BOOST_CHECK_THROW( parseArgs(info.arguments()), senf::console::SyntaxErrorException ); + + senf::console::CheckedArgumentIteratorWrapper arg (info.arguments()); + BOOST_CHECK( arg == info.arguments().begin() ); + BOOST_CHECK( arg != info.arguments().end() ); + BOOST_CHECK( arg ); + ++ arg; + BOOST_CHECK( arg ); + arg.clear(); + BOOST_CHECK( arg.done() ); - BOOST_REQUIRE_EQUAL( info.arguments().begin()[5].size(), 1u ); - BOOST_CHECK_EQUAL( info.arguments().begin()[5].begin()->value(), tokens[12] ); + senf::console::ParseCommandInfo::ArgumentIterator i (arg); + BOOST_CHECK( i == info.arguments().end() ); } ///////////////////////////////cc.e////////////////////////////////////////