Skip to content

Commit

Permalink
re-organize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mazznoer committed Aug 3, 2024
1 parent 2be71bf commit 23135ac
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 75 deletions.
17 changes: 0 additions & 17 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,21 +483,4 @@ mod tests {
assert_eq!(parse_angle(s), expected);
}
}

#[cfg(feature = "named-colors")]
#[test]
fn test_named_colors() {
for (&name, &rgb) in NAMED_COLORS.entries() {
assert_eq!(parse(name).unwrap().to_rgba8()[0..3], rgb);
}

let skip_list = ["aqua", "cyan", "fuchsia", "magenta"];

for &name in NAMED_COLORS.keys() {
if skip_list.contains(&name) || name.contains("gray") || name.contains("grey") {
continue;
}
assert_eq!(parse(name).unwrap().name(), Some(name));
}
}
}
80 changes: 55 additions & 25 deletions tests/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,61 @@ fn basic() {
assert_eq!(c.to_rgba16(), [65535, 32768, 0, 65535]);
}

#[test]
fn convert_colors() {
let data = &[
"#000000",
"#ffffff",
"#7f7f7f",
"#ff0000",
"#825dfa6d",
"#fa8072",
"#87ceeb",
"#ff6347",
"#ee82ee",
"#9acd32",
];
for s in data {
let col = csscolorparser::parse(s).unwrap();
assert_eq!(s, &col.to_hex_string());

let [a, b, c, d] = col.to_rgba8();
let x = Color::from_rgba8(a, b, c, d);
assert_eq!(s, &x.to_hex_string());

let [a, b, c, d] = col.to_hsva();
let x = Color::from_hsva(a, b, c, d);
assert_eq!(s, &x.to_hex_string());

let [a, b, c, d] = col.to_hsla();
let x = Color::from_hsla(a, b, c, d);
assert_eq!(s, &x.to_hex_string());

let [a, b, c, d] = col.to_hwba();
let x = Color::from_hwba(a, b, c, d);
assert_eq!(s, &x.to_hex_string());

let [a, b, c, d] = col.to_linear_rgba();
let x = Color::from_linear_rgba(a, b, c, d);
assert_eq!(s, &x.to_hex_string());

let [a, b, c, d] = col.to_oklaba();
let x = Color::from_oklaba(a, b, c, d);
assert_eq!(s, &x.to_hex_string());

#[cfg(feature = "lab")]
{
let [a, b, c, d] = col.to_laba();
let x = Color::from_laba(a, b, c, d);
assert_eq!(s, &x.to_hex_string());

let [a, b, c, d] = col.to_lcha();
let x = Color::from_lcha(a, b, c, d);
assert_eq!(s, &x.to_hex_string());
}
}
}

#[test]
fn red() {
let data = &[
Expand All @@ -128,31 +183,6 @@ fn red() {
}
}

#[cfg(feature = "named-colors")]
#[test]
fn color_name() {
let test_data = [
(Color::new(0.0, 0.0, 0.0, 1.0), "black"),
(Color::new(1.0, 1.0, 1.0, 1.0), "white"),
(Color::new(1.0, 0.0, 0.0, 1.0), "red"),
(Color::from_html("gold").unwrap(), "gold"),
(Color::from_html("pink").unwrap(), "pink"),
(Color::from_html("tomato").unwrap(), "tomato"),
];
for (color, name) in test_data {
assert_eq!(color.name(), Some(name));
}

let test_data = [
Color::new(0.7, 0.8, 0.9, 1.0),
Color::new(1.0, 0.5, 0.0, 1.0),
Color::from_rgba8(0, 50, 100, 255),
];
for c in test_data {
assert!(c.name().is_none());
}
}

#[test]
fn interpolate() {
let a = Color::new(0.0, 1.0, 0.0, 1.0);
Expand Down
66 changes: 66 additions & 0 deletions tests/named_colors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use csscolorparser::Color;

#[cfg(feature = "named-colors")]
#[test]
fn named_colors() {
let skip_list = ["aqua", "cyan", "fuchsia", "magenta"];

for (&name, &rgb) in csscolorparser::NAMED_COLORS.entries() {
let c = csscolorparser::parse(name).unwrap();
assert_eq!(c.to_rgba8()[0..3], rgb);

if skip_list.contains(&name) || name.contains("gray") || name.contains("grey") {
continue;
}
assert_eq!(c.name(), Some(name));

let [r, g, b] = rgb;
let c = Color::from_rgba8(r, g, b, 255);
assert_eq!(c.name(), Some(name));
}

// Hex

#[rustfmt::skip]
let test_data = [
("aliceblue", "#f0f8ff"),
("bisque", "#ffe4c4"),
("black", "#000000"),
("chartreuse", "#7fff00"),
("coral", "#ff7f50"),
("crimson", "#dc143c"),
("dodgerblue", "#1e90ff"),
("firebrick", "#b22222"),
("gold", "#ffd700"),
("hotpink", "#ff69b4"),
("indigo", "#4b0082"),
("lavender", "#e6e6fa"),
("lime", "#00ff00"),
("plum", "#dda0dd"),
("red", "#ff0000"),
("salmon", "#fa8072"),
("skyblue", "#87ceeb"),
("tomato", "#ff6347"),
("violet", "#ee82ee"),
("yellowgreen", "#9acd32"),
];

for (name, hex) in test_data {
let c = csscolorparser::parse(name).unwrap();
assert_eq!(c.to_hex_string(), hex);

let c = csscolorparser::parse(hex).unwrap();
assert_eq!(c.name(), Some(name));
}

// Colors without names

let test_data = [
Color::new(0.7, 0.8, 0.9, 1.0),
Color::new(1.0, 0.5, 0.0, 1.0),
Color::from_rgba8(0, 50, 100, 255),
];
for c in test_data {
assert!(c.name().is_none());
}
}
33 changes: 0 additions & 33 deletions tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,39 +64,6 @@ fn equal() {
}
}

#[cfg(feature = "named-colors")]
#[test]
fn named_colors() {
#[rustfmt::skip]
let test_data = [
("aliceblue", "#f0f8ff"),
("bisque", "#ffe4c4"),
("black", "#000000"),
("chartreuse", "#7fff00"),
("coral", "#ff7f50"),
("crimson", "#dc143c"),
("dodgerblue", "#1e90ff"),
("firebrick", "#b22222"),
("gold", "#ffd700"),
("hotpink", "#ff69b4"),
("indigo", "#4b0082"),
("lavender", "#e6e6fa"),
("lime", "#00ff00"),
("plum", "#dda0dd"),
("red", "#ff0000"),
("salmon", "#fa8072"),
("skyblue", "#87ceeb"),
("tomato", "#ff6347"),
("violet", "#ee82ee"),
("yellowgreen", "#9acd32"),
];

for (s, hex) in test_data {
let c = parse(s).unwrap();
assert_eq!(hex, c.to_hex_string());
}
}

#[test]
fn black() {
let data = [
Expand Down

0 comments on commit 23135ac

Please sign in to comment.