public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Petteri Räty" <betelgeuse@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/libbash:master commit in: src/, /, src/builtins/tests/, src/builtins/
Date: Sat, 25 Jun 2011 10:05:49 +0000 (UTC)	[thread overview]
Message-ID: <a0fa189c0f2322ab00e2034b0d01a98da4e36625.betelgeuse@gentoo> (raw)

commit:     a0fa189c0f2322ab00e2034b0d01a98da4e36625
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Mon Jun 20 14:10:54 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Thu Jun 23 03:24:42 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=a0fa189c

Builtin: implement break built-in

---
 Makefile.am                        |    3 ++
 src/builtins/break_builtin.cpp     |   51 ++++++++++++++++++++++++++
 src/builtins/break_builtin.h       |   35 ++++++++++++++++++
 src/builtins/tests/break_tests.cpp |   69 ++++++++++++++++++++++++++++++++++++
 src/cppbash_builtin.cpp            |    2 +
 5 files changed, 160 insertions(+), 0 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index a706c43..3c70a0f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -101,6 +101,7 @@ cppunittests_SOURCES =  test/run_tests.cpp \
 						src/core/tests/bash_ast_test.cpp \
 						src/core/tests/bash_condition_test.cpp \
 						src/builtins/tests/continue_tests.cpp \
+						src/builtins/tests/break_tests.cpp \
 						src/builtins/tests/echo_tests.cpp \
 						src/builtins/tests/declare_tests.cpp \
 						src/builtins/tests/boolean_tests.cpp \
@@ -181,6 +182,8 @@ libcppbash_la_SOURCES = src/common.h \
 						src/cppbash_builtin.h \
 						src/builtins/continue_builtin.cpp \
 						src/builtins/continue_builtin.h \
+						src/builtins/break_builtin.cpp \
+						src/builtins/break_builtin.h \
 						src/builtins/echo_builtin.cpp \
 						src/builtins/echo_builtin.h \
 						src/builtins/eval_builtin.cpp \

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*>()},



             reply	other threads:[~2011-06-25 10:06 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-25 10:05 Petteri Räty [this message]
  -- strict thread matches above, loose matches on Subject: below --
2012-07-08  9:31 [gentoo-commits] proj/libbash:master commit in: src/, /, src/builtins/tests/, src/builtins/ Petteri Räty
2012-07-08  9:31 Petteri Räty
2011-05-29 11:20 Petteri Räty
2011-05-25 19:42 Petteri Räty
2011-05-24 14:50 Petteri Räty
2011-03-06 12:05 Petteri Räty
2011-03-06 12:05 Petteri Räty

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a0fa189c0f2322ab00e2034b0d01a98da4e36625.betelgeuse@gentoo \
    --to=betelgeuse@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox