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