use super::{assert_reported, assert_silent_about, codes_of};
use crate::DiagnosticCode;
#[test]
fn an_unclosed_parameter_list_is_reported() {
let codes = codes_of("int main( { return 0; }\n");
assert_reported(
&codes,
DiagnosticCode::SyntaxError,
"the `(` of `main` is never closed, so the file does not parse",
);
}
#[test]
fn a_file_that_parses_is_quiet() {
let plain = codes_of("int add(int a, int b) { return a + b; }\n");
assert_silent_about(
&plain,
DiagnosticCode::SyntaxError,
"a plain function is a program the parser reads without complaint",
);
let declarations = codes_of("struct Point { int x; int y; };\nPoint origin;\n");
assert_silent_about(
&declarations,
DiagnosticCode::SyntaxError,
"a class and a variable are ordinary declarations",
);
}
#[test]
fn a_comment_is_not_part_of_the_program() {
let commented_out = codes_of("// a brace that opens nothing: {\nint f() { return 0; }\n");
assert_silent_about(
&commented_out,
DiagnosticCode::SyntaxError,
"a `{` inside a comment is trivia and the parser never sees a brace",
);
}
#[test]
fn a_string_literal_holds_no_syntax() {
let braced = codes_of("const char* text = \"{\";\nint f() { return 0; }\n");
assert_silent_about(
&braced,
DiagnosticCode::SyntaxError,
"the `{` is inside a string literal, which is one token and not a brace",
);
}
#[test]
fn a_macro_definition_is_not_a_statement() {
let defined = codes_of("#define TWICE(x) ((x) * 2)\nint f() { return TWICE(2); }\n");
assert_silent_about(
&defined,
DiagnosticCode::SyntaxError,
"a `#define` is a directive, and its body is not C++ the parser parses as code",
);
}