use cpp_code_analysis::file::coordinates::FileOffset;
use cpp_code_analysis::{
CompilerConfig, Known, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
const HEADER: &str = "\
#pragma once
namespace ns {
struct Table {
int& operator[](int at);
double& operator[](const char* key);
};
struct Boxed {
using reference = int&;
using const_reference = const int&;
reference operator[](int at);
const_reference operator[](int at) const;
};
using Json = Table;
using Crate = Boxed;
}
";
const SOURCE: &str = "\
#include \"t.h\"
int by_index(ns::Json table) { return table[0]; }
double by_key(ns::Json table) { return table[\"k\"]; }
int by_const(const ns::Crate crate) { return crate[0]; }
int by_mutable(ns::Crate boxed) { return boxed[0]; }
";
fn session_with(files: &[(&str, &str)]) -> Session<MemoryFiles> {
let mut memory = MemoryFiles::new();
for (path, source) in files {
memory = memory.with_file(*path, *source);
}
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files(
files
.iter()
.map(|(path, _)| std::path::PathBuf::from(*path)),
);
session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
session.index_everything();
session.load(std::path::Path::new("/p/a.cpp"));
session
}
fn the_subscripts_type(session: &Session<MemoryFiles>, expression: &str) -> String {
let view = session.view("/p/a.cpp").expect("the file has a rendering");
let at = SOURCE
.find(expression)
.unwrap_or_else(|| panic!("{expression} is written"));
let bracket = at + expression.find('[').expect("a bracket");
let reading = view
.reading_offset_of(FileOffset::new(bracket))
.expect("the cursor is in the rendering");
match session.type_at(&view, reading) {
Known::Yes(found) => found.type_of,
other => format!("{other:?}"),
}
}
#[test]
fn the_arguments_choose_the_overload_through_an_alias() {
let session = session_with(&[("/p/t.h", HEADER), ("/p/a.cpp", SOURCE)]);
assert_eq!(
the_subscripts_type(&session, "table[0]"),
"int&",
"an `int` index selects `int& operator[](int)`"
);
assert_eq!(
the_subscripts_type(&session, "table[\"k\"]"),
"double&",
"a `const char*` key selects `double& operator[](const char*)`, the second declaration"
);
}
#[test]
fn the_objects_constness_chooses_the_overload_through_an_alias() {
let session = session_with(&[("/p/t.h", HEADER), ("/p/a.cpp", SOURCE)]);
assert_eq!(
the_subscripts_type(&session, "crate[0]"),
"const int&",
"a `const` object can only call the `const` overload"
);
assert_eq!(
the_subscripts_type(&session, "boxed[0]"),
"int&",
"while a mutable object keeps the non-const one"
);
}