diff options
author | Mu Qiao <qiaomuf@gentoo.org> | 2011-06-22 22:32:53 +0800 |
---|---|---|
committer | Mu Qiao <qiaomuf@gentoo.org> | 2011-06-22 23:07:15 +0800 |
commit | afd5e979e8f97d557b55113550b0673effb1ff5f (patch) | |
tree | 30365ed8efebd57b738c00b8f7e11cbecddb7d7d /src | |
parent | Core: add more exceptions (diff) | |
download | libbash-afd5e979e8f97d557b55113550b0673effb1ff5f.tar.gz libbash-afd5e979e8f97d557b55113550b0673effb1ff5f.tar.bz2 libbash-afd5e979e8f97d557b55113550b0673effb1ff5f.zip |
Core: improve current exception usage
Diffstat (limited to 'src')
-rw-r--r-- | src/builtins/builtin_exceptions.h | 2 | ||||
-rw-r--r-- | src/builtins/continue_builtin.cpp | 4 | ||||
-rw-r--r-- | src/builtins/declare_builtin.cpp | 2 | ||||
-rw-r--r-- | src/builtins/printf_builtin.cpp | 4 | ||||
-rw-r--r-- | src/builtins/return_builtin.cpp | 2 | ||||
-rw-r--r-- | src/builtins/shopt_builtin.cpp | 8 | ||||
-rw-r--r-- | src/builtins/source_builtin.cpp | 4 | ||||
-rw-r--r-- | src/builtins/tests/continue_tests.cpp | 8 | ||||
-rw-r--r-- | src/builtins/tests/printf_tests.cpp | 2 | ||||
-rw-r--r-- | src/builtins/tests/return_tests.cpp | 2 | ||||
-rw-r--r-- | src/builtins/tests/shopt_tests.cpp | 38 | ||||
-rw-r--r-- | src/builtins/tests/source_tests.cpp | 4 | ||||
-rw-r--r-- | src/builtins/unset_builtin.cpp | 1 | ||||
-rw-r--r-- | src/core/bash_ast.cpp | 12 | ||||
-rw-r--r-- | src/core/bash_condition.cpp | 10 | ||||
-rw-r--r-- | src/core/interpreter.cpp | 17 | ||||
-rw-r--r-- | src/core/symbols.hpp | 4 | ||||
-rw-r--r-- | src/core/tests/bash_ast_test.cpp | 4 | ||||
-rw-r--r-- | src/core/tests/bash_condition_test.cpp | 4 | ||||
-rw-r--r-- | src/core/tests/interpreter_test.cpp | 37 | ||||
-rw-r--r-- | src/core/unset_exception.h | 43 |
21 files changed, 89 insertions, 123 deletions
diff --git a/src/builtins/builtin_exceptions.h b/src/builtins/builtin_exceptions.h index 1e2f140..9b0c1d8 100644 --- a/src/builtins/builtin_exceptions.h +++ b/src/builtins/builtin_exceptions.h @@ -46,7 +46,7 @@ public: explicit continue_exception(int c): count(c) { if(c < 1) - throw libbash::interpreter_exception("continue: argument should be greater than or equal to 1"); + throw libbash::illegal_argument_exception("continue: argument should be greater than or equal to 1"); } void rethrow_unless_correct_frame() diff --git a/src/builtins/continue_builtin.cpp b/src/builtins/continue_builtin.cpp index 97ac0cd..9bb9469 100644 --- a/src/builtins/continue_builtin.cpp +++ b/src/builtins/continue_builtin.cpp @@ -33,7 +33,7 @@ int continue_builtin::exec(const std::vector<std::string>& bash_args) if(bash_args.size() > 1) { - throw libbash::interpreter_exception("continue: too many arguments"); + throw libbash::illegal_argument_exception("continue: too many arguments"); } else if(bash_args.size() == 1) { @@ -43,7 +43,7 @@ int continue_builtin::exec(const std::vector<std::string>& bash_args) } catch(boost::bad_lexical_cast& e) { - throw libbash::interpreter_exception("continue: argument should be an integer"); + throw libbash::illegal_argument_exception("continue: argument should be an integer"); } } diff --git a/src/builtins/declare_builtin.cpp b/src/builtins/declare_builtin.cpp index bc03930..412b9ba 100644 --- a/src/builtins/declare_builtin.cpp +++ b/src/builtins/declare_builtin.cpp @@ -92,7 +92,7 @@ int declare_builtin::exec(const std::vector<std::string>& bash_args) } else { - throw libbash::interpreter_exception("We do not support declare -p without arguments for now"); + throw libbash::unsupported_exception("We do not support declare -p without arguments for now"); } return result; case 'a': diff --git a/src/builtins/printf_builtin.cpp b/src/builtins/printf_builtin.cpp index 8b3842c..9b5fd41 100644 --- a/src/builtins/printf_builtin.cpp +++ b/src/builtins/printf_builtin.cpp @@ -34,7 +34,7 @@ int printf_builtin::exec(const std::vector<std::string>& bash_args) if(!(bash_args[0] == "-v")) begin = bash_args.begin(); else if(bash_args.size() < 3) - throw libbash::interpreter_exception("printf: illegal number of arguments"); + throw libbash::illegal_argument_exception("printf: illegal number of arguments"); else begin = bash_args.begin() + 2; @@ -56,7 +56,7 @@ int printf_builtin::exec(const std::vector<std::string>& bash_args) } else { - throw libbash::interpreter_exception("printf: invalid option: " + bash_args[0]); + throw libbash::illegal_argument_exception("printf: invalid option: " + bash_args[0]); } return 0; diff --git a/src/builtins/return_builtin.cpp b/src/builtins/return_builtin.cpp index 8c0bb32..068467c 100644 --- a/src/builtins/return_builtin.cpp +++ b/src/builtins/return_builtin.cpp @@ -30,7 +30,7 @@ int return_builtin::exec(const std::vector<std::string>& bash_args) { if(bash_args.size() > 1) - throw libbash::interpreter_exception("return: too many arguments"); + throw libbash::illegal_argument_exception("return: too many arguments"); else if(bash_args.size() == 1) _walker.set_status(boost::lexical_cast<int>(bash_args[0])); diff --git a/src/builtins/shopt_builtin.cpp b/src/builtins/shopt_builtin.cpp index e60ddac..f55c49e 100644 --- a/src/builtins/shopt_builtin.cpp +++ b/src/builtins/shopt_builtin.cpp @@ -42,9 +42,9 @@ void shopt_builtin::print_opts() const int shopt_builtin::exec(const std::vector<std::string>& bash_args) { if(bash_args.empty()) - throw libbash::interpreter_exception("Arguments required for shopt"); + throw libbash::illegal_argument_exception("Arguments required for shopt"); else if(bash_args[0].size() != 2) - throw libbash::interpreter_exception("Multiple arguments are not supported"); + throw libbash::unsupported_exception("Multiple arguments are not supported"); switch(bash_args[0][1]) { @@ -59,9 +59,9 @@ int shopt_builtin::exec(const std::vector<std::string>& bash_args) break; case 'q': case 'o': - throw libbash::interpreter_exception("shopt " + bash_args[0] + " is not supported yet"); + throw libbash::unsupported_exception("shopt " + bash_args[0] + " is not supported yet"); default: - throw libbash::interpreter_exception("Unrecognized option for shopt: " + bash_args[0]); + throw libbash::illegal_argument_exception("Unrecognized option for shopt: " + bash_args[0]); } return 0; diff --git a/src/builtins/source_builtin.cpp b/src/builtins/source_builtin.cpp index 9dc81ca..bec83ad 100644 --- a/src/builtins/source_builtin.cpp +++ b/src/builtins/source_builtin.cpp @@ -39,7 +39,7 @@ int source_builtin::exec(const std::vector<std::string>& bash_args) static std::unordered_map<std::string, std::shared_ptr<bash_ast>> ast_cache; if(bash_args.size() == 0) - throw libbash::interpreter_exception("should provide one argument for source builtin"); + throw libbash::illegal_argument_exception("should provide one argument for source builtin"); // we need fix this to pass extra arguments as positional parameters const std::string& path = bash_args[0]; @@ -55,7 +55,7 @@ int source_builtin::exec(const std::vector<std::string>& bash_args) } else if(!(stored_ast->second)) { - throw libbash::interpreter_exception(path + " cannot be fully parsed"); + throw libbash::parse_exception(path + " cannot be fully parsed"); } const std::string& original_path = _walker.resolve<std::string>("0"); diff --git a/src/builtins/tests/continue_tests.cpp b/src/builtins/tests/continue_tests.cpp index f6edef6..c753291 100644 --- a/src/builtins/tests/continue_tests.cpp +++ b/src/builtins/tests/continue_tests.cpp @@ -30,10 +30,10 @@ TEST(continue_builtin_test, bad_argument) { interpreter walker; - EXPECT_THROW(cppbash_builtin::exec("continue", {"abc"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception); - EXPECT_THROW(cppbash_builtin::exec("continue", {"1", "2"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception); - EXPECT_THROW(cppbash_builtin::exec("continue", {"0"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception); - EXPECT_THROW(cppbash_builtin::exec("continue", {"-1"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception); + EXPECT_THROW(cppbash_builtin::exec("continue", {"abc"}, std::cout, std::cerr, std::cin, walker), libbash::illegal_argument_exception); + EXPECT_THROW(cppbash_builtin::exec("continue", {"1", "2"}, std::cout, std::cerr, std::cin, walker), libbash::illegal_argument_exception); + EXPECT_THROW(cppbash_builtin::exec("continue", {"0"}, std::cout, std::cerr, std::cin, walker), libbash::illegal_argument_exception); + EXPECT_THROW(cppbash_builtin::exec("continue", {"-1"}, std::cout, std::cerr, std::cin, walker), libbash::illegal_argument_exception); } TEST(continue_builtin_test, throw_exception) diff --git a/src/builtins/tests/printf_tests.cpp b/src/builtins/tests/printf_tests.cpp index c4d461d..9017b18 100644 --- a/src/builtins/tests/printf_tests.cpp +++ b/src/builtins/tests/printf_tests.cpp @@ -36,7 +36,7 @@ namespace cppbash_builtin::exec("printf", arguments, std::cout, std::cerr, std::cin, walker); FAIL(); } - catch(libbash::interpreter_exception& e) + catch(libbash::illegal_argument_exception& e) { EXPECT_STREQ(expected.c_str(), e.what()); } diff --git a/src/builtins/tests/return_tests.cpp b/src/builtins/tests/return_tests.cpp index 528c70f..a9829b0 100644 --- a/src/builtins/tests/return_tests.cpp +++ b/src/builtins/tests/return_tests.cpp @@ -33,7 +33,7 @@ TEST(return_builtin_test, bad_argument) { interpreter walker; EXPECT_THROW(cppbash_builtin::exec("return", {"abc"}, std::cout, std::cerr, std::cin, walker), boost::bad_lexical_cast); - EXPECT_THROW(cppbash_builtin::exec("return", {"abc", "def"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception); + EXPECT_THROW(cppbash_builtin::exec("return", {"abc", "def"}, std::cout, std::cerr, std::cin, walker), libbash::illegal_argument_exception); } TEST(return_builtin_test, bad_location) diff --git a/src/builtins/tests/shopt_tests.cpp b/src/builtins/tests/shopt_tests.cpp index 5b158b6..9fe4949 100644 --- a/src/builtins/tests/shopt_tests.cpp +++ b/src/builtins/tests/shopt_tests.cpp @@ -23,28 +23,32 @@ #include <gtest/gtest.h> #include "builtins/builtin_exceptions.h" +#include "core/exceptions.h" #include "core/interpreter.h" #include "cppbash_builtin.h" -static void test_shopt_builtin(const std::string& expected, const std::vector<std::string>& args) +namespace { - std::stringstream output; - interpreter walker; - try - { - cppbash_builtin::exec("shopt", args, std::cout, output, std::cin, walker); - FAIL(); - } - catch(libbash::interpreter_exception& e) + template <typename T> + void test_shopt_builtin(const std::string& expected, const std::vector<std::string>& args) { - EXPECT_STREQ(expected.c_str(), e.what()); + std::stringstream output; + interpreter walker; + try + { + cppbash_builtin::exec("shopt", args, std::cout, output, std::cin, walker); + FAIL(); + } + catch(T& e) + { + EXPECT_STREQ(expected.c_str(), e.what()); + } } } - TEST(shopt_builtin_test, disable_extglob) { - test_shopt_builtin("not exist is not a valid bash option", {"-u", "not exist"}); + test_shopt_builtin<libbash::illegal_argument_exception>("not exist is not a valid bash option", {"-u", "not exist"}); interpreter walker; walker.set_additional_option("autocd", true); @@ -55,7 +59,7 @@ TEST(shopt_builtin_test, disable_extglob) TEST(shopt_builtin_test, enable_extglob) { - test_shopt_builtin("not exist is not a valid bash option", {"-s", "not exist"}); + test_shopt_builtin<libbash::illegal_argument_exception>("not exist is not a valid bash option", {"-s", "not exist"}); interpreter walker; EXPECT_EQ(0, cppbash_builtin::exec("shopt", {"-s", "autocd", "cdspell"}, std::cout, std::cerr, std::cin, walker)); @@ -65,8 +69,8 @@ TEST(shopt_builtin_test, enable_extglob) TEST(shopt_builtin_test, invalid_argument) { - test_shopt_builtin("Arguments required for shopt", {}); - test_shopt_builtin("Multiple arguments are not supported", {"-so"}); - test_shopt_builtin("shopt -q is not supported yet", {"-q"}); - test_shopt_builtin("Unrecognized option for shopt: -d", {"-d"}); + test_shopt_builtin<libbash::illegal_argument_exception>("Arguments required for shopt", {}); + test_shopt_builtin<libbash::unsupported_exception>("Multiple arguments are not supported", {"-so"}); + test_shopt_builtin<libbash::unsupported_exception>("shopt -q is not supported yet", {"-q"}); + test_shopt_builtin<libbash::illegal_argument_exception>("Unrecognized option for shopt: -d", {"-d"}); } diff --git a/src/builtins/tests/source_tests.cpp b/src/builtins/tests/source_tests.cpp index 759e456..bfcac19 100644 --- a/src/builtins/tests/source_tests.cpp +++ b/src/builtins/tests/source_tests.cpp @@ -92,12 +92,12 @@ TEST(source_builtin_test, invalid) std::cerr, std::cin, walker), - libbash::interpreter_exception); + libbash::illegal_argument_exception); EXPECT_THROW(cppbash_builtin::exec("source", {get_src_dir() + "/scripts/illegal_script.sh"}, std::cout, std::cerr, std::cin, walker), - libbash::interpreter_exception); + libbash::parse_exception); } diff --git a/src/builtins/unset_builtin.cpp b/src/builtins/unset_builtin.cpp index 078ee46..d8178f9 100644 --- a/src/builtins/unset_builtin.cpp +++ b/src/builtins/unset_builtin.cpp @@ -25,7 +25,6 @@ #include <functional> #include "core/interpreter.h" -#include "core/unset_exception.h" int unset_builtin::exec(const std::vector<std::string>& bash_args) { diff --git a/src/core/bash_ast.cpp b/src/core/bash_ast.cpp index e0c4df1..c3b63d8 100644 --- a/src/core/bash_ast.cpp +++ b/src/core/bash_ast.cpp @@ -47,7 +47,7 @@ bash_ast::bash_ast(const std::string& script_path, std::stringstream stream; std::ifstream file_stream(script_path); if(!file_stream) - throw libbash::interpreter_exception(script_path + " can't be read"); + throw libbash::parse_exception(script_path + " can't be read"); stream << file_stream.rdbuf(); script = stream.str(); @@ -63,7 +63,7 @@ void bash_ast::init_parser(const std::string& script, const std::string& script_ NULL)); if(!input) - throw libbash::interpreter_exception("Unable to open file " + script + " due to malloc() failure"); + throw libbash::parse_exception("Unable to open file " + script + " due to malloc() failure"); input->fileName = input->strFactory->newStr( input->strFactory, @@ -71,20 +71,20 @@ void bash_ast::init_parser(const std::string& script, const std::string& script_ lexer.reset(libbashLexerNew(input.get())); if(!lexer) - throw libbash::interpreter_exception("Unable to create the lexer due to malloc() failure"); + throw libbash::parse_exception("Unable to create the lexer due to malloc() failure"); token_stream.reset(antlr3CommonTokenStreamSourceNew( ANTLR3_SIZE_HINT, lexer->pLexer->rec->state->tokSource)); if(!token_stream) - throw libbash::interpreter_exception("Out of memory trying to allocate token stream"); + throw libbash::parse_exception("Out of memory trying to allocate token stream"); parser.reset(libbashParserNew(token_stream.get())); if(!parser) - throw libbash::interpreter_exception("Out of memory trying to allocate parser"); + throw libbash::parse_exception("Out of memory trying to allocate parser"); ast = parse(parser.get()); if(parser->pParser->rec->getNumberOfSyntaxErrors(parser->pParser->rec)) - throw libbash::interpreter_exception("Something wrong happened while parsing"); + throw libbash::parse_exception("Something wrong happened while parsing"); nodes.reset(antlr3CommonTreeNodeStreamNewTree(ast, ANTLR3_SIZE_HINT)); } diff --git a/src/core/bash_condition.cpp b/src/core/bash_condition.cpp index 59a21e9..af57b90 100644 --- a/src/core/bash_condition.cpp +++ b/src/core/bash_condition.cpp @@ -85,7 +85,7 @@ namespace case 'N': return info.st_mtime >= info.st_atime; default: - throw libbash::interpreter_exception(std::string("Unrecognized test operator -") + op); + throw libbash::illegal_argument_exception(std::string("Unrecognized test operator -") + op); } } } @@ -99,7 +99,7 @@ bool internal::test_unary(char op, const std::string& target) case 'n': return !target.empty(); case 'o': - throw libbash::interpreter_exception("Shell option test is not supported"); + throw libbash::unsupported_exception("Shell option test is not supported"); case 't': try { @@ -141,7 +141,7 @@ namespace /* -ef */ return (lst.st_dev == rst.st_dev && lst.st_ino == rst.st_ino); default: - throw libbash::interpreter_exception(std::string("Unrecognized option for file test ") + op); + throw libbash::illegal_argument_exception(std::string("Unrecognized option for file test ") + op); } } } @@ -152,7 +152,7 @@ bool internal::test_binary(const std::string& op, interpreter& walker) { if(op.size() != 2) - throw libbash::interpreter_exception("Unrecognized operator " + op); + throw libbash::illegal_argument_exception("Unrecognized operator " + op); try { @@ -177,7 +177,7 @@ bool internal::test_binary(const std::string& op, else if(op == "ge") return walker.eval_arithmetic(lhs) >= walker.eval_arithmetic(rhs); else - throw libbash::interpreter_exception("Unrecognized operator " + op); + throw libbash::illegal_argument_exception("Unrecognized operator " + op); } catch(boost::bad_lexical_cast& e) { diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp index d34ce96..267183a 100644 --- a/src/core/interpreter.cpp +++ b/src/core/interpreter.cpp @@ -38,7 +38,6 @@ #include <boost/range/algorithm/copy.hpp> #include "core/bash_ast.h" -#include "core/unset_exception.h" namespace { @@ -178,7 +177,7 @@ const std::string interpreter::do_substring_expansion(const std::string& name, const unsigned index) const { if(length < 0) - throw libbash::interpreter_exception("length of substring expression should be greater or equal to zero"); + throw libbash::illegal_argument_exception("length of substring expression should be greater or equal to zero"); return get_substring(name, offset, boost::numeric_cast<unsigned>(length), index); } @@ -226,7 +225,7 @@ const std::string interpreter::do_subarray_expansion(const std::string& name, int length) const { if(length < 0) - throw libbash::interpreter_exception("length of substring expression should be greater or equal to zero"); + throw libbash::illegal_argument_exception("length of substring expression should be greater or equal to zero"); return get_subarray(name, offset, boost::numeric_cast<unsigned>(length)); } @@ -318,7 +317,7 @@ void interpreter::call(const std::string& name, if(iter != functions.end()) iter->second.call(*this); else - throw libbash::interpreter_exception(name + " is not defined."); + throw libbash::runtime_exception(name + " is not defined."); } void interpreter::replace_all(std::string& value, @@ -367,7 +366,7 @@ namespace { // Unsetting positional parameters is not allowed if(isdigit(name[0])) - throw unset_exception("unset: not a valid identifier"); + throw libbash::runtime_exception("unset: not a valid identifier"); } } @@ -380,7 +379,7 @@ void interpreter::unset(const std::string& name) if(iter_local != frame.end()) { if(iter_local->second->is_readonly()) - throw unset_exception("unset a readonly variable"); + throw libbash::readonly_exception("unset a readonly variable"); frame.erase(iter_local); return true; } @@ -408,7 +407,7 @@ void interpreter::unset(const std::string& name, if(var) { if(var->is_readonly()) - throw unset_exception("unset a readonly variable"); + throw libbash::readonly_exception("unset a readonly variable"); var->unset_value(index); } } @@ -417,7 +416,7 @@ bool interpreter::get_additional_option(const std::string& name) const { auto iter = additional_options.find(name); if(iter == additional_options.end()) - throw libbash::interpreter_exception("Invalid bash option"); + throw libbash::illegal_argument_exception("Invalid bash option"); return iter->second; } @@ -426,7 +425,7 @@ void interpreter::set_additional_option(const std::string& name, bool value) { auto iter = additional_options.find(name); if(iter == additional_options.end()) - throw libbash::interpreter_exception(name + " is not a valid bash option"); + throw libbash::illegal_argument_exception(name + " is not a valid bash option"); iter->second = value; } diff --git a/src/core/symbols.hpp b/src/core/symbols.hpp index 1c5627a..a5e2e1d 100644 --- a/src/core/symbols.hpp +++ b/src/core/symbols.hpp @@ -181,7 +181,7 @@ public: const unsigned index=0) { if(readonly) - throw libbash::interpreter_exception(get_name() + " is readonly variable"); + throw libbash::readonly_exception(get_name() + " is readonly variable"); value[index] = new_value; } @@ -191,7 +191,7 @@ public: void unset_value(const unsigned index) { if(readonly) - throw libbash::interpreter_exception(get_name() + " is readonly variable"); + throw libbash::readonly_exception(get_name() + " is readonly variable"); value.erase(index); } diff --git a/src/core/tests/bash_ast_test.cpp b/src/core/tests/bash_ast_test.cpp index c6036fe..b01393d 100644 --- a/src/core/tests/bash_ast_test.cpp +++ b/src/core/tests/bash_ast_test.cpp @@ -33,7 +33,7 @@ TEST(bash_ast, parse_illegal_script) { - EXPECT_THROW(bash_ast ast(get_src_dir() + std::string("/scripts/illegal_script.sh")), libbash::interpreter_exception); + EXPECT_THROW(bash_ast ast(get_src_dir() + std::string("/scripts/illegal_script.sh")), libbash::parse_exception); } TEST(bash_ast, parse_legal_script) @@ -52,5 +52,5 @@ TEST(bash_ast, parse_arithmetics) TEST(bash_ast, illegal_path) { - EXPECT_THROW(bash_ast("not_exist"), libbash::interpreter_exception); + EXPECT_THROW(bash_ast("not_exist"), libbash::parse_exception); } diff --git a/src/core/tests/bash_condition_test.cpp b/src/core/tests/bash_condition_test.cpp index d3c704f..09f441a 100644 --- a/src/core/tests/bash_condition_test.cpp +++ b/src/core/tests/bash_condition_test.cpp @@ -135,7 +135,7 @@ TEST(bash_condition, string_unary_operator) EXPECT_FALSE(internal::test_unary('n', "")); EXPECT_TRUE(internal::test_unary('n', "hello")); - EXPECT_THROW(internal::test_unary('o', "extglob"), libbash::interpreter_exception); + EXPECT_THROW(internal::test_unary('o', "extglob"), libbash::unsupported_exception); } TEST_F(file_test, binary_operator) @@ -154,7 +154,7 @@ TEST_F(file_test, binary_operator) EXPECT_FALSE(internal::test_binary("ef", positive, negative, walker)); EXPECT_FALSE(internal::test_binary("ef", "not exist", negative, walker)); - EXPECT_THROW(internal::test_binary("efd", positive, negative, walker), libbash::interpreter_exception); + EXPECT_THROW(internal::test_binary("efd", positive, negative, walker), libbash::illegal_argument_exception); } TEST(bash_condition, arithmetic_operator) diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpreter_test.cpp index f207bfb..c449e3c 100644 --- a/src/core/tests/interpreter_test.cpp +++ b/src/core/tests/interpreter_test.cpp @@ -24,7 +24,6 @@ #include <gtest/gtest.h> #include "core/interpreter.h" -#include "core/unset_exception.h" using namespace std; @@ -98,7 +97,7 @@ TEST(interpreter, set_int_value) walker.define("aint_ro", 4, true); EXPECT_THROW(walker.set_value("aint_ro", 10), - libbash::interpreter_exception); + libbash::readonly_exception); EXPECT_EQ(4, walker.resolve<int>("aint_ro")); } @@ -113,7 +112,7 @@ TEST(interpreter, set_string_value) walker.define("astring_ro", "hi", true); EXPECT_THROW(walker.set_value<string>("astring_ro", "hello"), - libbash::interpreter_exception); + libbash::readonly_exception); EXPECT_STREQ("hi", walker.resolve<string>("astring_ro").c_str()); } @@ -129,7 +128,7 @@ TEST(interpreter, set_array_value) walker.define("ro_array", values, true); EXPECT_THROW(walker.set_value<string>("ro_array", "hello", 1), - libbash::interpreter_exception); + libbash::readonly_exception); EXPECT_STREQ("2", walker.resolve<string>("ro_array", 1).c_str()); } @@ -176,12 +175,12 @@ TEST(interpreter, unset_arrays) EXPECT_STREQ("", walker.resolve<string>("array", 2).c_str()); walker.unset("array"); - EXPECT_THROW(walker.unset("ro_array", 1), unset_exception); - EXPECT_THROW(walker.unset("ro_local_array", 1), unset_exception); - EXPECT_THROW(walker.unset("ro_array"), unset_exception); - EXPECT_THROW(walker.unset("ro_local_array"), unset_exception); + EXPECT_THROW(walker.unset("ro_array", 1), libbash::readonly_exception); + EXPECT_THROW(walker.unset("ro_local_array", 1), libbash::readonly_exception); + EXPECT_THROW(walker.unset("ro_array"), libbash::readonly_exception); + EXPECT_THROW(walker.unset("ro_local_array"), libbash::readonly_exception); - EXPECT_THROW(walker.unset("1", 1), libbash::interpreter_exception); + EXPECT_THROW(walker.unset("1", 1), libbash::runtime_exception); } TEST(interpreter, unset_variables) @@ -200,9 +199,9 @@ TEST(interpreter, unset_variables) EXPECT_STREQ("", walker.resolve<string>("var").c_str()); walker.unset("var"); - EXPECT_THROW(walker.unset("ro_var"), unset_exception); - EXPECT_THROW(walker.unset("ro_local_var"), unset_exception); - EXPECT_THROW(walker.unset("1"), libbash::interpreter_exception); + EXPECT_THROW(walker.unset("ro_var"), libbash::readonly_exception); + EXPECT_THROW(walker.unset("ro_local_var"), libbash::readonly_exception); + EXPECT_THROW(walker.unset("1"), libbash::runtime_exception); } TEST(interpreter, unset_functions) @@ -219,7 +218,7 @@ TEST(interperter, substring_expansion) { interpreter walker; EXPECT_STREQ("", walker.do_substring_expansion("@", 0, 1, 2).c_str()); - EXPECT_THROW(walker.do_substring_expansion("", 0, -1, 0), libbash::interpreter_exception); + EXPECT_THROW(walker.do_substring_expansion("", 0, -1, 0), libbash::illegal_argument_exception); } TEST(interpreter, word_split) @@ -240,8 +239,8 @@ TEST(interpreter, bash_additional_option) { interpreter walker; - EXPECT_THROW(walker.set_additional_option("not exist", false), libbash::interpreter_exception); - EXPECT_THROW(walker.get_additional_option("not exist"), libbash::interpreter_exception); + EXPECT_THROW(walker.set_additional_option("not exist", false), libbash::illegal_argument_exception); + EXPECT_THROW(walker.get_additional_option("not exist"), libbash::illegal_argument_exception); EXPECT_FALSE(walker.get_additional_option("extglob")); walker.set_additional_option("extglob", true); @@ -254,3 +253,11 @@ TEST(interpreter, bash_option) EXPECT_STREQ("Bh", walker.resolve<std::string>("-").c_str()); } + +TEST(interpreter, undefined_function) +{ + interpreter walker; + interpreter::local_scope temp_scope(walker); + + EXPECT_THROW(walker.call("undefined", {}), libbash::runtime_exception); +} diff --git a/src/core/unset_exception.h b/src/core/unset_exception.h deleted file mode 100644 index 4346ba6..0000000 --- a/src/core/unset_exception.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - 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 unset_exception.h -/// \brief implementation for unset_exception -/// - -#ifndef LIBBASH_CORE_UNSET_EXCEPTION_H_ -#define LIBBASH_CORE_UNSET_EXCEPTION_H_ - -#include <stdexcept> -#include <string> - -#include "interpreter_exception.h" - -/// -/// \class unset_exception -/// \brief exception for unsetting variables -/// -class unset_exception: public libbash::interpreter_exception -{ -public: - explicit unset_exception(const std::string& err_msg): - libbash::interpreter_exception(err_msg){} -}; - -#endif |