2015-02-06 15 views
5

Tôi muốn viết một chức năng nhắc gửi một chuỗi được truyền vào stdout và sau đó trả về chuỗi mà nó đọc từ stdin. Làm thế nào tôi có thể kiểm tra nó?Tôi có thể kiểm tra stdin và stdout bằng cách nào?

Dưới đây là một ví dụ của hàm:

fn prompt(question: String) -> String { 
    let mut stdin = BufferedReader::new(stdin()); 
    print!("{}", question); 
    match stdin.read_line() { 
     Ok(line) => line, 
     Err(e) => panic!(e), 
    } 
} 

Và đây là nỗ lực thử nghiệm của tôi

#[test] 
fn try_to_test_stdout() { 
    let writer: Vec<u8> = vec![]; 
    set_stdout(Box::new(writer)); 
    print!("testing"); 
// `writer` is now gone, can't check to see if "testing" was sent 
} 

Trả lời

4

tiêm sử dụng phụ thuộc. Khớp nối nó với Generics và monomorphism, bạn không mất bất kỳ hoạt động:

use std::io::{self, BufRead, Write}; 

fn prompt<R, W>(mut reader: R, mut writer: W, question: &str) -> String 
where 
    R: BufRead, 
    W: Write, 
{ 
    write!(&mut writer, "{}", question).expect("Unable to write"); 
    let mut s = String::new(); 
    reader.read_line(&mut s).expect("Unable to read"); 
    s 
} 

#[test] 
fn test_with_in_memory() { 
    let input = b"I'm George"; 
    let mut output = Vec::new(); 

    let answer = prompt(&input[..], &mut output, "Who goes there?"); 

    let output = String::from_utf8(output).expect("Not UTF-8"); 

    assert_eq!("Who goes there?", output); 
    assert_eq!("I'm George", answer); 
} 

fn main() { 
    let stdio = io::stdin(); 
    let input = stdio.lock(); 

    let output = io::stdout(); 

    let answer = prompt(input, output, "Who goes there?"); 
    println!("was: {}", answer); 
} 

Trong nhiều trường hợp, bạn muốn thực sự tuyên truyền các lỗi sao lưu cho người gọi thay vì sử dụng expect, như IO là một rất nơi chung cho thất bại xảy ra.


Điều này có thể được mở rộng hơn chức năng vào phương pháp:

use std::io::{self, BufRead, Write}; 

struct Quizzer<R, W> { 
    reader: R, 
    writer: W, 
} 

impl<R, W> Quizzer<R, W> 
where 
    R: BufRead, 
    W: Write, 
{ 
    fn prompt(&mut self, question: &str) -> String { 
     write!(&mut self.writer, "{}", question).expect("Unable to write"); 
     let mut s = String::new(); 
     self.reader.read_line(&mut s).expect("Unable to read"); 
     s 
    } 
} 

#[test] 
fn test_with_in_memory() { 
    let input = b"I'm George"; 
    let mut output = Vec::new(); 

    let answer = { 
     let mut quizzer = Quizzer { 
      reader: &input[..], 
      writer: &mut output, 
     }; 

     quizzer.prompt("Who goes there?") 
    }; 

    let output = String::from_utf8(output).expect("Not UTF-8"); 

    assert_eq!("Who goes there?", output); 
    assert_eq!("I'm George", answer); 
} 

fn main() { 
    let stdio = io::stdin(); 
    let input = stdio.lock(); 

    let output = io::stdout(); 

    let mut quizzer = Quizzer { 
     reader: input, 
     writer: output, 
    }; 

    let answer = quizzer.prompt("Who goes there?"); 
    println!("was: {}", answer); 
} 
Các vấn đề liên quan