mod utils;
use crate::utils::ink_version_from_path;
use ink_analyzer::{Analysis, TextRange, TextSize};
use test_utils::{PartialMatchStr, TestCaseResults};
#[test]
fn diagnostics_works() {
for test_group in test_utils::fixtures::diagnostics_fixtures() {
let original_code = test_utils::read_source_code(test_group.source);
for test_case in test_group.test_cases {
let mut test_code = original_code.clone();
if let Some(modifications) = test_case.modifications {
test_utils::apply_test_modifications(&mut test_code, &modifications);
}
let version = ink_version_from_path(test_group.source);
let results = Analysis::new(&test_code, version).diagnostics();
let expected_results = match test_case.results {
TestCaseResults::Diagnostic { n, quickfixes } => Some((n, quickfixes)),
_ => None,
}
.unwrap();
assert_eq!(
results.len(),
expected_results.0,
"source: {}",
test_group.source
);
for (idx, result) in results.iter().enumerate() {
assert_eq!(
result.quickfixes.as_ref().map(Vec::len),
expected_results
.1
.get(idx)
.map(|(expected_quickfixes, _)| expected_quickfixes.len()),
"source: {}",
test_group.source
);
let expected_quickfixes = &expected_results.1[idx].0;
if let Some(quickfixes) = result.quickfixes.as_ref() {
assert_eq!(
quickfixes
.iter()
.map(|action| (
PartialMatchStr::from(action.label.as_str()),
action
.edits
.iter()
.map(|edit| (
PartialMatchStr::from(edit.text.as_str()),
edit.range
))
.collect()
))
.collect::<Vec<(PartialMatchStr, Vec<(PartialMatchStr, TextRange)>)>>(),
expected_quickfixes
.iter()
.map(|expected_action| (
PartialMatchStr::from(expected_action.label),
expected_action
.edits
.iter()
.map(|result| (
PartialMatchStr::from(result.text),
TextRange::new(
TextSize::from(
test_utils::parse_offset_at(
&test_code,
result.start_pat
)
.unwrap()
as u32
),
TextSize::from(
test_utils::parse_offset_at(
&test_code,
result.end_pat
)
.unwrap()
as u32
),
)
))
.collect()
))
.collect::<Vec<(PartialMatchStr, Vec<(PartialMatchStr, TextRange)>)>>(),
"source: {}",
test_group.source
);
}
}
}
}
}