Console: Added Module
[senf.git] / Console / Parse.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Parse.test unit tests */
25
26 //#include "Parse.test.hh"
27 //#include "Parse.test.ih"
28
29 // Custom includes
30 #include <sstream>
31 #include "Parse.hh"
32 #include "Parse.ih"
33
34 #include <boost/test/auto_unit_test.hpp>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace 
41 {
42     struct TestParseDispatcher 
43     {
44         TestParseDispatcher(std::ostream & os) : os_ (os) {}
45
46         std::ostream & os_;
47
48         void beginCommand(std::string const & command) 
49             { os_ << "beginCommand( " << command << " )\n"; }
50         void endCommand() 
51             { os_ << "endCommand()\n"; }
52         
53         void pushArgument(std::string const & argument)
54             { os_ << "pushArgument( " << argument << " )\n"; }
55         void openGroup()
56             { os_ << "openGroup()\n"; }
57         void closeGroup()
58             { os_ << "closeGroup()\n"; }
59         void pushPunctuation(std::string const & token)
60             { os_ << "pushPunctuation( " << token << " )\n"; }
61         void pushWord(std::string const & token)
62             { os_ << "pushWord( " << token << " )\n"; }
63     };
64 }
65
66 BOOST_AUTO_UNIT_TEST(commandParser)
67 {
68     senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
69     std::stringstream ss;
70     TestParseDispatcher dispatcher (ss);
71     senf::console::detail::CommandGrammar<TestParseDispatcher> grammar (dispatcher, context);
72     senf::console::detail::SkipGrammar skipGrammar;
73
74     char text[] = 
75         "# Comment\n"
76         "doo / bii / doo arg/../path"
77         "                flab::blub"
78         "                123.434>a"
79         "                (a,b,c (huhu))"
80         "                \"foo\\\"bar\" #\n"
81         "                x\"01 02 # Inner comment\n"
82         "                   0304\"";
83
84     BOOST_CHECK( boost::spirit::parse( 
85                      text, 
86                      grammar, 
87                      skipGrammar ) . full );
88     BOOST_CHECK_EQUAL( ss.str(), 
89                        "beginCommand( doo/bii/doo )\n"
90                        "pushArgument( arg/../path )\n"
91                        "pushArgument( flab::blub )\n"
92                        "pushArgument( 123.434>a )\n"
93                        "openGroup()\n"
94                        "pushWord( a )\n"
95                        "pushPunctuation( , )\n"
96                        "pushWord( b )\n"
97                        "pushPunctuation( , )\n"
98                        "pushWord( c )\n"
99                        "pushPunctuation( ( )\n"
100                        "pushWord( huhu )\n"
101                        "pushPunctuation( ) )\n"
102                        "closeGroup()\n"
103                        "pushArgument( foo\"bar )\n"
104                        "pushArgument( \x01\x02\x03\x04 )\n"
105                        "endCommand()\n" );
106 }
107
108 BOOST_AUTO_UNIT_TEST(singleCommandParser)
109 {
110     senf::console::SingleCommandParser parser;
111
112     char const text[] = 
113         "# Comment\n"
114         "doo / bii / doo arg/../path"
115         "                flab::blub"
116         "                123.434>a"
117         "                (a,b,c (huhu))"
118         "                \"foo\\\"bar\" #\n"
119         "                x\"01 02 # Inner comment\n"
120         "                   0304\"";
121
122     senf::console::ParseCommandInfo info;
123     BOOST_CHECK( parser.parseCommand(text, info) );
124
125     BOOST_CHECK_EQUAL( info.commandPath(), "doo/bii/doo" );
126     BOOST_REQUIRE_EQUAL( info.arguments(), 6u );
127     BOOST_REQUIRE_EQUAL( info.tokens(), 13u );
128
129     char const * tokens[] = { "arg/../path", 
130                               "flab::blub", 
131                               "123.434>a", 
132                               "a", ",", "b", ",", "c", "(", "huhu", ")",
133                               "foo\"bar",
134                               "\x01\x02\x03\x04" };
135
136     BOOST_REQUIRE_EQUAL( info.begin_arguments()[0].size(), 1u );
137     BOOST_CHECK_EQUAL( info.begin_arguments()[0].begin()->value(), tokens[0] );
138
139     BOOST_REQUIRE_EQUAL( info.begin_arguments()[1].size(), 1u );
140     BOOST_CHECK_EQUAL( info.begin_arguments()[1].begin()->value(), tokens[1] );
141
142     BOOST_REQUIRE_EQUAL( info.begin_arguments()[2].size(), 1u );
143     BOOST_CHECK_EQUAL( info.begin_arguments()[2].begin()->value(), tokens[2] );
144
145     BOOST_REQUIRE_EQUAL( info.begin_arguments()[3].size(), 8u );
146     for (unsigned i (0); i<8; ++i)
147         BOOST_CHECK_EQUAL( info.begin_arguments()[3].begin()[i].value(), tokens[3+i] );
148
149     BOOST_REQUIRE_EQUAL( info.begin_arguments()[4].size(), 1u );
150     BOOST_CHECK_EQUAL( info.begin_arguments()[4].begin()->value(), tokens[11] );
151
152     BOOST_REQUIRE_EQUAL( info.begin_arguments()[5].size(), 1u );
153     BOOST_CHECK_EQUAL( info.begin_arguments()[5].begin()->value(), tokens[12] );
154 }
155
156 ///////////////////////////////cc.e////////////////////////////////////////
157 #undef prefix_
158
159 \f
160 // Local Variables:
161 // mode: c++
162 // fill-column: 100
163 // comment-column: 40
164 // c-file-style: "senf"
165 // indent-tabs-mode: nil
166 // ispell-local-dictionary: "american"
167 // compile-command: "scons -u test"
168 // End: