use cpp_code_analysis::{
CompilerConfig, Known, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
file::coordinates::FileOffset,
};
const REFLECTION: &str = "\
struct Widget { int x; double y; };
template <class T> struct Box { T value; };
constexpr auto r = ^^Widget;
constexpr auto m = ^^Widget::x;
using T = typename[:r:];
using S = typename[:^^Box<int>:]<int>;
int main() {
Widget w{};
auto& field = w.[:^^Widget::x:];
auto n = std::meta::identifier_of(^^Widget::x);
return field + n;
}
";
fn session() -> Session<MemoryFiles> {
let files = MemoryFiles::new().with_file("/p/a.cpp", REFLECTION);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([std::path::PathBuf::from("/p/a.cpp")]);
session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
session.index_everything();
session
}
#[test]
fn a_reflection_file_has_no_diagnostics() {
let session = session();
let found = session
.diagnostics("/p/a.cpp")
.expect("the file has a reading");
assert_eq!(found.errors.len(), 0, "{:?}", found.errors);
assert!(
found.checks.is_empty(),
"no check findings: {:?}",
found.checks
);
}
#[test]
fn a_reflection_is_a_std_meta_info() {
let session = session();
let view = session.view("/p/a.cpp").expect("the file is cooked");
let at = REFLECTION
.find("^^Widget;")
.expect("the fixture reflects a type");
let reading = view
.reading_offset_of(FileOffset::new(at))
.expect("the reflection is in the rendering");
match session.type_at(&view, reading) {
Known::Yes(found) => assert_eq!(
found.type_of, "std::meta::info",
"`^^` has one type, whichever operand it takes"
),
other => panic!("a reflection is an expression with a type: {other:?}"),
}
}
#[test]
fn a_member_splice_is_an_expression() {
let session = session();
let view = session.view("/p/a.cpp").expect("the file is cooked");
let at = REFLECTION
.find("w.[:")
.expect("the fixture splices a member");
let reading = view
.reading_offset_of(FileOffset::new(at + 2))
.expect("the splice is in the rendering");
let _ = session.type_at(&view, reading);
}