//! integration tests for ink! analyzer completions. mod utils; use crate::utils::ink_version_from_path; use ink_analyzer::{Analysis, TextRange, TextSize}; use test_utils::{TestCaseParams, TestCaseResults}; // The high-level methodology for completions test cases is: // - Read the source code of an ink! entity file in the `test-fixtures` directory // (e.g. https://github.com/ink-analyzer/ink-analyzer/blob/master/test-fixtures/v4/erc20.rs). // - (Optionally) Make some modifications to the source code at a specific offset/text range to create a specific test case. // - Compute completions for the modified source code and a specific offset position. // - Verify that the actual results match the expected results. // See inline comments for more details. #[test] fn completions_works() { // Iterates over all test case groups (see [`test_utils::fixtures::completions_fixtures`] doc and inline comments). for test_group in test_utils::fixtures::completions_fixtures() { // Gets the original source code. let original_code = test_utils::read_source_code(test_group.source); // Iterates over all test cases. for test_case in test_group.test_cases { // Creates a copy of test code for this test case. let mut test_code = original_code.clone(); // Applies test case modifications (if any). if let Some(modifications) = test_case.modifications { test_utils::apply_test_modifications(&mut test_code, &modifications); } // Sets the cursor position. let offset_pat = match test_case.params.unwrap() { TestCaseParams::Completion(it) => Some(it), _ => None, } .unwrap() .pat; let offset = TextSize::from(test_utils::parse_offset_at(&test_code, offset_pat).unwrap() as u32); // Computes completions. let version = ink_version_from_path(test_group.source); let results = Analysis::new(&test_code, version).completions(offset); // Verifies completion results. let expected_results = match test_case.results { TestCaseResults::Completion(it) => Some(it), _ => None, } .unwrap(); assert_eq!( results .into_iter() .map(|completion| ( test_utils::remove_whitespace(completion.edit.text), completion.range )) .collect::>(), expected_results .iter() .map(|result| ( test_utils::remove_whitespace(result.text.to_owned()), 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::>(), "source: {}", test_group.source ); } } }