File: css_variable_test.rb

package info (click to toggle)
ruby-sass 3.7.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,396 kB
  • sloc: ruby: 32,443; sh: 26; makefile: 25
file content (237 lines) | stat: -rwxr-xr-x 3,815 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

require File.dirname(__FILE__) + '/../test_helper'
require 'sass/engine'

# Most CSS variable tests are in sass-spec, but a few relate to formatting or
# conversion and so belong here.
class CssVariableTest < Minitest::Test
  def test_folded_inline_whitespace
    assert_variable_value "foo bar baz", "foo    bar        baz"
    assert_variable_value "foo bar", "foo \t   bar"
  end

  def test_folded_multiline_whitespace
    # We don't want to reformat newlines in nested and expanded mode, so we just
    # remove trailing whitespace before them.
    assert_equal <<CSS, render(<<SCSS)
.foo {
  --multiline: foo
    bar; }
CSS
.foo {
  --multiline: foo\s
    bar;
}
SCSS

    assert_equal <<CSS, render(<<SCSS)
.foo {
  --multiline: foo


    bar; }
CSS
.foo {
  --multiline: foo\s


    bar;
}
SCSS

    assert_equal <<CSS, render(<<SCSS, style: :expanded)
.foo {
  --multiline: foo
    bar;
}
CSS
.foo {
  --multiline: foo\s
    bar;
}
SCSS

    assert_equal <<CSS, render(<<SCSS, style: :expanded)
.foo {
  --multiline: foo


    bar;
}
CSS
.foo {
  --multiline: foo\s


    bar;
}
SCSS

    # In compact and compressed mode, we fold all whitespace around newlines
    # together.
    assert_equal <<CSS, render(<<SCSS, style: :compact)
.foo { --multiline: foo bar; }
CSS
.foo {
  --multiline: foo\s
    bar;
}
SCSS

    assert_equal <<CSS, render(<<SCSS, style: :compact)
.foo { --multiline: foo bar; }
CSS
.foo {
  --multiline: foo\s


    bar;
}
SCSS

    assert_equal <<CSS, render(<<SCSS, style: :compressed)
.foo{--multiline: foo bar}
CSS
.foo {
  --multiline: foo\s
    bar;
}
SCSS

    assert_equal <<CSS, render(<<SCSS, style: :compressed)
.foo{--multiline: foo bar}
CSS
.foo {
  --multiline: foo\s


    bar;
}
SCSS
  end

  # Conversion.

  def test_static_values_convert
    assert_converts <<SASS, <<SCSS
.foo
  --bar: baz
SASS
.foo {
  --bar: baz;
}
SCSS

    assert_converts <<SASS, <<SCSS
.foo
  --bar: [({{([!;])}})]
SASS
.foo {
  --bar: [({{([!;])}})];
}
SCSS

    assert_converts <<SASS, <<SCSS
.foo
  --bar: {a: b; c: d}
SASS
.foo {
  --bar: {a: b; c: d};
}
SCSS
  end

  def test_dynamic_values_convert
    assert_converts <<SASS, <<SCSS
.foo
  --bar: baz \#{bang} qux
SASS
.foo {
  --bar: baz \#{bang} qux;
}
SCSS

    assert_converts <<SASS, <<SCSS
.foo
  --bar: "baz \#{bang} qux"
SASS
.foo {
  --bar: "baz \#{bang} qux";
}
SCSS
  end

  def test_multiline_value_converts
    assert_scss_to_scss <<SCSS
.foo {
  --bar: {
    a: b;
    c: d;
  };
}
SCSS

    assert_scss_to_sass <<SASS, <<SCSS
.foo
  --bar: {     a: b;     c: d;   }
SASS
.foo {
  --bar: {
    a: b;
    c: d;
  };
}
SCSS
  end

  private

  def assert_variable_value(expected, source)
    expected = <<CSS
x {
  --variable: #{expected}; }
CSS

    assert_equal expected, render_variable(source)
    assert_equal expected, render_variable(source, syntax: :sass)
  end

  def render_variable(source, syntax: :scss)
    render(syntax == :scss ? <<SCSS : <<SASS, :syntax => syntax)
x {
  --variable: #{source};
}
SCSS
x
  --variable: #{source}
SASS
  end

  def render(sass, options = {})
    options[:syntax] ||= :scss
    options[:cache] = false
    munge_filename options
    Sass::Engine.new(sass, options).render
  end

  def resolve(str, opts = {}, environment = env)
    munge_filename opts
    val = eval(str, opts, environment)
    assert_kind_of Sass::Script::Value::Base, val
    val.options = opts
    val.is_a?(Sass::Script::Value::String) ? val.value : val.to_s
  end

  def eval(str, opts = {}, environment = env)
    munge_filename opts
    Sass::Script.parse(str, opts.delete(:line) || 1,
      opts.delete(:offset) || 0, opts).
      perform(Sass::Environment.new(environment, opts))
  end

  def env(hash = {})
    env = Sass::Environment.new
    hash.each {|k, v| env.set_var(k, v)}
    env
  end
end