Skip to content

Commit 9543838

Browse files
committed
Add some tests for the Json and Checkstyle emitters using stdin.
I've added #[rustfmt::skip] to a couple of long lines; this would probably be better as test files rather than inline.
1 parent 993782c commit 9543838

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/test/mod.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,58 @@ fn stdin_works_with_modified_lines() {
428428
assert_eq!(buf, output.as_bytes());
429429
}
430430

431+
/// Ensures that `EmitMode::Json` works with input from `stdin`.
432+
#[test]
433+
fn stdin_works_with_json() {
434+
init_log();
435+
let input = "\nfn\n some( )\n{\n}\nfn 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 = "\nfn\n some( )\n{\n}\nfn 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+
431483
#[test]
432484
fn stdin_disable_all_formatting_test() {
433485
init_log();

0 commit comments

Comments
 (0)