From 1e2a6e18043d0455db3a98ba90ca182815a0efaa Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 18 Sep 2024 15:28:17 +0100 Subject: [PATCH] test: add first tests for pattern matching --- src/main.rs | 8 ++--- src/patterns/mod.rs | 16 +++++++++ src/patterns/tests/issue.rs | 33 +++++++++++++++++ src/patterns/tests/marker.rs | 70 ++++++++++++++++++++++++++++++++++++ src/patterns/tests/mod.rs | 5 +++ 5 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 src/patterns/mod.rs create mode 100644 src/patterns/tests/issue.rs create mode 100644 src/patterns/tests/marker.rs create mode 100644 src/patterns/tests/mod.rs diff --git a/src/main.rs b/src/main.rs index d4f1844..bd2b834 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,10 @@ use std::path::Path; use anyhow::{Context, Result}; use model::{Config, FoundMarkers, Line, Marker, MarkerKind}; +use patterns::{issue_pattern, marker_pattern}; mod model; +mod patterns; fn main() -> Result<()> { println!("Forgejo TODO Checker!"); @@ -17,10 +19,8 @@ fn main() -> Result<()> { )) .repo(std::env::var("GITHUB_REPOSITORY").context("GITHUB_REPOSITORY")?) .server(std::env::var("GITHUB_SERVER_URL").context("GITHUB_SERVER_URL")?) - .prefix_pattern(regex::Regex::new(r"(#|//)\s*(TODO|FIXME)").context("prefix regex")?) - .issue_pattern( - regex::Regex::new(r"( |)(\(|\(#)(?P\d+)(\))").context("issue regex")?, - ) + .prefix_pattern(marker_pattern()?) + .issue_pattern(issue_pattern()?) .maybe_auth_token(std::env::var("REPO_TOKEN").ok()) .build(); diff --git a/src/patterns/mod.rs b/src/patterns/mod.rs new file mode 100644 index 0000000..0fa64ad --- /dev/null +++ b/src/patterns/mod.rs @@ -0,0 +1,16 @@ +// +#[cfg(test)] +mod tests; + +use anyhow::{Context as _, Result}; +use regex::Regex; + +/// The pattern to find a TODO or FIXME comment +pub fn marker_pattern() -> Result { + regex::Regex::new(r"(#|//)\s*(TODO|FIXME)").context("prefix regex") +} + +/// The pattern to find an issue number on an already found TODO or FIXME comment +pub fn issue_pattern() -> Result { + regex::Regex::new(r"( |)(\(|\(#)(?P\d+)(\))").context("issue regex") +} diff --git a/src/patterns/tests/issue.rs b/src/patterns/tests/issue.rs new file mode 100644 index 0000000..f0845a1 --- /dev/null +++ b/src/patterns/tests/issue.rs @@ -0,0 +1,33 @@ +// +use super::*; + +#[test] +fn when_issue_should_find_number() -> anyhow::Result<()> { + //given + let line = " a line with a // TODO: (#13) issue number"; + + //when + let result = issue_pattern()?.captures(line); + + //then + assert!(result.is_some()); + let captures = result.unwrap(); + let issue_number = captures.name("ISSUE_NUMBER").unwrap().as_str(); + assert_eq!(issue_number, "13"); + + Ok(()) +} + +#[test] +fn when_no_issue_should_find_nothing() -> anyhow::Result<()> { + //given + let line = " a line with a // TODO: and no issue number"; + + //when + let result = issue_pattern()?.captures(line); + + //then + assert!(result.is_none()); + + Ok(()) +} diff --git a/src/patterns/tests/marker.rs b/src/patterns/tests/marker.rs new file mode 100644 index 0000000..d596e8d --- /dev/null +++ b/src/patterns/tests/marker.rs @@ -0,0 +1,70 @@ +use super::*; + +#[test] +fn when_todo_find_marker() -> anyhow::Result<()> { + //given + let line = " a line // TODO: with a marker"; + + //when + let result = marker_pattern()?.find(line); + + //then + assert!(result.is_some()); + + Ok(()) +} + +#[test] +fn when_fixme_find_marker() -> anyhow::Result<()> { + //given + let line = " a line // FIXME: with a marker"; + + //when + let result = marker_pattern()?.find(line); + + //then + assert!(result.is_some()); + + Ok(()) +} + +#[test] +fn when_no_marker_find_nothing() -> anyhow::Result<()> { + //given + let line = " a line with no marker"; + + //when + let result = marker_pattern()?.find(line); + + //then + assert!(result.is_none()); + + Ok(()) +} + +#[test] +fn when_invalid_todo_find_nothing() -> anyhow::Result<()> { + //given + let line = " a line TODO: with no real marker"; + + //when + let result = marker_pattern()?.find(line); + + //then + assert!(result.is_none()); + + Ok(()) +} +#[test] +fn when_invalid_fixme_find_nothing() -> anyhow::Result<()> { + //given + let line = " a line FIXME: with no real marker"; + + //when + let result = marker_pattern()?.find(line); + + //then + assert!(result.is_none()); + + Ok(()) +} diff --git a/src/patterns/tests/mod.rs b/src/patterns/tests/mod.rs new file mode 100644 index 0000000..d1c0d14 --- /dev/null +++ b/src/patterns/tests/mod.rs @@ -0,0 +1,5 @@ +// +use super::*; + +mod issue; +mod marker;