use super::{assert_reported, assert_silent_about, codes_of};
use crate::DiagnosticCode;
#[test]
fn a_local_of_an_abstract_class_is_reported() {
let codes =
codes_of("struct Shape {\n virtual void draw() = 0;\n};\nvoid f() {\n Shape s;\n}\n");
assert_reported(
&codes,
DiagnosticCode::AbstractClassInstantiated,
"`Shape` has an unoverridden `= 0`",
);
}
#[test]
fn a_member_of_an_abstract_class_is_reported() {
let codes = codes_of(
"struct Shape {\n virtual void draw() = 0;\n};\nstruct Holder {\n Shape shape;\n};\n",
);
assert_reported(
&codes,
DiagnosticCode::AbstractClassInstantiated,
"`Holder` cannot hold a `Shape` by value",
);
}
#[test]
fn an_object_of_an_overriding_class_is_silent() {
let codes = codes_of(
"struct Shape {\n virtual void draw() = 0;\n};\nstruct Circle : Shape {\n void draw() override;\n};\nvoid f() {\n Circle c;\n}\n",
);
assert_silent_about(
&codes,
DiagnosticCode::AbstractClassInstantiated,
"`Circle::draw` implements `Shape::draw`",
);
}
#[test]
fn an_object_of_a_non_overriding_class_is_reported() {
let codes = codes_of(
"struct Shape {\n virtual void draw() = 0;\n};\nstruct Ring : Shape {};\nvoid f() {\n Ring r;\n}\n",
);
assert_reported(
&codes,
DiagnosticCode::AbstractClassInstantiated,
"`Ring` inherits the pure `draw` and still has no body for it",
);
}
#[test]
fn a_pointer_or_reference_to_an_abstract_class_is_silent() {
let codes = codes_of(
"struct Shape {\n virtual void draw() = 0;\n};\nvoid f(Shape &r) {\n Shape *p = &r;\n}\n",
);
assert_silent_about(
&codes,
DiagnosticCode::AbstractClassInstantiated,
"a pointer and a reference point at a derived object",
);
}
#[test]
fn a_defined_pure_virtual_does_not_make_the_class_concrete() {
let codes = codes_of(
"struct Shape {\n virtual void draw() = 0;\n};\nvoid Shape::draw() {}\nvoid f() {\n Shape s;\n}\n",
);
assert_reported(
&codes,
DiagnosticCode::AbstractClassInstantiated,
"`Shape::draw` has a body, and is still `= 0`",
);
}
#[test]
fn an_unknown_type_is_not_judged() {
let codes = codes_of("void f() {\n Widget w;\n}\n");
assert_silent_about(
&codes,
DiagnosticCode::AbstractClassInstantiated,
"`Widget` was not read, so there is nothing to ask",
);
}
#[test]
fn a_template_id_is_not_judged() {
let codes = codes_of(
"template <class T>\nstruct Box {\n virtual void put(T) = 0;\n};\nvoid f() {\n Box<int> b;\n}\n",
);
assert_silent_about(
&codes,
DiagnosticCode::AbstractClassInstantiated,
"`Box<int>` is not the spelling the member list is keyed by",
);
}