From cbd8a27a6d1ec705032486851203f8c4d1b4f56f Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 12 Oct 2013 12:04:31 -0700 Subject: [PATCH] Beef up completion tests --- fish_tests.cpp | 34 ++++++++++++++++++++++++++++++++-- function.cpp | 8 +++++++- function.h | 2 +- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/fish_tests.cpp b/fish_tests.cpp index bb13bd2ee..b52b612d1 100644 --- a/fish_tests.cpp +++ b/fish_tests.cpp @@ -1122,12 +1122,42 @@ static void test_complete(void) assert(completions.at(1).completion == L"$Bar1"); completions.clear(); - complete(L"echo (/bin/ech", completions, COMPLETION_REQUEST_DEFAULT); + complete(L"echo (/bin/mkdi", completions, COMPLETION_REQUEST_DEFAULT); assert(completions.size() == 1); - assert(completions.at(0).completion == L"o"); + assert(completions.at(0).completion == L"r"); + completions.clear(); + complete(L"echo (ls /bin/mkdi", completions, COMPLETION_REQUEST_DEFAULT); + assert(completions.size() == 1); + assert(completions.at(0).completion == L"r"); + + completions.clear(); + complete(L"echo (command ls /bin/mkdi", completions, COMPLETION_REQUEST_DEFAULT); + assert(completions.size() == 1); + assert(completions.at(0).completion == L"r"); + + /* Add a function and test completing it in various ways */ + struct function_data_t func_data; + func_data.name = L"scuttlebutt"; + func_data.definition = L"echo gongoozle"; + function_add(func_data, parser_t::principal_parser()); + /* Complete a function name */ + completions.clear(); + complete(L"echo (scuttlebut", completions, COMPLETION_REQUEST_DEFAULT); + assert(completions.size() == 1); + assert(completions.at(0).completion == L"t"); + /* But not with the command prefix */ + completions.clear(); + complete(L"echo (command scuttlebut", completions, COMPLETION_REQUEST_DEFAULT); + assert(completions.size() == 0); + + /* Not with the builtin prefix */ + completions.clear(); + complete(L"echo (builtin scuttlebut", completions, COMPLETION_REQUEST_DEFAULT); + assert(completions.size() == 0); + complete_set_variable_names(NULL); } diff --git a/function.cpp b/function.cpp index 08fb85560..fef342720 100644 --- a/function.cpp +++ b/function.cpp @@ -192,7 +192,13 @@ void function_add(const function_data_t &data, const parser_t &parser) /* Create and store a new function */ const wchar_t *filename = reader_current_filename(); - int def_offset = parser.line_number_of_character_at_offset(parser.current_block->tok_pos) - 1; + + int def_offset = -1; + if (parser.current_block != NULL) + { + def_offset = parser.line_number_of_character_at_offset(parser.current_block->tok_pos); + } + const function_map_t::value_type new_pair(data.name, function_info_t(data, filename, def_offset, is_autoload)); loaded_functions.insert(new_pair); diff --git a/function.h b/function.h index 2f8dfc36c..fd9455706 100644 --- a/function.h +++ b/function.h @@ -39,7 +39,7 @@ struct function_data_t /** Function definition */ - wchar_t *definition; + const wchar_t *definition; /** List of all event handlers for this function */