Skip to content

Commit 868f39b

Browse files
committed
chore: update tests
1 parent dc9ada3 commit 868f39b

File tree

7 files changed

+101
-92
lines changed

7 files changed

+101
-92
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["frytg"]
55
edition = "2021"
66

77
[dependencies]
8+
regex = "1.11.1"
89
reqwest = { version = "0.11", features = ["blocking", "json"] }
910
scraper = "0.17"
1011

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This compiles and runs the program directly (using dev profile and debug symbols
2020

2121
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.
2222

23-
Use `cargo fmt` to format the code.
23+
Use `cargo fmt` to format the code and `cargo test` to run the tests.
2424

2525
## Usage with binary
2626

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,6 @@ pub fn fetch_github_stats(
8989

9090
Ok(stats)
9191
}
92+
93+
#[cfg(test)]
94+
mod tests;

src/tests.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use crate::{Chart, COLOR_SCHEMES};
4+
5+
fn sample_stats() -> Vec<(String, i32)> {
6+
vec![
7+
("2024-01-01".to_string(), 0),
8+
("2024-01-02".to_string(), 1),
9+
("2024-01-03".to_string(), 4),
10+
("2024-01-04".to_string(), 7),
11+
("2024-01-05".to_string(), 10),
12+
]
13+
}
14+
15+
#[test]
16+
fn test_chart_creation() {
17+
let stats = sample_stats();
18+
let chart = Chart::new(stats.clone(), None);
19+
assert_eq!(chart.stats, stats);
20+
assert_eq!(chart.colors.len(), 5);
21+
}
22+
23+
#[test]
24+
fn test_chart_with_custom_colors() {
25+
let stats = sample_stats();
26+
let colors = Some(COLOR_SCHEMES[2].1.to_vec()); // Halloween scheme
27+
let chart = Chart::new(stats, colors.clone());
28+
assert_eq!(chart.colors, colors.unwrap());
29+
}
30+
31+
#[test]
32+
fn test_svg_rendering() {
33+
let stats = sample_stats();
34+
let chart = Chart::new(stats, None);
35+
let svg = chart.render().unwrap();
36+
37+
// Basic SVG structure
38+
assert!(svg.starts_with("<svg"));
39+
assert!(svg.ends_with("</svg>"));
40+
41+
// Check for required elements
42+
assert!(svg.contains("<rect")); // Contribution squares
43+
assert!(svg.contains("Mon")); // Weekday labels
44+
assert!(svg.contains("Jan")); // Month label
45+
46+
// Check color usage
47+
assert!(svg.contains("#eeeeee")); // Default color scheme
48+
assert!(svg.contains("#c6e48b"));
49+
}
50+
51+
#[test]
52+
fn test_color_schemes() {
53+
// Test default scheme
54+
assert_eq!(COLOR_SCHEMES[0].0, "default");
55+
assert_eq!(COLOR_SCHEMES[0].1.len(), 5);
56+
57+
// Test old scheme
58+
assert_eq!(COLOR_SCHEMES[1].0, "old");
59+
assert_eq!(COLOR_SCHEMES[1].1.len(), 5);
60+
61+
// Test halloween scheme
62+
assert_eq!(COLOR_SCHEMES[2].0, "halloween");
63+
assert_eq!(COLOR_SCHEMES[2].1.len(), 5);
64+
}
65+
}

tests/integration_tests.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use regex::Regex;
2+
3+
use githubchart::{fetch_github_stats, Chart, COLOR_SCHEMES};
4+
5+
#[test]
6+
fn test_github_stats_fetching() {
7+
// Note: This test requires internet connection
8+
match fetch_github_stats("frytg") {
9+
Ok(stats) => {
10+
assert!(!stats.is_empty());
11+
// Check date format
12+
assert!(Regex::new(r"^\d{4}-\d{2}-\d{2}$")
13+
.unwrap()
14+
.is_match(&stats[0].0));
15+
// Check contribution count is non-negative
16+
assert!(stats[0].1 >= 0);
17+
}
18+
Err(e) => panic!("Failed to fetch stats: {}", e),
19+
}
20+
}
21+
22+
#[test]
23+
fn test_full_chart_generation() {
24+
let stats = vec![("2024-01-01".to_string(), 0), ("2024-01-02".to_string(), 5)];
25+
let chart = Chart::new(stats, Some(COLOR_SCHEMES[0].1.to_vec()));
26+
let svg = chart.render().expect("Failed to render chart");
27+
28+
assert!(svg.contains("svg"));
29+
assert!(svg.contains("rect"));
30+
assert!(svg.contains("Jan")); // Month label
31+
}

tests/lib.rs

-43
This file was deleted.

tests/svg.rs

-48
This file was deleted.

0 commit comments

Comments
 (0)