use cpp_parser::{CppParser, CppSyntaxKind, ParserConfig};
fn tree(source: &str) -> cpp_parser::CppSyntaxNode {
CppParser::parse(source, ParserConfig::default()).get_red_root()
}
fn kinds(source: &str) -> Vec<CppSyntaxKind> {
tree(source)
.descendants()
.map(|node| CppSyntaxKind::from(node.kind()))
.collect()
}
fn parses(source: &str) {
let parsed = CppParser::parse(source, ParserConfig::default());
assert_eq!(
parsed.get_errors(),
[],
"{source:?} must parse cleanly, got {:?}",
parsed.get_errors()
);
assert_eq!(parsed.to_source_text(), source, "{source:?} stays lossless");
}
#[test]
fn a_reflection_is_an_expression_of_the_meta_info_type() {
for operand in [
"int",
"Widget",
"Widget::x",
"std::array",
"decltype(x)",
"::Widget",
] {
let source = format!("auto r = ^^{operand};\n");
parses(&source);
assert_eq!(
kinds(&source)
.into_iter()
.filter(|kind| *kind == CppSyntaxKind::ReflectExpr)
.count(),
1,
"`^^{operand}` is one `ReflectExpr`"
);
}
}
#[test]
fn a_splice_is_an_expression() {
for source in [
"constexpr auto r = ^^int;\nint v = [:r:];\n",
"constexpr auto r = ^^f;\nvoid g() { std::meta::info x = [:r:]; }\n",
"constexpr auto r = ^^int;\nauto f() { return [:r:]; }\n",
] {
parses(source);
assert!(
kinds(source).contains(&CppSyntaxKind::SpliceExpr),
"{source:?} holds a splice node"
);
}
}
#[test]
fn a_type_splice_stands_where_a_type_name_stands() {
parses("constexpr auto r = ^^int;\nusing T = typename[:r:];\n");
assert!(
kinds("constexpr auto r = ^^int;\nusing T = typename[:r:];\n")
.contains(&CppSyntaxKind::TypenameType),
"the type splice is a `TypenameType` holding a `SpliceExpr`"
);
parses("template <class T> struct Box {};\nusing S = typename[:^^Box<int>:]<int>;\n");
}
#[test]
fn a_member_splice_follows_the_access_operator() {
parses("constexpr auto r = ^^Widget::x;\nvoid f(Widget w) { auto& v = w.[:r:]; }\n");
parses("constexpr auto r = ^^Widget::x;\nvoid f(Widget* p) { auto& v = p->[:r:]; }\n");
}
#[test]
fn the_splice_is_told_apart_from_its_neighbours() {
for source in [
"void f() { auto g = [x]() { return x; }; }\n",
"void f(int* p) { int v = p[1]; }\n",
"struct S { unsigned a : 3; };\n",
"void f(int a, int b) { int c = a ? b : 1; }\n",
"auto x = [] { return 1; }();\n",
] {
parses(source);
assert!(
!kinds(source).contains(&CppSyntaxKind::SpliceExpr),
"{source:?} holds no splice"
);
}
}
#[test]
fn a_single_caret_is_still_a_block_literal() {
parses("void f() { auto b = ^(int n) { return n; }; }\n");
let kinds = kinds("void f() { auto b = ^(int n) { return n; }; }\n");
assert!(
!kinds.contains(&CppSyntaxKind::ReflectExpr),
"one caret is not a reflection"
);
}
#[test]
fn a_type_splice_stands_in_expression_position() {
let source = "\
struct Widget { int x; };
constexpr auto r = ^^Widget;
auto t = typename [: ^^Widget :]{5};
auto u = typename [: r :](1);
";
parses(source);
}
#[test]
fn an_expansion_statement_is_a_statement_of_its_own() {
let source = "\
void f() {
static constexpr auto enumerators = g();
template for (constexpr auto e : enumerators) {
use([:e:]);
}
}
";
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(
tree.get_errors(),
[],
"the expansion statement parses cleanly"
);
let expansion = tree
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::ExpansionStat)
.expect("the expansion statement has a node of its own");
assert!(
expansion.text().to_string().starts_with("template for"),
"the node covers the whole statement: {:?}",
expansion.text().to_string()
);
}
#[test]
fn a_reflection_of_the_global_scope_is_a_reflection() {
parses("constexpr auto g = ^^::;\n");
parses("constexpr auto g = ^^::Widget;\n");
}