# frozen_string_literal: true

RSpec.describe MiniCSS::Serializer do
  it "serializes a declaration" do
    style = "foo { a:b }"
    ast = MiniCSS.parse(style)
    dump = MiniCSS.serialize(ast)
    expect(dump).to eq "foo{a:b;}"
  end

  it "serialize multiple declarations" do
    style = "foo { a:b } bar { c:d }"
    ast = MiniCSS.parse(style)
    dump = MiniCSS.serialize(ast)
    expect(dump).to eq "foo{a:b;}bar{c:d;}"
  end

  it "reserialize a CSS file" do
    ast = File.open(fixture_path("vito_io.css")) { MiniCSS.parse(it) }
    MiniCSS.serialize(ast)
  end

  it "deserializes and reserializes strings with whitespaces" do
    style = "div { content: \" \"; }"
    ast = MiniCSS.parse(style)
    dump = MiniCSS.serialize(ast)
    expect(dump).to eq "div{content:\" \";}"
  end

  it "deserializes and reserializes @-rules" do
    style = <<~CSS
      @font-face {
        font-family: "MyFont";
        src: url("font.woff2");
      }

      @import "normalize.css";
    CSS

    ast = MiniCSS.parse(style)
    dump = MiniCSS.serialize(ast)
    expect(dump).to eq "@font-face{font-family:\"MyFont\";src:url(\"font.woff2\");}@import \"normalize.css\";"
  end

  it "deserializes and reserializes @-rules with dimensions in prelude" do
    style = "@media (max-width: 600px) { div { color: blue; } }"
    ast = MiniCSS.parse(style)
    dump = MiniCSS.serialize(ast)
    expect(dump).to eq "@media (max-width: 600px){div{color:blue;}}"
  end

  it "deserializes and reserializes nested rules" do
    style = ".base { & .value { color: blue; } }"
    ast = MiniCSS.parse(style)
    dump = MiniCSS.serialize(ast)
    expect(dump).to eq ".base{& .value {color:blue;}}"
  end
end
