tennpipes-helper 3.6.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +20 -0
  3. data/README.rdoc +239 -0
  4. data/Rakefile +1 -0
  5. data/lib/tennpipes-helper.rb +62 -0
  6. data/lib/tennpipes-helper/asset_tag_helpers.rb +394 -0
  7. data/lib/tennpipes-helper/form_builder/abstract_form_builder.rb +279 -0
  8. data/lib/tennpipes-helper/form_builder/standard_form_builder.rb +40 -0
  9. data/lib/tennpipes-helper/form_helpers.rb +639 -0
  10. data/lib/tennpipes-helper/form_helpers/errors.rb +138 -0
  11. data/lib/tennpipes-helper/form_helpers/options.rb +98 -0
  12. data/lib/tennpipes-helper/form_helpers/security.rb +70 -0
  13. data/lib/tennpipes-helper/format_helpers.rb +372 -0
  14. data/lib/tennpipes-helper/locale/cs.yml +103 -0
  15. data/lib/tennpipes-helper/locale/da.yml +91 -0
  16. data/lib/tennpipes-helper/locale/de.yml +81 -0
  17. data/lib/tennpipes-helper/locale/en.yml +103 -0
  18. data/lib/tennpipes-helper/locale/es.yml +103 -0
  19. data/lib/tennpipes-helper/locale/fr.yml +79 -0
  20. data/lib/tennpipes-helper/locale/hu.yml +103 -0
  21. data/lib/tennpipes-helper/locale/it.yml +89 -0
  22. data/lib/tennpipes-helper/locale/ja.yml +103 -0
  23. data/lib/tennpipes-helper/locale/lv.yml +103 -0
  24. data/lib/tennpipes-helper/locale/nl.yml +82 -0
  25. data/lib/tennpipes-helper/locale/no.yml +91 -0
  26. data/lib/tennpipes-helper/locale/pl.yml +95 -0
  27. data/lib/tennpipes-helper/locale/pt_br.yml +103 -0
  28. data/lib/tennpipes-helper/locale/ro.yml +103 -0
  29. data/lib/tennpipes-helper/locale/ru.yml +103 -0
  30. data/lib/tennpipes-helper/locale/sv.yml +103 -0
  31. data/lib/tennpipes-helper/locale/tr.yml +103 -0
  32. data/lib/tennpipes-helper/locale/uk.yml +103 -0
  33. data/lib/tennpipes-helper/locale/zh_cn.yml +103 -0
  34. data/lib/tennpipes-helper/locale/zh_tw.yml +103 -0
  35. data/lib/tennpipes-helper/number_helpers.rb +283 -0
  36. data/lib/tennpipes-helper/output_helpers.rb +226 -0
  37. data/lib/tennpipes-helper/output_helpers/abstract_handler.rb +61 -0
  38. data/lib/tennpipes-helper/output_helpers/erb_handler.rb +27 -0
  39. data/lib/tennpipes-helper/output_helpers/haml_handler.rb +25 -0
  40. data/lib/tennpipes-helper/output_helpers/slim_handler.rb +18 -0
  41. data/lib/tennpipes-helper/render_helpers.rb +63 -0
  42. data/lib/tennpipes-helper/tag_helpers.rb +294 -0
  43. data/lib/tennpipes-helper/translation_helpers.rb +36 -0
  44. data/lib/tennpipes/rendering.rb +369 -0
  45. data/lib/tennpipes/rendering/erb_template.rb +40 -0
  46. data/lib/tennpipes/rendering/erubis_template.rb +66 -0
  47. data/lib/tennpipes/rendering/haml_template.rb +26 -0
  48. data/lib/tennpipes/rendering/slim_template.rb +20 -0
  49. data/test/fixtures/apps/render.rb +25 -0
  50. data/test/fixtures/apps/views/article/comment/show.slim +1 -0
  51. data/test/fixtures/apps/views/blog/post.erb +1 -0
  52. data/test/fixtures/apps/views/layouts/specific.erb +1 -0
  53. data/test/fixtures/apps/views/test/post.erb +1 -0
  54. data/test/fixtures/layouts/layout.erb +1 -0
  55. data/test/fixtures/markup_app/app.rb +87 -0
  56. data/test/fixtures/markup_app/views/button_to.erb +8 -0
  57. data/test/fixtures/markup_app/views/button_to.haml +5 -0
  58. data/test/fixtures/markup_app/views/button_to.slim +6 -0
  59. data/test/fixtures/markup_app/views/capture_concat.erb +14 -0
  60. data/test/fixtures/markup_app/views/capture_concat.haml +12 -0
  61. data/test/fixtures/markup_app/views/capture_concat.slim +12 -0
  62. data/test/fixtures/markup_app/views/content_for.erb +23 -0
  63. data/test/fixtures/markup_app/views/content_for.haml +19 -0
  64. data/test/fixtures/markup_app/views/content_for.slim +19 -0
  65. data/test/fixtures/markup_app/views/content_tag.erb +13 -0
  66. data/test/fixtures/markup_app/views/content_tag.haml +11 -0
  67. data/test/fixtures/markup_app/views/content_tag.slim +11 -0
  68. data/test/fixtures/markup_app/views/current_engine.erb +5 -0
  69. data/test/fixtures/markup_app/views/current_engine.haml +5 -0
  70. data/test/fixtures/markup_app/views/current_engine.slim +5 -0
  71. data/test/fixtures/markup_app/views/fields_for.erb +20 -0
  72. data/test/fixtures/markup_app/views/fields_for.haml +15 -0
  73. data/test/fixtures/markup_app/views/fields_for.slim +15 -0
  74. data/test/fixtures/markup_app/views/form_for.erb +72 -0
  75. data/test/fixtures/markup_app/views/form_for.haml +59 -0
  76. data/test/fixtures/markup_app/views/form_for.slim +59 -0
  77. data/test/fixtures/markup_app/views/form_tag.erb +95 -0
  78. data/test/fixtures/markup_app/views/form_tag.haml +78 -0
  79. data/test/fixtures/markup_app/views/form_tag.slim +79 -0
  80. data/test/fixtures/markup_app/views/link_to.erb +5 -0
  81. data/test/fixtures/markup_app/views/link_to.haml +4 -0
  82. data/test/fixtures/markup_app/views/link_to.slim +4 -0
  83. data/test/fixtures/markup_app/views/mail_to.erb +3 -0
  84. data/test/fixtures/markup_app/views/mail_to.haml +3 -0
  85. data/test/fixtures/markup_app/views/mail_to.slim +3 -0
  86. data/test/fixtures/markup_app/views/meta_tag.erb +3 -0
  87. data/test/fixtures/markup_app/views/meta_tag.haml +3 -0
  88. data/test/fixtures/markup_app/views/meta_tag.slim +3 -0
  89. data/test/fixtures/markup_app/views/partials/_erb.erb +1 -0
  90. data/test/fixtures/markup_app/views/partials/_haml.haml +1 -0
  91. data/test/fixtures/markup_app/views/partials/_slim.slim +1 -0
  92. data/test/fixtures/markup_app/views/simple_partial.erb +1 -0
  93. data/test/fixtures/markup_app/views/simple_partial.haml +1 -0
  94. data/test/fixtures/markup_app/views/simple_partial.slim +1 -0
  95. data/test/fixtures/render_app/app.rb +110 -0
  96. data/test/fixtures/render_app/views/_deep.erb +3 -0
  97. data/test/fixtures/render_app/views/_deep.haml +2 -0
  98. data/test/fixtures/render_app/views/_deep.slim +2 -0
  99. data/test/fixtures/render_app/views/_partial_block_erb.erb +10 -0
  100. data/test/fixtures/render_app/views/_partial_block_haml.haml +7 -0
  101. data/test/fixtures/render_app/views/_partial_block_slim.slim +7 -0
  102. data/test/fixtures/render_app/views/_unsafe.html.builder +2 -0
  103. data/test/fixtures/render_app/views/_unsafe_object.html.builder +2 -0
  104. data/test/fixtures/render_app/views/current_engine.haml +5 -0
  105. data/test/fixtures/render_app/views/current_engines/_erb.erb +1 -0
  106. data/test/fixtures/render_app/views/current_engines/_haml.haml +1 -0
  107. data/test/fixtures/render_app/views/current_engines/_slim.slim +1 -0
  108. data/test/fixtures/render_app/views/dive_inner_erb.erb +3 -0
  109. data/test/fixtures/render_app/views/dive_inner_haml.haml +2 -0
  110. data/test/fixtures/render_app/views/dive_inner_slim.slim +2 -0
  111. data/test/fixtures/render_app/views/dive_outer_erb.erb +3 -0
  112. data/test/fixtures/render_app/views/dive_outer_haml.haml +2 -0
  113. data/test/fixtures/render_app/views/dive_outer_slim.slim +2 -0
  114. data/test/fixtures/render_app/views/double_capture_erb.erb +3 -0
  115. data/test/fixtures/render_app/views/double_capture_haml.haml +2 -0
  116. data/test/fixtures/render_app/views/double_capture_slim.slim +2 -0
  117. data/test/fixtures/render_app/views/erb/test.erb +1 -0
  118. data/test/fixtures/render_app/views/explicit_engine.haml +5 -0
  119. data/test/fixtures/render_app/views/haml/test.haml +1 -0
  120. data/test/fixtures/render_app/views/render_block_erb.erb +5 -0
  121. data/test/fixtures/render_app/views/render_block_haml.haml +4 -0
  122. data/test/fixtures/render_app/views/render_block_slim.slim +4 -0
  123. data/test/fixtures/render_app/views/ruby_block_capture_erb.erb +1 -0
  124. data/test/fixtures/render_app/views/ruby_block_capture_haml.haml +1 -0
  125. data/test/fixtures/render_app/views/ruby_block_capture_slim.slim +1 -0
  126. data/test/fixtures/render_app/views/template/_user.haml +7 -0
  127. data/test/fixtures/render_app/views/template/haml_template.haml +1 -0
  128. data/test/fixtures/render_app/views/template/some_template.haml +2 -0
  129. data/test/fixtures/render_app/views/wrong_capture_erb.erb +3 -0
  130. data/test/fixtures/render_app/views/wrong_capture_haml.haml +2 -0
  131. data/test/fixtures/render_app/views/wrong_capture_slim.slim +2 -0
  132. data/test/helper.rb +88 -0
  133. data/test/test_asset_tag_helpers.rb +401 -0
  134. data/test/test_form_builder.rb +1216 -0
  135. data/test/test_form_helpers.rb +1056 -0
  136. data/test/test_format_helpers.rb +251 -0
  137. data/test/test_helpers.rb +10 -0
  138. data/test/test_locale.rb +20 -0
  139. data/test/test_number_helpers.rb +142 -0
  140. data/test/test_output_helpers.rb +157 -0
  141. data/test/test_render_helpers.rb +225 -0
  142. data/test/test_rendering.rb +706 -0
  143. data/test/test_rendering_extensions.rb +14 -0
  144. data/test/test_tag_helpers.rb +131 -0
  145. metadata +299 -0
@@ -0,0 +1,110 @@
1
+ TENNPIPES_ROOT = File.dirname(__FILE__) unless defined? TENNPIPES_ROOT
2
+ RACK_ENV = 'test' unless defined? RACK_ENV
3
+
4
+ require 'tennpipes-base'
5
+
6
+ class RenderUser
7
+ attr_accessor :name
8
+ def initialize(name); @name = name; end
9
+ end
10
+
11
+ class RenderDemo < Tennpipes::Application
12
+ register Tennpipes::Helpers
13
+
14
+ configure do
15
+ set :logging, false
16
+ set :tennpipes_logging, false
17
+ set :environment, :test
18
+ end
19
+
20
+ # get current engines from partials
21
+ get '/current_engine' do
22
+ render :current_engine
23
+ end
24
+
25
+ # get current engines from explicit engine partials
26
+ get '/explicit_engine' do
27
+ render :explicit_engine
28
+ end
29
+
30
+ get '/double_capture_:ext' do
31
+ render "double_capture_#{params[:ext]}"
32
+ end
33
+
34
+ get '/wrong_capture_:ext' do
35
+ render "wrong_capture_#{params[:ext]}"
36
+ end
37
+
38
+ get '/ruby_block_capture_:ext' do
39
+ render "ruby_block_capture_#{params[:ext]}"
40
+ end
41
+
42
+ # partial with object
43
+ get '/partial/object' do
44
+ partial 'template/user', :object => RenderUser.new('John'), :locals => { :extra => "bar" }
45
+ end
46
+
47
+ # partial with collection
48
+ get '/partial/collection' do
49
+ partial 'template/user', :collection => [RenderUser.new('John'), RenderUser.new('Billy')], :locals => { :extra => "bar" }
50
+ end
51
+
52
+ # partial with collection and ext
53
+ get '/partial/collection.ext' do
54
+ partial 'template/user.haml', :collection => [RenderUser.new('John'), RenderUser.new('Billy')], :locals => { :extra => "bar" }
55
+ end
56
+
57
+ # partial with locals
58
+ get '/partial/locals' do
59
+ partial 'template/user', :locals => { :user => RenderUser.new('John'), :extra => "bar" }
60
+ end
61
+
62
+ # partial starting with forward slash
63
+ get '/partial/foward_slash' do
64
+ partial '/template/user', :object => RenderUser.new('John'), :locals => { :extra => "bar" }
65
+ end
66
+
67
+ # partial with unsafe engine
68
+ get '/partial/unsafe' do
69
+ block = params[:block] ? proc{ params[:block] } : nil
70
+ partial 'unsafe.html.builder', &block
71
+ end
72
+
73
+ get '/partial/unsafe_one' do
74
+ block = params[:block] ? proc{ params[:block] } : nil
75
+ partial 'unsafe_object', :object => 'Mary', &block
76
+ end
77
+
78
+ get '/partial/unsafe_many' do
79
+ block = params[:block] ? proc{ params[:block] } : nil
80
+ partial 'unsafe_object', :collection => ['John', 'Mary'], &block
81
+ end
82
+
83
+ get '/render_block_:ext' do
84
+ render "render_block_#{params[:ext]}" do
85
+ content_tag :div, 'go block!'
86
+ end
87
+ end
88
+
89
+ get '/partial_block_:ext' do
90
+ partial "partial_block_#{params[:ext]}" do
91
+ content_tag :div, 'go block!'
92
+ end
93
+ end
94
+
95
+ helpers do
96
+ def dive_helper(ext)
97
+ # @current_engine, save = nil
98
+ form_result = form_tag '/' do
99
+ render "dive_inner_#{ext}"
100
+ end
101
+ # @current_engine = save
102
+ content_tag('div', form_result, :class => 'wrapper')
103
+ end
104
+ end
105
+
106
+ get '/double_dive_:ext' do
107
+ @ext = params[:ext]
108
+ render "dive_outer_#{@ext}"
109
+ end
110
+ end
@@ -0,0 +1,3 @@
1
+ <div class="deep">
2
+ <%= yield %>
3
+ </div>
@@ -0,0 +1,2 @@
1
+ .deep
2
+ = yield
@@ -0,0 +1,2 @@
1
+ .deep
2
+ = yield
@@ -0,0 +1,10 @@
1
+ <h1>prefix</h1>
2
+ <div class="erb-block">
3
+ <%= yield %>
4
+ </div>
5
+ <div class="go-deep">
6
+ <% partial 'deep.erb' do %>
7
+ Done
8
+ <% end %>
9
+ </div>
10
+ <h3>postfix</h3>
@@ -0,0 +1,7 @@
1
+ %h1 prefix
2
+ .haml-block
3
+ = yield
4
+ .go-deep
5
+ = partial 'deep.haml' do
6
+ Done
7
+ %h3 postfix
@@ -0,0 +1,7 @@
1
+ h1 prefix
2
+ .slim-block
3
+ = yield
4
+ .go-deep
5
+ = partial 'deep.slim' do
6
+ | Done
7
+ h3 postfix
@@ -0,0 +1,2 @@
1
+ xml.h1 'User name is John'
2
+ xml.div{ |x| x << yield } if block_given?
@@ -0,0 +1,2 @@
1
+ xml.h1 "User name is #{unsafe_object}"
2
+ xml.div{ |x| x << yield } if block_given?
@@ -0,0 +1,5 @@
1
+ %p.start= current_engine
2
+ %p.haml= partial 'current_engines/haml'
3
+ %p.erb= partial 'current_engines/erb'
4
+ %p.slim= partial 'current_engines/slim'
5
+ %p.end= current_engine
@@ -0,0 +1 @@
1
+ <span><%= current_engine %></span>
@@ -0,0 +1 @@
1
+ %span=current_engine
@@ -0,0 +1 @@
1
+ span=current_engine
@@ -0,0 +1,3 @@
1
+ <div class=inner>
2
+ <div class=core></div>
3
+ </div>
@@ -0,0 +1,2 @@
1
+ .inner
2
+ .core
@@ -0,0 +1,2 @@
1
+ .inner
2
+ .core
@@ -0,0 +1,3 @@
1
+ <div class=outer>
2
+ <%= dive_helper(@ext) %>
3
+ </div>
@@ -0,0 +1,2 @@
1
+ .outer
2
+ = dive_helper(@ext)
@@ -0,0 +1,2 @@
1
+ .outer
2
+ = dive_helper(@ext)
@@ -0,0 +1,3 @@
1
+ <% form_for( :object, '/' ) do |f| %>
2
+ <%= $number_of_captures += 1 %>
3
+ <% end %>
@@ -0,0 +1,2 @@
1
+ = form_for :object, '/' do |f|
2
+ = $number_of_captures += 1
@@ -0,0 +1,2 @@
1
+ = form_for :object, '/' do |f|
2
+ = $number_of_captures += 1
@@ -0,0 +1 @@
1
+ <h1>This is a <%= @template %> template!</h1>
@@ -0,0 +1,5 @@
1
+ %p.start= current_engine
2
+ %p.haml= partial "current_engines/haml", :engine => :haml
3
+ %p.erb= partial 'current_engines/erb', :engine => :erb
4
+ %p.slim= partial 'current_engines/slim', :engine => :slim
5
+ %p.end= current_engine
@@ -0,0 +1 @@
1
+ %h1 This is a #{@template} template!
@@ -0,0 +1,5 @@
1
+ <h1>prefix</h1>
2
+ <div class="erb-block">
3
+ <%= yield %>
4
+ </div>
5
+ <h3>postfix</h3>
@@ -0,0 +1,4 @@
1
+ %h1 prefix
2
+ .haml-block
3
+ = yield
4
+ %h3 postfix
@@ -0,0 +1,4 @@
1
+ h1 prefix
2
+ .slim-block
3
+ = yield
4
+ h3 postfix
@@ -0,0 +1 @@
1
+ <%= content_tag(:a) { content_tag :b, 'c' } %>
@@ -0,0 +1 @@
1
+ = content_tag(:a) { content_tag :b, 'c' }
@@ -0,0 +1 @@
1
+ = content_tag(:a) { content_tag :b, 'c' }
@@ -0,0 +1,7 @@
1
+ %h1 User name is #{user.name}
2
+
3
+ - if defined?(extra)
4
+ %p Extra is #{extra}
5
+
6
+ - if defined?(user_counter)
7
+ %p My counter is #{user_counter}
@@ -0,0 +1 @@
1
+ %h1 This is a #{@template} template sent from render_template!
@@ -0,0 +1,2 @@
1
+
2
+ %h1 This is a haml template which was detected!
@@ -0,0 +1,3 @@
1
+ <%= form_for( :object, '/' ) do |f| %>
2
+ <%= content_tag(:p, 'this is wrong') %>
3
+ <% end %>
@@ -0,0 +1,2 @@
1
+ - form_for :object, '/' do |f|
2
+ = content_tag(:p, 'this is wrong')
@@ -0,0 +1,2 @@
1
+ - form_for :object, '/' do |f|
2
+ = content_tag(:p, 'this is wrong')
data/test/helper.rb ADDED
@@ -0,0 +1,88 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
5
+ require 'mocha/setup'
6
+ require 'rack/test'
7
+ require 'webrat'
8
+ require 'active_support/time'
9
+ require 'builder'
10
+ require 'tennpipes-helper'
11
+
12
+ require 'ext/minitest-spec'
13
+ require 'ext/rack-test-methods'
14
+
15
+ class MiniTest::Spec
16
+ include Tennpipes::Helpers::OutputHelpers
17
+ include Tennpipes::Helpers::TagHelpers
18
+ include Tennpipes::Helpers::AssetTagHelpers
19
+ include Rack::Test::Methods
20
+ include Webrat::Methods
21
+ include Webrat::Matchers
22
+
23
+ Webrat.configure do |config|
24
+ config.mode = :rack
25
+ end
26
+
27
+ def stop_time_for_test
28
+ time = Time.now
29
+ Time.stubs(:now).returns(time)
30
+ return time
31
+ end
32
+
33
+ # mock_model("Business", :new_record? => true) => <Business>
34
+ def mock_model(klazz, options={})
35
+ options.reverse_merge!(:class => klazz, :new_record? => false, :id => 20, :errors => {})
36
+ record = stub(options)
37
+ record.stubs(:to_ary => [record])
38
+ record
39
+ end
40
+
41
+ def create_template(name, content, options={})
42
+ FileUtils.mkdir_p(File.dirname(__FILE__) + "/views")
43
+ FileUtils.mkdir_p(File.dirname(__FILE__) + "/views/layouts")
44
+ path = "/views/#{name}"
45
+ path += ".#{options.delete(:locale)}" if options[:locale].present?
46
+ path += ".#{options[:format]}" if options[:format].present?
47
+ path += ".erb" unless options[:format].to_s =~ /erb|slim|haml|rss|atom|builder/
48
+ path += ".builder" if options[:format].to_s =~ /rss|atom/
49
+ file = File.dirname(__FILE__) + path
50
+ File.open(file, 'w') { |io| io.write content }
51
+ file
52
+ end
53
+ alias :create_view :create_template
54
+ alias :create_layout :create_template
55
+
56
+ def remove_views
57
+ FileUtils.rm_rf(File.dirname(__FILE__) + "/views")
58
+ end
59
+
60
+ def with_template(name, content, options={})
61
+ template = create_template(name, content, options)
62
+ yield
63
+ ensure
64
+ File.unlink(template) rescue nil
65
+ remove_views
66
+ end
67
+ alias :with_view :with_template
68
+ alias :with_layout :with_template
69
+
70
+ def mock_app(base=Tennpipes::Application, &block)
71
+ @app = Sinatra.new base do
72
+ register Tennpipes::Helpers
73
+ instance_eval &block
74
+ end
75
+ end
76
+
77
+ def app
78
+ Rack::Lint.new(@app)
79
+ end
80
+ end
81
+
82
+ module Webrat
83
+ module Logging
84
+ def logger # @private
85
+ @logger = nil
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,401 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/markup_app/app')
3
+
4
+ describe "AssetTagHelpers" do
5
+ include Tennpipes::Helpers::AssetTagHelpers
6
+
7
+ def app
8
+ MarkupDemo
9
+ end
10
+
11
+ def flash
12
+ @_flash ||= { :notice => "Demo notice" }
13
+ end
14
+
15
+ describe 'for #flash_tag method' do
16
+ it 'should display flash with no given attributes' do
17
+ assert_has_tag('div.notice', :content => "Demo notice") { flash_tag(:notice) }
18
+ end
19
+ it 'should display flash with given attributes' do
20
+ actual_html = flash_tag(:notice, :class => 'notice', :id => 'notice-area')
21
+ assert_has_tag('div.notice#notice-area', :content => "Demo notice") { actual_html }
22
+ end
23
+ it 'should display multiple flash tags with given attributes' do
24
+ flash[:error] = 'wrong'
25
+ flash[:success] = 'okey'
26
+ actual_html = flash_tag(:success, :warning, :error, :id => 'area')
27
+ assert_has_tag('div.success#area', :content => flash[:success]) { actual_html }
28
+ assert_has_tag('div.error#area', :content => flash[:error]) { actual_html }
29
+ assert_has_no_tag('div.notice') { actual_html }
30
+ end
31
+ end
32
+
33
+ describe 'for #link_to method' do
34
+ it 'should display link element with no given attributes' do
35
+ assert_has_tag('a', :content => "Sign up", :href => '/register') { link_to('Sign up', '/register') }
36
+ end
37
+
38
+ it 'should display link element with given attributes' do
39
+ actual_html = link_to('Sign up', '/register', :class => 'first', :id => 'linky')
40
+ assert_has_tag('a#linky.first', :content => "Sign up", :href => '/register') { actual_html }
41
+ end
42
+
43
+ it 'should display link element with void url and options' do
44
+ actual_link = link_to('Sign up', :class => "test")
45
+ assert_has_tag('a', :content => "Sign up", :href => '#', :class => 'test') { actual_link }
46
+ end
47
+
48
+ it 'should display link element with remote option' do
49
+ actual_link = link_to('Sign up', '/register', :remote => true)
50
+ assert_has_tag('a', :content => "Sign up", :href => '/register', 'data-remote' => 'true') { actual_link }
51
+ end
52
+
53
+ it 'should display link element with method option' do
54
+ actual_link = link_to('Sign up', '/register', :method => :delete)
55
+ assert_has_tag('a', :content => "Sign up", :href => '/register', 'data-method' => 'delete', :rel => 'nofollow') { actual_link }
56
+ end
57
+
58
+ it 'should display link element with confirm option' do
59
+ actual_link = link_to('Sign up', '/register', :confirm => "Are you sure?")
60
+ assert_has_tag('a', :content => "Sign up", :href => '/register', 'data-confirm' => 'Are you sure?') { actual_link }
61
+ end
62
+
63
+ it 'should display link element with ruby block' do
64
+ actual_link = link_to('/register', :class => 'first', :id => 'binky') { "Sign up" }
65
+ assert_has_tag('a#binky.first', :content => "Sign up", :href => '/register') { actual_link }
66
+ end
67
+
68
+ it 'should escape the link text' do
69
+ actual_link = link_to('/register', :class => 'first', :id => 'binky') { "<&>" }
70
+ assert_has_tag('a#binky.first', :href => '/register') { actual_link }
71
+ assert_match "&lt;&amp;&gt;", actual_link
72
+ end
73
+
74
+ it 'should not escape image_tag' do
75
+ actual_link = link_to(image_tag("/my/fancy/image.png"), :class => 'first', :id => 'binky')
76
+ assert_has_tag('img', :src => "/my/fancy/image.png") { actual_link }
77
+ end
78
+
79
+ it 'should display link block element in haml' do
80
+ visit '/haml/link_to'
81
+ assert_have_selector :a, :content => "Test 1 No Block", :href => '/test1', :class => 'test', :id => 'test1'
82
+ assert_have_selector :a, :content => "Test 2 With Block", :href => '/test2', :class => 'test', :id => 'test2'
83
+ end
84
+
85
+ it 'should display link block element in erb' do
86
+ visit '/erb/link_to'
87
+ assert_have_selector :a, :content => "Test 1 No Block", :href => '/test1', :class => 'test', :id => 'test1'
88
+ assert_have_selector :a, :content => "Test 2 With Block", :href => '/test2', :class => 'test', :id => 'test2'
89
+ end
90
+
91
+ it 'should display link block element in slim' do
92
+ visit '/slim/link_to'
93
+ assert_have_selector :a, :content => "Test 1 No Block", :href => '/test1', :class => 'test', :id => 'test1'
94
+ assert_have_selector :a, :content => "Test 2 With Block", :href => '/test2', :class => 'test', :id => 'test2'
95
+ end
96
+
97
+ it 'should not double-escape' do
98
+ actual_link = link_to('test escape', '?a=1&b=2')
99
+ assert_has_tag('a', :href => '?a=1&b=2') { actual_link }
100
+ assert_match %r{&amp;}, actual_link
101
+ refute_match %r{&amp;amp;}, actual_link
102
+ end
103
+
104
+ it 'should escape scary things' do
105
+ actual_link = link_to('test escape<adfs>', '?a=1&b=<script>alert(1)</script>')
106
+ refute_match('<script', actual_link)
107
+ end
108
+ end
109
+
110
+ describe 'for #mail_to method' do
111
+ it 'should display link element for mail to no caption' do
112
+ actual_html = mail_to('test@demo.com')
113
+ assert_has_tag(:a, :href => "mailto:test@demo.com", :content => 'test@demo.com') { actual_html }
114
+ end
115
+
116
+ it 'should display link element for mail to with caption' do
117
+ actual_html = mail_to('test@demo.com', "My Email", :class => 'demo')
118
+ assert_has_tag(:a, :href => "mailto:test@demo.com", :content => 'My Email', :class => 'demo') { actual_html }
119
+ end
120
+
121
+ it 'should display link element for mail to with caption and mail options' do
122
+ actual_html = mail_to('test@demo.com', "My Email", :subject => 'demo test', :class => 'demo', :cc => 'foo@test.com')
123
+ assert_has_tag(:a, :class => 'demo') { actual_html }
124
+ assert_match %r{mailto\:test\@demo.com\?}, actual_html
125
+ assert_match %r{cc=foo\@test\.com}, actual_html
126
+ assert_match %r{subject\=demo\%20test}, actual_html
127
+ end
128
+
129
+ it 'should escape & with encoded string and &amp; in HTML' do
130
+ actual_html = mail_to('test@demo.com', "My&Email", :subject => "this&that")
131
+ assert_match 'this%26that', actual_html
132
+ assert_match 'My&amp;Email', actual_html
133
+ end
134
+
135
+ it 'should not double-escape ampersands in query' do
136
+ actual_html = mail_to('to@demo.com', "Email", :subject => 'Hi there', :bcc => 'bcc@test.com')
137
+ assert_has_tag(:a, :href => 'mailto:to@demo.com?bcc=bcc@test.com&subject=Hi%20there', :content => 'Email') { actual_html }
138
+ assert_match %r{&amp;}, actual_html
139
+ refute_match %r{&amp;amp;}, actual_html
140
+ end
141
+
142
+ it 'should display mail link element in haml' do
143
+ visit '/haml/mail_to'
144
+ assert_have_selector 'p.simple a', :href => 'mailto:test@demo.com', :content => 'test@demo.com'
145
+ assert_have_selector 'p.captioned a', :href => 'mailto:test@demo.com', :content => 'Click my Email'
146
+ end
147
+
148
+ it 'should display mail link element in erb' do
149
+ visit '/erb/mail_to'
150
+ assert_have_selector 'p.simple a', :href => 'mailto:test@demo.com', :content => 'test@demo.com'
151
+ assert_have_selector 'p.captioned a', :href => 'mailto:test@demo.com', :content => 'Click my Email'
152
+ end
153
+
154
+ it 'should display mail link element in slim' do
155
+ visit '/slim/mail_to'
156
+ assert_have_selector 'p.simple a', :href => 'mailto:test@demo.com', :content => 'test@demo.com'
157
+ assert_have_selector 'p.captioned a', :href => 'mailto:test@demo.com', :content => 'Click my Email'
158
+ end
159
+ end
160
+
161
+ describe 'for #meta_tag method' do
162
+ it 'should display meta tag with given content and name' do
163
+ actual_html = meta_tag("weblog,news", :name => "keywords")
164
+ assert_has_tag("meta", :name => "keywords", "content" => "weblog,news") { actual_html }
165
+ end
166
+
167
+ it 'should display meta tag with given content and http-equiv' do
168
+ actual_html = meta_tag("text/html; charset=UTF-8", :"http-equiv" => "Content-Type")
169
+ assert_has_tag("meta", :"http-equiv" => "Content-Type", "content" => "text/html; charset=UTF-8") { actual_html }
170
+ end
171
+
172
+ it 'should display meta tag element in haml' do
173
+ visit '/haml/meta_tag'
174
+ assert_have_selector 'meta', "content" => "weblog,news", :name => "keywords"
175
+ assert_have_selector 'meta', "content" => "text/html; charset=UTF-8", :"http-equiv" => "Content-Type"
176
+ end
177
+
178
+ it 'should display meta tag element in erb' do
179
+ visit '/erb/meta_tag'
180
+ assert_have_selector 'meta', "content" => "weblog,news", :name => "keywords"
181
+ assert_have_selector 'meta', "content" => "text/html; charset=UTF-8", :"http-equiv" => "Content-Type"
182
+ end
183
+
184
+ it 'should display meta tag element in slim' do
185
+ visit '/slim/meta_tag'
186
+ assert_have_selector 'meta', "content" => "weblog,news", :name => "keywords"
187
+ assert_have_selector 'meta', "content" => "text/html; charset=UTF-8", :"http-equiv" => "Content-Type"
188
+ end
189
+ end
190
+
191
+ describe 'for #image_tag method' do
192
+ it 'should display image tag absolute link with no options' do
193
+ time = stop_time_for_test
194
+ assert_has_tag('img', :src => "/absolute/pic.gif") { image_tag('/absolute/pic.gif') }
195
+ end
196
+
197
+ it 'should display image tag relative link with specified uri root' do
198
+ time = stop_time_for_test
199
+ self.class.stubs(:uri_root).returns("/blog")
200
+ assert_has_tag('img', :src => "/blog/images/relative/pic.gif?#{time.to_i}") { image_tag('relative/pic.gif') }
201
+ end
202
+
203
+ it 'should display image tag relative link with options' do
204
+ time = stop_time_for_test
205
+ assert_has_tag('img.photo', :src => "/images/relative/pic.gif?#{time.to_i}") {
206
+ image_tag('relative/pic.gif', :class => 'photo') }
207
+ end
208
+
209
+ it 'should display image tag uri link with options' do
210
+ time = stop_time_for_test
211
+ assert_has_tag('img.photo', :src => "http://demo.org/pic.gif") { image_tag('http://demo.org/pic.gif', :class => 'photo') }
212
+ end
213
+
214
+ it 'should display image tag relative link with incorrect spacing' do
215
+ time = stop_time_for_test
216
+ assert_has_tag('img.photo', :src => "/images/%20relative/%20pic.gif%20%20?#{time.to_i}") {
217
+ image_tag(' relative/ pic.gif ', :class => 'photo')
218
+ }
219
+ end
220
+
221
+ it 'should not use a timestamp if stamp setting is false' do
222
+ assert_has_tag('img', :src => "/absolute/pic.gif") { image_tag('/absolute/pic.gif') }
223
+ end
224
+
225
+ it 'should have xhtml convention tag' do
226
+ assert_equal image_tag('/absolute/pic.gif'), '<img src="/absolute/pic.gif" />'
227
+ end
228
+ end
229
+
230
+ describe 'for #stylesheet_link_tag method' do
231
+ it 'should display stylesheet link item' do
232
+ time = stop_time_for_test
233
+ actual_html = stylesheet_link_tag('style')
234
+ expected_options = { :rel => "stylesheet", :type => "text/css" }
235
+ assert_has_tag('link', expected_options.merge(:href => "/stylesheets/style.css?#{time.to_i}")) { actual_html }
236
+ assert actual_html.html_safe?
237
+ end
238
+
239
+ it 'should display stylesheet link item for long relative path' do
240
+ time = stop_time_for_test
241
+ expected_options = { :rel => "stylesheet", :type => "text/css" }
242
+ actual_html = stylesheet_link_tag('example/demo/style')
243
+ assert_has_tag('link', expected_options.merge(:href => "/stylesheets/example/demo/style.css?#{time.to_i}")) { actual_html }
244
+ end
245
+
246
+ it 'should display stylesheet link item with absolute path' do
247
+ time = stop_time_for_test
248
+ expected_options = { :rel => "stylesheet", :type => "text/css" }
249
+ actual_html = stylesheet_link_tag('/css/style')
250
+ assert_has_tag('link', expected_options.merge(:href => "/css/style.css")) { actual_html }
251
+ end
252
+
253
+ it 'should display stylesheet link item with uri root' do
254
+ self.class.stubs(:uri_root).returns("/blog")
255
+ time = stop_time_for_test
256
+ expected_options = { :rel => "stylesheet", :type => "text/css" }
257
+ actual_html = stylesheet_link_tag('style')
258
+ assert_has_tag('link', expected_options.merge(:href => "/blog/stylesheets/style.css?#{time.to_i}")) { actual_html }
259
+ end
260
+
261
+ it 'should display stylesheet link items' do
262
+ time = stop_time_for_test
263
+ actual_html = stylesheet_link_tag('style', 'layout.css', 'http://google.com/style.css')
264
+ assert_has_tag('link', :rel => "stylesheet", :type => "text/css", :count => 3) { actual_html }
265
+ assert_has_tag('link', :href => "/stylesheets/style.css?#{time.to_i}") { actual_html }
266
+ assert_has_tag('link', :href => "/stylesheets/layout.css?#{time.to_i}") { actual_html }
267
+ assert_has_tag('link', :href => "http://google.com/style.css") { actual_html }
268
+ assert_equal actual_html, stylesheet_link_tag(['style', 'layout.css', 'http://google.com/style.css'])
269
+ end
270
+
271
+ it 'should not use a timestamp if stamp setting is false' do
272
+ self.class.expects(:asset_stamp).returns(false)
273
+ expected_options = { :rel => "stylesheet", :type => "text/css" }
274
+ assert_has_tag('link', expected_options.merge(:href => "/stylesheets/style.css")) { stylesheet_link_tag('style') }
275
+ end
276
+
277
+ it 'should display stylesheet link used custom options' do
278
+ assert_has_tag('link', :rel => 'stylesheet', :media => 'screen') { stylesheet_link_tag('style', :media => 'screen') }
279
+ end
280
+ end
281
+
282
+ describe 'for #javascript_include_tag method' do
283
+ it 'should display javascript item' do
284
+ time = stop_time_for_test
285
+ actual_html = javascript_include_tag('application')
286
+ assert_has_tag('script', :src => "/javascripts/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
287
+ assert actual_html.html_safe?
288
+ end
289
+
290
+ it 'should respond to js_asset_folder setting' do
291
+ time = stop_time_for_test
292
+ self.class.stubs(:js_asset_folder).returns('js')
293
+ assert_equal 'js', asset_folder_name(:js)
294
+ actual_html = javascript_include_tag('application')
295
+ assert_has_tag('script', :src => "/js/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
296
+ end
297
+
298
+ it 'should display javascript item for long relative path' do
299
+ time = stop_time_for_test
300
+ actual_html = javascript_include_tag('example/demo/application')
301
+ assert_has_tag('script', :src => "/javascripts/example/demo/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
302
+ end
303
+
304
+ it 'should display javascript item for path containing js' do
305
+ time = stop_time_for_test
306
+ actual_html = javascript_include_tag 'test/jquery.json'
307
+ assert_has_tag('script', :src => "/javascripts/test/jquery.json?#{time.to_i}", :type => "text/javascript") { actual_html }
308
+ end
309
+
310
+ it 'should display javascript item for path containing period' do
311
+ time = stop_time_for_test
312
+ actual_html = javascript_include_tag 'test/jquery.min'
313
+ assert_has_tag('script', :src => "/javascripts/test/jquery.min.js?#{time.to_i}", :type => "text/javascript") { actual_html }
314
+ end
315
+
316
+ it 'should display javascript item with absolute path' do
317
+ time = stop_time_for_test
318
+ actual_html = javascript_include_tag('/js/application')
319
+ assert_has_tag('script', :src => "/js/application.js", :type => "text/javascript") { actual_html }
320
+ end
321
+
322
+ it 'should display javascript item with uri root' do
323
+ self.class.stubs(:uri_root).returns("/blog")
324
+ time = stop_time_for_test
325
+ actual_html = javascript_include_tag('application')
326
+ assert_has_tag('script', :src => "/blog/javascripts/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
327
+ end
328
+
329
+ it 'should not append extension to absolute paths' do
330
+ time = stop_time_for_test
331
+ actual_html = javascript_include_tag('https://maps.googleapis.com/maps/api/js?key=value&sensor=false')
332
+ assert_has_tag('script', :src => "https://maps.googleapis.com/maps/api/js?key=value&sensor=false") { actual_html }
333
+ end
334
+
335
+ it 'should display javascript items' do
336
+ time = stop_time_for_test
337
+ actual_html = javascript_include_tag('application', 'base.js', 'http://google.com/lib.js')
338
+ assert_has_tag('script', :type => "text/javascript", :count => 3) { actual_html }
339
+ assert_has_tag('script', :src => "/javascripts/application.js?#{time.to_i}") { actual_html }
340
+ assert_has_tag('script', :src => "/javascripts/base.js?#{time.to_i}") { actual_html }
341
+ assert_has_tag('script', :src => "http://google.com/lib.js") { actual_html }
342
+ assert_equal actual_html, javascript_include_tag(['application', 'base.js', 'http://google.com/lib.js'])
343
+ end
344
+
345
+ it 'should not use a timestamp if stamp setting is false' do
346
+ self.class.expects(:asset_stamp).returns(false)
347
+ actual_html = javascript_include_tag('application')
348
+ assert_has_tag('script', :src => "/javascripts/application.js", :type => "text/javascript") { actual_html }
349
+ end
350
+ end
351
+
352
+ describe "for #favicon_tag method" do
353
+ it 'should display favicon' do
354
+ time = stop_time_for_test
355
+ actual_html = favicon_tag('icons/favicon.png')
356
+ assert_has_tag('link', :rel => 'icon', :type => 'image/png', :href => "/images/icons/favicon.png?#{time.to_i}") { actual_html }
357
+ end
358
+
359
+ it 'should match type with file ext' do
360
+ time = stop_time_for_test
361
+ actual_html = favicon_tag('favicon.ico')
362
+ assert_has_tag('link', :rel => 'icon', :type => 'image/ico', :href => "/images/favicon.ico?#{time.to_i}") { actual_html }
363
+ end
364
+
365
+ it 'should allow option overrides' do
366
+ time = stop_time_for_test
367
+ actual_html = favicon_tag('favicon.png', :type => 'image/ico')
368
+ assert_has_tag('link', :rel => 'icon', :type => 'image/ico', :href => "/images/favicon.png?#{time.to_i}") { actual_html }
369
+ end
370
+ end
371
+
372
+ describe 'for #feed_tag method' do
373
+ it 'should generate correctly link tag for rss' do
374
+ assert_has_tag('link', :type => 'application/rss+xml', :rel => 'alternate', :href => "/blog/post.rss", :title => 'rss') { feed_tag :rss, "/blog/post.rss" }
375
+ end
376
+
377
+ it 'should generate correctly link tag for atom' do
378
+ assert_has_tag('link', :type => 'application/atom+xml', :rel => 'alternate', :href => "/blog/post.atom", :title => 'atom') { feed_tag :atom, "/blog/post.atom" }
379
+ end
380
+
381
+ it 'should override options' do
382
+ assert_has_tag('link', :type => 'my-type', :rel => 'my-rel', :href => "/blog/post.rss", :title => 'my-title') { feed_tag :rss, "/blog/post.rss", :type => "my-type", :rel => "my-rel", :title => "my-title" }
383
+ end
384
+ end
385
+
386
+ describe 'for #asset_path method' do
387
+ it 'should generate proper paths for js and css' do
388
+ assert_match /\/javascripts\/app.js\?\d+/, asset_path(:js, 'app')
389
+ assert_match /\/stylesheets\/app.css\?\d+/, asset_path(:css, 'app')
390
+ end
391
+
392
+ it 'should generate proper paths for images and other files' do
393
+ assert_match /\/images\/app.png\?\d+/, asset_path(:images, 'app.png')
394
+ assert_match /\/documents\/app.pdf\?\d+/, asset_path(:documents, 'app.pdf')
395
+ end
396
+
397
+ it 'should generate proper paths for public folder' do
398
+ assert_match /\/files\/file.ext\?\d+/, asset_path('files/file.ext')
399
+ end
400
+ end
401
+ end