From 868f39b32b361903956e233ae03d587ec22e6f36 Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Tue, 12 Nov 2024 15:09:38 +0100 Subject: [PATCH] chore: update tests --- Cargo.toml | 1 + README.md | 2 +- src/lib.rs | 3 ++ src/tests.rs | 65 ++++++++++++++++++++++++++++++++++++++ tests/integration_tests.rs | 31 ++++++++++++++++++ tests/lib.rs | 43 ------------------------- tests/svg.rs | 48 ---------------------------- 7 files changed, 101 insertions(+), 92 deletions(-) create mode 100644 src/tests.rs create mode 100644 tests/integration_tests.rs delete mode 100644 tests/lib.rs delete mode 100644 tests/svg.rs diff --git a/Cargo.toml b/Cargo.toml index 33b7111..ac7bfd4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ authors = ["frytg"] edition = "2021" [dependencies] +regex = "1.11.1" reqwest = { version = "0.11", features = ["blocking", "json"] } scraper = "0.17" diff --git a/README.md b/README.md index a9e38bc..0bcf9aa 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ This compiles and runs the program directly (using dev profile and debug symbols To modify the color scheme used, you can provide `-c SCHEME`. For example, `cargo run -- output.svg -u frytg -c halloween` uses GitHub's halloween colors. -Use `cargo fmt` to format the code. +Use `cargo fmt` to format the code and `cargo test` to run the tests. ## Usage with binary diff --git a/src/lib.rs b/src/lib.rs index 44a699e..6e57ccd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,3 +89,6 @@ pub fn fetch_github_stats( Ok(stats) } + +#[cfg(test)] +mod tests; diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..aa92fa6 --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,65 @@ +#[cfg(test)] +mod tests { + use crate::{Chart, COLOR_SCHEMES}; + + fn sample_stats() -> Vec<(String, i32)> { + vec![ + ("2024-01-01".to_string(), 0), + ("2024-01-02".to_string(), 1), + ("2024-01-03".to_string(), 4), + ("2024-01-04".to_string(), 7), + ("2024-01-05".to_string(), 10), + ] + } + + #[test] + fn test_chart_creation() { + let stats = sample_stats(); + let chart = Chart::new(stats.clone(), None); + assert_eq!(chart.stats, stats); + assert_eq!(chart.colors.len(), 5); + } + + #[test] + fn test_chart_with_custom_colors() { + let stats = sample_stats(); + let colors = Some(COLOR_SCHEMES[2].1.to_vec()); // Halloween scheme + let chart = Chart::new(stats, colors.clone()); + assert_eq!(chart.colors, colors.unwrap()); + } + + #[test] + fn test_svg_rendering() { + let stats = sample_stats(); + let chart = Chart::new(stats, None); + let svg = chart.render().unwrap(); + + // Basic SVG structure + assert!(svg.starts_with("")); + + // Check for required elements + assert!(svg.contains(" { + assert!(!stats.is_empty()); + // Check date format + assert!(Regex::new(r"^\d{4}-\d{2}-\d{2}$") + .unwrap() + .is_match(&stats[0].0)); + // Check contribution count is non-negative + assert!(stats[0].1 >= 0); + } + Err(e) => panic!("Failed to fetch stats: {}", e), + } +} + +#[test] +fn test_full_chart_generation() { + let stats = vec![("2024-01-01".to_string(), 0), ("2024-01-02".to_string(), 5)]; + let chart = Chart::new(stats, Some(COLOR_SCHEMES[0].1.to_vec())); + let svg = chart.render().expect("Failed to render chart"); + + assert!(svg.contains("svg")); + assert!(svg.contains("rect")); + assert!(svg.contains("Jan")); // Month label +} diff --git a/tests/lib.rs b/tests/lib.rs deleted file mode 100644 index 0628d7f..0000000 --- a/tests/lib.rs +++ /dev/null @@ -1,43 +0,0 @@ -use super::*; - -#[test] -fn test_new_chart() { - let stats = vec![("2023-07-18".to_string(), 5)]; - let chart = Chart::new(stats.clone(), None); - assert_eq!(chart.stats, stats); - assert_eq!( - chart.colors, - vec!["#eeeeee", "#c6e48b", "#7bc96f", "#239a3b", "#196127"] - ); -} - -#[test] -fn test_render_svg() { - let stats = vec![("2023-07-18".to_string(), 5)]; - let chart = Chart::new(stats, None); - let svg = chart.render("svg").unwrap(); - assert!(svg.contains("")); -} - -#[test] -fn test_render_svg_square() { - let stats = vec![("2023-07-18".to_string(), 5)]; - let chart = Chart::new(stats, None); - let svg_square = chart.render("svg_square").unwrap(); - assert!(svg_square.contains("")); -} - -#[test] -fn test_color_schemes() { - let stats = vec![("2023-07-18".to_string(), 5)]; - let chart = Chart::new(stats.clone(), Some(vec!["#FF0000", "#00FF00", "#0000FF"])); - assert_eq!(chart.colors, vec!["#FF0000", "#00FF00", "#0000FF"]); - - let chart_default = Chart::new(stats, None); - assert_eq!( - chart_default.colors, - vec!["#eeeeee", "#c6e48b", "#7bc96f", "#239a3b", "#196127"] - ); -} diff --git a/tests/svg.rs b/tests/svg.rs deleted file mode 100644 index 8060770..0000000 --- a/tests/svg.rs +++ /dev/null @@ -1,48 +0,0 @@ -use super::*; - -#[test] -fn test_render_svg() { - let stats = vec![("2023-07-18".to_string(), 5)]; - let chart = Chart::new(stats, None); - let svg = chart.render("svg").unwrap(); - assert!(svg.contains("")); -} - -#[test] -fn test_render_svg_square() { - let stats = vec![("2023-07-18".to_string(), 5)]; - let chart = Chart::new(stats, None); - let svg_square = chart.render("svg_square").unwrap(); - assert!(svg_square.contains("")); -} - -#[test] -fn test_svg_add_points() { - let stats = vec![("2023-07-18".to_string(), 5)]; - let chart = Chart::new(stats, None); - let svg = chart.render("svg").unwrap(); - assert!(svg.contains("data-score")); - assert!(svg.contains("data-date")); -} - -#[test] -fn test_svg_add_weekdays() { - let stats = vec![("2023-07-18".to_string(), 5)]; - let chart = Chart::new(stats, None); - let svg = chart.render("svg").unwrap(); - assert!(svg.contains("Mon")); - assert!(svg.contains("Wed")); - assert!(svg.contains("Fri")); -} - -#[test] -fn test_svg_add_months() { - let stats = vec![("2023-07-18".to_string(), 5)]; - let chart = Chart::new(stats, None); - let svg = chart.render("svg").unwrap(); - assert!(svg.contains("Jan")); - assert!(svg.contains("Feb")); - assert!(svg.contains("Mar")); -}