|
| 1 | +use std::{ |
| 2 | + collections::BTreeMap, |
| 3 | + path::{Path, PathBuf}, |
| 4 | +}; |
| 5 | + |
| 6 | +use ropey::Rope; |
| 7 | + |
| 8 | +use crate::syntax::rope_offset_to_position; |
| 9 | + |
| 10 | +#[derive(Debug)] |
| 11 | +pub struct Location { |
| 12 | + pub path: PathBuf, |
| 13 | + pub line: u32, |
| 14 | + pub col: u32, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug)] |
| 18 | +pub struct TestInput { |
| 19 | + pub files: BTreeMap<PathBuf, String>, |
| 20 | + pub location: Option<Location>, |
| 21 | +} |
| 22 | + |
| 23 | +pub fn parse_test_input(input: &str) -> TestInput { |
| 24 | + let mut files = BTreeMap::new(); |
| 25 | + let mut lines = input.lines().peekable(); |
| 26 | + let mut current_path = PathBuf::from("/nowhere.nix"); |
| 27 | + let mut current_content = Vec::new(); |
| 28 | + let mut location = None; |
| 29 | + |
| 30 | + let mut add_file = |current_path: &Path, current_content: &[String]| { |
| 31 | + if !current_content.is_empty() { |
| 32 | + let mut content = current_content.join("\n"); |
| 33 | + if let Some(offset) = content.find("$0") { |
| 34 | + content = format!("{}{}", &content[0..offset], &content[offset + 2..]); |
| 35 | + let rope = Rope::from_str(&content); |
| 36 | + let position = rope_offset_to_position(&rope, offset); |
| 37 | + location = Some(Location { |
| 38 | + path: current_path.to_path_buf(), |
| 39 | + line: position.line, |
| 40 | + col: position.character, |
| 41 | + }) |
| 42 | + } |
| 43 | + files.insert(current_path.to_path_buf(), content); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + while let Some(line) = lines.next() { |
| 48 | + eprintln!("LINE = {:?}", line); |
| 49 | + if line.starts_with("## ") { |
| 50 | + add_file(¤t_path, ¤t_content); |
| 51 | + current_content.clear(); |
| 52 | + current_path = PathBuf::from(line[2..].trim()); |
| 53 | + } else { |
| 54 | + current_content.push(line.to_string()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + add_file(¤t_path, ¤t_content); |
| 59 | + |
| 60 | + TestInput { files, location } |
| 61 | +} |
| 62 | + |
| 63 | +// pub async fn create_test_analyzer(input: &TestInput) { |
| 64 | +// let evaluator = Evaluator::new().await; |
| 65 | +// let (fetcher_input_send, fetcher_input_recv) = crossbeam::channel::unbounded::<FetcherInput>(); |
| 66 | +// let (fetcher_output_send, fetcher_output_recv) = |
| 67 | +// crossbeam::channel::unbounded::<FetcherOutput>(); |
| 68 | + |
| 69 | +// let mut analyzer = Analyzer::new(evaluator, fetcher_input_send, fetcher_output_recv); |
| 70 | + |
| 71 | +// for (path, content) in input.files.iter() { |
| 72 | +// analyzer.change_file(&path, &content); |
| 73 | +// } |
| 74 | + |
| 75 | +// } |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod test { |
| 79 | + use expect_test::{expect, Expect}; |
| 80 | + use indoc::indoc; |
| 81 | + |
| 82 | + use super::parse_test_input; |
| 83 | + |
| 84 | + fn check_parse_test_input(input: &str, expected: Expect) { |
| 85 | + expected.assert_debug_eq(&parse_test_input(input.trim())); |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_parse_test_input() { |
| 90 | + check_parse_test_input( |
| 91 | + indoc! {r#" |
| 92 | + ## /test.nix |
| 93 | + aaaaaaaaaaaaaaa |
| 94 | + bbbbbb |
| 95 | + cccccccc |
| 96 | + ## /your mother.nix |
| 97 | + aaaaaaaaaaaaaaaaaaaazzzzzzzzzzzzz |
| 98 | + xyz.$0cyz |
| 99 | + "# }, |
| 100 | + expect![[r#" |
| 101 | + TestInput { |
| 102 | + files: { |
| 103 | + "/test.nix": "aaaaaaaaaaaaaaa\nbbbbbb\ncccccccc", |
| 104 | + "/your mother.nix": "aaaaaaaaaaaaaaaaaaaazzzzzzzzzzzzz\nxyz.cyz", |
| 105 | + }, |
| 106 | + location: Some( |
| 107 | + Location { |
| 108 | + path: "/your mother.nix", |
| 109 | + line: 1, |
| 110 | + col: 4, |
| 111 | + }, |
| 112 | + ), |
| 113 | + } |
| 114 | + "#]], |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments