make history searching case insensitive by default

Fixes #3236
This commit is contained in:
Kurtis Rader
2016-09-23 20:12:15 -07:00
parent dc6b538f56
commit f490b56378
8 changed files with 186 additions and 83 deletions

View File

@@ -161,6 +161,11 @@ static int chdir_set_pwd(const char *path) {
if (!(e)) err(L"Test failed on line %lu: %s", __LINE__, #e); \
} while (0)
#define do_test_from(e, from_line) \
do { \
if (!(e)) err(L"Test failed on line %lu (from %lu): %s", __LINE__, from_line, #e); \
} while (0)
#define do_test1(e, msg) \
do { \
if (!(e)) err(L"Test failed on line %lu: %ls", __LINE__, (msg)); \
@@ -2178,7 +2183,6 @@ static void perform_one_autosuggestion_should_ignore_test(const wcstring &comman
static void test_autosuggestion_ignores() {
say(L"Testing scenarios that should produce no autosuggestions");
const wcstring wd = L"/tmp/autosuggest_test/";
// Do not do file autosuggestions immediately after certain statement terminators - see #1631.
perform_one_autosuggestion_should_ignore_test(L"echo PIPE_TEST|", __LINE__);
perform_one_autosuggestion_should_ignore_test(L"echo PIPE_TEST&", __LINE__);
@@ -2201,18 +2205,20 @@ static void test_autosuggestion_combining() {
do_test(combine_command_and_autosuggestion(L"alpha", L"ALPHA") == L"alpha");
}
static void test_history_matches(history_search_t &search, size_t matches) {
static void test_history_matches(history_search_t &search, size_t matches, unsigned from_line) {
size_t i;
for (i = 0; i < matches; i++) {
do_test(search.go_backwards());
wcstring item = search.current_string();
}
do_test(!search.go_backwards());
// do_test_from(!search.go_backwards(), from_line);
bool result = search.go_backwards();
do_test_from(!result, from_line);
for (i = 1; i < matches; i++) {
do_test(search.go_forwards());
do_test_from(search.go_forwards(), from_line);
}
do_test(!search.go_forwards());
do_test_from(!search.go_forwards(), from_line);
}
static bool history_contains(history_t *history, const wcstring &txt) {
@@ -2518,28 +2524,64 @@ static wcstring random_string(void) {
}
void history_tests_t::test_history(void) {
history_search_t searcher;
say(L"Testing history");
history_t &history = history_t::history_with_name(L"test_history");
history.clear();
history.add(L"Gamma");
history.add(L"beta");
history.add(L"BetA");
history.add(L"Beta");
history.add(L"alpha");
history.add(L"AlphA");
history.add(L"Alpha");
history.add(L"alph");
history.add(L"ALPH");
history.add(L"ZZZ");
// All three items match "a".
history_search_t search1(history, L"a");
test_history_matches(search1, 3);
do_test(search1.current_string() == L"Alpha");
// Items matching "a", case-sensitive.
searcher = history_search_t(history, L"a");
test_history_matches(searcher, 6, __LINE__);
do_test(searcher.current_string() == L"alph");
// One item matches "et".
history_search_t search2(history, L"et");
test_history_matches(search2, 1);
do_test(search2.current_string() == L"Beta");
// Items matching "alpha", case-insensitive. Note that HISTORY_SEARCH_TYPE_CONTAINS but we have
// to explicitly specify it in order to be able to pass false for the case_sensitive parameter.
searcher = history_search_t(history, L"AlPhA", HISTORY_SEARCH_TYPE_CONTAINS, false);
test_history_matches(searcher, 3, __LINE__);
do_test(searcher.current_string() == L"Alpha");
// Test item removal.
// Items matching "et", case-sensitive.
searcher = history_search_t(history, L"et");
test_history_matches(searcher, 3, __LINE__);
do_test(searcher.current_string() == L"Beta");
// Items starting with "be", case-sensitive.
searcher = history_search_t(history, L"be", HISTORY_SEARCH_TYPE_PREFIX, true);
test_history_matches(searcher, 1, __LINE__);
do_test(searcher.current_string() == L"beta");
// Items starting with "be", case-insensitive.
searcher = history_search_t(history, L"be", HISTORY_SEARCH_TYPE_PREFIX, false);
test_history_matches(searcher, 3, __LINE__);
do_test(searcher.current_string() == L"Beta");
// Items exactly matchine "alph", case-sensitive.
searcher = history_search_t(history, L"alph", HISTORY_SEARCH_TYPE_EXACT, true);
test_history_matches(searcher, 1, __LINE__);
do_test(searcher.current_string() == L"alph");
// Items exactly matchine "alph", case-insensitive.
searcher = history_search_t(history, L"alph", HISTORY_SEARCH_TYPE_EXACT, false);
test_history_matches(searcher, 2, __LINE__);
do_test(searcher.current_string() == L"ALPH");
// Test item removal case-sensitive.
searcher = history_search_t(history, L"Alpha");
test_history_matches(searcher, 1, __LINE__);
history.remove(L"Alpha");
history_search_t search3(history, L"Alpha");
test_history_matches(search3, 0);
searcher = history_search_t(history, L"Alpha");
test_history_matches(searcher, 0, __LINE__);
// Test history escaping and unescaping, yaml, etc.
history_item_list_t before, after;