aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2011-06-20 22:10:54 +0800
committerMu Qiao <qiaomuf@gentoo.org>2011-06-23 11:24:42 +0800
commita0fa189c0f2322ab00e2034b0d01a98da4e36625 (patch)
tree957bc80b44ce19e1d36d1dc5a14927edf69630f8 /src
parentCore: abstract the role of continue exception (diff)
downloadlibbash-a0fa189c0f2322ab00e2034b0d01a98da4e36625.tar.gz
libbash-a0fa189c0f2322ab00e2034b0d01a98da4e36625.tar.bz2
libbash-a0fa189c0f2322ab00e2034b0d01a98da4e36625.zip
Builtin: implement break built-in
Diffstat (limited to 'src')
-rw-r--r--src/builtins/break_builtin.cpp51
-rw-r--r--src/builtins/break_builtin.h35
-rw-r--r--src/builtins/tests/break_tests.cpp69
-rw-r--r--src/cppbash_builtin.cpp2
4 files changed, 157 insertions, 0 deletions
diff --git a/src/builtins/break_builtin.cpp b/src/builtins/break_builtin.cpp
new file mode 100644
index 0000000..0ee20a7
--- /dev/null
+++ b/src/builtins/break_builtin.cpp
@@ -0,0 +1,51 @@
+/*
+ Please use git log for copyright holder and year information
+
+ This file is part of libbash.
+
+ libbash is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ libbash is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with libbash. If not, see <http://www.gnu.org/licenses/>.
+*/
+///
+/// \file break_builtin.h
+/// \brief implementation for the break builtin
+///
+#include "builtins/break_builtin.h"
+
+#include <boost/lexical_cast.hpp>
+
+#include "builtins/builtin_exceptions.h"
+#include "core/interpreter_exception.h"
+
+int break_builtin::exec(const std::vector<std::string>& bash_args)
+{
+ int nth = 1;
+
+ if(bash_args.size() > 1)
+ {
+ throw libbash::interpreter_exception("break: too many arguments");
+ }
+ else if(bash_args.size() == 1)
+ {
+ try
+ {
+ nth = boost::lexical_cast<int>(bash_args[0]);
+ }
+ catch(boost::bad_lexical_cast& e)
+ {
+ throw libbash::interpreter_exception("break: argument should be an integer");
+ }
+ }
+
+ throw break_exception(nth);
+}
diff --git a/src/builtins/break_builtin.h b/src/builtins/break_builtin.h
new file mode 100644
index 0000000..fec1bc7
--- /dev/null
+++ b/src/builtins/break_builtin.h
@@ -0,0 +1,35 @@
+/*
+ Please use git log for copyright holder and year information
+
+ This file is part of libbash.
+
+ libbash is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ libbash is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with libbash. If not, see <http://www.gnu.org/licenses/>.
+*/
+///
+/// \file break_builtin.h
+/// \brief implementation for the break builtin
+///
+#ifndef LIBBASH_BUILTINS_BREAK_BUILTIN_H_
+#define LIBBASH_BUILTINS_BREAK_BUILTIN_H_
+
+#include "cppbash_builtin.h"
+
+class break_builtin : public virtual cppbash_builtin
+{
+public:
+ BUILTIN_CONSTRUCTOR(break)
+ virtual int exec(const std::vector<std::string>& );
+};
+
+#endif
diff --git a/src/builtins/tests/break_tests.cpp b/src/builtins/tests/break_tests.cpp
new file mode 100644
index 0000000..eba422e
--- /dev/null
+++ b/src/builtins/tests/break_tests.cpp
@@ -0,0 +1,69 @@
+/*
+ Please use git log for copyright holder and year information
+
+ This file is part of libbash.
+
+ libbash is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ libbash is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with libbash. If not, see <http://www.gnu.org/licenses/>.
+*/
+///
+/// \file break_tests.cpp
+/// \brief series of unit tests for break builtin
+///
+#include <boost/lexical_cast.hpp>
+#include <gtest/gtest.h>
+
+#include "builtins/builtin_exceptions.h"
+#include "core/interpreter.h"
+#include "cppbash_builtin.h"
+
+TEST(break_builtin_test, bad_argument)
+{
+ interpreter walker;
+ EXPECT_THROW(cppbash_builtin::exec("break", {"abc"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception);
+ EXPECT_THROW(cppbash_builtin::exec("break", {"1", "2"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception);
+ EXPECT_THROW(cppbash_builtin::exec("break", {"0"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception);
+ EXPECT_THROW(cppbash_builtin::exec("break", {"-1"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception);
+}
+
+TEST(break_builtin_test, throw_exception)
+{
+ interpreter walker;
+ try
+ {
+ cppbash_builtin::exec("break", {}, std::cout, std::cerr, std::cin, walker);
+ FAIL();
+ }
+ catch(break_exception& e)
+ {
+ EXPECT_NO_THROW(e.rethrow_unless_correct_frame());
+ }
+
+ try
+ {
+ cppbash_builtin::exec("break", {"2"}, std::cout, std::cerr, std::cin, walker);
+ FAIL();
+ }
+ catch(break_exception& e)
+ {
+ try
+ {
+ e.rethrow_unless_correct_frame();
+ FAIL();
+ }
+ catch(break_exception& e)
+ {
+ EXPECT_NO_THROW(e.rethrow_unless_correct_frame());
+ }
+ }
+}
diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp
index 97ab789..52c5c9b 100644
--- a/src/cppbash_builtin.cpp
+++ b/src/cppbash_builtin.cpp
@@ -29,6 +29,7 @@
#include "builtins/boolean_builtins.h"
#include "builtins/builtin_exceptions.h"
+#include "builtins/break_builtin.h"
#include "builtins/continue_builtin.h"
#include "builtins/declare_builtin.h"
#include "builtins/echo_builtin.h"
@@ -51,6 +52,7 @@ cppbash_builtin::cppbash_builtin(BUILTIN_ARGS): _out_stream(&out), _err_stream(&
cppbash_builtin::builtins_type& cppbash_builtin::builtins() {
static boost::scoped_ptr<builtins_type> p(new builtins_type {
+ {"break", boost::factory<break_builtin*>()},
{"continue", boost::factory<continue_builtin*>()},
{"echo", boost::factory<echo_builtin*>()},
{"eval", boost::factory<eval_builtin*>()},