jeudi 29 octobre 2015

Creating a read stream over a string

I have a function that takes an input stream, processes its data, and then returns something, basically a more complicated version of this:

fn read_number_from_stream(input: &mut io::BufRead) -> io::Result<u32> {
  // code that does something useful here
  Ok(0)
}

Now I want to write a test for this function.

#[test]
fn input_with_zero_returns_zero() {
  let test_input = read_from_string("0\n");
  assert_eq!(Ok(0), read_number_from_stream(test_input));
}

How do I implement read_from_string? Older versions of Rust apparently provided std::io::mem::MemReader, but the entire std::io::mem module seems to be gone in more recent versions of Rust (I'm using the unstable 1.5 branch).

Aucun commentaire:

Enregistrer un commentaire