test: add first tests for pattern matching
All checks were successful
Test / test (push) Successful in 3s

This commit is contained in:
Paul Campbell 2024-09-18 15:28:17 +01:00
parent 21ff321402
commit 1e2a6e1804
5 changed files with 128 additions and 4 deletions

View file

@ -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<ISSUE_NUMBER>\d+)(\))").context("issue regex")?,
)
.prefix_pattern(marker_pattern()?)
.issue_pattern(issue_pattern()?)
.maybe_auth_token(std::env::var("REPO_TOKEN").ok())
.build();

16
src/patterns/mod.rs Normal file
View file

@ -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::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::Regex::new(r"( |)(\(|\(#)(?P<ISSUE_NUMBER>\d+)(\))").context("issue regex")
}

View file

@ -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(())
}

View file

@ -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(())
}

View file

@ -0,0 +1,5 @@
//
use super::*;
mod issue;
mod marker;