aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2011-06-23 17:46:16 +0800
committerPetteri Räty <petsku@petteriraty.eu>2011-07-03 23:09:19 +0300
commit2bd9927e275728d55698592cc7534b636d34f797 (patch)
tree73252dc6d69ba161b3dc9a1b223e3031fe6a3b20 /src/builtins
parentBuild: remove auto generated files (diff)
downloadlibbash-2bd9927e275728d55698592cc7534b636d34f797.tar.gz
libbash-2bd9927e275728d55698592cc7534b636d34f797.tar.bz2
libbash-2bd9927e275728d55698592cc7534b636d34f797.zip
Builtin: support shift built-in
Diffstat (limited to 'src/builtins')
-rw-r--r--src/builtins/shift_builtin.cpp44
-rw-r--r--src/builtins/shift_builtin.h35
-rw-r--r--src/builtins/tests/shift_tests.cpp69
3 files changed, 148 insertions, 0 deletions
diff --git a/src/builtins/shift_builtin.cpp b/src/builtins/shift_builtin.cpp
new file mode 100644
index 0000000..c78c6ae
--- /dev/null
+++ b/src/builtins/shift_builtin.cpp
@@ -0,0 +1,44 @@
+/*
+ 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 shift_builtin.h
+/// \brief implementation for the shift builtin
+///
+#include "builtins/shift_builtin.h"
+
+#include <boost/lexical_cast.hpp>
+
+#include "builtins/builtin_exceptions.h"
+#include "core/exceptions.h"
+#include "core/interpreter.h"
+
+int shift_builtin::exec(const std::vector<std::string>& bash_args)
+{
+ int shift_number = 1;
+
+ if(!bash_args.empty())
+ {
+ if(bash_args.size() != 1)
+ throw libbash::illegal_argument_exception("shift: the number of arguments should be 1");
+
+ shift_number = boost::lexical_cast<int>(bash_args[0]);
+ }
+
+ return _walker.shift(shift_number);
+}
diff --git a/src/builtins/shift_builtin.h b/src/builtins/shift_builtin.h
new file mode 100644
index 0000000..c413380
--- /dev/null
+++ b/src/builtins/shift_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 shift_builtin.h
+/// \brief implementation for the shift builtin
+///
+#ifndef LIBBASH_BUILTINS_SHIFT_BUILTIN_H_
+#define LIBBASH_BUILTINS_SHIFT_BUILTIN_H_
+
+#include "cppbash_builtin.h"
+
+class shift_builtin : public virtual cppbash_builtin
+{
+public:
+ BUILTIN_CONSTRUCTOR(shift)
+ virtual int exec(const std::vector<std::string>& );
+};
+
+#endif
diff --git a/src/builtins/tests/shift_tests.cpp b/src/builtins/tests/shift_tests.cpp
new file mode 100644
index 0000000..8625a6c
--- /dev/null
+++ b/src/builtins/tests/shift_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 shift_tests.cpp
+/// \brief series of unit tests for shift builtin
+///
+#include <boost/lexical_cast.hpp>
+#include <gtest/gtest.h>
+
+#include "core/exceptions.h"
+#include "core/interpreter.h"
+#include "cppbash_builtin.h"
+
+TEST(shift_builtin_test, bad_argument)
+{
+ interpreter walker;
+ std::map<unsigned, std::string> values = {{1, "1"}, {2, "2"}, {3, "3"}};
+ walker.define("*", values);
+
+ EXPECT_NE(0, cppbash_builtin::exec("shift", {"-1"}, std::cout, std::cerr, std::cin, walker));
+ EXPECT_NE(0, cppbash_builtin::exec("shift", {"4"}, std::cout, std::cerr, std::cin, walker));
+ EXPECT_THROW(cppbash_builtin::exec("shift", {"1", "2"}, std::cout, std::cerr, std::cin, walker),
+ libbash::illegal_argument_exception);
+ EXPECT_THROW(cppbash_builtin::exec("shift", {"abc"}, std::cout, std::cerr, std::cin, walker),
+ boost::bad_lexical_cast);
+}
+
+TEST(shift_builtin_test, shift_all)
+{
+ interpreter walker;
+ std::map<unsigned, std::string> values = {{1, "1"}, {2, "2"}, {3, "3"}};
+ walker.define("*", values);
+
+ EXPECT_EQ(0, cppbash_builtin::exec("shift", {"3"}, std::cout, std::cerr, std::cin, walker));
+ EXPECT_EQ(0, walker.get_array_length("*"));
+}
+
+TEST(shift_builtin_test, normal)
+{
+ interpreter walker;
+ std::map<unsigned, std::string> values = {{1, "1"}, {2, "2"}, {3, "3"}};
+ walker.define("*", values);
+
+ EXPECT_EQ(0, cppbash_builtin::exec("shift", {"2"}, std::cout, std::cerr, std::cin, walker));
+ EXPECT_EQ(1, walker.get_array_length("*"));
+ EXPECT_STREQ("3", walker.resolve<std::string>("*", 1).c_str());
+
+ walker.define("*", values);
+ EXPECT_EQ(0, cppbash_builtin::exec("shift", {"1"}, std::cout, std::cerr, std::cin, walker));
+ EXPECT_EQ(2, walker.get_array_length("*"));
+ EXPECT_STREQ("2", walker.resolve<std::string>("*", 1).c_str());
+ EXPECT_STREQ("3", walker.resolve<std::string>("*", 2).c_str());
+}