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
119 lines (101 loc) · 3.73 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
require "sprockets_test"
class DirectiveParserTest < Sprockets::TestCase
test "parsing double-slash comments" do
directive_parser("double_slash").tap do |parser|
assert_equal "// Header\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", 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(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 [["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", parser.processed_source
assert_equal directives, 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// =require \"x\"\n\n(function() {\n})();\n", parser.processed_source
assert_equal [], 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 [
["one"],
["one", "two"],
["one", "two", "three"],
["one", "two three"],
["six", "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 [["require", "foo"]], 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
[["require", "a"], ["require", "b"], ["require", "c"]]
end
end
class TestCustomDirectiveProcessor < Sprockets::TestCase
def setup
@env = Sprockets::Environment.new
@env.paths << fixture_path('context')
end
class TestDirectiveProcessor < Sprockets::DirectiveProcessor
def process_require_glob_directive(glob)
Dir["#{base_path}/#{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_format('.js', Sprockets::DirectiveProcessor)
@env.register_format('.js', TestDirectiveProcessor)
assert_equal "var Foo = {};\n\n", @env["require_glob.js"].to_s
end
end