diff options
Diffstat (limited to 'mingling_core/src/comp/suggest.rs')
| -rw-r--r-- | mingling_core/src/comp/suggest.rs | 263 |
1 files changed, 263 insertions, 0 deletions
diff --git a/mingling_core/src/comp/suggest.rs b/mingling_core/src/comp/suggest.rs index cd025a4..03842e1 100644 --- a/mingling_core/src/comp/suggest.rs +++ b/mingling_core/src/comp/suggest.rs @@ -183,3 +183,266 @@ impl From<(String, String)> for SuggestItem { Self::new_with_desc(suggest, description) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_suggest_new_creates_empty() { + let s = Suggest::new(); + match s { + Suggest::Suggest(set) => assert!(set.is_empty(), "expected empty BTreeSet"), + Suggest::FileCompletion => panic!("expected Suggest variant"), + } + } + + #[test] + fn test_suggest_file_comp() { + assert_eq!(Suggest::file_comp(), Suggest::FileCompletion); + } + + #[test] + fn test_from_vec_string() { + let items = vec!["foo".to_string(), "bar".to_string()]; + let suggest: Suggest = items.into(); + match suggest { + Suggest::Suggest(set) => { + assert_eq!(set.len(), 2); + assert!(set.contains(&SuggestItem::new("foo".to_string()))); + assert!(set.contains(&SuggestItem::new("bar".to_string()))); + } + Suggest::FileCompletion => panic!("expected Suggest variant"), + } + } + + #[test] + fn test_from_vec_str_ref() { + let items = vec!["a", "b", "c"]; + let suggest: Suggest = items.into(); + match suggest { + Suggest::Suggest(set) => { + assert_eq!(set.len(), 3); + } + Suggest::FileCompletion => panic!("expected Suggest variant"), + } + } + + #[test] + fn test_from_array_str_ref() { + let items = ["x", "y", "z"]; + let suggest: Suggest = items.into(); + match suggest { + Suggest::Suggest(set) => { + assert_eq!(set.len(), 3); + } + Suggest::FileCompletion => panic!("expected Suggest variant"), + } + } + + #[test] + fn test_deref_suggest() { + let s: Suggest = ["hello"].into(); + let set: &BTreeSet<SuggestItem> = &*s; + assert_eq!(set.len(), 1); + } + + #[test] + #[should_panic(expected = "Cannot deref FileCompletion variant")] + fn test_deref_file_completion_panics() { + let s = Suggest::FileCompletion; + let _ = &*s; + } + + #[test] + fn test_deref_mut_suggest() { + let mut s = Suggest::Suggest(BTreeSet::new()); + s.insert(SuggestItem::new("inserted".to_string())); + assert_eq!(s.len(), 1); + } + + #[test] + #[should_panic(expected = "Cannot deref_mut FileCompletion variant")] + fn test_deref_mut_file_completion_panics() { + let mut s = Suggest::FileCompletion; + let _ = &mut *s; + } + + #[test] + fn test_suggest_item_new() { + let item = SuggestItem::new("hello".to_string()); + assert!(matches!(item, SuggestItem::Simple(ref s) if s == "hello")); + } + + #[test] + fn test_suggest_item_new_with_desc() { + let item = SuggestItem::new_with_desc("hello".to_string(), "desc".to_string()); + assert!( + matches!(item, SuggestItem::WithDescription(ref s, ref d) if s == "hello" && d == "desc") + ); + } + + #[test] + fn test_with_desc_replaces_existing() { + let item = SuggestItem::new_with_desc("foo".to_string(), "old".to_string()) + .with_desc("new".to_string()); + assert_eq!(item.description(), Some(&"new".to_string())); + } + + #[test] + fn test_with_desc_on_simple() { + let item = SuggestItem::new("foo".to_string()).with_desc("added".to_string()); + assert_eq!(item.description(), Some(&"added".to_string())); + } + + #[test] + fn test_suggest_returns_text() { + let simple = SuggestItem::new("simple".to_string()); + let desc = SuggestItem::new_with_desc("desc".to_string(), "d".to_string()); + assert_eq!(simple.suggest(), &"simple".to_string()); + assert_eq!(desc.suggest(), &"desc".to_string()); + } + + #[test] + fn test_description() { + let simple = SuggestItem::new("x".to_string()); + assert_eq!(simple.description(), None); + + let desc = SuggestItem::new_with_desc("x".to_string(), "y".to_string()); + assert_eq!(desc.description(), Some(&"y".to_string())); + } + + #[test] + fn test_set_suggest() { + let mut item = SuggestItem::new("old".to_string()); + item.set_suggest("new".to_string()); + assert_eq!(item.suggest(), &"new".to_string()); + + let mut item = SuggestItem::new_with_desc("old".to_string(), "d".to_string()); + item.set_suggest("newer".to_string()); + assert_eq!(item.suggest(), &"newer".to_string()); + } + + #[test] + fn test_set_description_on_simple() { + let mut item = SuggestItem::new("text".to_string()); + item.set_description("added".to_string()); + assert_eq!(item.description(), Some(&"added".to_string())); + } + + #[test] + fn test_set_description_replaces_existing() { + let mut item = SuggestItem::new_with_desc("text".to_string(), "old".to_string()); + item.set_description("new".to_string()); + assert_eq!(item.description(), Some(&"new".to_string())); + } + + #[test] + fn test_remove_desc_on_simple() { + let mut item = SuggestItem::new("text".to_string()); + assert_eq!(item.remove_desc(), None); + assert!(matches!(item, SuggestItem::Simple(_))); + } + + #[test] + fn test_remove_desc_on_with_description() { + let mut item = SuggestItem::new_with_desc("text".to_string(), "desc".to_string()); + let desc = item.remove_desc(); + assert_eq!(desc, Some("desc".to_string())); + assert!(matches!(item, SuggestItem::Simple(ref s) if s == "text")); + } + + #[test] + fn test_ord_by_suggest_text() { + let mut items = vec![ + SuggestItem::new("z".to_string()), + SuggestItem::new("a".to_string()), + SuggestItem::new("m".to_string()), + ]; + items.sort(); + assert_eq!(items[0].suggest(), &"a".to_string()); + assert_eq!(items[1].suggest(), &"m".to_string()); + assert_eq!(items[2].suggest(), &"z".to_string()); + } + + #[test] + fn test_ord_with_description() { + let mut items = vec![ + SuggestItem::new_with_desc("z".to_string(), "zzz".to_string()), + SuggestItem::new("a".to_string()), + SuggestItem::new_with_desc("m".to_string(), "mmm".to_string()), + ]; + items.sort(); + assert_eq!(items[0].suggest(), &"a".to_string()); + assert_eq!(items[1].suggest(), &"m".to_string()); + assert_eq!(items[2].suggest(), &"z".to_string()); + } + + #[test] + fn test_from_string_for_suggest_item() { + let item: SuggestItem = "test".to_string().into(); + assert!(matches!(item, SuggestItem::Simple(ref s) if s == "test")); + } + + #[test] + fn test_from_tuple_for_suggest_item() { + let item: SuggestItem = ("key".to_string(), "val".to_string()).into(); + assert!( + matches!(item, SuggestItem::WithDescription(ref s, ref d) if s == "key" && d == "val") + ); + } + + #[test] + fn test_default_suggest_item() { + let item = SuggestItem::default(); + assert!(matches!(item, SuggestItem::Simple(ref s) if s.is_empty())); + } + + #[test] + fn test_strip_typed_argument_removes_typed() { + let ctx = ShellContext { + all_words: vec!["--verbose".to_string(), "--help".to_string()], + ..ShellContext::default() + }; + + let suggest: Suggest = vec!["--verbose", "--output", "--help"].into(); + let stripped = suggest.strip_typed_argument(&ctx); + + match stripped { + Suggest::Suggest(set) => { + assert_eq!(set.len(), 1); + assert!(set.contains(&SuggestItem::new("--output".to_string()))); + } + Suggest::FileCompletion => panic!("expected Suggest variant"), + } + } + + #[test] + fn test_strip_typed_argument_passes_file_completion() { + let ctx = ShellContext { + all_words: vec!["--verbose".to_string()], + ..ShellContext::default() + }; + + let stripped = Suggest::FileCompletion.strip_typed_argument(&ctx); + assert_eq!(stripped, Suggest::FileCompletion); + } + + #[test] + fn test_strip_typed_argument_keeps_untyped() { + let ctx = ShellContext { + all_words: vec!["--verbose".to_string()], + ..ShellContext::default() + }; + + let suggest: Suggest = vec!["--output", "--help"].into(); + let stripped = suggest.strip_typed_argument(&ctx); + + match stripped { + Suggest::Suggest(set) => { + assert_eq!(set.len(), 2); + } + Suggest::FileCompletion => panic!("expected Suggest variant"), + } + } +} |
