forme 2.2.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,238 +0,0 @@
1
- require_relative 'spec_helper'
2
- require_relative 'sequel_helper'
3
-
4
- describe "Sequel forme_set plugin" do
5
- before do
6
- @ab = Album.new
7
- @f = Forme::Form.new(@ab)
8
- end
9
-
10
- it "#forme_set should only set values in the form" do
11
- @ab.forme_set(:name=>'Foo')
12
- @ab.name.must_be_nil
13
-
14
- @f.input(:name)
15
- @ab.forme_set(:name=>'Foo')
16
- @ab.name.must_equal 'Foo'
17
-
18
- @ab.forme_set('copies_sold'=>'1')
19
- @ab.name.must_be_nil
20
- @ab.copies_sold.must_be_nil
21
-
22
- @f.input(:copies_sold)
23
- @ab.forme_set('name'=>'Bar', 'copies_sold'=>'1')
24
- @ab.name.must_equal 'Bar'
25
- @ab.copies_sold.must_equal 1
26
- end
27
-
28
- it "#forme_set should handle different ways to specify parameter names" do
29
- [{:attr=>{:name=>'foo'}}, {:attr=>{'name'=>:foo}}, {:name=>'foo'}, {:name=>'foo[]'}, {:name=>'bar[foo][]'}, {:name=>'bar[foo]'}, {:key=>:foo}].each do |opts|
30
- @f.input(:name, opts)
31
-
32
- @ab.forme_set(:name=>'Foo')
33
- @ab.name.must_be_nil
34
-
35
- @ab.forme_set('foo'=>'Foo')
36
- @ab.name.must_equal 'Foo'
37
- @ab.forme_inputs.clear
38
- end
39
- end
40
-
41
- it "#forme_set should ignore values where key is explicitly set to nil" do
42
- @f.input(:name, :key=>nil)
43
- @ab.forme_set(:name=>'Foo')
44
- @ab.name.must_be_nil
45
- @ab.forme_set(nil=>'Foo')
46
- @ab.name.must_be_nil
47
- end
48
-
49
- it "#forme_set should skip inputs with disabled/readonly formatter set on input" do
50
- [:disabled, :readonly, ::Forme::Formatter::Disabled, ::Forme::Formatter::ReadOnly].each do |formatter|
51
- @f.input(:name, :formatter=>formatter)
52
- @ab.forme_set(:name=>'Foo')
53
- @ab.name.must_be_nil
54
- end
55
-
56
- @f.input(:name, :formatter=>:default)
57
- @ab.forme_set(:name=>'Foo')
58
- @ab.name.must_equal 'Foo'
59
- end
60
-
61
- it "#forme_set should skip inputs with disabled/readonly formatter set on Form" do
62
- [:disabled, :readonly, ::Forme::Formatter::Disabled, ::Forme::Formatter::ReadOnly].each do |formatter|
63
- @f = Forme::Form.new(@ab, :formatter=>:disabled)
64
- @f.input(:name)
65
- @ab.forme_set(:name=>'Foo')
66
- @ab.name.must_be_nil
67
- end
68
-
69
- @f = Forme::Form.new(@ab, :formatter=>:default)
70
- @f.input(:name)
71
- @ab.forme_set(:name=>'Foo')
72
- @ab.name.must_equal 'Foo'
73
- end
74
-
75
- it "#forme_set should skip inputs with disabled/readonly formatter set using with_opts" do
76
- [:disabled, :readonly, ::Forme::Formatter::Disabled, ::Forme::Formatter::ReadOnly].each do |formatter|
77
- @f.with_opts(:formatter=>formatter) do
78
- @f.input(:name)
79
- end
80
- @ab.forme_set(:name=>'Foo')
81
- @ab.name.must_be_nil
82
- end
83
-
84
- @f.with_opts(:formatter=>:default) do
85
- @f.input(:name)
86
- end
87
- @ab.forme_set(:name=>'Foo')
88
- @ab.name.must_equal 'Foo'
89
- end
90
-
91
- it "#forme_set should prefer input formatter to with_opts formatter" do
92
- @f.with_opts(:formatter=>:default) do
93
- @f.input(:name, :formatter=>:readonly)
94
- end
95
- @ab.forme_set(:name=>'Foo')
96
- @ab.name.must_be_nil
97
-
98
- @f.with_opts(:formatter=>:readonly) do
99
- @f.input(:name, :formatter=>:default)
100
- end
101
- @ab.forme_set(:name=>'Foo')
102
- @ab.name.must_equal 'Foo'
103
- end
104
-
105
- it "#forme_set should prefer with_opts formatter to form formatter" do
106
- @f = Forme::Form.new(@ab, :formatter=>:default)
107
- @f.with_opts(:formatter=>:readonly) do
108
- @f.input(:name)
109
- end
110
- @ab.forme_set(:name=>'Foo')
111
- @ab.name.must_be_nil
112
-
113
- @f = Forme::Form.new(@ab, :formatter=>:readonly)
114
- @f.with_opts(:formatter=>:default) do
115
- @f.input(:name)
116
- end
117
- @ab.forme_set(:name=>'Foo')
118
- @ab.name.must_equal 'Foo'
119
- end
120
-
121
- it "#forme_set should handle setting values for associated objects" do
122
- @ab.forme_set(:artist_id=>1)
123
- @ab.artist_id.must_be_nil
124
-
125
- @f.input(:artist)
126
- @ab.forme_set(:artist_id=>'1')
127
- @ab.artist_id.must_equal 1
128
-
129
- @ab.forme_set('tag_pks'=>%w'1 2')
130
- @ab.artist_id.must_be_nil
131
- @ab.tag_pks.must_equal []
132
-
133
- @f.input(:tags)
134
- @ab.forme_set('artist_id'=>'1', 'tag_pks'=>%w'1 2')
135
- @ab.artist_id.must_equal 1
136
- @ab.tag_pks.must_equal [1, 2]
137
- end
138
-
139
- it "#forme_set should handle validations for filtered associations" do
140
- [
141
- [{:dataset=>proc{|ds| ds.exclude(:id=>1)}},
142
- {:dataset=>proc{|ds| ds.exclude(:id=>1)}}],
143
- [{:options=>Artist.exclude(:id=>1).select_order_map([:name, :id])},
144
- {:options=>Tag.exclude(:id=>1).select_order_map(:id), :name=>'tag_pks[]'}],
145
- [{:options=>Artist.exclude(:id=>1).all, :text_method=>:name, :value_method=>:id},
146
- {:options=>Tag.exclude(:id=>1).all, :text_method=>:name, :value_method=>:id}],
147
- ].each do |artist_opts, tag_opts|
148
- @ab.forme_inputs.clear
149
- @f.input(:artist, artist_opts)
150
- @f.input(:tags, tag_opts)
151
-
152
- @ab.forme_validations.clear
153
- @ab.forme_set('artist_id'=>'1', 'tag_pks'=>%w'1 2')
154
- @ab.artist_id.must_equal 1
155
- @ab.tag_pks.must_equal [1, 2]
156
-
157
- @ab.valid?.must_equal false
158
- @ab.errors[:artist_id].must_equal ['invalid value submitted']
159
- @ab.errors[:tag_pks].must_equal ['invalid value submitted']
160
-
161
- @ab.forme_validations.clear
162
- @ab.forme_set('artist_id'=>'1', 'tag_pks'=>['2'])
163
- @ab.valid?.must_equal false
164
- @ab.errors[:artist_id].must_equal ['invalid value submitted']
165
- @ab.errors[:tag_pks].must_be_nil
166
-
167
- @ab.forme_validations.clear
168
- @ab.forme_set('artist_id'=>'2', 'tag_pks'=>['2'])
169
- @ab.valid?.must_equal true
170
- end
171
- end
172
-
173
- it "#forme_set should not add validations for inputs with no options" do
174
- @f.input(:artist, :options=>nil)
175
- @ab.forme_set('artist_id'=>'1')
176
- @ab.valid?.must_equal true
177
- end
178
-
179
- it "#forme_set should not require associated values for many_to_one association with select boxes" do
180
- @f.input(:artist)
181
- @ab.forme_set({})
182
- @ab.valid?.must_equal true
183
- end
184
-
185
- it "#forme_set should not require associated values for many_to_one association with radio buttons" do
186
- @f.input(:artist, :as=>:radio)
187
- @ab.forme_set({})
188
- @ab.valid?.must_equal true
189
- end
190
-
191
- it "#forme_set should require associated values for many_to_one association with select boxes when :required is used" do
192
- @f.input(:artist, :required=>true)
193
- @ab.forme_set({})
194
- @ab.valid?.must_equal false
195
- @ab.errors[:artist_id].must_equal ['invalid value submitted']
196
- end
197
-
198
- it "#forme_set should require associated values for many_to_one association with radio buttons when :required is used" do
199
- @f.input(:artist, :as=>:radio, :required=>true)
200
- @ab.forme_set({})
201
- @ab.valid?.must_equal false
202
- @ab.errors[:artist_id].must_equal ['invalid value submitted']
203
- end
204
-
205
- it "#forme_set should handle cases where currently associated values is nil" do
206
- @f.input(:tags)
207
- @ab.forme_set({:tag_pks=>[1]})
208
- def @ab.tag_pks; nil; end
209
- @ab.valid?.must_equal true
210
- end
211
-
212
- it "#forme_parse should return hash with values and validations" do
213
- @f.input(:name)
214
- @ab.forme_parse(:name=>'Foo').must_equal(:values=>{:name=>'Foo'}, :validations=>{})
215
-
216
- @f.input(:artist, :dataset=>proc{|ds| ds.exclude(:id=>1)})
217
- hash = @ab.forme_parse(:name=>'Foo', 'artist_id'=>'1')
218
- hash[:values] = {:name=>'Foo', :artist_id=>'1'}
219
- @ab.set(hash[:values])
220
- @ab.valid?.must_equal true
221
-
222
- @ab.forme_validations.merge!(hash[:validations])
223
- @ab.valid?.must_equal false
224
- @ab.errors[:artist_id].must_equal ['invalid value submitted']
225
- end
226
-
227
- it "#forme_parse should return hash with values and validations" do
228
- @ab.forme_validations[:name] = [:bar, []]
229
- proc{@ab.valid?}.must_raise Sequel::Plugins::Forme::Error
230
- end
231
-
232
- it "should handle frozen instances as form inputs" do
233
- @ab.freeze
234
- @ab.forme_inputs.must_equal({})
235
- @ab.forme_validations.must_equal({})
236
- @f.input(:name).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text"/></label>'
237
- end
238
- end
@@ -1,71 +0,0 @@
1
- module FormeErbSpecs
2
- extend Minitest::Spec::DSL
3
-
4
- before do
5
- o = Object.new
6
- def o.puts(*) end
7
- @rack = {'rack.input'=>'', 'REQUEST_METHOD'=>'GET', 'rack.errors'=>o, 'SCRIPT_NAME'=>''}
8
- end
9
-
10
- it "#form should add start and end tags and yield Forme::Form instance" do
11
- sin_get('/').must_equal '<form action="/baz"> <p>FBB</p> <input id="first" name="first" type="text" value="foo"/> <input id="last" name="last" type="text" value="bar"/> <input type="submit" value="Save"/> </form>'
12
- end
13
-
14
- it "#form should have inputs work with a block" do
15
- sin_get('/inputs_block').must_equal '<form action="/baz"><fieldset class="inputs"><legend>FBB</legend> <input id="last" name="last" type="text" value="bar"/> </fieldset></form>'
16
- end
17
-
18
- it "#form should have inputs with fieldset_ol wrapper work with block" do
19
- sin_get('/inputs_block_wrapper').must_equal '<form action="/baz"><fieldset class="inputs"><legend>FBB</legend><ol> <input id="last" name="last" type="text" value="bar"/> </ol></fieldset></form>'
20
- end
21
-
22
- it "#form should add start and end tags and yield Forme::Form instance" do
23
- sin_get('/nest').must_equal '<form action="/baz"> <p>FBB</p> <div> <input id="first" name="first" type="text" value="foo"/> <input id="last" name="last" type="text" value="bar"/> </div> </form>'
24
- end
25
-
26
- it "#form should correctly handle situation where multiple templates are used with same form object" do
27
- sin_get('/nest_sep').sub(/ 4\z/, '4').must_equal '0 <form action="/baz"> 1 <p>FBB</p> 2 n1 <div> n2 <input id="first" name="first" type="text" value="foo"/> <input id="last" name="last" type="text" value="bar"/> n3 </div> n4 <fieldset class="inputs"><legend>Foo</legend><input id="first" name="first" type="text" value="foo"/><input id="last" name="last" type="text" value="bar"/></fieldset> n5 3 </form>4'
28
- end
29
-
30
- it "#form should correctly handle situation Sequel integration with subforms where multiple templates are used with same form object" do
31
- sin_get('/nest_seq_simple').sub(/ 4\z/, '4').sub(%r{<input name="_csrf" type="hidden" value="([^"]+)"/>}, '<input name="_csrf" type="hidden" value="csrf"/>').must_equal '0 <form action="/baz" class="forme album" method="post"><input name="_csrf" type="hidden" value="csrf"/> 1 n1 <input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Artist</legend> n2 <label>Name2: <input id="album_artist_attributes_name2" name="album[artist_attributes][name2]" type="text" value="A2"/></label> n3 </fieldset> n4 3 </form>4'
32
-
33
- sin_get('/nest_seq').sub(/ 4\z/, '4').sub(%r{<input name="_csrf" type="hidden" value="([^"]+)"/>}, '<input name="_csrf" type="hidden" value="csrf"/>').must_equal '0 <form action="/baz" class="forme album" method="post"><input name="_csrf" type="hidden" value="csrf"/> 1 <input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Foo</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="A"/></label></fieldset> 2 n1 <input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Artist</legend> n2 <label>Name2: <input id="album_artist_attributes_name2" name="album[artist_attributes][name2]" type="text" value="A2"/></label> n3 </fieldset> n4 <input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Bar</legend><label>Name3: <input id="album_artist_attributes_name3" name="album[artist_attributes][name3]" type="text" value="A3"/></label></fieldset> n5 3 </form>4'
34
- end
35
-
36
- it "#form should correctly handle subform with :grid option with block" do
37
- sin_get('/grid-block').sub(/ 4\z/, '4').sub(%r{<input name="_csrf" type="hidden" value="([^"]+)"/>}, '<input name="_csrf" type="hidden" value="csrf"/>').must_equal '0 <form action="/baz" class="forme album" method="post"><input name="_csrf" type="hidden" value="csrf"/> 1 <input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="2"/><table><caption>Foo</caption><thead><tr><th>Name</th></tr></thead><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="A"/></td> 2 </tr></tbody></table> 3 <input type="submit" value="Sub"/></form>4'
38
- end
39
-
40
- it "#form should correctly handle subform with :grid option without block" do
41
- sin_get('/grid-noblock').sub(/ 3\z/, '3').sub(%r{<input name="_csrf" type="hidden" value="([^"]+)"/>}, '<input name="_csrf" type="hidden" value="csrf"/>').must_equal '0 <form action="/baz" class="forme album" method="post"><input name="_csrf" type="hidden" value="csrf"/> 1 <input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="2"/><table><caption>Foo</caption><thead><tr><th>Name</th></tr></thead><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="A"/></td></tr></tbody></table> 2 <input type="submit" value="Sub"/></form>3'
42
- end
43
-
44
- it "#form should correctly handle subform with :grid option without block" do
45
- sin_get('/grid-noblock-multiple').sub(/ 3\z/, '3').sub(%r{<input name="_csrf" type="hidden" value="([^"]+)"/>}, '<input name="_csrf" type="hidden" value="csrf"/>').must_equal '0 <form action="/baz" class="forme artist" method="post"><input name="_csrf" type="hidden" value="csrf"/> 1 <input id="artist_albums_attributes_0_id" name="artist[albums_attributes][0][id]" type="hidden" value="1"/><table><caption>Foo</caption><thead><tr><th>Name</th><th>Copies</th></tr></thead><tbody><tr><td class="string"><input id="artist_albums_attributes_0_name" maxlength="255" name="artist[albums_attributes][0][name]" type="text" value="N"/></td><td class="integer"><input id="artist_albums_attributes_0_copies_sold" inputmode="numeric" name="artist[albums_attributes][0][copies_sold]" pattern="-?[0-9]*" type="text" value="2"/></td></tr></tbody></table> 2 <input type="submit" value="Sub"/></form>3'
46
- end
47
-
48
- it "#form should accept two hashes instead of requiring obj as first argument" do
49
- sin_get('/hash').must_equal '<form action="/baz"> <input id="first" name="first" type="text" value="foo"/> </form>'
50
- end
51
-
52
- it "#form should deal with emitted code" do
53
- sin_get('/legend').must_equal '<form action="/baz"> <p>FBB</p> <fieldset class="inputs"><legend>Foo</legend><input id="first" name="first" type="text" value="foo"/><input id="last" name="last" type="text" value="bar"/></fieldset> <p>FBB2</p> </form>'
54
- end
55
-
56
- it "#form should work with :inputs, :button, and :legend options" do
57
- sin_get('/combined').must_equal '<form action="/baz"><fieldset class="inputs"><legend>123</legend><input id="first" name="first" type="text" value="foo"/></fieldset> <p>FBB</p> <input id="last" name="last" type="text" value="bar"/> <input type="submit" value="xyz"/></form>'
58
- end
59
-
60
- it "#form should work without a block" do
61
- sin_get('/noblock').must_equal '<form action="/baz"><fieldset class="inputs"><legend>123</legend><input id="first" name="first" type="text" value="foo"/></fieldset><input type="submit" value="xyz"/></form>'
62
- end
63
-
64
- it "#form should work without a block and still have hidden tags emitted" do
65
- sin_get('/noblock_post').sub(%r{<input name=\"_csrf\" type=\"hidden\" value=\"([^\"]+)\"/>}, "<input name=\"_csrf\" type=\"hidden\" value=\"csrf\"/>").must_equal '<form method="post"><input name="_csrf" type="hidden" value="csrf"/><input type="submit" value="xyz"/></form>'
66
- end
67
-
68
- it "#form with an empty form should work" do
69
- sin_get('/noblock_empty').must_equal '<form action="/baz"></form>'
70
- end
71
- end
@@ -1,71 +0,0 @@
1
- require_relative 'spec_helper'
2
- require_relative 'sequel_helper'
3
- require_relative 'erb_helper'
4
-
5
- begin
6
- require 'sinatra/base'
7
- require 'rack/csrf'
8
- rescue LoadError
9
- warn "unable to load sinatra or rack/csrf, skipping sinatra spec"
10
- else
11
- begin
12
- require 'tilt/erubis'
13
- rescue LoadError
14
- require 'tilt/erb'
15
- begin
16
- require 'erubis'
17
- rescue LoadError
18
- require 'erb'
19
- end
20
- end
21
- require_relative '../lib/forme/erb'
22
- class FormeSinatraTest < Sinatra::Base
23
- helpers(Forme::ERB::Helper)
24
- disable :show_exceptions
25
- enable :raise_errors
26
- enable :sessions
27
- use Rack::Csrf
28
-
29
- def self.get(path, &block)
30
- super("/#{path}", &block)
31
- end
32
-
33
- instance_exec(self, &ERB_BLOCK)
34
-
35
- get 'no-session' do
36
- session = env.delete('rack.session')
37
- body = erb <<END
38
- <% form(:method=>'POST') do %>
39
- <% end %>
40
- END
41
- env['rack.session'] = session
42
- body
43
- end
44
-
45
- get 'no-out_buf' do
46
- erb(<<END, :outvar=>'@_foo')
47
- <% form(:method=>'POST') do |f| %>
48
- <%= f.input(:text) %>
49
- <% end %>
50
- END
51
- end
52
- end
53
-
54
- describe "Forme Sinatra ERB integration" do
55
- def sin_get(path)
56
- s = String.new
57
- FormeSinatraTest.new.call(@rack.merge('PATH_INFO'=>path))[2].each{|str| s << str}
58
- s.gsub(/\s+/, ' ').strip
59
- end
60
-
61
- include FormeErbSpecs
62
-
63
- it "should handle missing rack.session when using Rack::Csrf" do
64
- sin_get('/no-session').must_equal '<form method="POST"></form>'
65
- end
66
-
67
- it "should handle non-standard outvar, but without emitting into template" do
68
- sin_get('/no-out_buf').must_equal '<input type="text"/>'
69
- end
70
- end
71
- end
data/spec/spec_helper.rb DELETED
@@ -1,31 +0,0 @@
1
- $:.unshift(File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'lib'))
2
-
3
- if ENV['COVERAGE']
4
- require_relative 'forme_coverage'
5
- SimpleCov.forme_coverage
6
- end
7
-
8
- require_relative '../lib/forme'
9
-
10
- ENV['MT_NO_PLUGINS'] = '1' # Work around stupid autoloading of plugins
11
- gem 'minitest'
12
- require 'minitest/global_expectations/autorun'
13
-
14
- module Minitest::Spec::DSL
15
- def silence_warnings(name, &block)
16
- it(name) do
17
- silence_warnings do
18
- instance_exec(&block)
19
- end
20
- end
21
- end
22
- end
23
-
24
- class Minitest::Spec
25
- def silence_warnings
26
- verbose, $VERBOSE = $VERBOSE, nil
27
- yield
28
- ensure
29
- $VERBOSE = verbose
30
- end
31
- end