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