@@ -2,7 +2,7 @@ use std::cmp::Ord;
22use std:: collections:: HashMap ;
33use std:: env;
44use std:: fs:: { self , File } ;
5- use std:: io:: prelude:: * ;
5+ use std:: io:: { prelude:: * , stdin } ;
66use std:: ops:: Deref ;
77use std:: path:: { Path , PathBuf } ;
88
@@ -203,7 +203,9 @@ impl<'a> ServiceProvider<'a> for Leetcode<'a> {
203203
204204 async fn problem_test ( & self , test : cmd:: Test ) -> Result < ( ) > {
205205 let problem = service:: extract_problem ( test. filename ) ?;
206- let test_data = test. test_data . replace ( "\\ n" , "\n " ) ;
206+
207+ let test_data = self . get_test_data ( test. test_data ) ;
208+ debug ! ( "Test data: {:?}" , test_data) ;
207209 let typed_code = parse_code ( problem. typed_code . as_ref ( ) . expect ( "Expected typed_code" ) ) ;
208210 let body = json ! ( {
209211 "lang" : problem. lang. to_owned( ) ,
@@ -624,4 +626,31 @@ impl<'a> Leetcode<'a> {
624626
625627 Ok ( ( ) )
626628 }
629+
630+ /*
631+ * Parse Option<Option<String>> from structopt
632+ *
633+ * Get string from command line if provided, otherwise try to get string from stdin
634+ *
635+ * We can provide test data as multiline input using stdin.
636+ *
637+ * # Example:
638+ * ```bash
639+ * leetup test 3sum.java -t << END
640+ * [1,-1,0]
641+ * [0, 1, 1, 1, 2, -3, -1]
642+ * [1,2,3]
643+ * END
644+ * ```
645+ */
646+ fn get_test_data ( & self , test_data : Option < Option < String > > ) -> String {
647+ test_data. unwrap ( ) . unwrap_or_else ( || {
648+ let mut buf = String :: new ( ) ;
649+ stdin ( )
650+ . lock ( )
651+ . read_to_string ( & mut buf)
652+ . expect ( "test input expected from stdin" ) ;
653+ buf
654+ } )
655+ }
627656}
0 commit comments