aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2011-06-22 23:01:11 +0800
committerMu Qiao <qiaomuf@gentoo.org>2011-06-22 23:07:30 +0800
commit3016ad21687f5eb9f88c0e159536c4bfc23bfbd1 (patch)
treeb99245f122d619b35b594373078a68570d3c4141 /src
parentCore: improve current exception usage (diff)
downloadlibbash-3016ad21687f5eb9f88c0e159536c4bfc23bfbd1.tar.gz
libbash-3016ad21687f5eb9f88c0e159536c4bfc23bfbd1.tar.bz2
libbash-3016ad21687f5eb9f88c0e159536c4bfc23bfbd1.zip
Builtin: make declare built-in throw exceptions
Diffstat (limited to 'src')
-rw-r--r--src/builtins/declare_builtin.cpp11
-rw-r--r--src/builtins/tests/declare_tests.cpp70
2 files changed, 47 insertions, 34 deletions
diff --git a/src/builtins/declare_builtin.cpp b/src/builtins/declare_builtin.cpp
index 412b9ba..2644bb7 100644
--- a/src/builtins/declare_builtin.cpp
+++ b/src/builtins/declare_builtin.cpp
@@ -25,24 +25,25 @@
#include <algorithm>
#include <iostream>
+#include "core/exceptions.h"
#include "core/interpreter.h"
int declare_builtin::exec(const std::vector<std::string>& bash_args)
{
if(bash_args.empty())
{
- *_err_stream << "Arguments required for declare" << std::endl;
+ throw libbash::illegal_argument_exception("Arguments required for declare");
return 1;
}
else if(bash_args[0].size() != 2)
{
- *_err_stream << "Multiple arguments are not supported" << std::endl;
+ throw libbash::unsupported_exception("Multiple arguments are not supported");
return 1;
}
if(bash_args[0][0] != '-' && bash_args[0][0] != '+')
{
- *_err_stream << "Invalid option for declare builtin" << std::endl;
+ throw libbash::illegal_argument_exception("Invalid option for declare builtin");
return 1;
}
@@ -104,10 +105,10 @@ int declare_builtin::exec(const std::vector<std::string>& bash_args)
case 't':
case 'u':
case 'x':
- *_err_stream << "declare " << bash_args[0] << " is not supported yet" << std::endl;
+ throw libbash::unsupported_exception("declare " + bash_args[0] + " is not supported yet");
return 1;
default:
- *_err_stream << "Unrecognized option for declare: " << bash_args[0] << std::endl;
+ throw libbash::illegal_argument_exception("Unrecognized option for declare: " + bash_args[0]);
return 1;
}
}
diff --git a/src/builtins/tests/declare_tests.cpp b/src/builtins/tests/declare_tests.cpp
index 2bedfd3..95f4b61 100644
--- a/src/builtins/tests/declare_tests.cpp
+++ b/src/builtins/tests/declare_tests.cpp
@@ -27,25 +27,37 @@
#include <gtest/gtest.h>
#include "core/bash_ast.h"
+#include "core/exceptions.h"
#include "core/interpreter.h"
#include "cppbash_builtin.h"
using namespace std;
-static void test_declare(const string& expected, std::initializer_list<string> args)
+namespace
{
- stringstream test_output;
- interpreter walker;
- cppbash_builtin::exec("declare",args,cout,test_output,cin,walker);
- EXPECT_EQ(expected, test_output.str());
+ template <typename T>
+ void test_declare(const string& expected, std::initializer_list<string> args)
+ {
+ stringstream test_output;
+ interpreter walker;
+ try
+ {
+ cppbash_builtin::exec("declare",args,cout,test_output,cin,walker);
+ FAIL();
+ }
+ catch(T& e)
+ {
+ EXPECT_EQ(expected, e.what());
+ }
+ }
}
TEST(declare_builtin_test, invalid_arguments)
{
- test_declare("Arguments required for declare\n", {});
- test_declare("Multiple arguments are not supported\n", {"-ap"});
- test_declare("Invalid option for declare builtin\n", {"_a"});
- test_declare("Unrecognized option for declare: -L\n", {"-L"});
+ test_declare<libbash::illegal_argument_exception>("Arguments required for declare", {});
+ test_declare<libbash::unsupported_exception>("Multiple arguments are not supported", {"-ap"});
+ test_declare<libbash::illegal_argument_exception>("Invalid option for declare builtin", {"_a"});
+ test_declare<libbash::illegal_argument_exception>("Unrecognized option for declare: -L", {"-L"});
}
TEST(declare_builtin_test, _F)
@@ -87,23 +99,23 @@ TEST(declare_built_test, _p)
}
#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(_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")
-TEST_DECLARE(_t, "declare -t is not supported yet\n", "-t", "world")
-TEST_DECLARE(_u, "declare -u is not supported yet\n", "-u", "world")
-TEST_DECLARE(_x, "declare -x is not supported yet\n", "-x", "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(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")
-TEST_DECLARE(pt, "declare +t is not supported yet\n", "+t", "world")
-TEST_DECLARE(pu, "declare +u is not supported yet\n", "+u", "world")
-TEST_DECLARE(px, "declare +x is not supported yet\n", "+x", "world")
+ TEST(declare_builtin_test, name) { test_declare<libbash::unsupported_exception>(expected, {__VA_ARGS__}); }
+
+TEST_DECLARE(_a, "declare -a is not supported yet", "-a", "world")
+TEST_DECLARE(_A, "declare -A is not supported yet", "-A", "world")
+TEST_DECLARE(_f, "declare -f is not supported yet", "-f", "world")
+TEST_DECLARE(_i, "declare -i is not supported yet", "-i", "world")
+TEST_DECLARE(_l, "declare -l is not supported yet", "-l", "world")
+TEST_DECLARE(_r, "declare -r is not supported yet", "-r", "world")
+TEST_DECLARE(_t, "declare -t is not supported yet", "-t", "world")
+TEST_DECLARE(_u, "declare -u is not supported yet", "-u", "world")
+TEST_DECLARE(_x, "declare -x is not supported yet", "-x", "world")
+TEST_DECLARE(pa, "declare +a is not supported yet", "+a", "world")
+TEST_DECLARE(pA, "declare +A is not supported yet", "+A", "world")
+TEST_DECLARE(pf, "declare +f is not supported yet", "+f", "world")
+TEST_DECLARE(pi, "declare +i is not supported yet", "+i", "world")
+TEST_DECLARE(pl, "declare +l is not supported yet", "+l", "world")
+TEST_DECLARE(pr, "declare +r is not supported yet", "+r", "world")
+TEST_DECLARE(pt, "declare +t is not supported yet", "+t", "world")
+TEST_DECLARE(pu, "declare +u is not supported yet", "+u", "world")
+TEST_DECLARE(px, "declare +x is not supported yet", "+x", "world")