handlebars_assets 0.8.1 → 0.23.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ module HandlebarsAssets
4
+ class CompilingTest < ::MiniTest::Test
5
+
6
+ def teardown
7
+ HandlebarsAssets::Config.reset!
8
+ HandlebarsAssets::Handlebars.reset!
9
+ end
10
+
11
+ def test_custom_handlebars
12
+ source = "This is {{handlebars}}"
13
+
14
+ HandlebarsAssets::Config.compiler_path = File.expand_path '../../edge', __FILE__
15
+
16
+ compiled = Handlebars.precompile(source, HandlebarsAssets::Config.options)
17
+ assert_match /PRECOMPILE CALLED/, compiled
18
+ end
19
+
20
+ def test_patching_handlebars
21
+ source = "This is {{nested.handlebars}}"
22
+
23
+ HandlebarsAssets::Config.patch_path = File.expand_path '../../patch', __FILE__
24
+ HandlebarsAssets::Config.patch_files = ['patch.js']
25
+
26
+ compiled = Handlebars.precompile(source, HandlebarsAssets::Config.options)
27
+ assert_match /CALLED PATCH/, compiled
28
+ end
29
+ end
30
+ end
@@ -1,12 +1,12 @@
1
1
  require 'test_helper'
2
2
 
3
3
  module HandlebarsAssets
4
- class HamlbarsTest < Test::Unit::TestCase
4
+ class HamlbarsTest < ::Minitest::Test
5
5
  include SprocketsScope
6
6
  include CompilerSupport
7
7
 
8
8
  def compile_haml(source)
9
- Haml::Engine.new(source, HandlebarsAssets::Config.haml_options).render
9
+ (Haml::Engine.new(source, HandlebarsAssets::Config.haml_options).render).chomp
10
10
  end
11
11
 
12
12
  def teardown
@@ -20,7 +20,7 @@ module HandlebarsAssets
20
20
  scope = make_scope root, file
21
21
  source = "%p This is {{handlebars}}"
22
22
 
23
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
23
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
24
24
 
25
25
  assert_equal hbs_compiled('test_render', compile_haml(source)), template.render(scope, {})
26
26
  end
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+ require_relative 'shared/adapter_tests'
3
+
4
+ module HandlebarsAssets
5
+ class HandlebarsProcessorTest < Minitest::Test
6
+ include AdapterTests
7
+
8
+ def teardown
9
+ HandlebarsAssets::Config.reset!
10
+ HandlebarsAssets::Handlebars.reset!
11
+ end
12
+
13
+ def render_it(scope, source)
14
+ HandlebarsAssets::HandlebarsProcessor.call(filename: scope.pathname.to_s, data: source)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,199 @@
1
+ require 'test_helper'
2
+ require 'haml'
3
+ require 'slim'
4
+
5
+ module AdapterTests
6
+ include CompilerSupport
7
+ include SprocketsScope
8
+
9
+ def compile_haml(source)
10
+ ::Haml::Engine.new(source, HandlebarsAssets::Config.haml_options).render
11
+ end
12
+
13
+ def compile_slim(source)
14
+ ::Slim::Template.new(HandlebarsAssets::Config.slim_options) { source }.render
15
+ end
16
+
17
+ def test_render
18
+ root = '/myapp/app/assets/templates'
19
+ file = 'test_render.hbs'
20
+ scope = make_scope root, file
21
+ source = "This is {{handlebars}}"
22
+
23
+ assert_equal hbs_compiled('test_render', source), render_it(scope, source)
24
+ end
25
+
26
+ # Sprockets does not add nested root paths (i.e.
27
+ # app/assets/javascripts/templates is rooted at app/assets/javascripts)
28
+ def test_template_misnaming
29
+ root = '/myapp/app/assets/javascripts'
30
+ file = 'templates/test_template_misnaming.hbs'
31
+ scope = make_scope root, file
32
+ source = "This is {{handlebars}}"
33
+
34
+ assert_equal hbs_compiled('test_template_misnaming', source), render_it(scope, source)
35
+ end
36
+
37
+ def test_path_prefix
38
+ root = '/myapp/app/assets/javascripts'
39
+ file = 'app/templates/test_path_prefix.hbs'
40
+ scope = make_scope root, file
41
+ source = "This is {{handlebars}}"
42
+
43
+ HandlebarsAssets::Config.path_prefix = 'app/templates'
44
+
45
+ assert_equal hbs_compiled('test_path_prefix', source), render_it(scope, source)
46
+ end
47
+
48
+ def test_underscore_partials
49
+ root = '/myapp/app/assets/javascripts'
50
+ file1 = 'app/templates/_test_underscore.hbs'
51
+ scope1 = make_scope root, file1
52
+ file2 = 'app/templates/some/thing/_test_underscore.hbs'
53
+ scope2 = make_scope root, file2
54
+ source = "This is {{handlebars}}"
55
+
56
+ HandlebarsAssets::Config.path_prefix = 'app/templates'
57
+
58
+ assert_equal hbs_compiled_partial('_test_underscore', source), render_it(scope1, source)
59
+
60
+ assert_equal hbs_compiled_partial('some/thing/_test_underscore', source), render_it(scope2, source)
61
+ end
62
+
63
+ def test_chomped_underscore_partials
64
+ assert_equal HandlebarsAssets::Config.chomp_underscore_for_partials?, false
65
+
66
+ HandlebarsAssets::Config.chomp_underscore_for_partials = true
67
+ assert_equal HandlebarsAssets::Config.chomp_underscore_for_partials?, true
68
+
69
+ root = '/myapp/app/assets/javascripts'
70
+ file1 = 'app/templates/_test_underscore.hbs'
71
+ scope1 = make_scope root, file1
72
+ file2 = 'app/templates/some/thing/_test_underscore.hbs'
73
+ scope2 = make_scope root, file2
74
+ source = "This is {{handlebars}}"
75
+
76
+ HandlebarsAssets::Config.path_prefix = 'app/templates'
77
+
78
+ assert_equal hbs_compiled_partial('test_underscore', source), render_it(scope1, source)
79
+
80
+ assert_equal hbs_compiled_partial('some/thing/test_underscore', source), render_it(scope2, source)
81
+
82
+ end
83
+
84
+ def test_without_known_helpers_opt
85
+ root = '/myapp/app/assets/templates'
86
+ file = 'test_without_known.hbs'
87
+ scope = make_scope root, file
88
+ source = "{{#with author}}By {{first_name}} {{last_name}}{{/with}}"
89
+
90
+ assert_equal hbs_compiled('test_without_known', source), render_it(scope, source)
91
+ end
92
+
93
+ def test_known_helpers_opt
94
+ root = '/myapp/app/assets/templates'
95
+ file = 'test_known.hbs'
96
+ scope = make_scope root, file
97
+ source = "{{#with author}}By {{first_name}} {{last_name}}{{/with}}"
98
+
99
+ HandlebarsAssets::Config.known_helpers_only = true
100
+
101
+ assert_equal hbs_compiled('test_known', source), render_it(scope, source)
102
+ end
103
+
104
+ def test_with_custom_helpers
105
+ root = '/myapp/app/assets/templates'
106
+ file = 'test_custom_helper.hbs'
107
+ scope = make_scope root, file
108
+ source = "{{#custom author}}By {{first_name}} {{last_name}}{{/custom}}"
109
+
110
+ assert_equal hbs_compiled('test_custom_helper', source), render_it(scope, source)
111
+ end
112
+
113
+ def test_with_custom_known_helpers
114
+ root = '/myapp/app/assets/templates'
115
+ file = 'test_custom_known_helper.hbs'
116
+ scope = make_scope root, file
117
+ source = "{{#custom author}}By {{first_name}} {{last_name}}{{/custom}}"
118
+
119
+ HandlebarsAssets::Config.known_helpers_only = true
120
+ HandlebarsAssets::Config.known_helpers = %w(custom)
121
+
122
+ assert_equal hbs_compiled('test_custom_known_helper', source), render_it(scope, source)
123
+ end
124
+
125
+ def test_template_namespace
126
+ root = '/myapp/app/assets/javascripts/templates'
127
+ file = 'test_template_namespace.hbs'
128
+ scope = make_scope root, file
129
+ source = "This is {{handlebars}}"
130
+
131
+ HandlebarsAssets::Config.template_namespace = 'JST'
132
+
133
+ assert_equal hbs_compiled('test_template_namespace', source), render_it(scope, source)
134
+ end
135
+
136
+ def test_ember_render
137
+ root = '/myapp/app/assets/templates'
138
+ file = 'test_render.hbs'
139
+ scope = make_scope root, file
140
+ source = "This is {{handlebars}}"
141
+
142
+ HandlebarsAssets::Config.ember = true
143
+ HandlebarsAssets::Config.multiple_frameworks = false
144
+
145
+ expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("This is {{handlebars}}");};
146
+ assert_equal expected_compiled, render_it(scope, source)
147
+ end
148
+
149
+ def test_multiple_frameworks_with_ember_render
150
+ root = '/myapp/app/assets/templates'
151
+ non_ember = 'test_render.hbs'
152
+ non_ember_but_with_ember = 'test_member.hbs'
153
+ ember_ext_no_hbs = 'test_render.ember'
154
+ ember_ext = 'test_render.ember.hbs'
155
+ ember_with_haml = 'test_render.ember.hamlbars'
156
+ ember_with_slim = 'test_render.ember.slimbars'
157
+ ember_ext_with_erb = 'test_render.ember.hbs.erb'
158
+
159
+ HandlebarsAssets::Config.ember = true
160
+ HandlebarsAssets::Config.multiple_frameworks = true
161
+
162
+ # File without ember extension should compile to default namespace
163
+ scope = make_scope root, non_ember
164
+ source = "This is {{handlebars}}"
165
+ assert_equal hbs_compiled('test_render', source), render_it(scope, source)
166
+
167
+ # File without ember extension but with ember in it should compile to default namespace
168
+ scope = make_scope root, non_ember_but_with_ember
169
+ source = "This is {{handlebars}}"
170
+ assert_equal hbs_compiled('test_member', source), render_it(scope, source)
171
+
172
+ # File with ember extension should compile to ember specific namespace
173
+ expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("This is {{handlebars}}");};
174
+ scope = make_scope root, ember_ext_no_hbs
175
+ assert_equal expected_compiled, render_it(scope, source)
176
+
177
+ # File with ember and erb extension should compile to ember specific namespace
178
+ expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("This is {{handlebars}}");};
179
+ scope = make_scope root, ember_ext_with_erb
180
+ assert_equal expected_compiled, render_it(scope, source)
181
+
182
+ # File with ember.hbs extension should compile to ember specific namespace
183
+ expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("This is {{handlebars}}");};
184
+ scope = make_scope root, ember_ext
185
+ assert_equal expected_compiled, render_it(scope, source)
186
+
187
+ # File with ember.hamlbars extension should compile to ember specific namespace
188
+ expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("<p>This is {{handlebars}}</p>");};
189
+ scope = make_scope root, ember_with_haml
190
+ source = "%p This is {{handlebars}}"
191
+ assert_equal expected_compiled, render_it(scope, source)
192
+
193
+ # File with ember.slimbars extension should compile to ember specific namespace
194
+ expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("<p>This is {{handlebars}}</p>");};
195
+ source = "p This is {{handlebars}}"
196
+ scope = make_scope root, ember_with_slim
197
+ assert_equal expected_compiled, render_it(scope, source)
198
+ end
199
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ module HandlebarsAssets
4
+ class SlimbarsTest < ::Minitest::Test
5
+ include SprocketsScope
6
+ include CompilerSupport
7
+
8
+ def compile_slim(source)
9
+ Slim::Template.new(HandlebarsAssets::Config.slim_options) { source }.render
10
+ end
11
+
12
+ def teardown
13
+ HandlebarsAssets::Config.reset!
14
+ HandlebarsAssets::Handlebars.reset!
15
+ end
16
+
17
+ def test_render_slim
18
+ root = '/myapp/app/assets/templates'
19
+ file = 'test_render.slimbars'
20
+ scope = make_scope root, file
21
+ source = "p This is {{handlebars}}"
22
+
23
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
24
+
25
+ assert_equal hbs_compiled('test_render', compile_slim(source)), template.render(scope, {})
26
+ end
27
+ end
28
+ end
@@ -1,144 +1,18 @@
1
1
  require 'test_helper'
2
+ require_relative 'shared/adapter_tests'
2
3
 
3
4
  module HandlebarsAssets
4
- class TiltHandlebarsTest < Test::Unit::TestCase
5
- include CompilerSupport
6
- include SprocketsScope
5
+ class HandlebarsTemplateTest < Minitest::Test
6
+ include AdapterTests
7
7
 
8
8
  def teardown
9
9
  HandlebarsAssets::Config.reset!
10
10
  HandlebarsAssets::Handlebars.reset!
11
11
  end
12
12
 
13
- def test_render
14
- root = '/myapp/app/assets/templates'
15
- file = 'test_render.hbs'
16
- scope = make_scope root, file
17
- source = "This is {{handlebars}}"
18
-
19
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
20
-
21
- assert_equal hbs_compiled('test_render', source), template.render(scope, {})
22
- end
23
-
24
- # Sprockets does not add nested root paths (i.e.
25
- # app/assets/javascripts/templates is rooted at app/assets/javascripts)
26
- def test_template_misnaming
27
- root = '/myapp/app/assets/javascripts'
28
- file = 'templates/test_template_misnaming.hbs'
29
- scope = make_scope root, file
30
- source = "This is {{handlebars}}"
31
-
32
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
33
-
34
- assert_equal hbs_compiled('test_template_misnaming', source), template.render(scope, {})
35
- end
36
-
37
- def test_path_prefix
38
- root = '/myapp/app/assets/javascripts'
39
- file = 'app/templates/test_path_prefix.hbs'
40
- scope = make_scope root, file
41
- source = "This is {{handlebars}}"
42
-
43
- HandlebarsAssets::Config.path_prefix = 'app/templates'
44
-
45
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
46
-
47
- assert_equal hbs_compiled('test_path_prefix', source), template.render(scope, {})
48
- end
49
-
50
- def test_underscore_partials
51
- root = '/myapp/app/assets/javascripts'
52
- file1 = 'app/templates/_test_underscore.hbs'
53
- scope1 = make_scope root, file1
54
- file2 = 'app/templates/some/thing/_test_underscore.hbs'
55
- scope2 = make_scope root, file2
56
- source = "This is {{handlebars}}"
57
-
58
- HandlebarsAssets::Config.path_prefix = 'app/templates'
59
-
60
- template1 = HandlebarsAssets::TiltHandlebars.new(scope1.pathname.to_s) { source }
61
-
62
- assert_equal hbs_compiled_partial('_test_underscore', source), template1.render(scope1, {})
63
-
64
- template2 = HandlebarsAssets::TiltHandlebars.new(scope2.pathname.to_s) { source }
65
-
66
- assert_equal hbs_compiled_partial('_some_thing_test_underscore', source), template2.render(scope2, {})
67
- end
68
-
69
- def test_without_known_helpers_opt
70
- root = '/myapp/app/assets/templates'
71
- file = 'test_without_known.hbs'
72
- scope = make_scope root, file
73
- source = "{{#with author}}By {{first_name}} {{last_name}}{{/with}}"
74
-
75
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
76
-
77
- assert_equal hbs_compiled('test_without_known', source), template.render(scope, {})
78
- end
79
-
80
- def test_known_helpers_opt
81
- root = '/myapp/app/assets/templates'
82
- file = 'test_known.hbs'
83
- scope = make_scope root, file
84
- source = "{{#with author}}By {{first_name}} {{last_name}}{{/with}}"
85
-
86
- HandlebarsAssets::Config.known_helpers_only = true
87
-
88
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
89
-
90
- assert_equal hbs_compiled('test_known', source), template.render(scope, {})
91
- end
92
-
93
- def test_with_custom_helpers
94
- root = '/myapp/app/assets/templates'
95
- file = 'test_custom_helper.hbs'
96
- scope = make_scope root, file
97
- source = "{{#custom author}}By {{first_name}} {{last_name}}{{/custom}}"
98
-
99
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
100
-
101
- assert_equal hbs_compiled('test_custom_helper', source), template.render(scope, {})
102
- end
103
-
104
- def test_with_custom_known_helpers
105
- root = '/myapp/app/assets/templates'
106
- file = 'test_custom_known_helper.hbs'
107
- scope = make_scope root, file
108
- source = "{{#custom author}}By {{first_name}} {{last_name}}{{/custom}}"
109
-
110
- HandlebarsAssets::Config.known_helpers_only = true
111
- HandlebarsAssets::Config.known_helpers = %w(custom)
112
-
113
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
114
-
115
- assert_equal hbs_compiled('test_custom_known_helper', source), template.render(scope, {})
116
- end
117
-
118
- def test_template_namespace
119
- root = '/myapp/app/assets/javascripts'
120
- file = 'test_template_namespace.hbs'
121
- scope = make_scope root, file
122
- source = "This is {{handlebars}}"
123
-
124
- HandlebarsAssets::Config.template_namespace = 'JST'
125
-
126
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
127
-
128
- assert_equal hbs_compiled('test_template_namespace', source), template.render(scope, {})
129
- end
130
-
131
- def test_ember_render
132
- root = '/myapp/app/assets/templates'
133
- file = 'test_render.hbs'
134
- scope = make_scope root, file
135
- source = "This is {{handlebars}}"
136
-
137
- HandlebarsAssets::Config.ember = true
138
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
139
-
140
- expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("This is {{handlebars}}");};
141
- assert_equal expected_compiled, template.render(scope, {})
13
+ def render_it(scope, source)
14
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
15
+ template.render(scope, {})
142
16
  end
143
17
  end
144
18
  end
@@ -0,0 +1,10 @@
1
+ var originalLookup = Handlebars.JavaScriptCompiler.prototype.nameLookup;
2
+
3
+ Handlebars.JavaScriptCompiler.prototype.nameLookup = function(parent, name, type) {
4
+ if (type === 'context') {
5
+ return '"CALLED PATCH"';
6
+ }
7
+ else {
8
+ return originalLookup.call(this, parent, name, type);
9
+ }
10
+ };
data/test/test_helper.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require 'handlebars_assets'
2
2
  require 'handlebars_assets/config'
3
- require 'handlebars_assets/tilt_handlebars'
3
+ require 'handlebars_assets/handlebars_template'
4
4
  require 'handlebars_assets/handlebars'
5
5
 
6
- require 'test/unit'
6
+ require 'minitest/autorun'
7
7
 
8
8
  module SprocketsScope
9
9
  # Try to act like sprockets.
@@ -23,29 +23,29 @@ module CompilerSupport
23
23
 
24
24
  def compile_hbs(source)
25
25
  compiler_src = Pathname(HandlebarsAssets::Config.compiler_path).join(HandlebarsAssets::Config.compiler).read
26
- ExecJS.compile(compiler_src).call('Handlebars.precompile', source, HandlebarsAssets::Config.options)
26
+ ExecJS.compile("var window = {}; " + compiler_src).call('Handlebars.precompile', source, HandlebarsAssets::Config.options)
27
27
  end
28
28
 
29
29
  def hbs_compiled(template_name, source)
30
- compiled_hbs = compile_hbs(source)
30
+ compiled_hbs = compile_hbs(source).strip
31
31
  template_namespace = HandlebarsAssets::Config.template_namespace
32
32
 
33
- unindent <<-END_EXPECTED
34
- (function() {
35
- this.#{template_namespace} || (this.#{template_namespace} = {});
36
- this.#{template_namespace}[#{template_name.dump}] = Handlebars.template(#{compiled_hbs});
37
- return this.#{template_namespace}[#{template_name.dump}];
38
- }).call(this);
33
+ <<-END_EXPECTED
34
+ (function() {
35
+ this.#{template_namespace} || (this.#{template_namespace} = {});
36
+ this.#{template_namespace}[#{template_name.dump}] = Handlebars.template(#{compiled_hbs});
37
+ return this.#{template_namespace}[#{template_name.dump}];
38
+ }).call(this);
39
39
  END_EXPECTED
40
40
  end
41
41
 
42
42
  def hbs_compiled_partial(partial_name, source)
43
43
  compiled_hbs = compile_hbs(source)
44
44
 
45
- unindent <<-END_EXPECTED
46
- (function() {
47
- Handlebars.registerPartial(#{partial_name.dump}, Handlebars.template(#{compiled_hbs}));
48
- }).call(this);
45
+ <<-END_EXPECTED
46
+ (function() {
47
+ Handlebars.registerPartial(#{partial_name.dump}, Handlebars.template(#{compiled_hbs}));
48
+ }).call(this);
49
49
  END_EXPECTED
50
50
  end
51
51
  end
@@ -55,12 +55,15 @@ module HandlebarsAssets
55
55
  extend self
56
56
 
57
57
  def reset!
58
+ @chomp_underscore_for_partials = nil
58
59
  @compiler = nil
59
60
  @compiler_path = nil
60
61
  @haml_options = nil
61
62
  @known_helpers = nil
62
63
  @known_helpers_only = nil
63
64
  @options = nil
65
+ @patch_files = nil
66
+ @patch_path = nil
64
67
  @path_prefix = nil
65
68
  @template_namespace = nil
66
69
  @ember = nil
@@ -73,6 +76,7 @@ module HandlebarsAssets
73
76
  def self.reset!
74
77
  @context = nil
75
78
  @source = nil
79
+ @patch_path = nil
76
80
  @path = nil
77
81
  @assets_path = nil
78
82
  end