Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mazznoer committed Aug 14, 2024
1 parent 22cb93a commit 873e996
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 218 deletions.
143 changes: 142 additions & 1 deletion tests/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,96 @@ use colorgrad::{
BlendMode, Color, Gradient, GradientBuilder, GradientBuilderError, LinearGradient,
};

mod utils;
use utils::*;

#[test]
fn builder() {
// Builder pattern style 2
// Default colors

let g = GradientBuilder::new().build::<LinearGradient>().unwrap();
assert_eq!(g.domain(), (0.0, 1.0));
assert_eq!(g.at(0.0).to_hex_string(), "#000000");
assert_eq!(g.at(1.0).to_hex_string(), "#ffffff");

// Single color

let g = GradientBuilder::new()
.colors(&[Color::new(1.0, 0.0, 0.0, 1.0)])
.build::<LinearGradient>()
.unwrap();
assert_eq!(g.at(0.0).to_rgba8(), [255, 0, 0, 255]);
assert_eq!(g.at(0.5).to_rgba8(), [255, 0, 0, 255]);
assert_eq!(g.at(1.0).to_rgba8(), [255, 0, 0, 255]);

// Default domain

let g = GradientBuilder::new()
.html_colors(&["#f00", "#0f0", "#00f"])
.build::<LinearGradient>()
.unwrap();
assert_eq!(g.at(0.0).to_hex_string(), "#ff0000");
assert_eq!(g.at(0.5).to_hex_string(), "#00ff00");
assert_eq!(g.at(1.0).to_hex_string(), "#0000ff");

// Custom domain

let g = GradientBuilder::new()
.html_colors(&["#f00", "#0f0", "#00f"])
.domain(&[-100.0, 100.0])
.build::<LinearGradient>()
.unwrap();
assert_eq!(g.at(-100.0).to_hex_string(), "#ff0000");
assert_eq!(g.at(0.0).to_hex_string(), "#00ff00");
assert_eq!(g.at(100.0).to_hex_string(), "#0000ff");

// Color position

let g = GradientBuilder::new()
.html_colors(&["#f00", "#0f0", "#00f", "#f0f"])
.domain(&[13.0, 27.3, 90.0, 97.5])
.build::<LinearGradient>()
.unwrap();
assert_eq!(g.at(13.0).to_hex_string(), "#ff0000");
assert_eq!(g.at(27.3).to_hex_string(), "#00ff00");
assert_eq!(g.at(90.0).to_hex_string(), "#0000ff");
assert_eq!(g.at(97.5).to_hex_string(), "#ff00ff");

// Multiple colors, custom domain

let cols1 = vec!["#00f", "#00ffff"];
let cols2 = vec!["lime".to_string()];
let mut gb = GradientBuilder::new();
gb.html_colors(&cols1);
gb.colors(&[Color::new(1.0, 1.0, 0.0, 0.5)]);
gb.html_colors(&cols2);
gb.domain(&[10.0, 50.0]);
let g = gb.build::<LinearGradient>().unwrap();

assert_eq!(
&colors2hex(&gb.get_colors()),
&["#0000ff", "#00ffff", "#ffff0080", "#00ff00"]
);
assert_eq!(
&colors2hex(&g.colors(4)),
&["#0000ff", "#00ffff", "#ffff0080", "#00ff00"]
);

// Filter stops

let mut gb = GradientBuilder::new();
gb.html_colors(&["gold", "red", "blue", "yellow", "black", "white", "plum"]);
gb.domain(&[0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0]);
gb.build::<LinearGradient>().unwrap();

assert_eq!(&gb.get_positions(), &[0.0, 0.5, 0.5, 1.0]);
assert_eq!(
&colors2hex(&gb.get_colors()),
&["#ff0000", "#0000ff", "#000000", "#ffffff"]
);

// Reusing builder

let mut gb = GradientBuilder::new();
gb.colors(&[
Color::from_rgba8(255, 0, 0, 255),
Expand All @@ -30,6 +117,60 @@ fn builder() {
assert_eq!(g.at(100.0).to_rgba8(), [0, 255, 0, 255]);
}

#[test]
fn css_gradient() {
let test_data = [
(
"red, lime 75%, blue",
vec![0.0, 0.75, 1.0],
vec!["#ff0000", "#00ff00", "#0000ff"],
),
(
"red 13%, lime, blue",
vec![0.0, 0.13, 0.565, 1.0],
vec!["#ff0000", "#ff0000", "#00ff00", "#0000ff"],
),
(
"red, lime, blue 100",
vec![0.0, 50.0, 100.0],
vec!["#ff0000", "#00ff00", "#0000ff"],
),
(
"red -100, lime, blue 100",
vec![-100.0, 0.0, 100.0],
vec!["#ff0000", "#00ff00", "#0000ff"],
),
(
"red, lime -10, blue 15, gold",
vec![0.0, 15.0],
vec!["#00ff00", "#0000ff"],
),
];

for (s, positions, colors) in test_data {
let mut gb = GradientBuilder::new();
gb.css(s).build::<LinearGradient>().unwrap();
assert_eq!(gb.get_positions(), &positions);
assert_eq!(&colors2hex(gb.get_colors()), &colors);
}

// Invalid format

let invalid_css = [
"",
" ",
"0, red, lime",
"red, lime, 100%",
"deeppink, 0.4, 0.9, pink",
"0%, 100%",
];

for s in invalid_css {
let g = GradientBuilder::new().css(s).build::<LinearGradient>();
assert_eq!(g.unwrap_err(), GradientBuilderError::InvalidCssGradient);
}
}

#[test]
fn builder_error() {
// Invalid HTML colors
Expand Down
47 changes: 47 additions & 0 deletions tests/spread.rs → tests/gradient.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use colorgrad::{Gradient, GradientBuilder, LinearGradient};

mod utils;
use utils::*;

#[test]
fn spread_inside_domain() {
let g = GradientBuilder::new()
Expand Down Expand Up @@ -91,3 +94,47 @@ fn spread_reflect() {
assert_eq!(g.reflect_at(2.5).to_hex_string(), "#808080");
assert_eq!(g.reflect_at(2.9).to_hex_string(), "#e6e6e6");
}

#[test]
fn colors() {
let g = GradientBuilder::new()
.html_colors(&["#f00", "#0f0", "#00f"])
.build::<LinearGradient>()
.unwrap();

assert_eq!(g.domain(), (0.0, 1.0));

assert_eq!(g.colors(0).len(), 0);

assert_eq!(colors2hex(&g.colors(1)), &["#ff0000",]);

assert_eq!(colors2hex(&g.colors(2)), &["#ff0000", "#0000ff",]);

assert_eq!(
colors2hex(&g.colors(3)),
&["#ff0000", "#00ff00", "#0000ff",]
);

assert_eq!(
colors2hex(&g.colors(5)),
&["#ff0000", "#808000", "#00ff00", "#008080", "#0000ff",]
);

let g = GradientBuilder::new()
.html_colors(&["#f00", "#0f0", "#00f"])
.domain(&[-1.0, 1.0])
.build::<LinearGradient>()
.unwrap();

assert_eq!(g.domain(), (-1.0, 1.0));

assert_eq!(
colors2hex(&g.colors(3)),
&["#ff0000", "#00ff00", "#0000ff",]
);

assert_eq!(
colors2hex(&g.colors(5)),
&["#ff0000", "#808000", "#00ff00", "#008080", "#0000ff",]
);
}
Loading

0 comments on commit 873e996

Please sign in to comment.