darkhelmet-sinatra_more 0.3.33

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/.document +5 -0
  2. data/.gitignore +23 -0
  3. data/IDEAS.md +54 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +756 -0
  6. data/ROADMAP +88 -0
  7. data/Rakefile +63 -0
  8. data/TODO +60 -0
  9. data/VERSION +1 -0
  10. data/bin/sinatra_gen +6 -0
  11. data/generators/base_app/.gitignore +7 -0
  12. data/generators/base_app/Gemfile +13 -0
  13. data/generators/base_app/app/.empty_directory +0 -0
  14. data/generators/base_app/app/helpers/.empty_directory +0 -0
  15. data/generators/base_app/app/helpers/view_helpers.rb +3 -0
  16. data/generators/base_app/app/mailers/.empty_directory +0 -0
  17. data/generators/base_app/app/models/.empty_directory +0 -0
  18. data/generators/base_app/app/routes/.empty_directory +0 -0
  19. data/generators/base_app/app/views/.empty_directory +0 -0
  20. data/generators/base_app/config.ru.tt +10 -0
  21. data/generators/base_app/config/boot.rb.tt +42 -0
  22. data/generators/base_app/config/dependencies.rb.tt +41 -0
  23. data/generators/base_app/lib/.empty_directory +0 -0
  24. data/generators/base_app/public/images/.empty_directory +0 -0
  25. data/generators/base_app/public/images/.gitignore +0 -0
  26. data/generators/base_app/public/javascripts/.empty_directory +0 -0
  27. data/generators/base_app/public/stylesheets/.empty_directory +0 -0
  28. data/generators/base_app/test/models/.empty_directory +0 -0
  29. data/generators/base_app/test/routes/.empty_directory +0 -0
  30. data/generators/base_app/test/test_config.rb.tt +5 -0
  31. data/generators/base_app/vendor/gems/.empty_directory +0 -0
  32. data/generators/components/component_actions.rb +48 -0
  33. data/generators/components/mocks/mocha_mock_gen.rb +8 -0
  34. data/generators/components/mocks/rr_mock_gen.rb +8 -0
  35. data/generators/components/orms/activerecord_orm_gen.rb +99 -0
  36. data/generators/components/orms/couchrest_orm_gen.rb +64 -0
  37. data/generators/components/orms/datamapper_orm_gen.rb +52 -0
  38. data/generators/components/orms/mongomapper_orm_gen.rb +101 -0
  39. data/generators/components/orms/sequel_orm_gen.rb +61 -0
  40. data/generators/components/renderers/erb_renderer_gen.rb +7 -0
  41. data/generators/components/renderers/haml_renderer_gen.rb +22 -0
  42. data/generators/components/scripts/jquery_script_gen.rb +9 -0
  43. data/generators/components/scripts/prototype_script_gen.rb +10 -0
  44. data/generators/components/scripts/rightjs_script_gen.rb +10 -0
  45. data/generators/components/tests/bacon_test_gen.rb +21 -0
  46. data/generators/components/tests/riot_test_gen.rb +19 -0
  47. data/generators/components/tests/rspec_test_gen.rb +19 -0
  48. data/generators/components/tests/shoulda_test_gen.rb +19 -0
  49. data/generators/components/tests/testspec_test_gen.rb +19 -0
  50. data/generators/generator_actions.rb +79 -0
  51. data/generators/skeleton_generator.rb +53 -0
  52. data/lib/sinatra/mailer_plugin.rb +14 -0
  53. data/lib/sinatra/mailer_plugin/mail_object.rb +39 -0
  54. data/lib/sinatra/mailer_plugin/mailer_base.rb +75 -0
  55. data/lib/sinatra/markup_plugin.rb +19 -0
  56. data/lib/sinatra/markup_plugin/asset_tag_helpers.rb +109 -0
  57. data/lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb +133 -0
  58. data/lib/sinatra/markup_plugin/form_builder/standard_form_builder.rb +31 -0
  59. data/lib/sinatra/markup_plugin/form_helpers.rb +194 -0
  60. data/lib/sinatra/markup_plugin/format_helpers.rb +74 -0
  61. data/lib/sinatra/markup_plugin/output_helpers.rb +98 -0
  62. data/lib/sinatra/markup_plugin/tag_helpers.rb +42 -0
  63. data/lib/sinatra/render_plugin.rb +12 -0
  64. data/lib/sinatra/render_plugin/render_helpers.rb +63 -0
  65. data/lib/sinatra/routing_plugin.rb +50 -0
  66. data/lib/sinatra/routing_plugin/named_route.rb +27 -0
  67. data/lib/sinatra/routing_plugin/routing_helpers.rb +22 -0
  68. data/lib/sinatra/support_lite.rb +19 -0
  69. data/lib/sinatra/warden_plugin.rb +51 -0
  70. data/lib/sinatra/warden_plugin/warden_helpers.rb +60 -0
  71. data/lib/sinatra_more.rb +7 -0
  72. data/sinatra_more.gemspec +215 -0
  73. data/test/active_support_helpers.rb +7 -0
  74. data/test/fixtures/mailer_app/app.rb +51 -0
  75. data/test/fixtures/mailer_app/views/demo_mailer/sample_mail.erb +1 -0
  76. data/test/fixtures/mailer_app/views/sample_mailer/anniversary_message.erb +2 -0
  77. data/test/fixtures/mailer_app/views/sample_mailer/birthday_message.erb +2 -0
  78. data/test/fixtures/markup_app/app.rb +66 -0
  79. data/test/fixtures/markup_app/views/capture_concat.erb +14 -0
  80. data/test/fixtures/markup_app/views/capture_concat.haml +13 -0
  81. data/test/fixtures/markup_app/views/content_for.erb +11 -0
  82. data/test/fixtures/markup_app/views/content_for.haml +9 -0
  83. data/test/fixtures/markup_app/views/content_tag.erb +11 -0
  84. data/test/fixtures/markup_app/views/content_tag.haml +9 -0
  85. data/test/fixtures/markup_app/views/fields_for.erb +8 -0
  86. data/test/fixtures/markup_app/views/fields_for.haml +6 -0
  87. data/test/fixtures/markup_app/views/form_for.erb +56 -0
  88. data/test/fixtures/markup_app/views/form_for.haml +47 -0
  89. data/test/fixtures/markup_app/views/form_tag.erb +57 -0
  90. data/test/fixtures/markup_app/views/form_tag.haml +45 -0
  91. data/test/fixtures/markup_app/views/link_to.erb +5 -0
  92. data/test/fixtures/markup_app/views/link_to.haml +4 -0
  93. data/test/fixtures/markup_app/views/mail_to.erb +3 -0
  94. data/test/fixtures/markup_app/views/mail_to.haml +3 -0
  95. data/test/fixtures/markup_app/views/meta_tag.erb +3 -0
  96. data/test/fixtures/markup_app/views/meta_tag.haml +3 -0
  97. data/test/fixtures/render_app/app.rb +54 -0
  98. data/test/fixtures/render_app/views/erb/test.erb +1 -0
  99. data/test/fixtures/render_app/views/haml/test.haml +1 -0
  100. data/test/fixtures/render_app/views/template/_user.haml +7 -0
  101. data/test/fixtures/render_app/views/template/haml_template.haml +1 -0
  102. data/test/fixtures/render_app/views/template/some_template.haml +2 -0
  103. data/test/fixtures/routing_app/app.rb +48 -0
  104. data/test/fixtures/routing_app/views/index.haml +7 -0
  105. data/test/fixtures/warden_app/app.rb +75 -0
  106. data/test/fixtures/warden_app/views/dashboard.haml +6 -0
  107. data/test/generators/test_skeleton_generator.rb +190 -0
  108. data/test/helper.rb +73 -0
  109. data/test/mailer_plugin/test_mail_object.rb +24 -0
  110. data/test/mailer_plugin/test_mailer_base.rb +81 -0
  111. data/test/markup_plugin/test_asset_tag_helpers.rb +171 -0
  112. data/test/markup_plugin/test_form_builder.rb +627 -0
  113. data/test/markup_plugin/test_form_helpers.rb +417 -0
  114. data/test/markup_plugin/test_format_helpers.rb +96 -0
  115. data/test/markup_plugin/test_output_helpers.rb +63 -0
  116. data/test/markup_plugin/test_tag_helpers.rb +73 -0
  117. data/test/test_mailer_plugin.rb +33 -0
  118. data/test/test_render_plugin.rb +78 -0
  119. data/test/test_routing_plugin.rb +94 -0
  120. data/test/test_warden_plugin.rb +105 -0
  121. metadata +307 -0
data/test/helper.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'rack/test'
6
+ require 'webrat'
7
+
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+ require 'active_support_helpers'
11
+ require 'sinatra_more'
12
+
13
+ class Test::Unit::TestCase
14
+ include Sinatra::MarkupPlugin::OutputHelpers
15
+ include Sinatra::MarkupPlugin::TagHelpers
16
+ include Sinatra::MarkupPlugin::AssetTagHelpers
17
+ include Rack::Test::Methods
18
+ include Webrat::Methods
19
+ include Webrat::Matchers
20
+
21
+ Webrat.configure do |config|
22
+ config.mode = :rack
23
+ end
24
+
25
+ def stop_time_for_test
26
+ time = Time.now
27
+ Time.stubs(:now).returns(time)
28
+ return time
29
+ end
30
+
31
+ # assert_has_tag(:h1, :content => "yellow") { "<h1>yellow</h1>" }
32
+ # In this case, block is the html to evaluate
33
+ def assert_has_tag(name, attributes = {}, &block)
34
+ html = block && block.call
35
+ matcher = HaveSelector.new(name, attributes)
36
+ raise "Please specify a block!" if html.blank?
37
+ assert matcher.matches?(html), matcher.failure_message
38
+ end
39
+
40
+ # assert_has_no_tag, tag(:h1, :content => "yellow") { "<h1>green</h1>" }
41
+ # In this case, block is the html to evaluate
42
+ def assert_has_no_tag(name, attributes = {}, &block)
43
+ html = block && block.call
44
+ attributes.merge!(:count => 0)
45
+ matcher = HaveSelector.new(name, attributes)
46
+ raise "Please specify a block!" if html.blank?
47
+ assert matcher.matches?(html), matcher.failure_message
48
+ end
49
+
50
+ # Silences the output by redirecting to stringIO
51
+ # silence_logger { ...commands... } => "...output..."
52
+ def silence_logger(&block)
53
+ orig_stdout = $stdout
54
+ $stdout = log_buffer = StringIO.new
55
+ block.call
56
+ $stdout = orig_stdout
57
+ log_buffer.rewind && log_buffer.read
58
+ end
59
+
60
+ # Asserts that a file matches the pattern
61
+ def assert_match_in_file(pattern, file)
62
+ assert File.exist?(file), "File '#{file}' does not exist!"
63
+ assert_match pattern, File.read(file)
64
+ end
65
+ end
66
+
67
+ module Webrat
68
+ module Logging
69
+ def logger # :nodoc:
70
+ @logger = nil
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,24 @@
1
+ require 'helper'
2
+ require 'sinatra_more'
3
+
4
+ class TestMailObject < Test::Unit::TestCase
5
+ context 'for #deliver method' do
6
+ should "send mail with attributes default to sendmail no smtp" do
7
+ mail_object = Sinatra::MailerPlugin::MailObject.new({:to => "test@john.com", :from => "sender@sent.com", :body => "Hello"}, {})
8
+ Pony.expects(:mail).with(:to => "test@john.com", :from => "sender@sent.com", :body => "Hello", :via => :sendmail)
9
+ mail_object.deliver
10
+ end
11
+
12
+ should "send mail with attributes default to smtp if set" do
13
+ mail_object = Sinatra::MailerPlugin::MailObject.new({:to => "test@john.com", :body => "Hello"}, { :host => 'smtp.gmail.com' })
14
+ Pony.expects(:mail).with(:to => "test@john.com", :body => "Hello", :via => :smtp, :smtp => { :host => 'smtp.gmail.com' })
15
+ mail_object.deliver
16
+ end
17
+
18
+ should "send mail with attributes use sendmail if explicit" do
19
+ mail_object = Sinatra::MailerPlugin::MailObject.new({:to => "test@john.com", :via => :sendmail }, { :host => 'smtp.gmail.com' })
20
+ Pony.expects(:mail).with(:to => "test@john.com", :via => :sendmail)
21
+ mail_object.deliver
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,81 @@
1
+ require 'helper'
2
+ require 'sinatra_more'
3
+
4
+ class DemoMailer < Sinatra::MailerPlugin::MailerBase
5
+ def sample_mail
6
+ from 'test@default.com'
7
+ to 'test@test.com'
8
+ body "Hello world!"
9
+ via :sendmail
10
+ end
11
+
12
+ def sample_mail_smtp
13
+ from 'test@default.com'
14
+ to 'test@test.com'
15
+ body "SMTP Hello world!"
16
+ end
17
+ end
18
+
19
+ class TestMailerBase < Test::Unit::TestCase
20
+ context 'for defining email attributes' do
21
+ DemoMailer.mail_fields.each do |field|
22
+ should "support setting '#{field}' attribute" do
23
+ demo_mailer = DemoMailer.new(:sample_mail)
24
+ demo_mailer.send(field, "some_value")
25
+ assert_equal({ field => "some_value" }, demo_mailer.mail_attributes)
26
+ end
27
+ end
28
+
29
+ should "allow defining text body" do
30
+ demo_mailer = DemoMailer.new(:sample_mail)
31
+ demo_mailer.body("Hello world!")
32
+ assert_equal({ :body => "Hello world!" }, demo_mailer.mail_attributes)
33
+ end
34
+ end
35
+
36
+ context 'for retrieving template path' do
37
+ should "return correct path" do
38
+ demo_mailer = DemoMailer.new(:sample_mail)
39
+ assert_match %r{demo_mailer/sample_mail.erb}, demo_mailer.template_path
40
+ end
41
+ end
42
+
43
+ context 'for #deliver class method' do
44
+ should "perform the email delivery for sendmail" do
45
+ Pony.expects(:mail).with(:from => 'test@default.com', :to => 'test@test.com', :body => "Hello world!", :via => :sendmail)
46
+ DemoMailer.deliver(:sample_mail)
47
+ end
48
+
49
+ should "perform the email delivery for smtp" do
50
+ DemoMailer.smtp_settings = { :host => 'smtp.arcadic.com' }
51
+ Pony.expects(:mail).with(:from => 'test@default.com', :to => 'test@test.com',
52
+ :body => "SMTP Hello world!", :via => :smtp, :smtp => { :host => 'smtp.arcadic.com' })
53
+ DemoMailer.deliver(:sample_mail_smtp)
54
+ end
55
+ end
56
+
57
+ context 'for #respond_to? class method' do
58
+ should "respond as true for any delivery method calls for mails that exist" do
59
+ assert DemoMailer.respond_to?(:deliver_sample_mail)
60
+ end
61
+
62
+ should "respond as false for any delivery method calls for mails that don't exist" do
63
+ assert_equal false, DemoMailer.respond_to?(:deliver_faker_mail)
64
+ end
65
+
66
+ should "respond as true for any non-delivery methods that exist" do
67
+ assert DemoMailer.respond_to?(:inspect)
68
+ end
69
+
70
+ should "respond as false for any non-delivery methods that don't exist" do
71
+ assert_equal false, DemoMailer.respond_to?(:fake_method)
72
+ end
73
+ end
74
+
75
+ context 'for #method_missing dynamic delivery' do
76
+ should 'invoke deliver method with appropriate parameters' do
77
+ DemoMailer.expects(:deliver).with("example_name", "test", 5)
78
+ DemoMailer.deliver_example_name("test", 5)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,171 @@
1
+ require 'helper'
2
+ require 'fixtures/markup_app/app'
3
+
4
+ class TestAssetTagHelpers < Test::Unit::TestCase
5
+ include Sinatra::MarkupPlugin::AssetTagHelpers
6
+
7
+ def app
8
+ MarkupDemo.tap { |app| app.set :environment, :test }
9
+ end
10
+
11
+ def flash
12
+ { :notice => "Demo notice" }
13
+ end
14
+
15
+ context 'for #flash_tag method' do
16
+ should "display flash with no given attributes" do
17
+ assert_has_tag('div.flash', :content => "Demo notice") { flash_tag(:notice) }
18
+ end
19
+ 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
+ end
24
+
25
+ context 'for #link_to method' do
26
+ should "display link element with no given attributes" do
27
+ assert_has_tag('a', :content => "Sign up", :href => '/register') { link_to('Sign up', '/register') }
28
+ end
29
+ should "display link element with given attributes" do
30
+ actual_html = link_to('Sign up', '/register', :class => 'first', :id => 'linky')
31
+ assert_has_tag('a#linky.first', :content => "Sign up", :href => '/register') { actual_html }
32
+ end
33
+ should "display link element with ruby block" do
34
+ actual_link = link_to('/register', :class => 'first', :id => 'binky') { "Sign up" }
35
+ assert_has_tag('a#binky.first', :content => "Sign up", :href => '/register') { actual_link }
36
+ end
37
+ should "display link block element in haml" do
38
+ visit '/haml/link_to'
39
+ assert_have_selector :a, :content => "Test 1 No Block", :href => '/test1', :class => 'test', :id => 'test1'
40
+ assert_have_selector :a, :content => "Test 2 With Block", :href => '/test2', :class => 'test', :id => 'test2'
41
+ end
42
+ should "display link block element in erb" do
43
+ visit '/erb/link_to'
44
+ assert_have_selector :a, :content => "Test 1 No Block", :href => '/test1', :class => 'test', :id => 'test1'
45
+ assert_have_selector :a, :content => "Test 2 With Block", :href => '/test2', :class => 'test', :id => 'test2'
46
+ end
47
+ end
48
+
49
+ context 'for #mail_to method' do
50
+ should "display link element for mail to no caption" do
51
+ actual_html = mail_to('test@demo.com')
52
+ assert_has_tag(:a, :href => "mailto:test@demo.com", :content => 'test@demo.com') { actual_html }
53
+ end
54
+
55
+ should "display link element for mail to with caption" do
56
+ actual_html = mail_to('test@demo.com', "My Email", :class => 'demo')
57
+ assert_has_tag(:a, :href => "mailto:test@demo.com", :content => 'My Email', :class => 'demo') { actual_html }
58
+ end
59
+
60
+ should "display link element for mail to with caption and mail options" do
61
+ actual_html = mail_to('test@demo.com', "My Email", :subject => 'demo test', :class => 'demo', :cc => 'foo@test.com')
62
+ assert_has_tag(:a, :class => 'demo') { actual_html }
63
+ assert_match /mailto\:test\@demo.com\?/, actual_html
64
+ assert_match /cc=foo\@test\.com/, actual_html
65
+ assert_match /subject\=demo\%20test/, actual_html
66
+ end
67
+
68
+ should "display mail link element in haml" do
69
+ visit '/haml/mail_to'
70
+ assert_have_selector 'p.simple a', :href => 'mailto:test@demo.com', :content => 'test@demo.com'
71
+ assert_have_selector 'p.captioned a', :href => 'mailto:test@demo.com', :content => 'Click my Email'
72
+ end
73
+
74
+ should "display mail link element in erb" do
75
+ visit '/erb/mail_to'
76
+ assert_have_selector 'p.simple a', :href => 'mailto:test@demo.com', :content => 'test@demo.com'
77
+ assert_have_selector 'p.captioned a', :href => 'mailto:test@demo.com', :content => 'Click my Email'
78
+ end
79
+ end
80
+
81
+ context 'for #image_tag method' do
82
+ should "display image tag absolute link with no options" do
83
+ assert_has_tag('img', :src => "/absolute/pic.gif") { image_tag('/absolute/pic.gif') }
84
+ end
85
+ should "display image tag relative link with options" do
86
+ assert_has_tag('img.photo', :src => "/images/relative/pic.gif") { image_tag('relative/pic.gif', :class => 'photo') }
87
+ end
88
+ should "display image tag uri link with options" do
89
+ assert_has_tag('img.photo', :src => "http://demo.org/pic.gif") { image_tag('http://demo.org/pic.gif', :class => 'photo') }
90
+ end
91
+ should "display image tag relative link with incorrect spacing" do
92
+ assert_has_tag('img.photo', :src => "/images/relative/pic.gif") { image_tag(' relative/ pic.gif ', :class => 'photo') }
93
+ end
94
+ end
95
+
96
+ context 'for #stylesheet_link_tag method' do
97
+ should "display stylesheet link item" do
98
+ time = stop_time_for_test
99
+ expected_options = { :media => "screen", :rel => "stylesheet", :type => "text/css" }
100
+ assert_has_tag('link', expected_options.merge(:href => "/stylesheets/style.css?#{time.to_i}")) { stylesheet_link_tag('style') }
101
+ end
102
+ should "display stylesheet link item for long relative path" do
103
+ time = stop_time_for_test
104
+ expected_options = { :media => "screen", :rel => "stylesheet", :type => "text/css" }
105
+ actual_html = stylesheet_link_tag('example/demo/style')
106
+ assert_has_tag('link', expected_options.merge(:href => "/stylesheets/example/demo/style.css?#{time.to_i}")) { actual_html }
107
+ end
108
+ should "display stylesheet link items" do
109
+ time = stop_time_for_test
110
+ actual_html = stylesheet_link_tag('style', 'layout.css', 'http://google.com/style.css')
111
+ assert_has_tag('link', :media => "screen", :rel => "stylesheet", :type => "text/css", :count => 3) { actual_html }
112
+ assert_has_tag('link', :href => "/stylesheets/style.css?#{time.to_i}") { actual_html }
113
+ assert_has_tag('link', :href => "/stylesheets/layout.css?#{time.to_i}") { actual_html }
114
+ assert_has_tag('link', :href => "http://google.com/style.css") { actual_html }
115
+ end
116
+ end
117
+
118
+ context 'for #javascript_include_tag method' do
119
+ should "display javascript item" do
120
+ time = stop_time_for_test
121
+ actual_html = javascript_include_tag('application')
122
+ assert_has_tag('script', :src => "/javascripts/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
123
+ end
124
+ should "display javascript item for path containing js" do
125
+ time = stop_time_for_test
126
+ actual_html = javascript_include_tag 'test/jquery.json'
127
+ assert_has_tag('script', :src => "/javascripts/test/jquery.json?#{time.to_i}", :type => "text/javascript") { actual_html }
128
+ end
129
+ should "display javascript item for path containing period" do
130
+ time = stop_time_for_test
131
+ actual_html = javascript_include_tag 'test/jquery.min'
132
+ assert_has_tag('script', :src => "/javascripts/test/jquery.min.js?#{time.to_i}", :type => "text/javascript") { actual_html }
133
+ end
134
+ should "display javascript item for long relative path" do
135
+ time = stop_time_for_test
136
+ actual_html = javascript_include_tag('example/demo/application')
137
+ assert_has_tag('script', :src => "/javascripts/example/demo/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
138
+ end
139
+ should "display javascript items" do
140
+ time = stop_time_for_test
141
+ actual_html = javascript_include_tag('application', 'base.js', 'http://google.com/lib.js')
142
+ assert_has_tag('script', :type => "text/javascript", :count => 3) { actual_html }
143
+ assert_has_tag('script', :src => "/javascripts/application.js?#{time.to_i}") { actual_html }
144
+ assert_has_tag('script', :src => "/javascripts/base.js?#{time.to_i}") { actual_html }
145
+ assert_has_tag('script', :src => "http://google.com/lib.js") { actual_html }
146
+ end
147
+ end
148
+
149
+ context 'for #meta_tag method' do
150
+ should "display meta tag with given content and name" do
151
+ actual_html = meta_tag("weblog,news", :name => "keywords")
152
+ assert_has_tag("meta", :name => "keywords", "content" => "weblog,news") { actual_html }
153
+ end
154
+ should "display meta tag with given content and http-equiv" do
155
+ actual_html = meta_tag("text/html; charset=UTF-8", :"http-equiv" => "Content-Type")
156
+ assert_has_tag("meta", :"http-equiv" => "Content-Type", "content" => "text/html; charset=UTF-8") { actual_html }
157
+ end
158
+ should "display meta tag element in haml" do
159
+ visit '/haml/meta_tag'
160
+ assert_have_selector 'meta', "content" => "weblog,news", :name => "keywords"
161
+ assert_have_selector 'meta', "content" => "text/html; charset=UTF-8", :"http-equiv" => "Content-Type"
162
+ end
163
+ should "display meta tag element in erb" do
164
+ visit '/erb/meta_tag'
165
+ assert_have_selector 'meta', "content" => "weblog,news", :name => "keywords"
166
+ assert_have_selector 'meta', "content" => "text/html; charset=UTF-8", :"http-equiv" => "Content-Type"
167
+ end
168
+
169
+ end
170
+
171
+ end
@@ -0,0 +1,627 @@
1
+ require 'helper'
2
+ require 'fixtures/markup_app/app'
3
+
4
+ class TestFormBuilder < Test::Unit::TestCase
5
+ include Sinatra::MarkupPlugin::FormHelpers
6
+
7
+ def app
8
+ MarkupDemo.tap { |app| app.set :environment, :test }
9
+ end
10
+
11
+ def setup
12
+ error_stub = stub(:full_messages => ["1", "2"], :none? => false)
13
+ role_types = [stub(:name => 'Admin', :id => 1), stub(:name => 'Moderate', :id => 2), stub(:name => 'Limited', :id => 3)]
14
+ @user = stub(:errors => error_stub, :class => 'User', :first_name => "Joe", :session_id => 54)
15
+ @user.stubs(:role_types => role_types, :role => "1")
16
+ @user_none = stub(:errors => stub(:none? => true), :class => 'User')
17
+ end
18
+
19
+ def standard_builder(object=@user)
20
+ StandardFormBuilder.new(self, object)
21
+ end
22
+
23
+ context 'for #form_for method' do
24
+ should "display correct form html" do
25
+ actual_html = form_for(@user, '/register', :id => 'register', :method => 'post') { "Demo" }
26
+ assert_has_tag('form', :action => '/register', :id => 'register', :method => 'post', :content => "Demo") { actual_html }
27
+ assert_has_tag('form input[type=hidden]', :name => '_method', :count => 0) { actual_html } # no method action field
28
+ end
29
+
30
+ should "display correct form html with fake object" do
31
+ actual_html = form_for(:markup_user, '/register', :id => 'register', :method => 'post') { |f| f.text_field :username }
32
+ assert_has_tag('form', :action => '/register', :id => 'register', :method => 'post') { actual_html }
33
+ assert_has_tag('form input', :type => 'text', :name => 'markup_user[username]') { actual_html }
34
+ assert_has_tag('form input[type=hidden]', :name => '_method', :count => 0) { actual_html } # no method action field
35
+ end
36
+
37
+ should "display correct form html for namespaced object" do
38
+ actual_html = form_for(Outer::UserAccount.new, '/register', :method => 'post') { |f| f.text_field :username }
39
+ assert_has_tag('form', :action => '/register', :method => 'post') { actual_html }
40
+ assert_has_tag('form input', :type => 'text', :name => 'outer-user_account[username]') { actual_html }
41
+ end
42
+
43
+ should "display correct form html with method :put" do
44
+ actual_html = form_for(@user, '/update', :method => 'put') { "Demo" }
45
+ assert_has_tag('form', :action => '/update', :method => 'post') { actual_html }
46
+ assert_has_tag('form input', :type => 'hidden', :name => "_method", :value => 'put') { actual_html }
47
+ end
48
+
49
+ should "display correct form html with method :delete" do
50
+ actual_html = form_for(@user, '/destroy', :method => 'delete') { "Demo" }
51
+ assert_has_tag('form', :action => '/destroy', :method => 'post') { actual_html }
52
+ assert_has_tag('form input', :type => 'hidden', :name => "_method", :value => 'delete') { actual_html }
53
+ end
54
+
55
+ should "display correct form html with multipart" do
56
+ actual_html = form_for(@user, '/register', :multipart => true) { "Demo" }
57
+ assert_has_tag('form', :action => '/register', :enctype => "multipart/form-data") { actual_html }
58
+ end
59
+
60
+ should "support changing form builder type" do
61
+ form_html = lambda { form_for(@user, '/register', :builder => "AbstractFormBuilder") { |f| f.text_field_block(:name) } }
62
+ assert_raise(NoMethodError) { form_html.call }
63
+ end
64
+
65
+ should "support using default standard builder" do
66
+ actual_html = form_for(@user, '/register') { |f| f.text_field_block(:name) }
67
+ assert_has_tag('form p input[type=text]') { actual_html }
68
+ end
69
+
70
+ should "display fail for form with nil object" do
71
+ assert_raises(RuntimeError) { form_for(@not_real, '/register', :id => 'register', :method => 'post') { "Demo" } }
72
+ end
73
+
74
+ should "display correct form in haml" do
75
+ visit '/haml/form_for'
76
+ assert_have_selector :form, :action => '/demo', :id => 'demo'
77
+ assert_have_selector :form, :action => '/another_demo', :id => 'demo2', :method => 'get'
78
+ assert_have_selector :form, :action => '/third_demo', :id => 'demo3', :method => 'get'
79
+ end
80
+
81
+ should "display correct form in erb" do
82
+ visit '/erb/form_for'
83
+ assert_have_selector :form, :action => '/demo', :id => 'demo'
84
+ assert_have_selector :form, :action => '/another_demo', :id => 'demo2', :method => 'get'
85
+ assert_have_selector :form, :action => '/third_demo', :id => 'demo3', :method => 'get'
86
+ end
87
+ end
88
+
89
+ context 'for #fields_for method' do
90
+ should 'display correct fields html' do
91
+ actual_html = fields_for(@user) { |f| f.text_field(:first_name) }
92
+ assert_has_tag(:input, :type => 'text', :name => 'user[first_name]', :id => 'user_first_name') { actual_html }
93
+ end
94
+
95
+ should 'display correct fields html with symbol object' do
96
+ actual_html = fields_for(:markup_user) { |f| f.text_field(:first_name) }
97
+ assert_has_tag(:input, :type => 'text', :name => 'markup_user[first_name]', :id => 'markup_user_first_name') { actual_html }
98
+ end
99
+
100
+ should "display fail for nil object" do
101
+ assert_raises(RuntimeError) { fields_for(@not_real) { |f| "Demo" } }
102
+ end
103
+
104
+ should 'display correct simple fields in haml' do
105
+ visit '/haml/fields_for'
106
+ assert_have_selector :form, :action => '/demo1', :id => 'demo-fields-for'
107
+ assert_have_selector '#demo-fields-for input', :type => 'text', :name => 'markup_user[gender]', :value => 'male'
108
+ assert_have_selector '#demo-fields-for input', :type => 'checkbox', :name => 'permission[can_edit]', :value => '1', :checked => 'checked'
109
+ assert_have_selector '#demo-fields-for input', :type => 'checkbox', :name => 'permission[can_delete]'
110
+ end
111
+
112
+ should "display correct simple fields in erb" do
113
+ visit '/erb/fields_for'
114
+ assert_have_selector :form, :action => '/demo1', :id => 'demo-fields-for'
115
+ assert_have_selector '#demo-fields-for input', :type => 'text', :name => 'markup_user[gender]', :value => 'male'
116
+ assert_have_selector '#demo-fields-for input', :type => 'checkbox', :name => 'permission[can_edit]', :value => '1', :checked => 'checked'
117
+ assert_have_selector '#demo-fields-for input', :type => 'checkbox', :name => 'permission[can_delete]'
118
+ end
119
+ end
120
+
121
+ # ===========================
122
+ # AbstractFormBuilder
123
+ # ===========================
124
+
125
+ context 'for #error_messages method' do
126
+ should "display correct form html with no record" do
127
+ actual_html = standard_builder(@user_none).error_messages(:header_message => "Demo form cannot be saved")
128
+ assert actual_html.blank?
129
+ end
130
+
131
+ should "display correct form html with valid record" do
132
+ actual_html = standard_builder.error_messages(:header_message => "Demo form cannot be saved")
133
+ assert_has_tag('div.field-errors p', :content => "Demo form cannot be saved") { actual_html }
134
+ assert_has_tag('div.field-errors ul.errors-list li', :content => "1") { actual_html }
135
+ assert_has_tag('div.field-errors ul.errors-list li', :content => "2") { actual_html }
136
+ end
137
+
138
+ should "display correct form in haml" do
139
+ visit '/haml/form_for'
140
+ assert_have_selector '#demo div.field-errors p', :content => "custom MarkupUser cannot be saved!"
141
+ assert_have_selector '#demo div.field-errors ul.errors-list li', :content => "This is a fake error"
142
+ assert_have_selector '#demo2 div.field-errors p', :content => "custom MarkupUser cannot be saved!"
143
+ assert_have_selector '#demo2 div.field-errors ul.errors-list li', :content => "This is a fake error"
144
+ end
145
+
146
+ should "display correct form in erb" do
147
+ visit '/erb/form_for'
148
+ assert_have_selector '#demo div.field-errors p', :content => "custom MarkupUser cannot be saved!"
149
+ assert_have_selector '#demo div.field-errors ul.errors-list li', :content => "This is a fake error"
150
+ assert_have_selector '#demo2 div.field-errors p', :content => "custom MarkupUser cannot be saved!"
151
+ assert_have_selector '#demo2 div.field-errors ul.errors-list li', :content => "This is a fake error"
152
+ end
153
+ end
154
+
155
+ context 'for #label method' do
156
+ should "display correct label html" do
157
+ actual_html = standard_builder.label(:first_name, :class => 'large', :caption => "F. Name: ")
158
+ assert_has_tag('label', :class => 'large', :for => 'user_first_name', :content => "F. Name: ") { actual_html }
159
+ end
160
+
161
+ should "display correct label in haml" do
162
+ visit '/haml/form_for'
163
+ assert_have_selector '#demo label', :content => "Login: ", :class => 'user-label'
164
+ assert_have_selector '#demo label', :content => "About Me: "
165
+ assert_have_selector '#demo2 label', :content => "Nickname: ", :class => 'label'
166
+ end
167
+
168
+ should "display correct label in erb" do
169
+ visit '/erb/form_for'
170
+ assert_have_selector '#demo label', :content => "Login: ", :class => 'user-label'
171
+ assert_have_selector '#demo label', :content => "About Me: "
172
+ assert_have_selector '#demo2 label', :content => "Nickname: ", :class => 'label'
173
+ end
174
+ end
175
+
176
+ context 'for #hidden_field method' do
177
+ should "display correct hidden field html" do
178
+ actual_html = standard_builder.hidden_field(:session_id, :class => 'hidden')
179
+ assert_has_tag('input.hidden[type=hidden]', :value => "54", :id => 'user_session_id', :name => 'user[session_id]') { actual_html }
180
+ end
181
+
182
+ should "display correct hidden field in haml" do
183
+ visit '/haml/form_for'
184
+ assert_have_selector '#demo input[type=hidden]', :id => 'markup_user_session_id', :value => "45"
185
+ assert_have_selector '#demo2 input', :type => 'hidden', :name => 'markup_user[session_id]'
186
+ end
187
+
188
+ should "display correct hidden field in erb" do
189
+ visit '/erb/form_for'
190
+ assert_have_selector '#demo input[type=hidden]', :id => 'markup_user_session_id', :value => "45"
191
+ assert_have_selector '#demo2 input', :type => 'hidden', :name => 'markup_user[session_id]'
192
+ end
193
+ end
194
+
195
+ context 'for #text_field method' do
196
+ should "display correct text field html" do
197
+ actual_html = standard_builder.text_field(:first_name, :class => 'large')
198
+ assert_has_tag('input.large[type=text]', :value => "Joe", :id => 'user_first_name', :name => 'user[first_name]') { actual_html }
199
+ end
200
+
201
+ should "display correct text field in haml" do
202
+ visit '/haml/form_for'
203
+ assert_have_selector '#demo input.user-text[type=text]', :id => 'markup_user_username', :value => "John"
204
+ assert_have_selector '#demo2 input', :type => 'text', :class => 'input', :name => 'markup_user[username]'
205
+ end
206
+
207
+ should "display correct text field in erb" do
208
+ visit '/erb/form_for'
209
+ assert_have_selector '#demo input.user-text[type=text]', :id => 'markup_user_username', :value => "John"
210
+ assert_have_selector '#demo2 input', :type => 'text', :class => 'input', :name => 'markup_user[username]'
211
+ end
212
+ end
213
+
214
+ context 'for #check_box method' do
215
+ should "display correct checkbox html" do
216
+ actual_html = standard_builder.check_box(:confirm_destroy, :class => 'large')
217
+ assert_has_tag('input.large[type=checkbox]', :id => 'user_confirm_destroy', :name => 'user[confirm_destroy]') { actual_html }
218
+ assert_has_tag('input[type=hidden]', :name => 'user[confirm_destroy]', :value => '0') { actual_html }
219
+ end
220
+
221
+ should "display correct checkbox html when checked" do
222
+ actual_html = standard_builder.check_box(:confirm_destroy, :checked => true)
223
+ assert_has_tag('input[type=checkbox]', :checked => 'checked', :name => 'user[confirm_destroy]') { actual_html }
224
+ end
225
+
226
+ should "display correct checkbox html as checked when object value matches" do
227
+ @user.stubs(:show_favorites => 'human')
228
+ actual_html = standard_builder.check_box(:show_favorites, :value => 'human')
229
+ assert_has_tag('input[type=checkbox]', :checked => 'checked', :name => 'user[show_favorites]') { actual_html }
230
+ end
231
+
232
+ should "display correct checkbox html as checked when object value is true" do
233
+ @user.stubs(:show_favorites => true)
234
+ actual_html = standard_builder.check_box(:show_favorites, :value => '1')
235
+ assert_has_tag('input[type=checkbox]', :checked => 'checked', :name => 'user[show_favorites]') { actual_html }
236
+ end
237
+
238
+ should "display correct checkbox html as unchecked when object value doesn't match" do
239
+ @user.stubs(:show_favorites => 'alien')
240
+ actual_html = standard_builder.check_box(:show_favorites, :value => 'human')
241
+ assert_has_no_tag('input[type=checkbox]', :checked => 'checked') { actual_html }
242
+ end
243
+
244
+ should "display correct checkbox html as unchecked when object value is false" do
245
+ @user.stubs(:show_favorites => false)
246
+ actual_html = standard_builder.check_box(:show_favorites, :value => '1')
247
+ assert_has_no_tag('input[type=checkbox]', :checked => 'checked') { actual_html }
248
+ end
249
+
250
+ should "display correct unchecked hidden field when specified" do
251
+ actual_html = standard_builder.check_box(:show_favorites, :value => 'female', :uncheck_value => 'false')
252
+ assert_has_tag('input[type=hidden]', :name => 'user[show_favorites]', :value => 'false') { actual_html }
253
+ end
254
+
255
+ should "display correct checkbox in haml" do
256
+ visit '/haml/form_for'
257
+ assert_have_selector '#demo input[type=checkbox]', :checked => 'checked', :id => 'markup_user_remember_me', :name => 'markup_user[remember_me]'
258
+ end
259
+
260
+ should "display correct checkbox in erb" do
261
+ visit '/erb/form_for'
262
+ assert_have_selector '#demo input[type=checkbox]', :checked => 'checked', :id => 'markup_user_remember_me', :name => 'markup_user[remember_me]'
263
+ end
264
+ end
265
+
266
+ context 'for #radio_button method' do
267
+ should "display correct radio button html" do
268
+ actual_html = standard_builder.radio_button(:gender, :value => 'male', :class => 'large')
269
+ assert_has_tag('input.large[type=radio]', :id => 'user_gender_male', :name => 'user[gender]', :value => 'male') { actual_html }
270
+ end
271
+
272
+ should "display correct radio button html when checked" do
273
+ actual_html = standard_builder.radio_button(:gender, :checked => true)
274
+ assert_has_tag('input[type=radio]', :checked => 'checked', :name => 'user[gender]') { actual_html }
275
+ end
276
+
277
+ should "display correct radio button html as checked when object value matches" do
278
+ @user.stubs(:gender => 'male')
279
+ actual_html = standard_builder.radio_button(:gender, :value => 'male')
280
+ assert_has_tag('input[type=radio]', :checked => 'checked', :name => 'user[gender]') { actual_html }
281
+ end
282
+
283
+ should "display correct radio button html as unchecked when object value doesn't match" do
284
+ @user.stubs(:gender => 'male')
285
+ actual_html = standard_builder.radio_button(:gender, :value => 'female')
286
+ assert_has_no_tag('input[type=radio]', :checked => 'checked') { actual_html }
287
+ end
288
+
289
+ should "display correct radio button in haml" do
290
+ visit '/haml/form_for'
291
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_male', :name => 'markup_user[gender]', :value => 'male'
292
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_female', :name => 'markup_user[gender]', :value => 'female'
293
+ assert_have_selector '#demo input[type=radio][checked=checked]', :id => 'markup_user_gender_male'
294
+ end
295
+
296
+ should "display correct radio button in erb" do
297
+ visit '/erb/form_for'
298
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_male', :name => 'markup_user[gender]', :value => 'male'
299
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_female', :name => 'markup_user[gender]', :value => 'female'
300
+ assert_have_selector '#demo input[type=radio][checked=checked]', :id => 'markup_user_gender_male'
301
+ end
302
+ end
303
+
304
+ context 'for #text_area method' do
305
+ should "display correct text_area html" do
306
+ actual_html = standard_builder.text_area(:about, :class => 'large')
307
+ assert_has_tag('textarea.large', :id => 'user_about', :name => 'user[about]') { actual_html }
308
+ end
309
+
310
+ should "display correct text_area html and custom content" do
311
+ actual_html = standard_builder.text_area(:about, :value => "Demo")
312
+ assert_has_tag('textarea', :id => 'user_about', :content => 'Demo') { actual_html }
313
+ end
314
+
315
+ should "display correct text_area html and default content" do
316
+ actual_html = standard_builder(MarkupUser.new).text_area(:about)
317
+ assert_has_tag('textarea', :id => 'markup_user_about', :content => '') { actual_html }
318
+ end
319
+
320
+ should "display correct text_area in haml" do
321
+ visit '/haml/form_for'
322
+ assert_have_selector '#demo textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'user-about'
323
+ assert_have_selector '#demo2 textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'textarea'
324
+ end
325
+
326
+ should "display correct text_area in erb" do
327
+ visit '/erb/form_for'
328
+ assert_have_selector '#demo textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'user-about'
329
+ assert_have_selector '#demo2 textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'textarea'
330
+ end
331
+ end
332
+
333
+ context 'for #password_field method' do
334
+ should "display correct password_field html" do
335
+ actual_html = standard_builder.password_field(:code, :class => 'large')
336
+ assert_has_tag('input.large[type=password]', :id => 'user_code', :name => 'user[code]') { actual_html }
337
+ end
338
+
339
+ should "display correct password_field in haml" do
340
+ visit '/haml/form_for'
341
+ assert_have_selector '#demo input', :type => 'password', :class => 'user-password', :value => 'secret'
342
+ assert_have_selector '#demo2 input', :type => 'password', :class => 'input', :name => 'markup_user[code]'
343
+ end
344
+
345
+ should "display correct password_field in erb" do
346
+ visit '/erb/form_for'
347
+ assert_have_selector '#demo input', :type => 'password', :class => 'user-password', :value => 'secret'
348
+ assert_have_selector '#demo2 input', :type => 'password', :class => 'input', :name => 'markup_user[code]'
349
+ end
350
+ end
351
+
352
+ context 'for #file_field method' do
353
+ should "display correct file_field html" do
354
+ actual_html = standard_builder.file_field(:photo, :class => 'large')
355
+ assert_has_tag('input.large[type=file]', :id => 'user_photo', :name => 'user[photo]') { actual_html }
356
+ end
357
+
358
+ should "display correct file_field in haml" do
359
+ visit '/haml/form_for'
360
+ assert_have_selector '#demo input.user-photo', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
361
+ assert_have_selector '#demo2 input.upload', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
362
+ end
363
+
364
+ should "display correct file_field in erb" do
365
+ visit '/erb/form_for'
366
+ assert_have_selector '#demo input.user-photo', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
367
+ assert_have_selector '#demo2 input.upload', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
368
+ end
369
+ end
370
+
371
+ context 'for #select method' do
372
+ should "display correct select html" do
373
+ actual_html = standard_builder.select(:state, :options => ['California', 'Texas', 'Wyoming'], :class => 'selecty')
374
+ assert_has_tag('select.selecty', :id => 'user_state', :name => 'user[state]') { actual_html }
375
+ assert_has_tag('select.selecty option', :count => 3) { actual_html }
376
+ assert_has_tag('select.selecty option', :value => 'California', :content => 'California') { actual_html }
377
+ assert_has_tag('select.selecty option', :value => 'Texas', :content => 'Texas') { actual_html }
378
+ assert_has_tag('select.selecty option', :value => 'Wyoming', :content => 'Wyoming') { actual_html }
379
+ end
380
+
381
+ should "display correct select html with selected item if it matches value" do
382
+ @user.stubs(:state => 'California')
383
+ actual_html = standard_builder.select(:state, :options => ['California', 'Texas', 'Wyoming'])
384
+ assert_has_tag('select', :id => 'user_state', :name => 'user[state]') { actual_html }
385
+ assert_has_tag('select option', :selected => 'selected', :count => 1) { actual_html }
386
+ assert_has_tag('select option', :value => 'California', :selected => 'selected') { actual_html }
387
+ end
388
+
389
+ should "display correct select html with include_blank" do
390
+ actual_html = standard_builder.select(:state, :options => ['California', 'Texas', 'Wyoming'], :include_blank => true)
391
+ assert_has_tag('select', :id => 'user_state', :name => 'user[state]') { actual_html }
392
+ assert_has_tag('select option', :count => 4) { actual_html }
393
+ assert_has_tag('select option:first-child', :content => '') { actual_html }
394
+ end
395
+
396
+ should "display correct select html with collection passed in" do
397
+ actual_html = standard_builder.select(:role, :collection => @user.role_types, :fields => [:name, :id])
398
+ assert_has_tag('select', :id => 'user_role', :name => 'user[role]') { actual_html }
399
+ assert_has_tag('select option', :count => 3) { actual_html }
400
+ assert_has_tag('select option', :value => '1', :content => 'Admin', :selected => 'selected') { actual_html }
401
+ assert_has_tag('select option', :value => '2', :content => 'Moderate') { actual_html }
402
+ assert_has_tag('select option', :value => '3', :content => 'Limited') { actual_html }
403
+ end
404
+
405
+ should "display correct select in haml" do
406
+ visit '/haml/form_for'
407
+ assert_have_selector '#demo textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'user-about'
408
+ assert_have_selector '#demo2 textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'textarea'
409
+ end
410
+
411
+ should "display correct select in erb" do
412
+ visit '/erb/form_for'
413
+ assert_have_selector '#demo textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'user-about'
414
+ assert_have_selector '#demo2 textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'textarea'
415
+ end
416
+ end
417
+
418
+ context 'for #submit method' do
419
+ should "display correct submit button html with no options" do
420
+ actual_html = standard_builder.submit
421
+ assert_has_tag('input[type=submit]', :value => "Submit") { actual_html }
422
+ end
423
+
424
+ should "display correct submit button html" do
425
+ actual_html = standard_builder.submit("Commit", :class => 'large')
426
+ assert_has_tag('input.large[type=submit]', :value => "Commit") { actual_html }
427
+ end
428
+
429
+ should "display correct submit button in haml" do
430
+ visit '/haml/form_for'
431
+ assert_have_selector '#demo input', :type => 'submit', :id => 'demo-button', :class => 'success'
432
+ assert_have_selector '#demo2 input', :type => 'submit', :class => 'button', :value => "Create"
433
+ end
434
+
435
+ should "display correct submit button in erb" do
436
+ visit '/erb/form_for'
437
+ assert_have_selector '#demo input', :type => 'submit', :id => 'demo-button', :class => 'success'
438
+ assert_have_selector '#demo2 input', :type => 'submit', :class => 'button', :value => "Create"
439
+ end
440
+ end
441
+
442
+ context 'for #image_submit method' do
443
+ should "display correct image submit button html with no options" do
444
+ actual_html = standard_builder.image_submit('buttons/ok.png')
445
+ assert_has_tag('input[type=image]', :src => "/images/buttons/ok.png") { actual_html }
446
+ end
447
+
448
+ should "display correct image submit button html" do
449
+ actual_html = standard_builder.image_submit('/system/ok.png', :class => 'large')
450
+ assert_has_tag('input.large[type=image]', :src => "/system/ok.png") { actual_html }
451
+ end
452
+
453
+ should "display correct image submit button in haml" do
454
+ visit '/haml/form_for'
455
+ assert_have_selector '#demo input', :type => 'image', :id => 'image-button', :src => '/images/buttons/post.png'
456
+ assert_have_selector '#demo2 input', :type => 'image', :class => 'image', :src => "/images/buttons/ok.png"
457
+ end
458
+
459
+ should "display correct image submit button in erb" do
460
+ visit '/erb/form_for'
461
+ assert_have_selector '#demo input', :type => 'image', :id => 'image-button', :src => '/images/buttons/post.png'
462
+ assert_have_selector '#demo2 input', :type => 'image', :class => 'image', :src => "/images/buttons/ok.png"
463
+ end
464
+ end
465
+
466
+ # ===========================
467
+ # StandardFormBuilder
468
+ # ===========================
469
+
470
+ context 'for #text_field_block method' do
471
+ should "display correct text field block html" do
472
+ actual_html = standard_builder.text_field_block(:first_name, :class => 'large', :caption => "FName")
473
+ assert_has_tag('p label', :for => 'user_first_name', :content => "FName") { actual_html }
474
+ assert_has_tag('p input.large[type=text]', :value => "Joe", :id => 'user_first_name', :name => 'user[first_name]') { actual_html }
475
+ end
476
+
477
+ should "display correct text field block in haml" do
478
+ visit '/haml/form_for'
479
+ assert_have_selector '#demo2 p label', :for => 'markup_user_username', :content => "Nickname", :class => 'label'
480
+ assert_have_selector '#demo2 p input', :type => 'text', :name => 'markup_user[username]', :id => 'markup_user_username'
481
+ end
482
+
483
+ should "display correct text field block in erb" do
484
+ visit '/erb/form_for'
485
+ assert_have_selector '#demo2 p label', :for => 'markup_user_username', :content => "Nickname", :class => 'label'
486
+ assert_have_selector '#demo2 p input', :type => 'text', :name => 'markup_user[username]', :id => 'markup_user_username'
487
+ end
488
+ end
489
+
490
+ context 'for #text_area_block method' do
491
+ should "display correct text area block html" do
492
+ actual_html = standard_builder.text_area_block(:about, :class => 'large', :caption => "About Me")
493
+ assert_has_tag('p label', :for => 'user_about', :content => "About Me") { actual_html }
494
+ assert_has_tag('p textarea', :id => 'user_about', :name => 'user[about]') { actual_html }
495
+ end
496
+
497
+ should "display correct text area block in haml" do
498
+ visit '/haml/form_for'
499
+ assert_have_selector '#demo2 p label', :for => 'markup_user_about', :content => "About: "
500
+ assert_have_selector '#demo2 p textarea', :name => 'markup_user[about]', :id => 'markup_user_about'
501
+ end
502
+
503
+ should "display correct text area block in erb" do
504
+ visit '/erb/form_for'
505
+ assert_have_selector '#demo2 p label', :for => 'markup_user_about', :content => "About: "
506
+ assert_have_selector '#demo2 p textarea', :name => 'markup_user[about]', :id => 'markup_user_about'
507
+ end
508
+ end
509
+
510
+ context 'for #password_field_block method' do
511
+ should "display correct password field block html" do
512
+ actual_html = standard_builder.password_field_block(:keycode, :class => 'large', :caption => "Code: ")
513
+ assert_has_tag('p label', :for => 'user_keycode', :content => "Code: ") { actual_html }
514
+ assert_has_tag('p input.large[type=password]', :id => 'user_keycode', :name => 'user[keycode]') { actual_html }
515
+ end
516
+
517
+ should "display correct password field block in haml" do
518
+ visit '/haml/form_for'
519
+ assert_have_selector '#demo2 p label', :for => 'markup_user_code', :content => "Code: "
520
+ assert_have_selector '#demo2 p input', :type => 'password', :name => 'markup_user[code]', :id => 'markup_user_code'
521
+ end
522
+
523
+ should "display correct password field block in erb" do
524
+ visit '/erb/form_for'
525
+ assert_have_selector '#demo2 p label', :for => 'markup_user_code', :content => "Code: "
526
+ assert_have_selector '#demo2 p input', :type => 'password', :name => 'markup_user[code]', :id => 'markup_user_code'
527
+ end
528
+ end
529
+
530
+ context 'for #file_field_block method' do
531
+ should "display correct file field block html" do
532
+ actual_html = standard_builder.file_field_block(:photo, :class => 'large', :caption => "Photo: ")
533
+ assert_has_tag('p label', :for => 'user_photo', :content => "Photo: ") { actual_html }
534
+ assert_has_tag('p input.large[type=file]', :id => 'user_photo', :name => 'user[photo]') { actual_html }
535
+ end
536
+
537
+ should "display correct file field block in haml" do
538
+ visit '/haml/form_for'
539
+ assert_have_selector '#demo2 p label', :for => 'markup_user_photo', :content => "Photo: "
540
+ assert_have_selector '#demo2 p input.upload', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
541
+ end
542
+
543
+ should "display correct file field block in erb" do
544
+ visit '/erb/form_for'
545
+ assert_have_selector '#demo2 p label', :for => 'markup_user_photo', :content => "Photo: "
546
+ assert_have_selector '#demo2 p input.upload', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
547
+ end
548
+ end
549
+
550
+ context 'for #check_box_block method' do
551
+ should "display correct check box block html" do
552
+ actual_html = standard_builder.check_box_block(:remember_me, :class => 'large', :caption => "Remember session?")
553
+ assert_has_tag('p label', :for => 'user_remember_me', :content => "Remember session?") { actual_html }
554
+ assert_has_tag('p input.large[type=checkbox]', :id => 'user_remember_me', :name => 'user[remember_me]') { actual_html }
555
+ end
556
+
557
+ should "display correct check box block in haml" do
558
+ visit '/haml/form_for'
559
+ assert_have_selector '#demo2 p label', :for => 'markup_user_remember_me', :content => "Remember Me: "
560
+ assert_have_selector '#demo2 p input.checker', :type => 'checkbox', :name => 'markup_user[remember_me]'
561
+ end
562
+
563
+ should "display correct check box block in erb" do
564
+ visit '/erb/form_for'
565
+ assert_have_selector '#demo2 p label', :for => 'markup_user_remember_me', :content => "Remember Me: "
566
+ assert_have_selector '#demo2 p input.checker', :type => 'checkbox', :name => 'markup_user[remember_me]'
567
+ end
568
+ end
569
+
570
+ context 'for #select_block method' do
571
+ should "display correct select_block block html" do
572
+ actual_html = standard_builder.select_block(:country, :options => ['USA', 'Canada'], :class => 'large', :caption => "Your country")
573
+ assert_has_tag('p label', :for => 'user_country', :content => "Your country") { actual_html }
574
+ assert_has_tag('p select', :id => 'user_country', :name => 'user[country]') { actual_html }
575
+ assert_has_tag('p select option', :content => 'USA') { actual_html }
576
+ assert_has_tag('p select option', :content => 'Canada') { actual_html }
577
+ end
578
+
579
+ should "display correct select_block block in haml" do
580
+ visit '/haml/form_for'
581
+ assert_have_selector '#demo2 p label', :for => 'markup_user_state', :content => "State: "
582
+ assert_have_selector '#demo2 p select', :name => 'markup_user[state]', :id => 'markup_user_state'
583
+ assert_have_selector '#demo2 p select option', :content => 'California'
584
+ assert_have_selector '#demo2 p select option', :content => 'Texas'
585
+ end
586
+
587
+ should "display correct select_block block in erb" do
588
+ visit '/erb/form_for'
589
+ assert_have_selector '#demo2 p label', :for => 'markup_user_state', :content => "State: "
590
+ assert_have_selector '#demo2 p select', :name => 'markup_user[state]', :id => 'markup_user_state'
591
+ end
592
+ end
593
+
594
+ context 'for #submit_block method' do
595
+ should "display correct submit block html" do
596
+ actual_html = standard_builder.submit_block("Update", :class => 'large')
597
+ assert_has_tag('p input.large[type=submit]', :value => 'Update') { actual_html }
598
+ end
599
+
600
+ should "display correct submit block in haml" do
601
+ visit '/haml/form_for'
602
+ assert_have_selector '#demo2 p input', :type => 'submit', :class => 'button'
603
+ end
604
+
605
+ should "display correct submit block in erb" do
606
+ visit '/erb/form_for'
607
+ assert_have_selector '#demo2 p input', :type => 'submit', :class => 'button'
608
+ end
609
+ end
610
+
611
+ context 'for #image_submit_block method' do
612
+ should "display correct image submit block html" do
613
+ actual_html = standard_builder.image_submit_block("buttons/ok.png", :class => 'large')
614
+ assert_has_tag('p input.large[type=image]', :src => '/images/buttons/ok.png') { actual_html }
615
+ end
616
+
617
+ should "display correct image submit block in haml" do
618
+ visit '/haml/form_for'
619
+ assert_have_selector '#demo2 p input', :type => 'image', :class => 'image', :src => '/images/buttons/ok.png'
620
+ end
621
+
622
+ should "display correct image submit block in erb" do
623
+ visit '/erb/form_for'
624
+ assert_have_selector '#demo2 p input', :type => 'image', :class => 'image', :src => '/images/buttons/ok.png'
625
+ end
626
+ end
627
+ end