Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test stdin and stdout?

Tags:

I'd like to write a prompt function that sends a passed-in string to stdout and then returns the string that it reads from stdin. How could I test it?

Here is an example of the function:

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

And here is my testing attempt

#[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 } 
like image 207
rusty2iron Avatar asked Feb 06 '15 16:02

rusty2iron


People also ask

What is stdin and stdout in C?

Variable: FILE * stdin. The standard input stream, which is the normal source of input for the program. Variable: FILE * stdout. The standard output stream, which is used for normal output from the program.

What is stdin and stdout?

stdin − It stands for standard input, and is used for taking text as an input. stdout − It stands for standard output, and is used to text output of any command you type in the terminal, and then that output is stored in the stdout stream.

What is stdin and stdout in python?

stdin , stdout , and stderr are predefined file objects that correspond to Python's standard input, output, and error streams. You can rebind stdout and stderr to file-like objects (objects that supply a write method accepting a string argument) to redirect the destination of output and error messages.

What is stdout in Hackerrank?

This is the standard stream to write or print the output from your code. For example, for the above-mentioned coding question, you need to write the output from your program. If coding in C#, you must use the Console. WriteLine() statement to print the output value.

How do you test if stdin is connected?

We’ve used the file descriptor 0 as the argument to the test, which represents stdin. If stdin is connected to a terminal window the test will prove true. If stdin is connected to a file or a pipe, the test will fail. We can use any convenient text file to generate input to the script.

How to read stdin and stdout in Python?

You can refer to the below screenshot for python stdout. To read an input from stdin we can call read () and readlines () function in Python, for reading everything. After writing the above code (read from stdin in python), the stdin will read input and prints the input for each.

How to check if the stdout output went to the file?

We can see that both streams of output, stdout and stderr, have been displayed in the terminal windows. The error message that is delivered via stderr is still sent to the terminal window. We can check the contents of the file to see whether the stdout output went to the file.

What are input (stdin) output (stdout) and error (stderr) streams?

In this article, We are going to look into the Standard Input (stdin) Output (stdout) and Error (stderr) Streams in C Programming Language. In UNIX-like systems such as Linux, Everything is a file. So the input stream is also a file.


1 Answers

Use dependency injection. Coupling it with generics and monomorphism, you don't lose any performance:

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); } 

In many cases, you'd want to actually propagate the error back up to the caller instead of using expect, as IO is a very common place for failures to occur.


This can be extended beyond functions into methods:

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); } 
like image 71
Shepmaster Avatar answered Sep 22 '22 13:09

Shepmaster