use css_mediaquery::{matches, parse, Context, Modifier};
#[test]
fn parse_ast() {
let q = parse("screen and (min-width: 600px), print and (orientation: landscape)");
assert_eq!(q.len(), 2);
assert!(!q[0].inverse);
assert_eq!(q[0].media_type, "screen");
assert_eq!(q[0].expressions[0].modifier, Some(Modifier::Min));
assert_eq!(q[0].expressions[0].feature, "width");
assert_eq!(q[0].expressions[0].value.as_deref(), Some("600px"));
assert_eq!(q[1].media_type, "print");
assert_eq!(q[1].expressions[0].modifier, None);
assert_eq!(q[1].expressions[0].feature, "orientation");
}
#[test]
fn parse_defaults_type_to_all() {
let q = parse("(min-width: 600px)");
assert_eq!(q[0].media_type, "all");
assert!(!q[0].inverse);
}
#[test]
fn parse_not_and_only() {
assert!(parse("not screen and (min-width: 600px)")[0].inverse);
assert!(!parse("only screen and (min-width: 600px)")[0].inverse);
assert_eq!(parse("only screen")[0].media_type, "screen");
}
#[test]
fn parse_boolean_feature_has_no_value() {
let q = parse("(color)");
assert_eq!(q[0].expressions[0].feature, "color");
assert_eq!(q[0].expressions[0].value, None);
}
#[test]
fn matches_width_and_type() {
let ctx = Context::new().media_type("screen").width(640.0);
assert!(matches("screen and (min-width: 600px)", &ctx));
assert!(!matches("screen and (min-width: 800px)", &ctx));
assert!(matches("screen and (max-width: 700px)", &ctx));
assert!(matches("(width: 640px)", &ctx)); assert!(!matches("print", &ctx)); assert!(matches("screen", &ctx)); }
#[test]
fn comma_is_or() {
let ctx = Context::new().media_type("screen").width(500.0);
assert!(matches("(min-width: 800px), (max-width: 600px)", &ctx));
}
#[test]
fn length_units_convert_like_css_mediaquery() {
assert!(matches("(min-width: 30em)", &Context::new().width(481.0)));
assert!(!matches("(min-width: 30em)", &Context::new().width(479.0)));
assert!(matches("(min-width: 1in)", &Context::new().width(96.0)));
assert!(matches("(min-width: 2.54cm)", &Context::new().width(96.0)));
}
#[test]
fn resolution_units() {
assert!(matches(
"(min-resolution: 2dppx)",
&Context::new().resolution(192.0)
));
assert!(!matches(
"(min-resolution: 2dppx)",
&Context::new().resolution(96.0)
));
assert!(matches(
"(min-resolution: 100dpi)",
&Context::new().resolution(150.0)
));
}
#[test]
fn aspect_ratio_is_decimal() {
assert!(matches(
"(min-aspect-ratio: 4/3)",
&Context::new().aspect_ratio(16.0 / 9.0)
));
assert!(!matches(
"(min-aspect-ratio: 16/9)",
&Context::new().aspect_ratio(4.0 / 3.0)
));
}
#[test]
fn orientation_must_be_explicit() {
assert!(!matches(
"(orientation: landscape)",
&Context::new().width(800.0).height(600.0)
));
assert!(matches(
"(orientation: landscape)",
&Context::new().orientation("landscape")
));
assert!(matches(
"(orientation: LANDSCAPE)",
&Context::new().orientation("landscape")
)); }
#[test]
fn missing_or_zero_values_do_not_match() {
assert!(!matches(
"(min-width: 100px)",
&Context::new().media_type("screen")
));
assert!(!matches("(min-width: 0px)", &Context::new().width(0.0)));
}
#[test]
fn not_logic_matches_css_mediaquery() {
let ctx = Context::new().media_type("screen").width(500.0);
assert!(!matches("not screen and (min-width: 600px)", &ctx));
let screen = Context::new().media_type("screen").width(500.0);
assert!(matches("not print and (min-width: 600px)", &screen));
assert!(!matches(
"not all and (min-width: 800px)",
&Context::new().width(500.0)
));
}
#[test]
fn head_parsing_matches_reference() {
let q = parse("notscreen");
assert!(q[0].inverse && q[0].media_type == "screen");
let q = parse("onlyscreen");
assert!(!q[0].inverse && q[0].media_type == "screen");
assert_eq!(parse("only")[0].media_type, "only");
assert!(!parse("only")[0].inverse);
assert_eq!(parse("not")[0].media_type, "not");
assert!(!parse("not")[0].inverse);
assert_eq!(parse("screen) and (color)")[0].media_type, "screen");
assert_eq!(parse(")")[0].media_type, "all");
assert!(matches("onlyscreen", &Context::new().media_type("screen")));
assert!(!matches("only", &Context::new().media_type("screen")));
}
#[test]
fn unit_quirks_match_reference() {
assert!(matches("(min-width: 10IN)", &Context::new().width(200.0)));
assert!(!matches("(min-width: 10in)", &Context::new().width(200.0)));
assert!(matches("(max-width: 1e3px)", &Context::new().width(500.0)));
assert!(!matches("(min-width: 1e3px)", &Context::new().width(500.0)));
}
#[test]
fn count_features() {
assert!(matches("(min-color: 4)", &Context::new().color(8)));
assert!(!matches("(min-color: 4)", &Context::new().color(2)));
assert!(matches("(monochrome)", &Context::new().monochrome(1))); assert!(!matches("(monochrome)", &Context::new().monochrome(2)));
}