rustyle_css/testing/
mod.rs1use crate::css::Color;
6
7pub struct CssAssertions;
9
10impl CssAssertions {
11 pub fn assert_contains_property(css: &str, property: &str) -> bool {
13 css.contains(property)
14 }
15
16 pub fn assert_contains_value(css: &str, value: &str) -> bool {
18 css.contains(value)
19 }
20
21 pub fn assert_contains_selector(css: &str, selector: &str) -> bool {
23 css.contains(selector)
24 }
25
26 pub fn validate_syntax(css: &str) -> bool {
28 let mut brace_count = 0;
30 for ch in css.chars() {
31 match ch {
32 '{' => brace_count += 1,
33 '}' => {
34 brace_count -= 1;
35 if brace_count < 0 {
36 return false;
37 }
38 }
39 _ => {}
40 }
41 }
42 brace_count == 0
43 }
44}
45
46pub struct StyleValidator;
48
49impl StyleValidator {
50 pub fn validate_contrast(foreground: &Color, background: &Color) -> bool {
52 use crate::a11y::contrast::meets_wcag_aa;
53 meets_wcag_aa(foreground, background, false)
54 }
55
56 pub fn validate_required_properties(css: &str, required: &[&str]) -> Vec<String> {
58 let mut missing = Vec::new();
59 for prop in required {
60 if !css.contains(prop) {
61 missing.push(prop.to_string());
62 }
63 }
64 missing
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_css_assertions() {
74 let css = "color: red; background-color: blue;";
75 assert!(CssAssertions::assert_contains_property(css, "color"));
76 assert!(CssAssertions::assert_contains_value(css, "red"));
77 }
78
79 #[test]
80 fn test_validate_syntax() {
81 assert!(CssAssertions::validate_syntax(".test { color: red; }"));
82 assert!(!CssAssertions::validate_syntax(".test { color: red; "));
83 }
84}