use cpp_code_analysis::{FileAnalysis, FileTokens, Origin};
use cpp_parser::{CppParser, ParserConfig, SourceRange};
fn analyse(source: &str) -> FileAnalysis {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(
tree.to_source_text(),
source,
"the parse must round-trip before anything is read out of it"
);
FileAnalysis::new(source, &tree)
}
fn rendered(expansion: &cpp_code_analysis::Expansion) -> String {
let mut out = String::new();
for token in &expansion.tokens {
if token.space_before && !out.is_empty() {
out.push(' ');
}
out.push_str(token.text());
}
out
}
fn joined(expansion: &cpp_code_analysis::Expansion) -> String {
expansion
.tokens
.iter()
.filter(|token| !cpp_code_analysis::token::is_trivia(token.kind()))
.map(|token| token.text())
.collect()
}
#[test]
fn a_comment_is_the_trees_spelling_not_the_lexers() {
let analysis = analyse("// hi\nint x;\n");
let kinds: Vec<String> = analysis
.tokens
.tokens()
.iter()
.map(|token| format!("{:?}", token.kind))
.collect();
assert!(
kinds.iter().any(|kind| kind == "LineComment"),
"the opener is there: {kinds:?}"
);
assert!(
kinds.iter().any(|kind| kind == "DocText"),
"and so is the comment's text, as its own token: {kinds:?}"
);
assert!(
!kinds.iter().any(|kind| kind == "Identifier" && false),
"sanity"
);
}
#[test]
fn the_tokens_tile_the_file() {
let source = "int x = 1;\n// c\n#define A ( 2 )\nint y = A ;\n";
let analysis = analyse(source);
let mut expected = 0usize;
for token in analysis.tokens.tokens() {
assert_eq!(
token.range.start_offset, expected,
"a hole or overlap at {expected}: {token:?}"
);
expected = token.range.end_offset();
}
assert_eq!(expected, source.len());
}
#[test]
fn tokens_can_be_asked_for_by_range() {
let source = "int x = 1;\nint y = 2;\n";
let analysis = analyse(source);
let second_line = SourceRange::new(11, 11);
let tokens = analysis.tokens.in_range(second_line);
let text: String = tokens.iter().map(|token| token.text()).collect();
assert_eq!(text, "int y = 2;\n");
}
#[test]
fn a_partially_covered_token_is_excluded() {
let analysis = analyse("int x;\n");
let tokens = analysis.tokens.in_range(SourceRange::new(1, 4));
let text: String = tokens.iter().map(|token| token.text()).collect();
assert_eq!(text, " x", "`int` and `;` are excluded, not clipped");
}
#[test]
fn the_token_at_an_offset_is_the_one_covering_it() {
let analysis = analyse("int x;\n");
assert_eq!(analysis.tokens.token_at(0).map(|t| t.text()), Some("int"));
assert_eq!(analysis.tokens.token_at(3).map(|t| t.text()), Some(" "));
assert_eq!(analysis.tokens.token_at(4).map(|t| t.text()), Some("x"));
assert_eq!(analysis.tokens.token_at(5).map(|t| t.text()), Some(";"));
assert_eq!(analysis.tokens.token_at(6).map(|t| t.text()), Some("\n"));
}
#[test]
fn looking_up_past_the_end_finds_nothing() {
let analysis = analyse("int x;\n");
assert!(analysis.tokens.token_at(99).is_none());
assert!(analyse("").tokens.token_at(0).is_none());
}
#[test]
fn a_line_range_includes_its_ending() {
let analysis = analyse("int x;\nint y;\n");
let first = analysis.line_range_at(2);
assert_eq!(first.start_offset, 0);
assert_eq!(first.length, 7, "`int x;\\n`, newline included");
let second = analysis.line_range_at(9);
assert_eq!(second.start_offset, 7);
assert_eq!(second.length, 7);
}
#[test]
fn the_last_line_without_a_newline_is_still_a_line() {
let analysis = analyse("int x;\nint y;");
let last = analysis.line_range_at(9);
assert_eq!(last.start_offset, 7);
assert_eq!(last.length, 6);
}
#[test]
fn a_region_expands_its_macros() {
let analysis = analyse("#define VERSION 3\nint x = VERSION;\n");
let use_range = analysis.line_range_at(20);
assert_eq!(joined(&analysis.expand_range(use_range)), "intx=3;");
}
#[test]
fn a_region_is_expanded_against_the_macros_in_force_there() {
let source = "#define N 1\nint a = N;\n#define N 2\nint b = N;\n";
let analysis = analyse(source);
assert_eq!(joined(&analysis.expand_line_at(12)), "inta=1;");
assert_eq!(joined(&analysis.expand_line_at(35)), "intb=2;");
}
#[test]
fn a_use_before_the_definition_is_not_expanded() {
let source = "int a = N;\n#define N 1\n";
let analysis = analyse(source);
assert_eq!(joined(&analysis.expand_line_at(0)), "inta=N;");
}
#[test]
fn a_line_with_no_macros_expands_to_itself() {
let analysis = analyse("int x = 1;\n");
assert_eq!(rendered(&analysis.expand_line_at(0)), "int x = 1;\n");
}
#[test]
fn a_function_like_macro_expands_in_a_region() {
let analysis = analyse("#define MAX(a, b) ((a) > (b) ? (a) : (b))\nint m = MAX(x, y);\n");
assert_eq!(
joined(&analysis.expand_line_at(42)),
"intm=((x)>(y)?(x):(y));"
);
}
#[test]
fn an_expanded_token_keeps_its_origin_through_the_file_api() {
let source = "#define VERSION 3\nint x = VERSION;\n";
let analysis = analyse(source);
let expansion = analysis.expand_line_at(20);
let expanded = expansion
.tokens
.iter()
.find(|token| token.is_expanded())
.expect("a token from the macro");
assert!(matches!(expanded.origin, Origin::Expanded { .. }));
assert_eq!(expanded.text(), "3");
let (name_file, name_range) = expanded.navigation_at().expect("a definition in a file");
assert_eq!(name_file, cpp_code_analysis::macros::MacroFile::Here);
assert_eq!(
&source[name_range.start_offset..name_range.end_offset()],
"VERSION"
);
assert!(
expanded.diagnostic_range().start_offset >= 18,
"a diagnostic is in the use's line, not the definition's: {:?}",
expanded.diagnostic_range()
);
}
#[test]
fn a_node_can_be_expanded() {
let source = "#define A 1\nint x = A;\n";
let tree = CppParser::parse(source, ParserConfig::default());
let analysis = FileAnalysis::new(source, &tree);
let declaration = tree
.get_red_root()
.descendants()
.find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::Declaration
})
.expect("a declaration");
assert_eq!(joined(&analysis.expand_node(&declaration)), "intx=1;");
}
#[test]
fn file_analysis_never_panics_on_odd_ranges() {
let sources = [
"",
"\n",
"int",
"int x",
"#define",
"#define A",
"#define A (",
"// unterminated",
"/* unterminated",
"#if\n",
"\\",
];
for source in sources {
let analysis = analyse(source);
for offset in 0..=source.len() + 2 {
let _ = analysis.tokens.token_at(offset);
let _ = analysis.line_range_at(offset);
let _ = analysis.expand_line_at(offset);
let _ = analysis.expand_range(SourceRange::new(offset, 3));
}
let _ = analysis.expand_range(SourceRange::new(source.len() + 10, 5));
let _ = analysis.expand_range(SourceRange::new(0, 0));
}
}
#[test]
fn an_empty_file_has_no_span() {
let analysis = analyse("");
assert!(analysis.tokens.is_empty());
assert!(analysis.tokens.span().is_none());
}
#[test]
fn a_comment_only_file_has_the_comment_s_tokens() {
let analysis = analyse("// only\n");
assert!(!analysis.tokens.is_empty());
assert_eq!(
analysis
.tokens
.tokens()
.iter()
.map(|token| token.text())
.collect::<String>(),
"// only\n"
);
}
#[test]
fn tokens_can_be_read_without_preprocessing() {
let source = "int x;\n";
let tree = CppParser::parse(source, ParserConfig::default());
let tokens = FileTokens::from_tree(source, &tree);
assert_eq!(tokens.source(), source);
assert_eq!(tokens.span().map(|span| span.length), Some(source.len()));
assert_eq!(tokens.len(), 5, "int, space, x, semicolon, newline");
}