Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing Custom Completer's is impossible? #261

Closed
bgianfo opened this issue Aug 10, 2019 · 1 comment · Fixed by #262
Closed

Testing Custom Completer's is impossible? #261

bgianfo opened this issue Aug 10, 2019 · 1 comment · Fixed by #262

Comments

@bgianfo
Copy link

bgianfo commented Aug 10, 2019

The fact that the rustyline::completion::Completer.complete contract takes a rustyline::Context<'_>, which can't be constructed from outside the crate, makes it very difficult to write unit tests for any custom Completer impl that you might have.

I've been trying to update some old code that was using rustyline 1.0, before this parameter was added, and it was very easy. Now it seems like there isn't any option but to delete all my tests?

Example tests (pardon the hacky rust):

#[cfg(test)]    
fn verify_completion(input: &str, expected_completion: &str)    
{    
    let completer = MyCustomCompleter::new();    
    assert_eq!(completer.complete(input, 0).unwrap(), (0, vec![String::from(expected_completion)]));    
}    
    
#[test]    
fn completion_test_items() {    
    // Verify that the completion for i completes to items. 
    verify_completion("i", "items");    
    verify_completion("ite", "items");    
}    

Since history/history_index on the Context are private we don't have the option of doing something like:

#[cfg(test)]    
fn verify_completion(input: &str, expected_completion: &str)    
{    
    let hist = rustyline::history::History::new();    
    
    let ctx = rustyline::Context {    
        history: &hist,    
        history_index: 0,    
    };    
    
    let completer = CommandCompleter::new();    
    assert_eq!(completer.complete(input, 0, &ctx).unwrap(), (0, vec![String::from(expected_completion)]));    
}   

Thanks!

@bgianfo
Copy link
Author

bgianfo commented Aug 10, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant