From: "Petteri Räty" <betelgeuse@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/libbash:master commit in: src/builtins/tests/, src/builtins/
Date: Wed, 25 May 2011 19:42:37 +0000 (UTC) [thread overview]
Message-ID: <811fd37b618ff770549e53ade07cfdd3afb0baab.betelgeuse@gentoo> (raw)
commit: 811fd37b618ff770549e53ade07cfdd3afb0baab
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Wed May 25 07:43:15 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Wed May 25 13:44:49 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=811fd37b
Builtin: support declare -F and +F
---
src/builtins/declare_builtin.cpp | 29 ++++++++++++++++++++++++++++-
src/builtins/tests/declare_tests.cpp | 27 +++++++++++++++++++++++++--
2 files changed, 53 insertions(+), 3 deletions(-)
diff --git a/src/builtins/declare_builtin.cpp b/src/builtins/declare_builtin.cpp
index 5d21bb3..90b127f 100644
--- a/src/builtins/declare_builtin.cpp
+++ b/src/builtins/declare_builtin.cpp
@@ -21,8 +21,11 @@
/// \author Mu Qiao
/// \brief class that implements the declare builtin
///
+#include <algorithm>
#include <iostream>
+#include "core/interpreter.h"
+
#include "builtins/declare_builtin.h"
int declare_builtin::exec(const std::vector<std::string>& bash_args)
@@ -44,12 +47,36 @@ int declare_builtin::exec(const std::vector<std::string>& bash_args)
return 1;
}
+ int result = 0;
switch(bash_args[0][1])
{
+ case 'F':
+ if(bash_args[0][0] == '+')
+ return 0;
+ if(bash_args.size() > 1)
+ {
+ for(auto iter = bash_args.begin() + 1; iter != bash_args.end(); ++iter)
+ {
+ if(_walker.has_function(*iter))
+ *_out_stream << *iter << std::endl;
+ else
+ result = 1;
+ }
+ }
+ else
+ {
+ std::vector<std::string> functions;
+
+ _walker.get_all_function_names(functions);
+ sort(functions.begin(), functions.end());
+
+ for(auto iter = functions.begin(); iter != functions.end(); ++iter)
+ *_out_stream << "declare -f " << *iter << std::endl;
+ }
+ return result;
case 'a':
case 'A':
case 'f':
- case 'F':
case 'i':
case 'l':
case 'r':
diff --git a/src/builtins/tests/declare_tests.cpp b/src/builtins/tests/declare_tests.cpp
index 925c5b9..21657dd 100644
--- a/src/builtins/tests/declare_tests.cpp
+++ b/src/builtins/tests/declare_tests.cpp
@@ -27,6 +27,7 @@
#include <gtest/gtest.h>
+#include "core/bash_ast.h"
#include "core/interpreter.h"
#include "cppbash_builtin.h"
@@ -40,13 +41,36 @@ static void test_declare(const string& expected, std::initializer_list<string> a
EXPECT_EQ(expected, test_output.str());
}
+TEST(declare_builtin_test, _F)
+{
+ stringstream expression("function foo() { :; }; function bar() { :; }");
+ interpreter walker;
+ bash_ast ast(expression);
+ ast.interpret_with(walker);
+
+ stringstream test_output1;
+ EXPECT_EQ(0, cppbash_builtin::exec("declare", {"-F", "foo"}, test_output1, cerr, cin, walker));
+ EXPECT_EQ("foo\n", test_output1.str());
+
+ stringstream test_output2;
+ EXPECT_EQ(1, cppbash_builtin::exec("declare", {"-F", "foo", "bar", "test"}, test_output2, cerr, cin, walker));
+ EXPECT_EQ("foo\nbar\n", test_output2.str());
+
+ stringstream test_output3;
+ EXPECT_EQ(0, cppbash_builtin::exec("declare", {"+F", "foo", "bar", "test"}, test_output3, cerr, cin, walker));
+ EXPECT_EQ("", test_output3.str());
+
+ stringstream test_output4;
+ EXPECT_EQ(0, cppbash_builtin::exec("declare", {"-F"}, test_output3, cerr, cin, walker));
+ EXPECT_EQ("declare -f bar\ndeclare -f foo\n", test_output3.str());
+}
+
#define TEST_DECLARE(name, expected, ...) \
TEST(declare_builtin_test, name) { test_declare(expected, {__VA_ARGS__}); }
TEST_DECLARE(_a, "declare -a is not supported yet\n", "-a", "world")
TEST_DECLARE(_A, "declare -A is not supported yet\n", "-A", "world")
TEST_DECLARE(_f, "declare -f is not supported yet\n", "-f", "world")
-TEST_DECLARE(_F, "declare -F is not supported yet\n", "-F", "world")
TEST_DECLARE(_i, "declare -i is not supported yet\n", "-i", "world")
TEST_DECLARE(_l, "declare -l is not supported yet\n", "-l", "world")
TEST_DECLARE(_r, "declare -r is not supported yet\n", "-r", "world")
@@ -57,7 +81,6 @@ TEST_DECLARE(_p, "declare -p is not supported yet\n", "-p", "world")
TEST_DECLARE(pa, "declare +a is not supported yet\n", "+a", "world")
TEST_DECLARE(pA, "declare +A is not supported yet\n", "+A", "world")
TEST_DECLARE(pf, "declare +f is not supported yet\n", "+f", "world")
-TEST_DECLARE(pF, "declare +F is not supported yet\n", "+F", "world")
TEST_DECLARE(pi, "declare +i is not supported yet\n", "+i", "world")
TEST_DECLARE(pl, "declare +l is not supported yet\n", "+l", "world")
TEST_DECLARE(pr, "declare +r is not supported yet\n", "+r", "world")
next reply other threads:[~2011-05-25 19:42 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-25 19:42 Petteri Räty [this message]
-- strict thread matches above, loose matches on Subject: below --
2012-06-03 9:08 [gentoo-commits] proj/libbash:master commit in: src/builtins/tests/, src/builtins/ Petteri Räty
2011-06-25 10:05 Petteri Räty
2011-06-09 7:27 Petteri Räty
2011-05-27 23:03 Petteri Räty
2011-05-25 19:42 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=811fd37b618ff770549e53ade07cfdd3afb0baab.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