aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/tests/test-comp/tests/integration.rs
blob: 4e6455e2081355a9c29603d398493ef4abf4ed93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use mingling::Groupped;
use mingling::Program;
use mingling::ProgramCollect;
use mingling::RenderResult;
use mingling::comp::{ShellContext, ShellFlag, Suggest, SuggestItem};

/// Minimal mock collector that satisfies `C: ProgramCollect<Enum = C>`
/// by setting `Enum = Self`.
#[derive(Debug, Clone, PartialEq)]
struct MockCollect;

impl Groupped<MockCollect> for MockCollect {
    fn member_id() -> MockCollect {
        MockCollect
    }
}

impl ProgramCollect for MockCollect {
    type Enum = MockCollect;
    type ErrorDispatcherNotFound = MockCollect;
    type ErrorRendererNotFound = MockCollect;
    type ResultEmpty = MockCollect;

    fn build_renderer_not_found(_member_id: MockCollect) -> mingling::AnyOutput<MockCollect> {
        unimplemented!()
    }
    fn build_dispatcher_not_found(_args: Vec<String>) -> mingling::AnyOutput<MockCollect> {
        unimplemented!()
    }
    fn build_empty_result() -> mingling::AnyOutput<MockCollect> {
        unimplemented!()
    }
    fn render(_any: mingling::AnyOutput<MockCollect>, _r: &mut RenderResult) {
        unimplemented!()
    }
    fn render_help(_any: mingling::AnyOutput<MockCollect>, _r: &mut RenderResult) {
        unimplemented!()
    }
    fn do_chain(_any: mingling::AnyOutput<MockCollect>) -> mingling::ChainProcess<MockCollect> {
        unimplemented!()
    }
    fn do_comp(_any: &mingling::AnyOutput<MockCollect>, _ctx: &ShellContext) -> Suggest {
        unimplemented!()
    }
    fn has_renderer(_any: &mingling::AnyOutput<MockCollect>) -> bool {
        unimplemented!()
    }
    fn has_chain(_any: &mingling::AnyOutput<MockCollect>) -> bool {
        unimplemented!()
    }
}

#[test]
fn test_shell_context_parsing_full() {
    let args = vec![
        "-f".to_string(),
        "myapp hello ^world".to_string(),
        "-C".to_string(),
        "14".to_string(),
        "-w".to_string(),
        "hello".to_string(),
        "-p".to_string(),
        "myapp".to_string(),
        "-c".to_string(),
        "myapp".to_string(),
        "-i".to_string(),
        "1".to_string(),
        "-F".to_string(),
        "bash".to_string(),
    ];
    let ctx = ShellContext::try_from(args).unwrap();
    assert_eq!(ctx.command_line, "myapp hello -world");
    assert_eq!(ctx.cursor_position, 14);
    assert_eq!(ctx.current_word, "hello");
    assert_eq!(ctx.previous_word, "myapp");
    assert_eq!(ctx.command_name, "myapp");
    assert_eq!(ctx.word_index, 1);
    assert!(matches!(ctx.shell_flag, ShellFlag::Bash));
}

#[test]
fn test_shell_context_parsing_empty() {
    let ctx = ShellContext::try_from(vec![]).unwrap();
    assert!(ctx.all_words.is_empty());
    assert!(matches!(ctx.shell_flag, ShellFlag::Other(_)));
}

#[test]
fn test_suggest_from_vec() {
    let s: Suggest = vec!["--help".to_string(), "--version".to_string()].into();
    match &s {
        Suggest::Suggest(items) => {
            assert_eq!(items.len(), 2);
        }
        _ => panic!("expected Suggest::Suggest"),
    }
}

#[test]
fn test_suggest_item_new() {
    let item = SuggestItem::new("hello".to_string());
    assert_eq!(item.suggest(), "hello");
    assert!(item.description().is_none());
}

#[test]
fn test_suggest_item_with_description() {
    let item = SuggestItem::new_with_desc("hello".to_string(), "a greeting".to_string());
    assert_eq!(item.suggest(), "hello");
    assert_eq!(item.description(), Some(&"a greeting".to_string()));
}

#[test]
fn test_program_is_completing() {
    let program: Program<MockCollect> = Program::new_with_args(["myapp", "__comp", "hello", ""]);
    assert!(program.is_completing());
}

#[test]
fn test_program_is_not_completing() {
    let program: Program<MockCollect> = Program::new_with_args(["myapp", "hello"]);
    assert!(!program.is_completing());
}