sass-embedded 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "test_helper"
3
+ require_relative 'test_helper'
4
4
 
5
5
  module Sass
6
6
  class CustomImporterTest < MiniTest::Test
@@ -11,6 +11,7 @@ module Sass
11
11
  end
12
12
 
13
13
  def teardown
14
+ @compiler.close
14
15
  end
15
16
 
16
17
  def render(data, importer)
@@ -18,143 +19,144 @@ module Sass
18
19
  end
19
20
 
20
21
  def test_custom_importer_works
21
- temp_file("fonts.scss", ".font { color: $var1; }")
22
+ temp_file('fonts.scss', '.font { color: $var1; }')
22
23
 
23
- data = <<SCSS
24
- @import "styles";
25
- @import "fonts";
26
- SCSS
24
+ data = <<~SCSS
25
+ @import "styles";
26
+ @import "fonts";
27
+ SCSS
27
28
 
28
29
  output = render(data, [
29
- lambda { |url, prev|
30
- if url =~ /styles/
31
- { contents: "$var1: #000; .hi { color: $var1; }" }
32
- end
30
+ lambda { |url, _prev|
31
+ { contents: '$var1: #000; .hi { color: $var1; }' } if url =~ /styles/
32
+ }
33
+ ])
34
+
35
+ assert_equal <<~CSS.chomp, output
36
+ .hi {
37
+ color: #000;
33
38
  }
34
- ])
35
39
 
36
- assert_equal <<CSS.chomp, output
37
- .hi {
38
- color: #000;
39
- }
40
-
41
- .font {
42
- color: #000;
43
- }
44
- CSS
40
+ .font {
41
+ color: #000;
42
+ }
43
+ CSS
45
44
  end
46
45
 
47
46
  def test_custom_importer_works_with_empty_contents
48
47
  output = render("@import 'fake.scss';", [
49
- lambda { |url, prev|
50
- { contents: "" }
51
- }
52
- ])
48
+ lambda { |_url, _prev|
49
+ { contents: '' }
50
+ }
51
+ ])
53
52
 
54
- assert_equal "", output
53
+ assert_equal '', output
55
54
  end
56
55
 
57
56
  def test_custom_importer_works_with_file
58
- temp_file("test.scss", ".test { color: #000; }")
57
+ temp_file('test.scss', '.test { color: #000; }')
59
58
 
60
59
  output = render("@import 'fake.scss';", [
61
- lambda { |url, prev|
62
- { file: File.absolute_path("test.scss") }
60
+ lambda { |_url, _prev|
61
+ { file: File.absolute_path('test.scss') }
62
+ }
63
+ ])
64
+
65
+ assert_equal <<~CSS.chomp, output
66
+ .test {
67
+ color: #000;
63
68
  }
64
- ])
65
-
66
- assert_equal <<CSS.chomp, output
67
- .test {
68
- color: #000;
69
- }
70
- CSS
69
+ CSS
71
70
  end
72
71
 
73
72
  def test_custom_importer_comes_after_local_file
74
- temp_file("test.scss", ".test { color: #000; }")
73
+ temp_file('test.scss', '.test { color: #000; }')
75
74
 
76
75
  output = render("@import 'test.scss';", [
77
- lambda { |url, prev|
78
- return { contents: '.h1 { color: #fff; }' }
76
+ lambda { |_url, _prev|
77
+ return { contents: '.h1 { color: #fff; }' }
78
+ }
79
+ ])
80
+
81
+ assert_equal <<~CSS.chomp, output
82
+ .test {
83
+ color: #000;
79
84
  }
80
- ])
81
-
82
- assert_equal <<CSS.chomp, output
83
- .test {
84
- color: #000;
85
- }
86
- CSS
85
+ CSS
87
86
  end
88
87
 
89
88
  def test_custom_importer_that_does_not_resolve
90
89
  assert_raises(CompilationError) do
91
- output = render("@import 'test.scss';", [
92
- lambda { |url, prev|
93
- return nil
94
- }
95
- ])
90
+ render("@import 'test.scss';", [
91
+ lambda { |_url, _prev|
92
+ return nil
93
+ }
94
+ ])
96
95
  end
97
96
  end
98
97
 
99
98
  def test_custom_importer_that_returns_error
100
99
  assert_raises(CompilationError) do
101
- output = render("@import 'test.scss';", [
102
- lambda { |url, prev|
103
- IOError.new "test error"
104
- }
105
- ])
100
+ render("@import 'test.scss';", [
101
+ lambda { |_url, _prev|
102
+ IOError.new 'test error'
103
+ }
104
+ ])
106
105
  end
107
106
  end
108
107
 
109
108
  def test_custom_importer_that_raises_error
110
109
  assert_raises(CompilationError) do
111
- output = render("@import 'test.scss';", [
112
- lambda { |url, prev|
113
- raise IOError.new "test error"
114
- }
115
- ])
110
+ render("@import 'test.scss';", [
111
+ lambda { |_url, _prev|
112
+ raise IOError, 'test error'
113
+ }
114
+ ])
116
115
  end
117
116
  end
118
117
 
119
118
  def test_parent_path_is_accessible
120
119
  output = @compiler.render({
121
- data: "@import 'parent.scss';",
122
- file: "import-parent-filename.scss",
123
- importer: [
124
- lambda { |url, prev|
125
- { contents: ".#{prev} { color: red; }" }
126
- }
127
- ]})[:css]
128
-
129
- assert_equal <<CSS.chomp, output
130
- .import-parent-filename.scss {
131
- color: red;
132
- }
133
- CSS
120
+ data: "@import 'parent.scss';",
121
+ file: 'import-parent-filename.scss',
122
+ importer: [
123
+ lambda { |_url, prev|
124
+ { contents: ".#{prev} { color: red; }" }
125
+ }
126
+ ]
127
+ })[:css]
128
+
129
+ assert_equal <<~CSS.chomp, output
130
+ .import-parent-filename.scss {
131
+ color: red;
132
+ }
133
+ CSS
134
134
  end
135
135
 
136
136
  def test_call_compiler_importer
137
137
  output = @compiler.render({
138
- data: "@import 'parent.scss';",
139
- importer: [
140
- lambda { |url, prev|
141
- {
142
- contents: @compiler.render({
143
- data: "@import 'parent-parent.scss'",
144
- importer: [
145
- lambda { |url, prev|
146
- { contents: 'h1 { color: black; }' }
147
- }
148
- ]})[:css]
149
- }
150
- }
151
- ]})[:css]
152
-
153
- assert_equal <<CSS.chomp, output
154
- h1 {
155
- color: black;
156
- }
157
- CSS
138
+ data: "@import 'parent.scss';",
139
+ importer: [
140
+ lambda { |_url, _prev|
141
+ {
142
+ contents: @compiler.render({
143
+ data: "@import 'parent-parent.scss'",
144
+ importer: [
145
+ lambda { |_url, _prev|
146
+ { contents: 'h1 { color: black; }' }
147
+ }
148
+ ]
149
+ })[:css]
150
+ }
151
+ }
152
+ ]
153
+ })[:css]
154
+
155
+ assert_equal <<~CSS.chomp, output
156
+ h1 {
157
+ color: black;
158
+ }
159
+ CSS
158
160
  end
159
161
  end
160
162
  end
data/test/error_test.rb CHANGED
@@ -1,33 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "test_helper"
3
+ require_relative 'test_helper'
4
4
 
5
5
  module Sass
6
6
  class ErrorTest < MiniTest::Test
7
-
8
7
  def setup
9
8
  @compiler = Embedded::Compiler.new
10
9
  end
11
10
 
12
11
  def teardown
12
+ @compiler.close
13
13
  end
14
14
 
15
15
  def test_first_backtrace_is_sass
16
- begin
17
- template = <<-SCSS
18
- .foo {
19
- baz: bang;
20
- padding top: 10px;
21
- }
16
+ template = <<~SCSS
17
+ .foo {
18
+ baz: bang;
19
+ padding top: 10px;
20
+ }
22
21
  SCSS
23
22
 
24
- @compiler.render({
25
- data: template,
26
- })
27
- rescue Sass::CompilationError => err
28
- expected = "stdin:3:20"
29
- assert_equal expected, err.backtrace.first
30
- end
23
+ @compiler.render({
24
+ data: template
25
+ })
26
+ rescue Sass::CompilationError => e
27
+ expected = 'stdin:3:20'
28
+ assert_equal expected, e.backtrace.first
31
29
  end
32
30
  end
33
31
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "test_helper"
4
- require "stringio"
3
+ require_relative 'test_helper'
5
4
 
6
5
  module Sass
7
6
  class FunctionsTest < MiniTest::Test
@@ -10,142 +9,160 @@ module Sass
10
9
  end
11
10
 
12
11
  def teardown
12
+ @compiler.close
13
13
  end
14
14
 
15
15
  def render(sass)
16
16
  @compiler.render({
17
- data: sass,
18
- functions: {
19
- 'javascript_path($path)': lambda { |path|
20
- path.string.text = "/js/#{path.string.text}"
21
- path
22
- },
23
- 'stylesheet_path($path)': lambda { |path|
24
- path.string.text = "/css/#{path.string.text}"
25
- path
26
- },
27
- 'sass_return_path($path)': lambda { |path|
28
- path
29
- },
30
- 'no_return_path($path)': lambda { |path|
31
- Sass::EmbeddedProtocol::Value.new(
32
- :singleton => Sass::EmbeddedProtocol::SingletonValue::NULL
33
- )
34
- },
35
- 'optional_arguments($path, $optional: null)': lambda { |path, optional|
36
- Sass::EmbeddedProtocol::Value.new(
37
- :string => Sass::EmbeddedProtocol::Value::String.new(
38
- :text => "#{path.string.text}/#{optional.singleton == :NULL ? "bar" : optional.string.text}",
39
- :quoted => true
40
- )
41
- )
42
- },
43
- 'function_that_raises_errors()': lambda {
44
- raise StandardError, "Intentional wrong thing happened somewhere inside the custom function"
45
- },
46
- 'nice_color_argument($color)': lambda { |color|
47
- color
48
- },
49
- 'returns_a_color()': lambda {
50
- Sass::EmbeddedProtocol::Value.new(
51
- :rgb_color => Sass::EmbeddedProtocol::Value::RgbColor.new(
52
- :red => 0,
53
- :green => 0,
54
- :blue => 0,
55
- :alpha => 1
56
- )
57
- )
58
- },
59
- 'returns_a_number()': lambda {
60
- Sass::EmbeddedProtocol::Value.new(
61
- :number => Sass::EmbeddedProtocol::Value::Number.new(
62
- :value => -312,
63
- :numerators => ['rem']
64
- )
65
- )
66
- },
67
- 'returns_a_bool()': lambda {
68
- Sass::EmbeddedProtocol::Value.new(
69
- :singleton => Sass::EmbeddedProtocol::SingletonValue::TRUE
70
- )
71
- },
72
- 'inspect_bool($argument)': lambda { |argument|
73
- raise StandardError.new "passed value is not a Sass::EmbeddedProtocol::SingletonValue::TRUE or Sass::EmbeddedProtocol::SingletonValue::FALSE" unless argument&.singleton == :TRUE || argument.singleton == :FALSE
74
- argument
75
- },
76
- 'inspect_number($argument)': lambda { |argument|
77
- raise StandardError.new "passed value is not a Sass::EmbeddedProtocol::Value::Number" unless argument&.number&.is_a? Sass::EmbeddedProtocol::Value::Number
78
- argument
79
- },
80
- 'inspect_map($argument)': lambda { |argument|
81
- raise StandardError.new "passed value is not a Sass::EmbeddedProtocol::Value::Map" unless argument&.map&.is_a? Sass::EmbeddedProtocol::Value::Map
82
- argument
83
- },
84
- 'inspect_list($argument)': lambda { |argument|
85
- raise StandardError.new "passed value is not a Sass::EmbeddedProtocol::Value::List" unless argument&.list&.is_a? Sass::EmbeddedProtocol::Value::List
86
- argument
87
- },
88
- 'returns_sass_value()': lambda {
89
- Sass::EmbeddedProtocol::Value.new(
90
- :rgb_color => Sass::EmbeddedProtocol::Value::RgbColor.new(
91
- :red => 0,
92
- :green => 0,
93
- :blue => 0,
94
- :alpha => 1
95
- )
96
- )
97
- },
98
- 'returns_sass_map()': lambda {
99
- Sass::EmbeddedProtocol::Value.new(
100
- :map => Sass::EmbeddedProtocol::Value::Map.new(
101
- :entries => [
102
- Sass::EmbeddedProtocol::Value::Map::Entry.new(
103
- :key => Sass::EmbeddedProtocol::Value.new(
104
- :string => Sass::EmbeddedProtocol::Value::String.new(
105
- :text => "color",
106
- :quoted => true
107
- )
108
- ),
109
- :value => Sass::EmbeddedProtocol::Value.new(
110
- :rgb_color => Sass::EmbeddedProtocol::Value::RgbColor.new(
111
- :red => 0,
112
- :green => 0,
113
- :blue => 0,
114
- :alpha => 1
115
- )
116
- )
117
- )
118
- ]
119
- )
120
- )
121
- },
122
- 'returns_sass_list()': lambda {
123
- Sass::EmbeddedProtocol::Value.new(
124
- :list => Sass::EmbeddedProtocol::Value::List.new(
125
- :separator => Sass::EmbeddedProtocol::ListSeparator::COMMA,
126
- :has_brackets => true,
127
- :contents => [
128
- Sass::EmbeddedProtocol::Value.new(
129
- :number => Sass::EmbeddedProtocol::Value::Number.new(
130
- :value => 10
131
- )
132
- ),
133
- Sass::EmbeddedProtocol::Value.new(
134
- :number => Sass::EmbeddedProtocol::Value::Number.new(
135
- :value => 20
136
- )
137
- ),
138
- Sass::EmbeddedProtocol::Value.new(
139
- :number => Sass::EmbeddedProtocol::Value::Number.new(
140
- :value => 30
141
- )
142
- ),
143
- ]
144
- )
145
- )
146
- }
147
- }
148
- })[:css]
17
+ data: sass,
18
+ functions: {
19
+ 'javascript_path($path)': lambda { |path|
20
+ path.string.text = "/js/#{path.string.text}"
21
+ path
22
+ },
23
+ 'stylesheet_path($path)': lambda { |path|
24
+ path.string.text = "/css/#{path.string.text}"
25
+ path
26
+ },
27
+ 'sass_return_path($path)': lambda { |path|
28
+ path
29
+ },
30
+ 'no_return_path($path)': lambda { |_path|
31
+ Sass::EmbeddedProtocol::Value.new(
32
+ singleton: Sass::EmbeddedProtocol::SingletonValue::NULL
33
+ )
34
+ },
35
+ 'optional_arguments($path, $optional: null)': lambda { |path, optional|
36
+ Sass::EmbeddedProtocol::Value.new(
37
+ string: Sass::EmbeddedProtocol::Value::String.new(
38
+ text: "#{path.string.text}/#{optional.singleton == :NULL ? 'bar' : optional.string.text}",
39
+ quoted: true
40
+ )
41
+ )
42
+ },
43
+ 'function_that_raises_errors()': lambda {
44
+ raise StandardError,
45
+ 'Intentional wrong thing happened somewhere inside the custom function'
46
+ },
47
+ 'nice_color_argument($color)': lambda { |color|
48
+ color
49
+ },
50
+ 'returns_a_color()': lambda {
51
+ Sass::EmbeddedProtocol::Value.new(
52
+ rgb_color: Sass::EmbeddedProtocol::Value::RgbColor.new(
53
+ red: 0,
54
+ green: 0,
55
+ blue: 0,
56
+ alpha: 1
57
+ )
58
+ )
59
+ },
60
+ 'returns_a_number()': lambda {
61
+ Sass::EmbeddedProtocol::Value.new(
62
+ number: Sass::EmbeddedProtocol::Value::Number.new(
63
+ value: -312,
64
+ numerators: ['rem']
65
+ )
66
+ )
67
+ },
68
+ 'returns_a_bool()': lambda {
69
+ Sass::EmbeddedProtocol::Value.new(
70
+ singleton: Sass::EmbeddedProtocol::SingletonValue::TRUE
71
+ )
72
+ },
73
+ 'inspect_bool($argument)': lambda { |argument|
74
+ unless argument&.singleton == :TRUE || argument.singleton == :FALSE
75
+ raise StandardError,
76
+ 'passed value is not a Sass::EmbeddedProtocol::SingletonValue::TRUE or Sass::EmbeddedProtocol::SingletonValue::FALSE'
77
+ end
78
+
79
+ argument
80
+ },
81
+ 'inspect_number($argument)': lambda { |argument|
82
+ unless argument&.number.is_a? Sass::EmbeddedProtocol::Value::Number
83
+ raise StandardError,
84
+ 'passed value is not a Sass::EmbeddedProtocol::Value::Number'
85
+ end
86
+
87
+ argument
88
+ },
89
+ 'inspect_map($argument)': lambda { |argument|
90
+ unless argument&.map.is_a? Sass::EmbeddedProtocol::Value::Map
91
+ raise StandardError,
92
+ 'passed value is not a Sass::EmbeddedProtocol::Value::Map'
93
+ end
94
+
95
+ argument
96
+ },
97
+ 'inspect_list($argument)': lambda { |argument|
98
+ unless argument&.list.is_a? Sass::EmbeddedProtocol::Value::List
99
+ raise StandardError,
100
+ 'passed value is not a Sass::EmbeddedProtocol::Value::List'
101
+ end
102
+
103
+ argument
104
+ },
105
+ 'returns_sass_value()': lambda {
106
+ Sass::EmbeddedProtocol::Value.new(
107
+ rgb_color: Sass::EmbeddedProtocol::Value::RgbColor.new(
108
+ red: 0,
109
+ green: 0,
110
+ blue: 0,
111
+ alpha: 1
112
+ )
113
+ )
114
+ },
115
+ 'returns_sass_map()': lambda {
116
+ Sass::EmbeddedProtocol::Value.new(
117
+ map: Sass::EmbeddedProtocol::Value::Map.new(
118
+ entries: [
119
+ Sass::EmbeddedProtocol::Value::Map::Entry.new(
120
+ key: Sass::EmbeddedProtocol::Value.new(
121
+ string: Sass::EmbeddedProtocol::Value::String.new(
122
+ text: 'color',
123
+ quoted: true
124
+ )
125
+ ),
126
+ value: Sass::EmbeddedProtocol::Value.new(
127
+ rgb_color: Sass::EmbeddedProtocol::Value::RgbColor.new(
128
+ red: 0,
129
+ green: 0,
130
+ blue: 0,
131
+ alpha: 1
132
+ )
133
+ )
134
+ )
135
+ ]
136
+ )
137
+ )
138
+ },
139
+ 'returns_sass_list()': lambda {
140
+ Sass::EmbeddedProtocol::Value.new(
141
+ list: Sass::EmbeddedProtocol::Value::List.new(
142
+ separator: Sass::EmbeddedProtocol::ListSeparator::COMMA,
143
+ has_brackets: true,
144
+ contents: [
145
+ Sass::EmbeddedProtocol::Value.new(
146
+ number: Sass::EmbeddedProtocol::Value::Number.new(
147
+ value: 10
148
+ )
149
+ ),
150
+ Sass::EmbeddedProtocol::Value.new(
151
+ number: Sass::EmbeddedProtocol::Value::Number.new(
152
+ value: 20
153
+ )
154
+ ),
155
+ Sass::EmbeddedProtocol::Value.new(
156
+ number: Sass::EmbeddedProtocol::Value::Number.new(
157
+ value: 30
158
+ )
159
+ )
160
+ ]
161
+ )
162
+ )
163
+ }
164
+ }
165
+ })[:css]
149
166
  end
150
167
 
151
168
  def test_functions_may_return_sass_string_type
@@ -253,8 +270,8 @@ module Sass
253
270
  end
254
271
 
255
272
  def test_function_with_error
256
- exception = assert_raises(Sass::CompilationError) do
257
- render("div {url: function_that_raises_errors();}")
273
+ assert_raises(Sass::CompilationError) do
274
+ render('div {url: function_that_raises_errors();}')
258
275
  end
259
276
  end
260
277
 
@@ -301,28 +318,27 @@ module Sass
301
318
  end
302
319
 
303
320
  def test_concurrency
304
- skip 'ProtocolError: Bad state: Future already completed'
305
321
  10.times do
306
322
  threads = []
307
- 2.times do |i|
323
+ 10.times do |i|
308
324
  threads << Thread.new(i) do |id|
309
325
  output = @compiler.render({
310
- data: "div { url: test-function() }",
311
- functions: {
312
- 'test_function()': lambda {
313
- Sass::EmbeddedProtocol::Value.new(
314
- :string => Sass::EmbeddedProtocol::Value::String.new(
315
- :text => "{test_key1: 'test_value', test_key2: #{id}}",
316
- :quoted => true
317
- )
318
- )
319
- }
320
- }
321
- })[:css]
322
- assert_match /test_key1/, output
323
- assert_match /test_key2/, output
324
- assert_match /test_value/, output
325
- assert_match /#{id}/, output
326
+ data: 'div { url: test-function() }',
327
+ functions: {
328
+ 'test_function()': lambda {
329
+ Sass::EmbeddedProtocol::Value.new(
330
+ string: Sass::EmbeddedProtocol::Value::String.new(
331
+ text: "{test_key1: 'test_value', test_key2: #{id}}",
332
+ quoted: true
333
+ )
334
+ )
335
+ }
336
+ }
337
+ })[:css]
338
+ assert_match(/test_key1/, output)
339
+ assert_match(/test_key2/, output)
340
+ assert_match(/test_value/, output)
341
+ assert_match(/#{id}/, output)
326
342
  end
327
343
  end
328
344
  threads.each(&:join)
@@ -331,32 +347,32 @@ module Sass
331
347
 
332
348
  def test_pass_custom_functions_as_a_parameter
333
349
  output = @compiler.render({
334
- data: "div { url: test-function(); }",
335
- functions: {
336
- 'test_function()': lambda {
337
- Sass::EmbeddedProtocol::Value.new(
338
- :string => Sass::EmbeddedProtocol::Value::String.new(
339
- :text => "custom_function",
340
- :quoted => true
341
- )
342
- )
343
- }
344
- }
345
- })[:css]
350
+ data: 'div { url: test-function(); }',
351
+ functions: {
352
+ 'test_function()': lambda {
353
+ Sass::EmbeddedProtocol::Value.new(
354
+ string: Sass::EmbeddedProtocol::Value::String.new(
355
+ text: 'custom_function',
356
+ quoted: true
357
+ )
358
+ )
359
+ }
360
+ }
361
+ })[:css]
346
362
 
347
- assert_match /custom_function/, output
363
+ assert_match(/custom_function/, output)
348
364
  end
349
365
 
350
366
  def test_pass_incompatible_type_to_custom_functions
351
367
  assert_raises(CompilationError) do
352
- output = @compiler.render({
353
- data: "div { url: test-function(); }",
354
- functions: {
355
- 'test_function()': lambda {
356
- Class.new
357
- }
358
- }
359
- })[:css]
368
+ @compiler.render({
369
+ data: 'div { url: test-function(); }',
370
+ functions: {
371
+ 'test_function()': lambda {
372
+ Class.new
373
+ }
374
+ }
375
+ })[:css]
360
376
  end
361
377
  end
362
378
 
@@ -364,8 +380,8 @@ module Sass
364
380
 
365
381
  def assert_sass(sass, expected_css)
366
382
  output = render(sass)
367
- assert_equal expected_css.strip.gsub!(/\s+/, " "), # poor man's String#squish
368
- output.strip.gsub!(/\s+/, " ")
383
+ assert_equal expected_css.strip.gsub!(/\s+/, ' '), # poor man's String#squish
384
+ output.strip.gsub!(/\s+/, ' ')
369
385
  end
370
386
 
371
387
  def stderr_output