Fix documentation
[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 // #define BOOST_SPIRIT_DEBUG
30 // #define BOOST_SPIRIT_DEBUG_TRACENODE 0
31
32 // Custom includes
33 #include <sstream>
34 #include "Parse.hh"
35 #include "Parse.ih"
36 #include "../Utils/String.hh"
37
38 #include "../Utils/auto_unit_test.hh"
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 ///////////////////////////////cc.p////////////////////////////////////////
43
44 namespace 
45 {
46     struct TestParseDispatcher 
47     {
48         TestParseDispatcher(std::ostream & os) : os_ (os) {}
49
50         std::ostream & os_;
51
52         void pushDirectory(std::vector<std::string> const & path)
53             { os_ << "pushDirectory( " << senf::stringJoin(path,"/") << " )\n"; }
54         void popDirectory()
55             { os_ << "popDirectory()\n"; }
56
57         void beginCommand(std::vector<std::string> const & command) 
58             { os_ << "beginCommand( " << senf::stringJoin(command, "/") << " )\n"; }
59         void endCommand() 
60             { os_ << "endCommand()\n"; }
61         
62         void pushArgument(std::string const & argument)
63             { os_ << "pushArgument( " << argument << " )\n"; }
64         void openGroup()
65             { os_ << "openGroup()\n"; }
66         void closeGroup()
67             { os_ << "closeGroup()\n"; }
68         void pushPunctuation(std::string const & token)
69             { os_ << "pushPunctuation( " << token << " )\n"; }
70         void pushWord(std::string const & token)
71             { os_ << "pushWord( " << token << " )\n"; }
72
73         void builtin_cd(std::vector<std::string> const & path)
74             { os_ << "builtin_cd( " << senf::stringJoin(path, "/") << " )\n"; }
75         void builtin_ls(std::vector<std::string> const & path)
76             { os_ << "builtin_ls( " << senf::stringJoin(path, "/") << " )\n"; }
77         void builtin_exit()
78             { os_ << "builtin_exit()\n"; }
79         void builtin_help(std::vector<std::string> const & path)
80             { os_ << "builtin_help( " << senf::stringJoin(path, "/") << " )\n"; }
81     };
82 }
83
84 BOOST_AUTO_UNIT_TEST(commandGrammar)
85 {
86     senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
87     std::stringstream ss;
88     TestParseDispatcher dispatcher (ss);
89     
90     typedef senf::console::detail::CommandGrammar<TestParseDispatcher> Grammar;
91     Grammar grammar (dispatcher, context);
92
93     char text[] = 
94         "# Comment\n"
95         "doo / bii / doo arg"
96         "                flab::blub"
97         "                123.434>a"
98         "                (a,b;c (huhu/{haha}))"
99         "                \"foo\\\"bar\" #\n"
100         "                x\"01 02 # Inner comment\n"
101         "                   0304\";"
102         "ls /foo/bar;"
103         "cd /foo/bar;"
104         "exit;"
105         "foo/bar/ { ls; }"
106         "help /foo/bar";
107
108     BOOST_CHECK( boost::spirit::parse( 
109                      text, 
110                      grammar.use_parser<Grammar::CommandParser>(), 
111                      grammar.use_parser<Grammar::SkipParser>() ) . full );
112     BOOST_CHECK_EQUAL( ss.str(), 
113                        "beginCommand( doo/bii/doo )\n"
114                        "pushArgument( arg )\n"
115                        "pushArgument( flab::blub )\n"
116                        "pushArgument( 123.434>a )\n"
117                        "openGroup()\n"
118                        "pushWord( a )\n"
119                        "pushPunctuation( , )\n"
120                        "pushWord( b )\n"
121                        "pushPunctuation( ; )\n"
122                        "pushWord( c )\n"
123                        "pushPunctuation( ( )\n"
124                        "pushWord( huhu )\n"
125                        "pushPunctuation( / )\n"
126                        "pushPunctuation( { )\n"
127                        "pushWord( haha )\n"
128                        "pushPunctuation( } )\n"
129                        "pushPunctuation( ) )\n"
130                        "closeGroup()\n"
131                        "pushArgument( foo\"bar )\n"
132                        "pushArgument( \x01\x02\x03\x04 )\n"
133                        "endCommand()\n"
134                        "builtin_ls( /foo/bar )\n"
135                        "builtin_cd( /foo/bar )\n"
136                        "builtin_exit()\n"
137                        "pushDirectory( foo/bar/ )\n"
138                        "builtin_ls(  )\n"
139                        "popDirectory()\n"
140                        "builtin_help( /foo/bar )\n" );
141 }
142
143 namespace {
144     senf::console::ParseCommandInfo info;
145     void setInfo(senf::console::ParseCommandInfo const & i)
146     { info = i; }
147 }
148
149 BOOST_AUTO_UNIT_TEST(commandParser)
150 {
151     senf::console::CommandParser parser;
152
153     char const text[] = 
154         "# Comment\n"
155         "doo / bii / doo arg"
156         "                flab::blub"
157         "                123.434>a"
158         "                (a,b,c (huhu))"
159         "                \"foo\\\"bar\" #\n"
160         "                x\"01 02 # Inner comment\n"
161         "                   0304\"";
162
163     BOOST_CHECK( parser.parse(text, &setInfo) );
164
165     char const * path[] = { "doo", "bii", "doo" };
166
167     BOOST_CHECK_EQUAL_COLLECTIONS( info.commandPath().begin(), info.commandPath().end(),
168                                    path, path + sizeof(path)/sizeof(path[0]) );
169     BOOST_REQUIRE_EQUAL( info.arguments().size(), 6u );
170     BOOST_REQUIRE_EQUAL( info.tokens().size(), 13u );
171
172     char const * tokens[] = { "arg", 
173                               "flab::blub", 
174                               "123.434>a", 
175                               "a", ",", "b", ",", "c", "(", "huhu", ")",
176                               "foo\"bar",
177                               "\x01\x02\x03\x04" };
178
179     BOOST_REQUIRE_EQUAL( info.arguments().begin()[0].size(), 1u );
180     BOOST_CHECK_EQUAL( info.arguments().begin()[0].begin()->value(), tokens[0] );
181
182     BOOST_REQUIRE_EQUAL( info.arguments().begin()[1].size(), 1u );
183     BOOST_CHECK_EQUAL( info.arguments().begin()[1].begin()->value(), tokens[1] );
184
185     BOOST_REQUIRE_EQUAL( info.arguments().begin()[2].size(), 1u );
186     BOOST_CHECK_EQUAL( info.arguments().begin()[2].begin()->value(), tokens[2] );
187
188     BOOST_REQUIRE_EQUAL( info.arguments().begin()[3].size(), 8u );
189     for (unsigned i (0); i<8; ++i)
190         BOOST_CHECK_EQUAL( info.arguments().begin()[3].begin()[i].value(), tokens[3+i] );
191
192     BOOST_REQUIRE_EQUAL( info.arguments().begin()[4].size(), 1u );
193     BOOST_CHECK_EQUAL( info.arguments().begin()[4].begin()->value(), tokens[11] );
194
195     BOOST_REQUIRE_EQUAL( info.arguments().begin()[5].size(), 1u );
196     BOOST_CHECK_EQUAL( info.arguments().begin()[5].begin()->value(), tokens[12] );
197 }
198
199 ///////////////////////////////cc.e////////////////////////////////////////
200 #undef prefix_
201
202 \f
203 // Local Variables:
204 // mode: c++
205 // fill-column: 100
206 // comment-column: 40
207 // c-file-style: "senf"
208 // indent-tabs-mode: nil
209 // ispell-local-dictionary: "american"
210 // compile-command: "scons -u test"
211 // End: