use super::{assert_reported, assert_silent_about, codes_in, codes_of};
use crate::DiagnosticCode;
#[test]
fn a_call_with_too_many_or_too_few_arguments_is_reported() {
let too_many = codes_of("void f(int a, int b) {}\nvoid g() { f(1, 2, 3); }\n");
assert_reported(&too_many, DiagnosticCode::ArgumentCount, "`f` takes two");
let too_few = codes_of("void f(int a, int b) {}\nvoid g() { f(1); }\n");
assert_reported(&too_few, DiagnosticCode::ArgumentCount, "`f` needs two");
let exact = codes_of("void f(int a, int b) {}\nvoid g() { f(1, 2); }\n");
assert_silent_about(&exact, DiagnosticCode::ArgumentCount, "two for two");
}
#[test]
fn a_default_argument_makes_the_parameter_optional() {
let codes = codes_in(
&[
(
"/p/api.h",
"void report(int a, int b = 0);\nint twice(int a, int b = 1);\n",
),
(
"/p/use.cpp",
"#include \"api.h\"\n\
void report(int a, int b) { (void)a; (void)b; }\n\
int twice(int a, int b) { return a * b; }\n\
void g() { report(1); (void)twice(2); report(1, 2); }\n",
),
],
"/p/use.cpp",
);
assert_silent_about(
&codes,
DiagnosticCode::ArgumentCount,
"both parameters are defaulted in the header",
);
}
#[test]
fn a_pack_and_a_c_style_ellipsis_take_any_number() {
let pack = codes_of(
"template <class... Args>\nvoid f(int first, Args&&... rest) {}\nvoid g() { f(1, 2, 3, 4); }\n",
);
assert_silent_about(
&pack,
DiagnosticCode::ArgumentCount,
"a pack takes the rest",
);
let c_style =
codes_of("#include <cstdarg>\nvoid f(int count, ...);\nvoid g() { f(1, 2, 3); }\n");
assert_silent_about(
&c_style,
DiagnosticCode::ArgumentCount,
"`...` takes the rest",
);
}
#[test]
fn an_overload_set_is_silent_when_one_candidate_fits() {
let fits = codes_of(
"struct W {\n void resize(int n);\n void resize(int n, int extra);\n};\n\
void g(W& w) { w.resize(1); w.resize(1, 2); }\n",
);
assert_silent_about(
&fits,
DiagnosticCode::ArgumentCount,
"each call fits one overload",
);
let none = codes_of(
"struct W {\n void resize(int n);\n void resize(int n, int extra);\n};\n\
void g(W& w) { w.resize(1, 2, 3); }\n",
);
assert_reported(
&none,
DiagnosticCode::ArgumentCount,
"no overload takes three",
);
}
#[test]
fn a_call_of_a_class_is_not_judged_today() {
let empty = codes_of("struct W {\n W(int a);\n};\nW make() { return W(); }\n");
assert_silent_about(
&empty,
DiagnosticCode::ArgumentCount,
"the gap: `W` declares one constructor taking one, and this rule does not ask it",
);
let member =
codes_of("struct W {\n void resize(int n);\n};\nvoid g(W& w) { w.resize(); }\n");
assert_reported(&member, DiagnosticCode::ArgumentCount, "`resize` takes one");
}
#[test]
fn an_unresolvable_callee_is_not_judged() {
let codes = codes_of("void g() { unknown(1, 2, 3); }\n");
assert_silent_about(
&codes,
DiagnosticCode::ArgumentCount,
"nothing declares `unknown`, so nothing says how many it takes",
);
}
#[test]
fn a_call_under_a_member_access_is_not_that_member_call() {
let fewer = codes_of(
"struct Manager {\n\
\x20 static Manager& instance();\n\
\x20 void init(int size);\n\
};\n\
void g() { Manager::instance().init(4); }\n",
);
assert_silent_about(
&fewer,
DiagnosticCode::ArgumentCount,
"`init` gets its one argument; `instance()` is a different call",
);
let more = codes_of(
"struct Span {\n\
\x20 int count() const;\n\
};\n\
Span make(int seed);\n\
int g() { return make(1).count(); }\n",
);
assert_silent_about(
&more,
DiagnosticCode::ArgumentCount,
"`count()` gets none; `make(1)` is the object",
);
}
#[test]
fn a_call_through_a_variable_is_not_a_free_function_of_that_name() {
let codes = codes_of(
"int copy(int first, int last) { return first + last; }\n\
void g() {\n\
auto [increment, copy] = std::pair{1, 2};\n\
(void)increment;\n\
(void)copy();\n\
}\n",
);
assert_silent_about(
&codes,
DiagnosticCode::ArgumentCount,
"the name is a structured binding here, not the function",
);
}