forked from rails/sprockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_processor_utils.rb
353 lines (288 loc) · 10.4 KB
/
test_processor_utils.rb
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# frozen_string_literal: true
require 'minitest/autorun'
require 'sprockets/processor_utils'
require 'sprockets/cache'
require 'sprockets/coffee_script_processor'
require 'sprockets/uglifier_compressor'
require 'sprockets_test'
class TestCallingProcessors < Sprockets::TestCase
def setup
@env = Sprockets::Environment.new
@env.append_path fixture_path('default')
end
def test_charset_supersedes_default_when_nil
processor = Proc.new do |input|
{data: input[:data], charset: nil}
end
@env.register_processor('image/png' , processor)
asset = @env['troll.png']
assert_nil asset.charset
ensure
@env.unregister_preprocessor('image/png' , processor)
end
def test_charset_supersedes_default
processor = Proc.new do |input|
{data: input[:data], charset: 'foo'}
end
@env.register_processor('image/png' , processor)
asset = @env['troll.png']
assert_equal 'foo', asset.charset
ensure
@env.unregister_preprocessor('image/png' , processor)
end
end
class TestProcessorUtils < MiniTest::Test
include Sprockets::ProcessorUtils
class Processor
def initialize(cache_key = nil, &block)
@cache_key, @proc = cache_key, block
end
attr_reader :cache_key
def call(*args)
@proc.call(*args)
end
end
def setup
@env = Sprockets::Environment.new
@env.append_path File.expand_path("../fixtures", __FILE__)
end
def test_call_nothing
a = proc {}
input = { data: " " }.freeze
assert result = call_processor(a, input)
assert_equal " ", result[:data]
end
def test_call_function
a = proc { |input| { data: input[:data] + ",a" } }
input = { data: " " }.freeze
assert result = call_processor(a, input)
assert_equal " ,a", result[:data]
end
def test_call_single_function
a = proc { |input| { data: input[:data] + ",a" } }
input = { data: " " }.freeze
assert result = call_processors([a], input)
assert_equal " ,a", result[:data]
end
def test_call_hash_return
a = proc { |input| { data: input[:data] + ",a" } }
b = proc { |input| { data: input[:data] + ",b" } }
input = { data: " " }.freeze
assert result = call_processors([b, a], input)
assert_equal " ,a,b", result[:data]
end
def test_call_string_return
a = proc { |input| input[:data] + ",a" }
b = proc { |input| input[:data] + ",b" }
input = { data: " " }.freeze
assert result = call_processors([b, a], input)
assert_equal " ,a,b", result[:data]
end
def test_call_noop_return
a = proc { |input| input[:data] + ",a" }
b = proc { |input| nil }
input = { data: " " }.freeze
assert result = call_processors([a, b], input)
assert_equal " ,a", result[:data]
assert result = call_processors([b, a], input)
assert_equal " ,a", result[:data]
end
def test_call_metadata
a = proc { |input| { a: true } }
b = proc { |input| { b: true } }
input = {}
assert result = call_processors([a, b], input)
assert result[:a]
assert result[:b]
end
def test_call_metadata_merge
a = proc { |input| { trace: input[:metadata][:trace] + [:a] } }
b = proc { |input| { trace: input[:metadata][:trace] + [:b] } }
input = { metadata: { trace: [] }.freeze }.freeze
assert result = call_processors([b, a], input)
assert_equal [:a, :b], result[:trace]
end
def test_compose_nothing
a = compose_processors()
input = { data: " " }.freeze
assert result = a.call(input)
assert_equal " ", result[:data]
end
def test_compose_single_function
a = proc { |input| { data: input[:data] + ",a" } }
b = compose_processors(a)
input = { data: " " }.freeze
assert result = b.call(input)
assert_equal " ,a", result[:data]
end
def test_compose_hash_return
a = proc { |input| { data: input[:data] + ",a" } }
b = proc { |input| { data: input[:data] + ",b" } }
c = compose_processors(b, a)
input = { data: " " }.freeze
assert result = c.call(input)
assert_equal " ,a,b", result[:data]
end
def test_compose_string_return
a = proc { |input| input[:data] + ",a" }
b = proc { |input| input[:data] + ",b" }
c = compose_processors(b, a)
input = { data: " " }.freeze
assert result = c.call(input)
assert_equal " ,a,b", result[:data]
end
def test_compose_noop_return
a = proc { |input| input[:data] + ",a" }
b = proc { |input| nil }
c = compose_processors(a, b)
d = compose_processors(b, a)
input = { data: " " }.freeze
assert result = c.call(input)
assert_equal " ,a", result[:data]
assert result = d.call(input)
assert_equal " ,a", result[:data]
end
def test_compose_metadata
a = proc { |input| { a: true } }
b = proc { |input| { b: true } }
c = compose_processors(a, b)
input = {}
assert result = c.call(input)
assert result[:a]
assert result[:b]
end
def test_compose_metadata_merge
a = proc { |input| { trace: input[:metadata][:trace] + [:a] } }
b = proc { |input| { trace: input[:metadata][:trace] + [:b] } }
c = compose_processors(b, a)
input = { metadata: { trace: [] }.freeze }.freeze
assert result = c.call(input)
assert_equal [:a, :b], result[:trace]
end
def test_multiple_functional_compose
a = proc { |input| { data: input[:data] + ",a" } }
b = proc { |input| { data: input[:data] + ",b" } }
c = proc { |input| { data: input[:data] + ",c" } }
d = proc { |input| { data: input[:data] + ",d" } }
e = compose_processors(d, compose_processors(c, compose_processors(b, compose_processors(a))))
input = { data: " " }.freeze
assert result = e.call(input)
assert_equal " ,a,b,c,d", result[:data]
end
def test_multiple_functional_compose_metadata
a = proc { |input| { trace: input[:metadata][:trace] + [:a] } }
b = proc { |input| { trace: input[:metadata][:trace] + [:b] } }
c = proc { |input| { trace: input[:metadata][:trace] + [:c] } }
d = proc { |input| { trace: input[:metadata][:trace] + [:d] } }
e = compose_processors(d, compose_processors(c, compose_processors(b, compose_processors(a))))
input = { metadata: { trace: [].freeze }.freeze }.freeze
assert result = e.call(input)
assert_equal [:a, :b, :c, :d], result[:trace]
end
def test_multiple_array_compose
a = proc { |input| { data: input[:data] + ",a" } }
b = proc { |input| { data: input[:data] + ",b" } }
c = proc { |input| { data: input[:data] + ",c" } }
d = proc { |input| { data: input[:data] + ",d" } }
e = compose_processors(d, c, b, a)
input = { data: " " }
assert result = e.call(input)
assert_equal " ,a,b,c,d", result[:data]
end
def test_multiple_array_compose_metadata
a = proc { |input| { trace: input[:metadata][:trace] + [:a] } }
b = proc { |input| { trace: input[:metadata][:trace] + [:b] } }
c = proc { |input| { trace: input[:metadata][:trace] + [:c] } }
d = proc { |input| { trace: input[:metadata][:trace] + [:d] } }
e = compose_processors(d, c, b, a)
input = { metadata: { trace: [].freeze }.freeze }.freeze
assert result = e.call(input)
assert_equal [:a, :b, :c, :d], result[:trace]
end
def test_compose_coffee_and_uglifier
processor = compose_processors(Sprockets::UglifierCompressor, Sprockets::CoffeeScriptProcessor)
input = {
environment: @env,
load_path: File.expand_path("../fixtures", __FILE__),
filename: File.expand_path("../fixtures/compose.coffee", __FILE__),
content_type: 'application/javascript',
data: "self.square = (n) -> n * n",
cache: Sprockets::Cache.new
}.freeze
assert result = processor.call(input)
assert_match "self.square=function", result[:data]
end
def test_bad_processor_return_type
a = proc { |input| Object.new }
b = compose_processors(a)
input = { data: " " }.freeze
assert_raises(TypeError) do
b.call(input)
end
end
def test_compose_class_processors
a = Processor.new { |input| { data: input[:data] + ",a" } }
b = Processor.new { |input| { data: input[:data] + ",b" } }
c = compose_processors(b, a)
input = { data: " " }.freeze
assert result = c.call(input)
assert_equal " ,a,b", result[:data]
end
def test_compose_processors_cache_keys
a = Processor.new("a")
b = Processor.new("b")
c = compose_processors(b, a)
assert_equal "a", a.cache_key
assert_equal "b", b.cache_key
assert_equal ["b", "a"], c.cache_key
end
def test_compose_processors_missing_cache_keys
a = Processor.new("a")
b = proc {}
c = Processor.new("c")
e = compose_processors(c, b, a)
assert_equal "a", a.cache_key
assert_equal "c", c.cache_key
assert_equal ["c", nil, "a"], e.cache_key
end
def test_multiple_array_compose_cache_keys
a = Processor.new("a")
b = Processor.new("b")
c = Processor.new("c")
d = Processor.new("d")
e = compose_processors(d, c, b, a)
assert_equal "a", a.cache_key
assert_equal "b", b.cache_key
assert_equal "c", c.cache_key
assert_equal "d", d.cache_key
assert_equal ["d", "c", "b", "a"], e.cache_key
end
def test_multiple_functional_compose_cache_keys
a = Processor.new("a")
b = Processor.new("b")
c = Processor.new("c")
d = Processor.new("d")
e = compose_processors(d, compose_processors(c, compose_processors(b, compose_processors(a))))
assert_equal "a", a.cache_key
assert_equal "b", b.cache_key
assert_equal "c", c.cache_key
assert_equal "d", d.cache_key
assert_equal ["d", ["c", ["b", ["a"]]]], e.cache_key
end
def test_validate_processor_result
validate_processor_result!({data: "hello"})
validate_processor_result!({data: "hello", foo: nil})
validate_processor_result!({data: "hello", foo: 1})
validate_processor_result!({data: "hello", foo: "bye"})
validate_processor_result!({data: "hello", foo: :bye})
validate_processor_result!({data: "hello", foo: [1, 2, 3]})
validate_processor_result!({data: "hello", foo: Set.new([1, 2, 3])})
validate_processor_result!({data: "hello", foo: {bar: 1}})
my_string = Class.new(String)
assert_raises(TypeError) { validate_processor_result!(nil) }
assert_raises(TypeError) { validate_processor_result!({}) }
assert_raises(TypeError) { validate_processor_result!({data: 123}) }
assert_raises(TypeError) { validate_processor_result!({data: "hello", "foo" => 1}) }
assert_raises(TypeError) { validate_processor_result!({data: my_string.new("hello")}) }
end
end