@@ -428,6 +428,58 @@ fn stdin_works_with_modified_lines() {
428
428
assert_eq ! ( buf, output. as_bytes( ) ) ;
429
429
}
430
430
431
+ /// Ensures that `EmitMode::Json` works with input from `stdin`.
432
+ #[ test]
433
+ fn stdin_works_with_json ( ) {
434
+ init_log ( ) ;
435
+ let input = "\n fn\n some( )\n {\n }\n fn main () {}\n " ;
436
+ #[ rustfmt:: skip]
437
+ let output = r#"[{"name":"stdin","mismatches":[{"original_begin_line":1,"original_end_line":6,"expected_begin_line":1,"expected_end_line":2,"original":"\nfn\n some( )\n{\n}\nfn main () {}","expected":"fn some() {}\nfn main() {}"}]}]"# ;
438
+
439
+ let input = Input :: Text ( input. to_owned ( ) ) ;
440
+ let mut config = Config :: default ( ) ;
441
+ config. set ( ) . newline_style ( NewlineStyle :: Unix ) ;
442
+ config. set ( ) . emit_mode ( EmitMode :: Json ) ;
443
+ let mut buf: Vec < u8 > = vec ! [ ] ;
444
+ {
445
+ let mut session = Session :: new ( config, Some ( & mut buf) ) ;
446
+ session. format ( input) . unwrap ( ) ;
447
+ let errors = ReportedErrors {
448
+ has_diff : true ,
449
+ ..Default :: default ( )
450
+ } ;
451
+ assert_eq ! ( session. errors, errors) ;
452
+ }
453
+ assert_eq ! ( buf, output. as_bytes( ) ) ;
454
+ }
455
+
456
+ /// Ensures that `EmitMode::Checkstyle` works with input from `stdin`.
457
+ #[ test]
458
+ fn stdin_works_with_checkstyle ( ) {
459
+ init_log ( ) ;
460
+ let input = "\n fn\n some( )\n {\n }\n fn main () {}\n " ;
461
+ #[ rustfmt:: skip]
462
+ let output = r#"<?xml version="1.0" encoding="utf-8"?>
463
+ <checkstyle version="4.3"><file name="stdin"><error line="1" severity="warning" message="Should be `fn some() {}`" /><error line="2" severity="warning" message="Should be `fn main() {}`" /></file></checkstyle>
464
+ "# ;
465
+
466
+ let input = Input :: Text ( input. to_owned ( ) ) ;
467
+ let mut config = Config :: default ( ) ;
468
+ config. set ( ) . newline_style ( NewlineStyle :: Unix ) ;
469
+ config. set ( ) . emit_mode ( EmitMode :: Checkstyle ) ;
470
+ let mut buf: Vec < u8 > = vec ! [ ] ;
471
+ {
472
+ let mut session = Session :: new ( config, Some ( & mut buf) ) ;
473
+ session. format ( input) . unwrap ( ) ;
474
+ let errors = ReportedErrors {
475
+ has_diff : false ,
476
+ ..Default :: default ( )
477
+ } ;
478
+ assert_eq ! ( session. errors, errors) ;
479
+ }
480
+ assert_eq ! ( buf, output. as_bytes( ) ) ;
481
+ }
482
+
431
483
#[ test]
432
484
fn stdin_disable_all_formatting_test ( ) {
433
485
init_log ( ) ;
0 commit comments