Skip to content

Commit

Permalink
chore: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Nov 12, 2024
1 parent dc9ada3 commit 868f39b
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 92 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["frytg"]
edition = "2021"

[dependencies]
regex = "1.11.1"
reqwest = { version = "0.11", features = ["blocking", "json"] }
scraper = "0.17"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ pub fn fetch_github_stats(

Ok(stats)
}

#[cfg(test)]
mod tests;
65 changes: 65 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -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("<svg"));
assert!(svg.ends_with("</svg>"));

// Check for required elements
assert!(svg.contains("<rect")); // Contribution squares
assert!(svg.contains("Mon")); // Weekday labels
assert!(svg.contains("Jan")); // Month label

// Check color usage
assert!(svg.contains("#eeeeee")); // Default color scheme
assert!(svg.contains("#c6e48b"));
}

#[test]
fn test_color_schemes() {
// Test default scheme
assert_eq!(COLOR_SCHEMES[0].0, "default");
assert_eq!(COLOR_SCHEMES[0].1.len(), 5);

// Test old scheme
assert_eq!(COLOR_SCHEMES[1].0, "old");
assert_eq!(COLOR_SCHEMES[1].1.len(), 5);

// Test halloween scheme
assert_eq!(COLOR_SCHEMES[2].0, "halloween");
assert_eq!(COLOR_SCHEMES[2].1.len(), 5);
}
}
31 changes: 31 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use regex::Regex;

use githubchart::{fetch_github_stats, Chart, COLOR_SCHEMES};

#[test]
fn test_github_stats_fetching() {
// Note: This test requires internet connection
match fetch_github_stats("frytg") {
Ok(stats) => {
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
}
43 changes: 0 additions & 43 deletions tests/lib.rs

This file was deleted.

48 changes: 0 additions & 48 deletions tests/svg.rs

This file was deleted.

0 comments on commit 868f39b

Please sign in to comment.