From 197129aa6dfe0d47d58da187f99d2cc5faea3263 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 5 Jan 2020 12:22:10 -0500 Subject: [PATCH] Test multiline and deeply nested styles --- src/format.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/src/format.rs b/src/format.rs index 432037c..583b010 100644 --- a/src/format.rs +++ b/src/format.rs @@ -2,6 +2,10 @@ use std::io::{self, Write}; use crate::{RuleSet, Stmt, Style, StyleSheet}; +/// Pretty print SCSS +/// +/// The result should be an exact copy of the SCSS input +/// Empty rules are included pub(crate) struct PrettyPrinter { buf: W, scope: usize, @@ -51,6 +55,64 @@ impl PrettyPrinter { } } +/// Used solely in debugging to ensure that selectors aren't the issue +pub(crate) struct CssPrettyPrinter { + buf: W, + scope: usize, +} + +impl CssPrettyPrinter { + pub(crate) fn new(buf: W) -> Self { + CssPrettyPrinter { buf, scope: 0 } + } + + fn pretty_print_stmt(&mut self, stmt: &Stmt) -> io::Result<()> { + let padding = vec![' '; self.scope * 2].iter().collect::(); + match stmt { + Stmt::RuleSet(RuleSet { + selector, + rules, + super_selector, + }) => { + writeln!( + self.buf, + "{}{} {{", + padding, + super_selector.clone().zip(selector.clone()) + )?; + self.scope += 1; + for rule in rules { + self.pretty_print_stmt(rule)?; + } + writeln!(self.buf, "{}}}", padding)?; + self.scope -= 1; + } + Stmt::Style(Style { property, value }) => { + writeln!( + self.buf, + "{}{}: {};", + padding, + property, + value + .iter() + .map(ToString::to_string) + .collect::>() + .join(" ") + )?; + } + } + Ok(()) + } + + pub(crate) fn pretty_print(&mut self, s: &StyleSheet) -> io::Result<()> { + for rule in &s.rules { + self.pretty_print_stmt(rule)?; + } + writeln!(self.buf)?; + Ok(()) + } +} + #[cfg(test)] mod test { use super::StyleSheet; @@ -120,6 +182,11 @@ mod test { test!(basic_style, "a {\n color: red;\n}\n"); test!(two_styles, "a {\n color: red;\n color: blue;\n}\n"); + test!( + multiline_style, + "a {\n color: red\n blue;\n}\n", + "a {\n color: red blue;\n}\n" + ); test!(hyphenated_style_property, "a {\n font-family: Arial;\n}\n"); test!(hyphenated_style_value, "a {\n color: Open-Sans;\n}\n"); test!( @@ -156,4 +223,27 @@ mod test { test!(unit_em, "a {\n height: 1em;\n}\n"); test!(unit_rem, "a {\n height: 1rem;\n}\n"); test!(unit_percent, "a {\n height: 1%;\n}\n"); + test!( + deeply_nested_selector, + "\ +a { + b { + c { + d { + e { + f { + g { + h { + i { + } + } + } + } + } + } + } + } +} +" + ); }