use cpp_code_analysis::{
CompilerConfig, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
fn session_with(source: &str) -> (Session<MemoryFiles>, cpp_code_analysis::FileView) {
let memory = MemoryFiles::new().with_file("/p/a.cpp", source);
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.load("/p/a.cpp");
let view = session
.view_of_the_file(std::path::Path::new("/p/a.cpp"))
.expect("the file is held");
(session, view)
}
fn type_after(source: &str, needle: &str) -> Option<String> {
let (session, view) = session_with(source);
let at = source.find(needle).expect("the fixture writes it");
session
.type_at(
&view,
cpp_code_analysis::file::coordinates::ReadingOffset::new(at),
)
.value()
.map(|found| found.type_of.clone())
}
#[test]
fn a_pipe_chain_keeps_the_element_of_its_left_operand() {
let source = "
#include <ranges>
#include <vector>
int main() {
std::vector<int> numbers{5, 3, 1};
for (auto value : numbers | std::views::filter([](int v) { return v > 2; }) | std::views::take(2)) {
value = 0;
}
}
";
assert_eq!(
type_after(source, "value :"),
Some("int".to_string()),
"`numbers` yields `int`, and filter/take do not change it"
);
}
#[test]
fn a_counted_range_yields_its_bound() {
let source = "
#include <ranges>
int main() {
for (auto value : std::views::iota(1, 6)) {
value = 0;
}
}
";
assert_eq!(type_after(source, "value :"), Some("int".to_string()));
}
#[test]
fn the_key_of_a_map_is_a_view_of_the_key() {
let source = "
#include <map>
#include <ranges>
#include <string>
int main() {
std::map<std::string, int> map;
for (auto key : map | std::views::keys) {
key = 0;
}
for (auto value : map | std::views::values) {
value = 0;
}
}
";
assert_eq!(
type_after(source, "key :"),
Some("std::string".to_string()),
"the key, not `std::pair<const std::string, int>`"
);
assert_eq!(type_after(source, "value :"), Some("int".to_string()));
}
#[test]
fn a_joined_range_yields_the_inner_element() {
let source = "
#include <ranges>
#include <vector>
int main() {
std::vector<std::vector<int>> rows;
for (auto value : rows | std::views::join) {
value = 0;
}
}
";
assert_eq!(type_after(source, "value :"), Some("int".to_string()));
}
#[test]
fn a_ranges_algorithm_answers_the_element() {
let source = "
#include <ranges>
#include <vector>
struct Person { int age; };
int main() {
std::vector<Person> people;
auto found = std::ranges::find(people, Person{}, &Person::age);
}
";
assert_eq!(
type_after(source, "found ="),
Some("Person".to_string()),
"an iterator to the element is, to a reader, the element"
);
}
#[test]
fn the_counting_algorithms_are_ptrdiff_t() {
let source = "
#include <ranges>
#include <vector>
int main() {
std::vector<int> numbers;
auto count = std::ranges::size(numbers);
auto from = std::ranges::distance(numbers);
}
";
assert_eq!(
type_after(source, "count ="),
Some("std::size_t".to_string())
);
assert_eq!(
type_after(source, "from ="),
Some("std::ptrdiff_t".to_string())
);
}
#[test]
fn a_transform_answers_the_lambdas_return() {
let source = "
#include <ranges>
#include <vector>
int main() {
std::vector<int> numbers;
for (auto value : numbers | std::views::transform([](int v) { return v * 10; })) {
value = 0;
}
}
";
assert_eq!(
type_after(source, "value :"),
Some("int".to_string()),
"`v * 10` is an `int`, and that is what the transform yields"
);
}
#[test]
fn a_transform_of_an_unclearable_body_is_refused() {
let source = "
#include <ranges>
#include <vector>
struct S { int field; };
void f(S other) {
std::vector<int> numbers;
for (auto value : numbers | std::views::transform([&](int v) { return other; })) {
value = 0;
}
}
";
assert_eq!(type_after(source, "value :"), None);
}