1 |
commit: 1b7da0e59f91f963e616e8762f2d03e736908ad4 |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Thu May 26 13:51:42 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Sun May 29 11:44:46 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=1b7da0e5 |
7 |
|
8 |
Builtin: support continue built-in |
9 |
|
10 |
--- |
11 |
Makefile.am | 3 + |
12 |
src/builtins/builtin_exceptions.h | 21 ++++++ |
13 |
.../{builtin_exceptions.h => continue_builtin.cpp} | 43 ++++++++----- |
14 |
.../{builtin_exceptions.h => continue_builtin.h} | 22 ++---- |
15 |
src/builtins/tests/continue_tests.cpp | 69 ++++++++++++++++++++ |
16 |
src/cppbash_builtin.cpp | 2 + |
17 |
6 files changed, 130 insertions(+), 30 deletions(-) |
18 |
|
19 |
diff --git a/Makefile.am b/Makefile.am |
20 |
index ebe12ee..423bcc4 100644 |
21 |
--- a/Makefile.am |
22 |
+++ b/Makefile.am |
23 |
@@ -98,6 +98,7 @@ cppunittests_SOURCES = test/run_tests.cpp \ |
24 |
src/core/tests/interpreter_test.cpp \ |
25 |
src/core/tests/bash_ast_test.cpp \ |
26 |
src/core/tests/bash_condition_test.cpp \ |
27 |
+ src/builtins/tests/continue_tests.cpp \ |
28 |
src/builtins/tests/echo_tests.cpp \ |
29 |
src/builtins/tests/declare_tests.cpp \ |
30 |
src/builtins/tests/boolean_tests.cpp \ |
31 |
@@ -174,6 +175,8 @@ libcppbash_la_SOURCES = src/common.h \ |
32 |
src/libbash.cpp \ |
33 |
src/cppbash_builtin.cpp \ |
34 |
src/cppbash_builtin.h \ |
35 |
+ src/builtins/continue_builtin.cpp \ |
36 |
+ src/builtins/continue_builtin.h \ |
37 |
src/builtins/echo_builtin.cpp \ |
38 |
src/builtins/echo_builtin.h \ |
39 |
src/builtins/declare_builtin.cpp \ |
40 |
|
41 |
diff --git a/src/builtins/builtin_exceptions.h b/src/builtins/builtin_exceptions.h |
42 |
index 5548d14..a5cc28b 100644 |
43 |
--- a/src/builtins/builtin_exceptions.h |
44 |
+++ b/src/builtins/builtin_exceptions.h |
45 |
@@ -27,6 +27,8 @@ |
46 |
|
47 |
#include <stdexcept> |
48 |
|
49 |
+#include "core/interpreter_exception.h" |
50 |
+ |
51 |
/// |
52 |
/// \class return_exception |
53 |
/// \brief thrown when executing the return builtin |
54 |
@@ -38,4 +40,23 @@ public: |
55 |
runtime_error("return exception"){} |
56 |
}; |
57 |
|
58 |
+class continue_exception: public std::exception |
59 |
+{ |
60 |
+ int count; |
61 |
+public: |
62 |
+ explicit continue_exception(int c): count(c) |
63 |
+ { |
64 |
+ if(c < 1) |
65 |
+ throw interpreter_exception("continue: argument should be greater than or equal to 1"); |
66 |
+ } |
67 |
+ |
68 |
+ void rethrow_unless_correct_frame() |
69 |
+ { |
70 |
+ if(count != 1) |
71 |
+ { |
72 |
+ --count; |
73 |
+ throw *this; |
74 |
+ } |
75 |
+ } |
76 |
+}; |
77 |
#endif |
78 |
|
79 |
diff --git a/src/builtins/builtin_exceptions.h b/src/builtins/continue_builtin.cpp |
80 |
similarity index 52% |
81 |
copy from src/builtins/builtin_exceptions.h |
82 |
copy to src/builtins/continue_builtin.cpp |
83 |
index 5548d14..87dd59a 100644 |
84 |
--- a/src/builtins/builtin_exceptions.h |
85 |
+++ b/src/builtins/continue_builtin.cpp |
86 |
@@ -17,25 +17,36 @@ |
87 |
along with libbash. If not, see <http://www.gnu.org/licenses/>. |
88 |
*/ |
89 |
/// |
90 |
-/// \file builtin_exceptions.h |
91 |
+/// \file continue_builtin.h |
92 |
/// \author Mu Qiao |
93 |
-/// \brief implementations for builtin exceptions |
94 |
+/// \brief implementation for the continue builtin |
95 |
/// |
96 |
+#include <boost/lexical_cast.hpp> |
97 |
|
98 |
-#ifndef LIBBASH_BUILTINS_BUILTIN_EXCEPTIONS_H_ |
99 |
-#define LIBBASH_BUILTINS_BUILTIN_EXCEPTIONS_H_ |
100 |
+#include "builtins/builtin_exceptions.h" |
101 |
+#include "core/interpreter_exception.h" |
102 |
|
103 |
-#include <stdexcept> |
104 |
+#include "builtins/continue_builtin.h" |
105 |
|
106 |
-/// |
107 |
-/// \class return_exception |
108 |
-/// \brief thrown when executing the return builtin |
109 |
-/// |
110 |
-class return_exception: public std::runtime_error |
111 |
+int continue_builtin::exec(const std::vector<std::string>& bash_args) |
112 |
{ |
113 |
-public: |
114 |
- explicit return_exception(): |
115 |
- runtime_error("return exception"){} |
116 |
-}; |
117 |
- |
118 |
-#endif |
119 |
+ int nth = 1; |
120 |
+ |
121 |
+ if(bash_args.size() > 1) |
122 |
+ { |
123 |
+ throw interpreter_exception("continue: too many arguments"); |
124 |
+ } |
125 |
+ else if(bash_args.size() == 1) |
126 |
+ { |
127 |
+ try |
128 |
+ { |
129 |
+ nth = boost::lexical_cast<int>(bash_args[0]); |
130 |
+ } |
131 |
+ catch(boost::bad_lexical_cast& e) |
132 |
+ { |
133 |
+ throw interpreter_exception("continue: argument should be an integer"); |
134 |
+ } |
135 |
+ } |
136 |
+ |
137 |
+ throw continue_exception(nth); |
138 |
+} |
139 |
|
140 |
diff --git a/src/builtins/builtin_exceptions.h b/src/builtins/continue_builtin.h |
141 |
similarity index 64% |
142 |
copy from src/builtins/builtin_exceptions.h |
143 |
copy to src/builtins/continue_builtin.h |
144 |
index 5548d14..cd5223b 100644 |
145 |
--- a/src/builtins/builtin_exceptions.h |
146 |
+++ b/src/builtins/continue_builtin.h |
147 |
@@ -17,25 +17,19 @@ |
148 |
along with libbash. If not, see <http://www.gnu.org/licenses/>. |
149 |
*/ |
150 |
/// |
151 |
-/// \file builtin_exceptions.h |
152 |
-/// \author Mu Qiao |
153 |
-/// \brief implementations for builtin exceptions |
154 |
+/// \file continue_builtin.h |
155 |
+/// \brief implementation for the continue builtin |
156 |
/// |
157 |
+#ifndef LIBBASH_BUILTINS_CONTINUE_BUILTIN_H_ |
158 |
+#define LIBBASH_BUILTINS_CONTINUE_BUILTIN_H_ |
159 |
|
160 |
-#ifndef LIBBASH_BUILTINS_BUILTIN_EXCEPTIONS_H_ |
161 |
-#define LIBBASH_BUILTINS_BUILTIN_EXCEPTIONS_H_ |
162 |
+#include "cppbash_builtin.h" |
163 |
|
164 |
-#include <stdexcept> |
165 |
- |
166 |
-/// |
167 |
-/// \class return_exception |
168 |
-/// \brief thrown when executing the return builtin |
169 |
-/// |
170 |
-class return_exception: public std::runtime_error |
171 |
+class continue_builtin : public virtual cppbash_builtin |
172 |
{ |
173 |
public: |
174 |
- explicit return_exception(): |
175 |
- runtime_error("return exception"){} |
176 |
+ BUILTIN_CONSTRUCTOR(continue) |
177 |
+ virtual int exec(const std::vector<std::string>& ); |
178 |
}; |
179 |
|
180 |
#endif |
181 |
|
182 |
diff --git a/src/builtins/tests/continue_tests.cpp b/src/builtins/tests/continue_tests.cpp |
183 |
new file mode 100644 |
184 |
index 0000000..4a8d67c |
185 |
--- /dev/null |
186 |
+++ b/src/builtins/tests/continue_tests.cpp |
187 |
@@ -0,0 +1,69 @@ |
188 |
+/* |
189 |
+ Please use git log for copyright holder and year information |
190 |
+ |
191 |
+ This file is part of libbash. |
192 |
+ |
193 |
+ libbash is free software: you can redistribute it and/or modify |
194 |
+ it under the terms of the GNU General Public License as published by |
195 |
+ the Free Software Foundation, either version 2 of the License, or |
196 |
+ (at your option) any later version. |
197 |
+ |
198 |
+ libbash is distributed in the hope that it will be useful, |
199 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of |
200 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
201 |
+ GNU General Public License for more details. |
202 |
+ |
203 |
+ You should have received a copy of the GNU General Public License |
204 |
+ along with libbash. If not, see <http://www.gnu.org/licenses/>. |
205 |
+*/ |
206 |
+/// |
207 |
+/// \file continue_tests.cpp |
208 |
+/// \brief series of unit tests for continue builtin |
209 |
+/// |
210 |
+#include <boost/lexical_cast.hpp> |
211 |
+#include <gtest/gtest.h> |
212 |
+ |
213 |
+#include "builtins/builtin_exceptions.h" |
214 |
+#include "core/interpreter.h" |
215 |
+#include "cppbash_builtin.h" |
216 |
+ |
217 |
+TEST(continue_builtin_test, bad_argument) |
218 |
+{ |
219 |
+ interpreter walker; |
220 |
+ EXPECT_THROW(cppbash_builtin::exec("continue", {"abc"}, std::cout, std::cerr, std::cin, walker), interpreter_exception); |
221 |
+ EXPECT_THROW(cppbash_builtin::exec("continue", {"1", "2"}, std::cout, std::cerr, std::cin, walker), interpreter_exception); |
222 |
+ EXPECT_THROW(cppbash_builtin::exec("continue", {"0"}, std::cout, std::cerr, std::cin, walker), interpreter_exception); |
223 |
+ EXPECT_THROW(cppbash_builtin::exec("continue", {"-1"}, std::cout, std::cerr, std::cin, walker), interpreter_exception); |
224 |
+} |
225 |
+ |
226 |
+TEST(continue_builtin_test, throw_exception) |
227 |
+{ |
228 |
+ interpreter walker; |
229 |
+ try |
230 |
+ { |
231 |
+ cppbash_builtin::exec("continue", {}, std::cout, std::cerr, std::cin, walker); |
232 |
+ FAIL(); |
233 |
+ } |
234 |
+ catch(continue_exception& e) |
235 |
+ { |
236 |
+ EXPECT_NO_THROW(e.rethrow_unless_correct_frame()); |
237 |
+ } |
238 |
+ |
239 |
+ try |
240 |
+ { |
241 |
+ cppbash_builtin::exec("continue", {"2"}, std::cout, std::cerr, std::cin, walker); |
242 |
+ FAIL(); |
243 |
+ } |
244 |
+ catch(continue_exception& e) |
245 |
+ { |
246 |
+ try |
247 |
+ { |
248 |
+ e.rethrow_unless_correct_frame(); |
249 |
+ FAIL(); |
250 |
+ } |
251 |
+ catch(continue_exception& e) |
252 |
+ { |
253 |
+ EXPECT_NO_THROW(e.rethrow_unless_correct_frame()); |
254 |
+ } |
255 |
+ } |
256 |
+} |
257 |
|
258 |
diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp |
259 |
index 98d3acc..c762f5f 100644 |
260 |
--- a/src/cppbash_builtin.cpp |
261 |
+++ b/src/cppbash_builtin.cpp |
262 |
@@ -25,6 +25,7 @@ |
263 |
#include "cppbash_builtin.h" |
264 |
|
265 |
#include "builtins/boolean_builtins.h" |
266 |
+#include "builtins/continue_builtin.h" |
267 |
#include "builtins/declare_builtin.h" |
268 |
#include "builtins/echo_builtin.h" |
269 |
#include "builtins/inherit_builtin.h" |
270 |
@@ -40,6 +41,7 @@ cppbash_builtin::cppbash_builtin(BUILTIN_ARGS): _out_stream(&out), _err_stream(& |
271 |
|
272 |
cppbash_builtin::builtins_type& cppbash_builtin::builtins() { |
273 |
static boost::scoped_ptr<builtins_type> p(new builtins_type { |
274 |
+ {"continue", boost::factory<continue_builtin*>()}, |
275 |
{"echo", boost::factory<echo_builtin*>()}, |
276 |
{"declare", boost::factory<declare_builtin*>()}, |
277 |
{"source", boost::factory<source_builtin*>()}, |