From 0dfe89f3538d7ed8f08d8b9b7b05e515d82989c0 Mon Sep 17 00:00:00 2001 From: Patrick Thomson Date: Wed, 21 Oct 2020 13:32:04 -0400 Subject: [PATCH] parse assertions from regex capture --- cli/src/query/assert.rs | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/cli/src/query/assert.rs b/cli/src/query/assert.rs index 35a7f4b7..d5998eaf 100644 --- a/cli/src/query/assert.rs +++ b/cli/src/query/assert.rs @@ -7,8 +7,7 @@ use tree_sitter::Point; // TODO: It would be cooler to do this with a comments query rather than with a regex // directly. lazy_static! { - static ref METADATA_PAIR_REGEX: Regex = Regex::new(r#"(\w+): ([^\s,]+)"#).unwrap(); - static ref NUMBER_REGEX: Regex = Regex::new(r#"\d+"#).unwrap(); + static ref METADATA_REGEX: Regex = Regex::new(r#"(\w+): ([^\s,]+), (\d+), (\d+)"#).unwrap(); } pub struct CaptureInfo { @@ -18,14 +17,44 @@ pub struct CaptureInfo { #[derive(Debug, Eq, PartialEq)] struct Assertion { position: Point, - line_numbers: Vec, + capture_class: String, capture_type: String, } +impl From> for Assertion { + fn from(re: regex::Captures) -> Assertion { + Assertion { + capture_class: re.get(1).unwrap().as_str().to_string(), + capture_type: re.get(2).unwrap().as_str().to_string(), + position: Point { + row: re + .get(3) + .iter() + .flat_map(|m| m.as_str().parse::()) + .next() + .unwrap(), + column: re + .get(4) + .iter() + .flat_map(|m| m.as_str().parse::()) + .next() + .unwrap(), + }, + } + } +} + pub fn assert_expected_captures(_captures: Vec, path: String) -> Result<()> { let contents = fs::read_to_string(path)?; - for m in METADATA_PAIR_REGEX.captures_iter(&contents) { - println!("pair: {:?}", m); + + let assertions: Vec = METADATA_REGEX + .captures_iter(&contents) + .map(|c| Assertion::from(c)) + .collect(); + + for a in assertions { + println!("a: {:?}", a); } + Ok(()) }