File: sass_converter_spec.rb

package info (click to toggle)
ruby-jekyll-sass-converter 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 312 kB
  • sloc: ruby: 818; sh: 22; makefile: 6
file content (130 lines) | stat: -rw-r--r-- 3,503 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# frozen_string_literal: true

require "spec_helper"

describe(Jekyll::Converters::Sass) do
  let(:site) do
    Jekyll::Site.new(site_configuration)
  end

  let(:sass_converter) do
    sass_converter_instance(site)
  end

  let(:content) do
    <<~SASS
      // tl;dr some sass
      $font-stack: Helvetica, sans-serif
      body
        font-family: $font-stack
        font-color: fuschia
    SASS
  end

  let(:expanded_css_output) do
    <<~CSS
      body {
        font-family: Helvetica, sans-serif;
        font-color: fuschia;
      }
    CSS
  end

  let(:compact_css_output) do
    <<~CSS
      body { font-family: Helvetica, sans-serif; font-color: fuschia; }
    CSS
  end

  let(:invalid_content) do
    <<~SASS
      font-family: $font-stack;
    SASS
  end

  def converter(overrides = {})
    sass_converter_instance(site).dup.tap do |obj|
      obj.instance_variable_get(:@config)["sass"] = overrides
    end
  end

  context "matching file extensions" do
    it "does not match .scss files" do
      expect(converter.matches(".scss")).to be_falsey
    end

    it "matches .sass files" do
      expect(converter.matches(".sass")).to be_truthy
    end
  end

  context "converting sass" do
    it "produces CSS" do
      expected = sass_embedded? ? expanded_css_output : compact_css_output
      expect(converter.convert(content)).to eql(expected)
    end

    it "includes the syntax error line in the syntax error message" do
      expected = if sass_embedded?
                   %r!Expected newline!i
                 else
                   error_message = 'Error: Invalid CSS after "f": expected 1 selector or at-rule.'
                   %r!\A#{error_message} was "font-family: \$font-"\s+on line 1:1 of stdin!
                 end

      expect do
        converter.convert(invalid_content)
      end.to raise_error(Jekyll::Converters::Scss::SyntaxError, expected)
    end

    it "removes byte order mark from compressed Sass" do
      result = converter("style" => :compressed).convert(%(a\n  content: "\uF015"))
      expect(result).to eql(%(a{content:"\uF015"}\n))
      expect(result.bytes.to_a[0..2]).not_to eql([0xEF, 0xBB, 0xBF])
    end

    it "does not include the charset unless asked to" do
      overrides = { "style" => :compressed, "add_charset" => true }
      result = converter(overrides).convert(%(a\n  content: "\uF015"))
      expect(result).to eql(%(@charset "UTF-8";a{content:"\uF015"}\n))
      expect(result.bytes.to_a[0..2]).not_to eql([0xEF, 0xBB, 0xBF])
    end
  end

  context "in a site with a collection labelled 'pages'" do
    let(:site) do
      make_site(
        "source"      => File.expand_path("pages-collection", __dir__),
        "sass"        => {
          "style" => :expanded,
        },
        "collections" => {
          "pages" => {
            "output" => true,
          },
        }
      )
    end

    it "produces CSS without raising errors" do
      expect { site.process }.not_to raise_error
      expect(sass_converter.convert(content)).to eql(expanded_css_output)
    end
  end

  context "in a site nested inside directory with square brackets" do
    let(:site) do
      make_site(
        "source" => File.expand_path("[alpha]beta", __dir__),
        "sass"   => {
          "style" => :expanded,
        }
      )
    end

    it "produces CSS without raising errors" do
      expect { site.process }.not_to raise_error
      expect(sass_converter.convert(content)).to eql(expanded_css_output)
    end
  end
end