1 |
commit: b0d4f64632b09ffdc8cbba767ae723469e26485a |
2 |
Author: André Aparício <aparicio99 <AT> gmail <DOT> com> |
3 |
AuthorDate: Tue May 29 01:16:26 2012 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Sun Jul 8 09:22:27 2012 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=b0d4f646 |
7 |
|
8 |
Builtin: Implement unset array support |
9 |
|
10 |
--- |
11 |
Makefile.am | 1 + |
12 |
src/builtins/tests/unset_tests.cpp | 50 ++++++++++++++++++++++++++++++++++++ |
13 |
src/builtins/unset_builtin.cpp | 11 +++++++- |
14 |
src/cppbash_builtin.cpp | 1 + |
15 |
4 files changed, 62 insertions(+), 1 deletions(-) |
16 |
|
17 |
diff --git a/Makefile.am b/Makefile.am |
18 |
index cc57503..9cb00c9 100644 |
19 |
--- a/Makefile.am |
20 |
+++ b/Makefile.am |
21 |
@@ -107,6 +107,7 @@ cppunittests_SOURCES = test/run_tests.cpp \ |
22 |
src/builtins/tests/return_tests.cpp \ |
23 |
src/builtins/tests/read_tests.cpp \ |
24 |
src/builtins/tests/set_tests.cpp \ |
25 |
+ src/builtins/tests/unset_tests.cpp \ |
26 |
src/builtins/tests/printf_tests.cpp \ |
27 |
test/test.h \ |
28 |
test/test.cpp \ |
29 |
|
30 |
diff --git a/src/builtins/tests/unset_tests.cpp b/src/builtins/tests/unset_tests.cpp |
31 |
new file mode 100644 |
32 |
index 0000000..8350507 |
33 |
--- /dev/null |
34 |
+++ b/src/builtins/tests/unset_tests.cpp |
35 |
@@ -0,0 +1,50 @@ |
36 |
+/* |
37 |
+ Please use git log for copyright holder and year information |
38 |
+ |
39 |
+ This file is part of libbash. |
40 |
+ |
41 |
+ libbash is free software: you can redistribute it and/or modify |
42 |
+ it under the terms of the GNU General Public License as published by |
43 |
+ the Free Software Foundation, either version 2 of the License, or |
44 |
+ (at your option) any later version. |
45 |
+ |
46 |
+ libbash is distributed in the hope that it will be useful, |
47 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of |
48 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
49 |
+ GNU General Public License for more details. |
50 |
+ |
51 |
+ You should have received a copy of the GNU General Public License |
52 |
+ along with libbash. If not, see <http://www.gnu.org/licenses/>. |
53 |
+*/ |
54 |
+/// |
55 |
+/// \file unset_tests.cpp |
56 |
+/// \brief series of unit tests for unset builtin |
57 |
+/// |
58 |
+#include <boost/lexical_cast.hpp> |
59 |
+#include <gtest/gtest.h> |
60 |
+ |
61 |
+#include "core/interpreter.h" |
62 |
+#include "cppbash_builtin.h" |
63 |
+#include "exceptions.h" |
64 |
+ |
65 |
+TEST(unset_builtin_test, normal) |
66 |
+{ |
67 |
+ interpreter walker; |
68 |
+ |
69 |
+ walker.set_value("var", "foo"); |
70 |
+ EXPECT_EQ(0, cppbash_builtin::exec("unset", {"var"}, std::cout, std::cerr, std::cin, walker)); |
71 |
+ EXPECT_STREQ("", walker.resolve<std::string>("var").c_str()); |
72 |
+} |
73 |
+ |
74 |
+TEST(unset_builtin_test, array) |
75 |
+{ |
76 |
+ interpreter walker; |
77 |
+ |
78 |
+ walker.set_value("array", "foo bar", 2); |
79 |
+ EXPECT_EQ(0, cppbash_builtin::exec("unset", {"array"}, std::cout, std::cerr, std::cin, walker)); |
80 |
+ EXPECT_STREQ("", walker.resolve<std::string>("array", 2).c_str()); |
81 |
+ |
82 |
+ walker.set_value("array", "foo bar", 2); |
83 |
+ EXPECT_EQ(0, cppbash_builtin::exec("unset", {"array[2]"}, std::cout, std::cerr, std::cin, walker)); |
84 |
+ EXPECT_STREQ("", walker.resolve<std::string>("array", 2).c_str()); |
85 |
+} |
86 |
|
87 |
diff --git a/src/builtins/unset_builtin.cpp b/src/builtins/unset_builtin.cpp |
88 |
index d8178f9..2eaafc2 100644 |
89 |
--- a/src/builtins/unset_builtin.cpp |
90 |
+++ b/src/builtins/unset_builtin.cpp |
91 |
@@ -26,6 +26,8 @@ |
92 |
|
93 |
#include "core/interpreter.h" |
94 |
|
95 |
+using namespace boost::xpressive; |
96 |
+ |
97 |
int unset_builtin::exec(const std::vector<std::string>& bash_args) |
98 |
{ |
99 |
if(bash_args.empty()) |
100 |
@@ -51,7 +53,14 @@ int unset_builtin::exec(const std::vector<std::string>& bash_args) |
101 |
* */ |
102 |
for_each(bash_args.front() == "-v" ? bash_args.begin() + 1 : bash_args.begin(), |
103 |
bash_args.end(), |
104 |
- [&](const std::string& name) { _walker.unset(name); }); |
105 |
+ [&](const std::string& name) { |
106 |
+ static const sregex index_pattern = sregex::compile("^(.*)\\[(\\d*)\\]$"); |
107 |
+ smatch match; |
108 |
+ if(regex_match(name, match, index_pattern)) |
109 |
+ _walker.unset(match[1], boost::lexical_cast<unsigned int>(match[2])); |
110 |
+ else |
111 |
+ _walker.unset(name); |
112 |
+ }); |
113 |
|
114 |
return 0; |
115 |
} |
116 |
|
117 |
diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp |
118 |
index e4a7c29..51cd15c 100644 |
119 |
--- a/src/cppbash_builtin.cpp |
120 |
+++ b/src/cppbash_builtin.cpp |
121 |
@@ -46,6 +46,7 @@ |
122 |
#include "builtins/unset_builtin.h" |
123 |
#include "builtins/read_builtin.h" |
124 |
#include "builtins/set_builtin.h" |
125 |
+#include "builtins/unset_builtin.h" |
126 |
|
127 |
namespace qi = boost::spirit::qi; |
128 |
namespace karma = boost::spirit::karma; |