Console: Added lots of unit-tests
[senf.git] / Console / Executor.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 Executor.test unit tests */
25
26 //#include "Executor.test.hh"
27 //#include "Executor.test.ih"
28
29 // Custom includes
30 #include <sstream>
31 #include <vector>
32 #include "Executor.hh"
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     std::vector<senf::console::ParseCommandInfo> commands;
42     void setCommand(senf::console::ParseCommandInfo const & cmd) {
43         commands.push_back(cmd);
44     }
45     void testCommand(std::ostream & os, senf::console::Executor::Arguments) {
46         os << "testCommand\n";
47     }
48 }
49
50 BOOST_AUTO_UNIT_TEST(executor)
51 {
52     senf::console::root().mkdir("dir1").mkdir("dir3");
53     senf::console::root().mkdir("dir2").doc("Helptext").add("test",&testCommand);
54
55     senf::console::Executor executor;
56     senf::console::CommandParser parser;
57
58     BOOST_CHECK( &executor.cwd() == &senf::console::root() );
59     
60     {
61         std::stringstream os;
62         parser.parse("cd dir1", &setCommand);
63         executor(commands.back(), os);
64         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinCD );
65         BOOST_CHECK( &executor.cwd() == &senf::console::root()["dir1"] );
66         BOOST_CHECK_EQUAL( os.str(), "" );
67     }
68
69     {
70         std::stringstream os;
71         parser.parse("cd /dir2", &setCommand);
72         executor(commands.back(), os);
73         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinCD );
74         BOOST_CHECK( &executor.cwd() == &senf::console::root()["dir2"] );
75         BOOST_CHECK_EQUAL( os.str(), "" );
76     }
77
78     {
79         std::stringstream os;
80         parser.parse("cd dir1", &setCommand);
81         executor(commands.back(), os);
82         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinCD );
83         BOOST_CHECK( &executor.cwd() == &senf::console::root()["dir2"] );
84         BOOST_CHECK_EQUAL( os.str(), "invalid directory\n" );
85     }
86
87     {
88         std::stringstream os;
89         parser.parse("cd /", &setCommand);
90         executor(commands.back(), os);
91         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinCD );
92         BOOST_CHECK( &executor.cwd() == &senf::console::root() );
93         BOOST_CHECK_EQUAL( os.str(), "" );
94     }
95
96     {
97         std::stringstream os;
98         parser.parse("ls", &setCommand);
99         executor(commands.back(), os);
100         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinLS );
101         BOOST_CHECK_EQUAL( os.str(), "dir1/\ndir2/\n" );
102     }
103
104     {
105         std::stringstream os;
106         parser.parse("ls dir1", &setCommand);
107         executor(commands.back(), os);
108         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinLS );
109         BOOST_CHECK_EQUAL( os.str(), "dir3/\n" );
110     }
111
112     {
113         std::stringstream os;
114         parser.parse("ls dir3", &setCommand);
115         executor(commands.back(), os);
116         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinLS );
117         BOOST_CHECK_EQUAL( os.str(), "invalid directory\n" );
118     }
119     
120     {
121         std::stringstream os;
122         parser.parse("dir1/dir3 { }", &setCommand);
123         executor(commands.rbegin()[1], os);
124         BOOST_CHECK_EQUAL( commands.rbegin()[1].builtin(), senf::console::ParseCommandInfo::BuiltinPUSHD );
125         BOOST_CHECK( &executor.cwd() == &senf::console::root()["dir1"]["dir3"] );
126         BOOST_CHECK_EQUAL( os.str(), "" );
127     }
128
129     {
130         std::stringstream os;
131         executor(commands.back(), os);
132         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinPOPD );
133         BOOST_CHECK( &executor.cwd() == &senf::console::root() );
134         BOOST_CHECK_EQUAL( os.str(), "" );
135     }
136
137     {
138         std::stringstream os;
139         parser.parse("exit", &setCommand);
140         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinEXIT );
141         BOOST_CHECK_THROW( executor(commands.back(), os), senf::console::Executor::ExitException );
142         BOOST_CHECK_EQUAL( os.str(), "" );
143     }
144
145     {
146         std::stringstream os;
147         parser.parse("help /dir2", &setCommand);
148         executor(commands.back(), os);
149         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::BuiltinHELP );
150         BOOST_CHECK_EQUAL( os.str(), "senf::console::DirectoryNode at /dir2\n\nHelptext\n" );
151     }
152
153     {
154         std::stringstream os;
155         parser.parse("dir2/test", &setCommand);
156         executor(commands.back(), os);
157         BOOST_CHECK_EQUAL( commands.back().builtin(), senf::console::ParseCommandInfo::NoBuiltin );
158         BOOST_CHECK_EQUAL( os.str(), "testCommand\n" );
159     }
160
161     commands.clear();
162     senf::console::root().remove("dir1");
163     senf::console::root().remove("dir2");
164 }
165
166 ///////////////////////////////cc.e////////////////////////////////////////
167 #undef prefix_
168
169 \f
170 // Local Variables:
171 // mode: c++
172 // fill-column: 100
173 // comment-column: 40
174 // c-file-style: "senf"
175 // indent-tabs-mode: nil
176 // ispell-local-dictionary: "american"
177 // compile-command: "scons -u test"
178 // End: