use super::{assert_reported, assert_silent_about, codes_in, codes_of};
use crate::DiagnosticCode;
#[test]
fn a_type_used_without_its_header_is_reported() {
let a_variable = codes_in(
&[
("/p/widget.h", "class Widget { int size; };\n"),
("/p/use.cpp", "Widget w;\n"),
],
"/p/use.cpp",
);
assert_reported(
&a_variable,
DiagnosticCode::MissingInclude,
"`Widget` is declared in widget.h, which use.cpp never includes",
);
let a_parameter = codes_in(
&[
("/p/nothere.h", "class NotHere {};\n"),
("/p/uses.cpp", "void f(NotHere* value) {}\n"),
],
"/p/uses.cpp",
);
assert_reported(
&a_parameter,
DiagnosticCode::MissingInclude,
"a type in a parameter list is a position this rule reads",
);
}
#[test]
fn a_type_the_file_can_already_see_is_not_missing_an_include() {
let included = codes_in(
&[
("/p/widget.h", "class Widget { int size; };\n"),
(
"/p/use.cpp",
"#include \"widget.h\"\nvoid f(Widget* w) {}\n",
),
],
"/p/use.cpp",
);
assert_silent_about(
&included,
DiagnosticCode::MissingInclude,
"widget.h is already in this file's include closure",
);
let declared_here = codes_of("struct Widget { int size; };\nvoid f(Widget* w) {}\n");
assert_silent_about(
&declared_here,
DiagnosticCode::MissingInclude,
"`Widget` is declared in this file, so nothing is missing from its includes",
);
}
#[test]
fn a_qualified_spelling_is_a_namespace_question() {
let qualified = codes_in(
&[
("/p/gui.h", "namespace gui { class Widget {}; }\n"),
("/p/use.cpp", "void f(gui::Widget* w) {}\n"),
],
"/p/use.cpp",
);
assert_silent_about(
&qualified,
DiagnosticCode::MissingInclude,
"`gui::Widget` is written with a `::`, which this rule leaves to the namespace question",
);
}
#[test]
fn a_name_the_file_binds_is_not_from_elsewhere() {
let a_template_parameter = codes_in(
&[
("/p/container.h", "class Container {};\n"),
(
"/p/db.h",
"template <class K, class V, class Container>\n\
class DBase {\n\
public:\n\
V* find(const K& key);\n\
private:\n\
Container _hash;\n\
};\n",
),
],
"/p/db.h",
);
assert_silent_about(
&a_template_parameter,
DiagnosticCode::MissingInclude,
"`Container` is this class's own template parameter, which the scope tree binds here",
);
let a_local = codes_in(
&[
("/p/widget.h", "class Widget { int size; };\n"),
("/p/use.cpp", "void f() { Widget w; }\n"),
],
"/p/use.cpp",
);
assert_silent_about(
&a_local,
DiagnosticCode::MissingInclude,
"a declaration inside a body is local, and its type is not this rule's question",
);
}
#[test]
fn a_type_declared_in_a_source_file_is_not_offered() {
let codes = codes_in(
&[
("/p/detail/gtest_unittest.cc", "class T {};\n"),
("/p/use.hpp", "void f(T value) {}\n"),
],
"/p/use.hpp",
);
assert_silent_about(
&codes,
DiagnosticCode::MissingInclude,
"a `.cc` is a translation unit, not a header to include",
);
}
#[test]
fn a_template_id_inside_a_template_is_silent() {
let dependent = codes_in(
&[
("/p/convertible.h", "class is_convertible {};\n"),
(
"/p/use.h",
"template <typename T>\nvoid f(typename constraint<is_convertible<T&, int&>::value>::type = 0);\n",
),
],
"/p/use.h",
);
assert_silent_about(
&dependent,
DiagnosticCode::MissingInclude,
"a dependent template-id is not a name a header can be named for",
);
let ordinary = codes_in(
&[
("/p/widget.h", "class Widget {};\n"),
("/p/uses.h", "template <class T>\nvoid g(Widget w) {}\n"),
],
"/p/uses.h",
);
assert_reported(
&ordinary,
DiagnosticCode::MissingInclude,
"a plain type in a template's parameter list is still this rule's question",
);
}
#[test]
fn a_header_inside_its_includers_order_is_silent() {
let fragment = codes_in(
&[
("/p/style_enum.h", "enum class Space { Always };\n"),
(
"/p/style_struct.h",
"struct Before { Space table = Space::Always; };\n",
),
(
"/p/style.h",
"#include \"style_enum.h\"\n#include \"style_struct.h\"\n",
),
],
"/p/style_struct.h",
);
assert_silent_about(
&fragment,
DiagnosticCode::MissingInclude,
"the only includer reaches the enum header before the fragment",
);
let broken = codes_in(
&[
("/p/style_enum.h", "enum class Space { Always };\n"),
(
"/p/style_struct.h",
"struct Before { Space table = Space::Always; };\n",
),
("/p/only_struct.cpp", "#include \"style_struct.h\"\n"),
],
"/p/style_struct.h",
);
assert_reported(
&broken,
DiagnosticCode::MissingInclude,
"an includer that does not reach the enum is the broken translation unit",
);
}