use cpp_parser::{CppParser, CppSyntaxKind, ParserConfig};
fn parsed(source: &str) -> cpp_parser::CppSyntaxNode {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(
tree.get_errors(),
[],
"{source:?} must parse cleanly, got {:?}",
tree.get_errors()
);
let root = tree.get_red_root();
assert_eq!(root.text().to_string(), source, "the tree stays lossless");
root
}
fn count(source: &str, kind: CppSyntaxKind) -> usize {
parsed(source)
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == kind)
.count()
}
#[test]
fn a_function_can_carry_preconditions_and_postconditions() {
let source = "\
int safe_division(int numerator, int denominator)
pre(denominator != 0)
post(result: result * denominator == numerator)
{
return numerator / denominator;
}
";
assert_eq!(count(source, CppSyntaxKind::ContractSpecifierSeq), 1);
assert_eq!(count(source, CppSyntaxKind::ContractSpecifier), 2);
let declaration = "int f(int x) pre(x > 0);\n";
assert_eq!(count(declaration, CppSyntaxKind::ContractSpecifier), 1);
}
#[test]
fn several_specifiers_and_a_postcondition_without_a_result_name() {
let source = "\
void copy(const char* src, char* dst, unsigned long n)
pre(src != nullptr)
pre(dst != nullptr)
pre(n > 0)
post(dst[0] == src[0])
{
}
";
assert_eq!(count(source, CppSyntaxKind::ContractSpecifier), 4);
}
#[test]
fn contract_assert_is_a_statement() {
let source = "\
int f(int x) {
contract_assert(x > 0);
if (x > 10) {
contract_assert(x < 100);
}
return x;
}
";
assert_eq!(count(source, CppSyntaxKind::ContractAssertStat), 2);
}
#[test]
fn a_lambda_can_carry_contracts() {
let source = "\
auto f = [](int x) pre(x > 0) post(r: r > x) { return x + 1; };
";
assert_eq!(count(source, CppSyntaxKind::ContractSpecifier), 2);
}
#[test]
fn pre_and_post_are_still_identifiers() {
for source in [
"int pre = 42;\n",
"void post() {}\n",
"struct S { int pre; int post; };\n",
"void f() { pre(); post(1); }\n",
"int g(int pre) { return pre; }\n",
] {
let tree = parsed(source);
assert_eq!(
tree.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::ContractSpecifier)
.count(),
0,
"{source:?} has no contract specifier"
);
}
}
#[test]
fn an_attribute_between_the_keyword_and_the_parenthesis() {
let source = "\
int f(int x)
pre [[likely]] (x >= 0)
post (r [[maybe_unused]] : r >= 0)
{
return x;
}
";
assert_eq!(count(source, CppSyntaxKind::ContractSpecifier), 2);
}
#[test]
fn a_constructor_contract_precedes_its_member_initializers() {
let source = "\
struct X {
int i;
X(int v) pre(v > 0) : i(v) {}
};
";
assert_eq!(count(source, CppSyntaxKind::ContractSpecifier), 1);
}
#[test]
fn a_predicate_may_be_any_expression() {
let source = "\
bool check(int);
int f(int x, int y)
pre(check(x) && (x > 0 ? y != 0 : true))
{
return x;
}
";
assert_eq!(count(source, CppSyntaxKind::ContractSpecifier), 1);
}