|
| 1 | +use crate::query_testing::{parse_position_comments, Assertion}; |
| 2 | +use ansi_term::Colour; |
| 3 | +use anyhow::{anyhow, Result}; |
| 4 | +use std::fs; |
| 5 | +use std::path::Path; |
| 6 | +use tree_sitter::Point; |
| 7 | +use tree_sitter_loader::Loader; |
| 8 | +use tree_sitter_tags::{TagsConfiguration, TagsContext}; |
| 9 | + |
| 10 | +#[derive(Debug)] |
| 11 | +pub struct Failure { |
| 12 | + row: usize, |
| 13 | + column: usize, |
| 14 | + expected_tag: String, |
| 15 | + actual_tags: Vec<String>, |
| 16 | +} |
| 17 | + |
| 18 | +impl std::error::Error for Failure {} |
| 19 | + |
| 20 | +impl std::fmt::Display for Failure { |
| 21 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 22 | + write!( |
| 23 | + f, |
| 24 | + "Failure - row: {}, column: {}, expected tag: '{}', actual tag: ", |
| 25 | + self.row, self.column, self.expected_tag |
| 26 | + )?; |
| 27 | + if self.actual_tags.is_empty() { |
| 28 | + write!(f, "none.")?; |
| 29 | + } else { |
| 30 | + for (i, actual_tag) in self.actual_tags.iter().enumerate() { |
| 31 | + if i > 0 { |
| 32 | + write!(f, ", ")?; |
| 33 | + } |
| 34 | + write!(f, "'{}'", actual_tag)?; |
| 35 | + } |
| 36 | + } |
| 37 | + Ok(()) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +pub fn test_tags(loader: &Loader, directory: &Path) -> Result<()> { |
| 42 | + let mut failed = false; |
| 43 | + let mut tags_context = TagsContext::new(); |
| 44 | + |
| 45 | + println!("tags:"); |
| 46 | + for tag_test_file in fs::read_dir(directory)? { |
| 47 | + let tag_test_file = tag_test_file?; |
| 48 | + let test_file_path = tag_test_file.path(); |
| 49 | + let test_file_name = tag_test_file.file_name(); |
| 50 | + let (language, language_config) = loader |
| 51 | + .language_configuration_for_file_name(&test_file_path)? |
| 52 | + .ok_or_else(|| anyhow!("No language found for path {:?}", test_file_path))?; |
| 53 | + let tags_config = language_config |
| 54 | + .tags_config(language)? |
| 55 | + .ok_or_else(|| anyhow!("No tags config found for {:?}", test_file_path))?; |
| 56 | + match test_tag( |
| 57 | + &mut tags_context, |
| 58 | + tags_config, |
| 59 | + fs::read(&test_file_path)?.as_slice(), |
| 60 | + ) { |
| 61 | + Ok(assertion_count) => { |
| 62 | + println!( |
| 63 | + " ✓ {} ({} assertions)", |
| 64 | + Colour::Green.paint(test_file_name.to_string_lossy().as_ref()), |
| 65 | + assertion_count |
| 66 | + ); |
| 67 | + } |
| 68 | + Err(e) => { |
| 69 | + println!( |
| 70 | + " ✗ {}", |
| 71 | + Colour::Red.paint(test_file_name.to_string_lossy().as_ref()) |
| 72 | + ); |
| 73 | + println!(" {}", e); |
| 74 | + failed = true; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if failed { |
| 80 | + Err(anyhow!("")) |
| 81 | + } else { |
| 82 | + Ok(()) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +pub fn test_tag( |
| 87 | + tags_context: &mut TagsContext, |
| 88 | + tags_config: &TagsConfiguration, |
| 89 | + source: &[u8], |
| 90 | +) -> Result<usize> { |
| 91 | + let tags = get_tag_positions(tags_context, tags_config, source)?; |
| 92 | + let assertions = parse_position_comments(tags_context.parser(), tags_config.language, source)?; |
| 93 | + |
| 94 | + // Iterate through all of the assertions, checking against the actual tags. |
| 95 | + let mut i = 0; |
| 96 | + let mut actual_tags = Vec::<&String>::new(); |
| 97 | + for Assertion { |
| 98 | + position, |
| 99 | + expected_capture_name: expected_tag, |
| 100 | + } in &assertions |
| 101 | + { |
| 102 | + let mut passed = false; |
| 103 | + |
| 104 | + 'tag_loop: loop { |
| 105 | + if let Some(tag) = tags.get(i) { |
| 106 | + if tag.1 <= *position { |
| 107 | + i += 1; |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + // Iterate through all of the tags that start at or before this assertion's |
| 112 | + // position, looking for one that matches the assertion |
| 113 | + let mut j = i; |
| 114 | + while let (false, Some(tag)) = (passed, tags.get(j)) { |
| 115 | + if tag.0 > *position { |
| 116 | + break 'tag_loop; |
| 117 | + } |
| 118 | + |
| 119 | + let tag_name = &tag.2; |
| 120 | + if *tag_name == *expected_tag { |
| 121 | + passed = true; |
| 122 | + break 'tag_loop; |
| 123 | + } else { |
| 124 | + actual_tags.push(tag_name); |
| 125 | + } |
| 126 | + |
| 127 | + j += 1; |
| 128 | + } |
| 129 | + } else { |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if !passed { |
| 135 | + return Err(Failure { |
| 136 | + row: position.row, |
| 137 | + column: position.column, |
| 138 | + expected_tag: expected_tag.clone(), |
| 139 | + actual_tags: actual_tags.into_iter().cloned().collect(), |
| 140 | + } |
| 141 | + .into()); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + Ok(assertions.len()) |
| 146 | +} |
| 147 | + |
| 148 | +pub fn get_tag_positions( |
| 149 | + tags_context: &mut TagsContext, |
| 150 | + tags_config: &TagsConfiguration, |
| 151 | + source: &[u8], |
| 152 | +) -> Result<Vec<(Point, Point, String)>> { |
| 153 | + let (tags_iter, _has_error) = tags_context.generate_tags(&tags_config, &source, None)?; |
| 154 | + let tag_positions = tags_iter |
| 155 | + .filter_map(|t| t.ok()) |
| 156 | + .map(|tag| { |
| 157 | + let tag_postfix = tags_config.syntax_type_name(tag.syntax_type_id).to_string(); |
| 158 | + let tag_name = if tag.is_definition { |
| 159 | + format!("definition.{}", tag_postfix) |
| 160 | + } else { |
| 161 | + format!("reference.{}", tag_postfix) |
| 162 | + }; |
| 163 | + (tag.span.start, tag.span.end, tag_name) |
| 164 | + }) |
| 165 | + .collect(); |
| 166 | + Ok(tag_positions) |
| 167 | +} |
0 commit comments