1+ use assert_cmd:: Command ;
2+ use predicates:: prelude:: * ;
3+ use std:: fs;
4+ use anyhow:: Result ;
5+
6+ #[ test]
7+ fn dies_no_args ( ) -> Result < ( ) > {
8+ let mut cmd = Command :: cargo_bin ( "echor" ) ?;
9+ cmd. assert ( )
10+ . failure ( )
11+ . stderr ( predicate:: str:: contains ( "Usage" ) ) ;
12+ Ok ( ( ) )
13+ }
14+
15+ #[ test]
16+ fn hello1 ( ) -> Result < ( ) > {
17+ let expected = fs:: read_to_string ( "tests/expected/hello1.txt" ) ?;
18+ let mut cmd = Command :: cargo_bin ( "echor" ) ?;
19+ cmd. arg ( "Hello there" ) . assert ( ) . success ( ) . stdout ( expected) ;
20+ Ok ( ( ) )
21+ }
22+
23+ #[ test]
24+ fn hello2 ( ) -> Result < ( ) > {
25+ let expected = fs:: read_to_string ( "tests/expected/hello2.txt" ) ?;
26+ let mut cmd = Command :: cargo_bin ( "echor" ) ?;
27+ cmd. args ( vec ! [ "Hello" , "there" ] )
28+ . assert ( )
29+ . success ( )
30+ . stdout ( expected) ;
31+ Ok ( ( ) )
32+ }
33+
34+ fn run ( args : & [ & str ] , expected_file : & str ) -> Result < ( ) > {
35+ let expected = fs:: read_to_string ( expected_file) ?;
36+ Command :: cargo_bin ( "echor" ) ?
37+ . args ( args)
38+ . assert ( )
39+ . success ( )
40+ . stdout ( expected) ;
41+ Ok ( ( ) )
42+ }
43+
44+ // #[test]
45+ // fn hello1() -> Result<()> {
46+ // run(&["Hello there"], "tests/expected/hello1.txt")
47+ // }
48+
49+ // #[test]
50+ // fn hello2() -> Result<()> {
51+ // run(&["Hello", "there"], "tests/expected/hello2.txt")
52+ // }
53+
54+ #[ test]
55+ fn hello1_no_newline ( ) -> Result < ( ) > {
56+ run ( & [ "Hello there" , "-n" ] , "tests/expected/hello1.n.txt" ) // Two spaces!
57+ }
58+
59+ #[ test]
60+ fn hello2_no_newline ( ) -> Result < ( ) > {
61+ run ( & [ "-n" , "Hello" , "there" ] , "tests/expected/hello2.n.txt" )
62+ }
0 commit comments