-
Notifications
You must be signed in to change notification settings - Fork 789
/
test_erb_processor.rb
94 lines (78 loc) · 2.57 KB
/
test_erb_processor.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
require 'minitest/autorun'
require 'sprockets'
require 'sprockets/cache'
require 'sprockets/erb_processor'
class TestERBProcessor < Minitest::Test
def uri_path(path)
path = '/' + path if path[1] == ':' # Windows path / drive letter
path
end
def tset_compile_js_erb_template
environment = Sprockets::Environment.new
input = {
environment: environment,
filename: "foo.js.erb",
content_type: 'application/javascript',
data: "var data = <%= JSON.generate({foo: true}) %>;",
metadata: {},
cache: Sprockets::Cache.new
}
output = "var data = {\"foo\":true};"
assert_equal output, Sprockets::ERBProcessor.call(input)[:data]
end
def test_compile_erb_template_with_depend_on_call
root = File.expand_path("../fixtures", __FILE__)
environment = Sprockets::Environment.new(root)
environment.append_path 'default'
path = "#{root}/default/gallery.js"
input = {
environment: environment,
filename: "foo.js.erb",
content_type: 'application/javascript',
data: "<%= depend_on('#{path}') %>\nvar data = 'DATA';",
metadata: {},
cache: Sprockets::Cache.new
}
output = "var data = 'DATA';"
result = Sprockets::ERBProcessor.call(input)
assert_equal output, result[:data]
assert_equal "file-digest://#{uri_path(path)}", result[:dependencies].first
end
def test_compile_erb_template_with_depend_on_call_outside_load_paths
root = File.expand_path("../fixtures", __FILE__)
environment = Sprockets::Environment.new(root)
environment.append_path 'default'
path = "#{root}/asset/application.js"
assert File.exist?(path)
input = {
environment: environment,
filename: "foo.js.erb",
content_type: 'application/javascript',
data: "<%= depend_on('#{path}') %>\nvar data = 'DATA';",
metadata: {},
cache: Sprockets::Cache.new
}
output = "var data = 'DATA';"
result = Sprockets::ERBProcessor.call(input)
assert_equal output, result[:data]
assert_equal "file-digest://#{uri_path(path)}", result[:dependencies].first
end
def test_pass_custom_erb_helpers_to_template
environment = Sprockets::Environment.new
template = Sprockets::ERBProcessor.new do
def foo
:bar
end
end
input = {
environment: environment,
filename: "foo.js.erb",
content_type: 'application/javascript',
data: "var foo = <%= foo %>;",
metadata: {},
cache: Sprockets::Cache.new
}
output = "var foo = bar;"
assert_equal output, template.call(input)[:data]
end
end