This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
forked from rails/sprockets
-
Notifications
You must be signed in to change notification settings - Fork 24
/
test_directive_processor.rb
131 lines (112 loc) · 4.34 KB
/
test_directive_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
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
require "sprockets_test"
class DirectiveProcessorTest < Sprockets::TestCase
test "parsing double-slash comments" do
directive_parser("double_slash").tap do |parser|
assert_equal "// Header\n//\n\n\n//\n\n(function() {\n})();\n", parser.processed_source
assert_equal directives, parser.directives
end
end
test "parsing hash comments" do
directive_parser("hash").tap do |parser|
assert_equal "# Header\n#\n\n\n#\n\n(->)()\n", parser.processed_source
assert_equal directives, parser.directives
end
end
test "parsing slash-star comments" do
directive_parser("slash_star").tap do |parser|
assert_equal "/* Header\n *\n\n\n *\n\n */\n\n(function() {\n})();\n", parser.processed_source
assert_equal directives, parser.directives
end
end
test "parsing single line slash-star comments" do
directive_parser("slash_star_single").tap do |parser|
assert_equal "\n\n(function() {\n})();\n", parser.processed_source
assert_equal [[1, "require", "a"]], parser.directives
end
end
test "parsing triple-hash comments" do
directive_parser("triple_hash").tap do |parser|
assert_equal "###\nHeader\n\n\n\n\n\n###\n\n(->)()\n", parser.processed_source
assert_equal directives(1), parser.directives
end
end
test "header comment without directives is unmodified" do
directive_parser("comment_without_directives").tap do |parser|
assert_equal "/*\n * Comment\n */\n\n(function() {\n})();\n", parser.processed_source
assert_equal [], parser.directives
end
end
test "directives in comment after header are not parsed" do
directive_parser("directives_after_header").tap do |parser|
assert_equal "/*\n * Header\n\n */\n\n\n\n\n\n\n/* Not a directive */\n\n(function() {\n})();\n\n/*= require e */\n", parser.processed_source
assert_equal [
[3, "require", "a"],
[6, "require", "b"],
[7, "require", "c"],
[9, "require", "d"]
], parser.directives
end
end
test "headers must occur at the beginning of the file" do
directive_parser("code_before_comment").tap do |parser|
assert_equal "", parser.processed_header
assert_equal directive_fixture("code_before_comment"), parser.processed_source
end
end
test "no header" do
directive_parser("no_header").tap do |parser|
assert_equal directive_fixture("no_header"), parser.processed_source
assert_equal [], parser.directives
end
end
test "directive word splitting" do
directive_parser("directive_word_splitting").tap do |parser|
assert_equal [
[1, "require"],
[2, "require", "two"],
[3, "require", "two", "three"],
[4, "require", "two three"],
[6, "require", "seven"]
], parser.directives
end
end
test "space between = and directive word" do
directive_parser("space_between_directive_word").tap do |parser|
assert_equal "var foo;\n", parser.processed_source
assert_equal [[1, "require", "foo"]], parser.directives
end
end
test "documentation headers" do
directive_parser("documentation").tap do |parser|
assert_equal "\n//\n// = Foo\n//\n// == Examples\n//\n// Foo.bar()\n// => \"baz\"var Foo;\n", parser.processed_source
assert_equal [[1, "require", "project"]], parser.directives
end
end
def directive_parser(fixture_name)
Sprockets::DirectiveProcessor.new(fixture_path("directives/#{fixture_name}"))
end
def directive_fixture(name)
fixture("directives/#{name}")
end
def directives(offset = 0)
[[3 + offset, "require", "a"], [4 + offset, "require", "b"], [6 + offset, "require", "c"]]
end
end
class TestCustomDirectiveProcessor < Sprockets::TestCase
def setup
@env = Sprockets::Environment.new
@env.append_path(fixture_path('context'))
end
class TestDirectiveProcessor < Sprockets::DirectiveProcessor
def process_require_glob_directive(glob)
Dir["#{pathname.dirname}/#{glob}"].sort.each do |filename|
context.require_asset(filename)
end
end
end
test "custom processor using Context#sprockets_resolve and Context#sprockets_depend" do
@env.unregister_preprocessor('application/javascript', Sprockets::DirectiveProcessor)
@env.register_preprocessor('application/javascript', TestDirectiveProcessor)
assert_equal "var Foo = {};\n\n", @env["require_glob.js"].to_s
end
end