search_history.rs

  1use project::search_history::{QueryInsertionBehavior, SearchHistory, SearchHistoryCursor};
  2
  3#[test]
  4fn test_add() {
  5    const MAX_HISTORY_LEN: usize = 20;
  6    let mut search_history = SearchHistory::new(
  7        Some(MAX_HISTORY_LEN),
  8        QueryInsertionBehavior::ReplacePreviousIfContains,
  9    );
 10    let mut cursor = SearchHistoryCursor::default();
 11
 12    assert_eq!(
 13        search_history.current(&cursor),
 14        None,
 15        "No current selection should be set for the default search history"
 16    );
 17
 18    search_history.add(&mut cursor, "rust".to_string());
 19    assert_eq!(
 20        search_history.current(&cursor),
 21        Some("rust"),
 22        "Newly added item should be selected"
 23    );
 24
 25    // check if duplicates are not added
 26    search_history.add(&mut cursor, "rust".to_string());
 27    assert_eq!(search_history.len(), 1, "Should not add a duplicate");
 28    assert_eq!(search_history.current(&cursor), Some("rust"));
 29
 30    // check if new string containing the previous string replaces it
 31    search_history.add(&mut cursor, "rustlang".to_string());
 32    assert_eq!(
 33        search_history.len(),
 34        1,
 35        "Should replace previous item if it's a substring"
 36    );
 37    assert_eq!(search_history.current(&cursor), Some("rustlang"));
 38
 39    // add item when it equals to current item if it's not the last one
 40    search_history.add(&mut cursor, "php".to_string());
 41    search_history.previous(&mut cursor);
 42    assert_eq!(search_history.current(&cursor), Some("rustlang"));
 43    search_history.add(&mut cursor, "rustlang".to_string());
 44    assert_eq!(search_history.len(), 3, "Should add item");
 45    assert_eq!(search_history.current(&cursor), Some("rustlang"));
 46
 47    // push enough items to test SEARCH_HISTORY_LIMIT
 48    for i in 0..MAX_HISTORY_LEN * 2 {
 49        search_history.add(&mut cursor, format!("item{i}"));
 50    }
 51    assert!(search_history.len() <= MAX_HISTORY_LEN);
 52}
 53
 54#[test]
 55fn test_next_and_previous() {
 56    let mut search_history = SearchHistory::new(None, QueryInsertionBehavior::AlwaysInsert);
 57    let mut cursor = SearchHistoryCursor::default();
 58
 59    assert_eq!(
 60        search_history.next(&mut cursor),
 61        None,
 62        "Default search history should not have a next item"
 63    );
 64
 65    search_history.add(&mut cursor, "Rust".to_string());
 66    assert_eq!(search_history.next(&mut cursor), None);
 67    search_history.add(&mut cursor, "JavaScript".to_string());
 68    assert_eq!(search_history.next(&mut cursor), None);
 69    search_history.add(&mut cursor, "TypeScript".to_string());
 70    assert_eq!(search_history.next(&mut cursor), None);
 71
 72    assert_eq!(search_history.current(&cursor), Some("TypeScript"));
 73
 74    assert_eq!(search_history.previous(&mut cursor), Some("JavaScript"));
 75    assert_eq!(search_history.current(&cursor), Some("JavaScript"));
 76
 77    assert_eq!(search_history.previous(&mut cursor), Some("Rust"));
 78    assert_eq!(search_history.current(&cursor), Some("Rust"));
 79
 80    assert_eq!(search_history.previous(&mut cursor), None);
 81    assert_eq!(search_history.current(&cursor), Some("Rust"));
 82
 83    assert_eq!(search_history.next(&mut cursor), Some("JavaScript"));
 84    assert_eq!(search_history.current(&cursor), Some("JavaScript"));
 85
 86    assert_eq!(search_history.next(&mut cursor), Some("TypeScript"));
 87    assert_eq!(search_history.current(&cursor), Some("TypeScript"));
 88
 89    assert_eq!(search_history.next(&mut cursor), None);
 90    assert_eq!(search_history.current(&cursor), Some("TypeScript"));
 91}
 92
 93#[test]
 94fn test_reset_selection() {
 95    let mut search_history = SearchHistory::new(None, QueryInsertionBehavior::AlwaysInsert);
 96    let mut cursor = SearchHistoryCursor::default();
 97
 98    search_history.add(&mut cursor, "Rust".to_string());
 99    search_history.add(&mut cursor, "JavaScript".to_string());
100    search_history.add(&mut cursor, "TypeScript".to_string());
101
102    assert_eq!(search_history.current(&cursor), Some("TypeScript"));
103    cursor.reset();
104    assert_eq!(search_history.current(&cursor), None);
105    assert_eq!(
106        search_history.previous(&mut cursor),
107        Some("TypeScript"),
108        "Should start from the end after reset on previous item query"
109    );
110
111    search_history.previous(&mut cursor);
112    assert_eq!(search_history.current(&cursor), Some("JavaScript"));
113    search_history.previous(&mut cursor);
114    assert_eq!(search_history.current(&cursor), Some("Rust"));
115
116    cursor.reset();
117    assert_eq!(search_history.current(&cursor), None);
118}
119
120#[test]
121fn test_multiple_cursors() {
122    let mut search_history = SearchHistory::new(None, QueryInsertionBehavior::AlwaysInsert);
123    let mut cursor1 = SearchHistoryCursor::default();
124    let mut cursor2 = SearchHistoryCursor::default();
125
126    search_history.add(&mut cursor1, "Rust".to_string());
127    search_history.add(&mut cursor1, "JavaScript".to_string());
128    search_history.add(&mut cursor1, "TypeScript".to_string());
129
130    search_history.add(&mut cursor2, "Python".to_string());
131    search_history.add(&mut cursor2, "Java".to_string());
132    search_history.add(&mut cursor2, "C++".to_string());
133
134    assert_eq!(search_history.current(&cursor1), Some("TypeScript"));
135    assert_eq!(search_history.current(&cursor2), Some("C++"));
136
137    assert_eq!(search_history.previous(&mut cursor1), Some("JavaScript"));
138    assert_eq!(search_history.previous(&mut cursor2), Some("Java"));
139
140    assert_eq!(search_history.next(&mut cursor1), Some("TypeScript"));
141    assert_eq!(search_history.next(&mut cursor1), Some("Python"));
142
143    cursor1.reset();
144    cursor2.reset();
145
146    assert_eq!(search_history.current(&cursor1), None);
147    assert_eq!(search_history.current(&cursor2), None);
148}