Skip to content

Commit 049c56c

Browse files
committed
WIP: Get tests to pass (except lab/lch)
1 parent aeb35c9 commit 049c56c

File tree

1 file changed

+75
-23
lines changed

1 file changed

+75
-23
lines changed

Diff for: src/tests.rs

+75-23
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ use serde_json::{self, json, Map, Value};
1313
use self::test::Bencher;
1414

1515
use super::{
16-
color::{rgb, rgba},
1716
parse_important, parse_nth, parse_one_declaration, parse_one_rule, stylesheet_encoding,
18-
AbsoluteColor, AtRuleParser, BasicParseError, BasicParseErrorKind, Color, CowRcStr,
19-
DeclarationListParser, DeclarationParser, Delimiter, EncodingSupport, ParseError,
20-
ParseErrorKind, Parser, ParserInput, ParserState, QualifiedRuleParser, RuleListParser,
21-
SourceLocation, ToCss, Token, TokenSerializationType, UnicodeRange, RGBA,
17+
AtRuleParser, BasicParseError, BasicParseErrorKind, CielabColor, Color, CowRcStr, CurrentColor,
18+
DeclarationListParser, DeclarationParser, Delimiter, DeprecatedColor, EncodingSupport,
19+
NamedColor, OklabColor, ParseError, ParseErrorKind, Parser, ParserInput, ParserState,
20+
QualifiedRuleParser, RuleListParser, SourceLocation, SrgbColor, ToCss, Token,
21+
TokenSerializationType, UnicodeRange,
2222
};
2323

2424
macro_rules! JArray {
@@ -398,7 +398,14 @@ fn color4_lab_lch_oklab_oklch() {
398398
run_color_tests(
399399
include_str!("css-parsing-tests/color4_lab_lch_oklab_oklch.json"),
400400
|c| match c {
401-
Ok(color) => Value::Array(vec![color.to_json(), color.to_css_string().to_json()]),
401+
Ok(color) => Value::Array(vec![
402+
color.to_json(),
403+
match color {
404+
Color::CielabColor(cielab_color) => cielab_color.to_css_string().to_json(),
405+
Color::OklabColor(oklab_color) => oklab_color.to_css_string().to_json(),
406+
_ => Value::Null,
407+
},
408+
]),
402409
Err(_) => Value::Null,
403410
},
404411
)
@@ -427,9 +434,27 @@ fn parse_comma_separated_ignoring_errors() {
427434
Color::parse(input).map_err(Into::<ParseError<()>>::into)
428435
});
429436
assert_eq!(result.len(), 3);
430-
assert_eq!(result[0].to_css_string(), "rgb(255, 0, 0)");
431-
assert_eq!(result[1].to_css_string(), "rgb(255, 255, 0)");
432-
assert_eq!(result[2].to_css_string(), "rgb(0, 0, 255)");
437+
assert_eq!(
438+
result[0],
439+
Color::NamedColor(NamedColor::new(
440+
"red".to_string(),
441+
SrgbColor::from_ints(255, 0, 0, 255)
442+
))
443+
);
444+
assert_eq!(
445+
result[1],
446+
Color::NamedColor(NamedColor::new(
447+
"yellow".to_string(),
448+
SrgbColor::from_ints(255, 255, 0, 255)
449+
))
450+
);
451+
assert_eq!(
452+
result[2],
453+
Color::NamedColor(NamedColor::new(
454+
"blue".to_string(),
455+
SrgbColor::from_ints(0, 0, 255, 255)
456+
))
457+
);
433458
}
434459

435460
#[test]
@@ -547,25 +572,25 @@ fn serialize_bad_tokens() {
547572

548573
#[test]
549574
fn serialize_current_color() {
550-
let c = Color::CurrentColor;
575+
let c = CurrentColor;
551576
assert!(c.to_css_string() == "currentcolor");
552577
}
553578

554579
#[test]
555580
fn serialize_rgb_full_alpha() {
556-
let c = rgb(255, 230, 204);
581+
let c = SrgbColor::from_ints(255, 230, 204, 255);
557582
assert_eq!(c.to_css_string(), "rgb(255, 230, 204)");
558583
}
559584

560585
#[test]
561586
fn serialize_rgba() {
562-
let c = rgba(26, 51, 77, 0.125);
563-
assert_eq!(c.to_css_string(), "rgba(26, 51, 77, 0.125)");
587+
let c = SrgbColor::from_ints(26, 51, 77, 32);
588+
assert_eq!(c.to_css_string(), "rgba(26, 51, 77, 0.12549)");
564589
}
565590

566591
#[test]
567592
fn serialize_rgba_two_digit_float_if_roundtrips() {
568-
let c = Color::Absolute(AbsoluteColor::Rgba(RGBA::from_floats(0., 0., 0., 0.5)));
593+
let c = SrgbColor::from_floats(0., 0., 0., 0.5);
569594
assert_eq!(c.to_css_string(), "rgba(0, 0, 0, 0.5)");
570595
}
571596

@@ -857,16 +882,43 @@ where
857882
impl ToJson for Color {
858883
fn to_json(&self) -> Value {
859884
match *self {
860-
Color::CurrentColor => "currentcolor".to_json(),
861-
Color::Absolute(absolute) => match absolute {
862-
AbsoluteColor::Rgba(ref rgba) => {
863-
json!([rgba.red, rgba.green, rgba.blue, rgba.alpha])
885+
Color::SrgbColor(ref srgb_color)
886+
| Color::NamedColor(NamedColor {
887+
value: ref srgb_color,
888+
..
889+
}) => {
890+
let rgba = srgb_color.to_rgba();
891+
json!([rgba.red, rgba.green, rgba.blue, srgb_color.to_floats().3])
892+
}
893+
Color::SystemColor(ref system_color)
894+
| Color::DeprecatedColor(DeprecatedColor {
895+
same_as: ref system_color,
896+
..
897+
}) => system_color.name.to_json(),
898+
Color::CurrentColor(CurrentColor) => "currentcolor".to_json(),
899+
Color::CielabColor(CielabColor::CieLab(ref lab_coords))
900+
| Color::OklabColor(OklabColor::OkLab(ref lab_coords)) => json!([
901+
lab_coords.lightness,
902+
lab_coords.a,
903+
lab_coords.b,
904+
match lab_coords.alpha {
905+
Some(alpha) => alpha.number,
906+
None => 0.,
864907
}
865-
AbsoluteColor::Lab(ref c) => json!([c.lightness, c.a, c.b, c.alpha]),
866-
AbsoluteColor::Lch(ref c) => json!([c.lightness, c.chroma, c.hue, c.alpha]),
867-
AbsoluteColor::Oklab(ref c) => json!([c.lightness, c.a, c.b, c.alpha]),
868-
AbsoluteColor::Oklch(ref c) => json!([c.lightness, c.chroma, c.hue, c.alpha]),
869-
},
908+
]),
909+
Color::CielabColor(CielabColor::CieLch(ref lch_coords))
910+
| Color::OklabColor(OklabColor::OkLch(ref lch_coords)) => json!([
911+
lch_coords.lightness,
912+
lch_coords.chroma,
913+
match lch_coords.hue {
914+
Some(hue) => hue.degrees,
915+
None => 0.,
916+
},
917+
match lch_coords.alpha {
918+
Some(alpha) => alpha.number,
919+
None => 0.,
920+
}
921+
]),
870922
}
871923
}
872924
}

0 commit comments

Comments
 (0)