use super::{assert_reported, assert_silent_about, codes_of};
use crate::DiagnosticCode;
#[test]
fn a_one_statement_then_branch_is_reported() {
let call = codes_of("void start();\nvoid f(bool ready) {\n if (ready) start();\n}\n");
assert_reported(
&call,
DiagnosticCode::BracesAroundStatements,
"`start();` is one statement",
);
let returning = codes_of("void f(bool ready) {\n if (ready) return;\n}\n");
assert_reported(
&returning,
DiagnosticCode::BracesAroundStatements,
"`return;` is one statement",
);
}
#[test]
fn a_dangling_else_is_two_one_statement_bodies() {
let codes = codes_of(
"void act();\nvoid f(bool a, bool b) {\n if (a) if (b) act(); else act();\n}\n",
);
let reported = codes
.iter()
.filter(|code| **code == DiagnosticCode::BracesAroundStatements)
.count();
assert_eq!(
reported, 2,
"the outer branch is an `if` and the inner one a call: {codes:?}"
);
}
#[test]
fn a_braced_body_is_silent() {
let block = codes_of("void start();\nvoid f(bool ready) {\n if (ready) { start(); }\n}\n");
assert_silent_about(
&block,
DiagnosticCode::BracesAroundStatements,
"the branch begins with `{`",
);
let empty = codes_of("void f(bool ready) {\n if (ready) { }\n}\n");
assert_silent_about(
&empty,
DiagnosticCode::BracesAroundStatements,
"an empty `{ }` is still a block",
);
}
#[test]
fn a_one_statement_while_body_is_silent() {
let codes = codes_of("void start();\nvoid f(bool ready) {\n while (ready) start();\n}\n");
assert_silent_about(
&codes,
DiagnosticCode::BracesAroundStatements,
"the walk covers `if` statements",
);
}
#[test]
fn a_braced_then_branch_with_an_unbraced_else_is_silent() {
let codes =
codes_of("void a();\nvoid b();\nvoid f(bool x) {\n if (x) { a(); } else b();\n}\n");
assert_silent_about(
&codes,
DiagnosticCode::BracesAroundStatements,
"the then-branch is a block, and the `else` body is not read",
);
}