campo 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ module Campo
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,889 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+ require_relative "../lib/campo.rb"
5
+ require "rspec"
6
+ require "logger"
7
+
8
+
9
+ module Campo
10
+ describe Campo do
11
+
12
+ let(:logger){
13
+ require 'logger'
14
+ logger = Logger.new(STDOUT)
15
+ logger.level = Logger::DEBUG
16
+ logger
17
+ }
18
+
19
+ let(:top_bit) { s = <<-'STR'
20
+ - atts = {} if atts.nil?
21
+ - atts.default = {} if atts.default.nil?
22
+ - inners = {} if inners.nil?
23
+ - inners.default = "" if inners.default.nil?
24
+ - i = 0 # for tabindex
25
+
26
+ STR
27
+ s
28
+ }
29
+
30
+
31
+ describe :output do
32
+ context "Given a form with no fields" do
33
+ let(:expected) {
34
+ expected = top_bit + %q!%form{ atts[:myform], method: "POST", name: "myform", }!.strip + "\n"
35
+ }
36
+
37
+ subject{ Campo.output Campo::Form.new( "myform" ) }
38
+ it { should_not be_nil }
39
+ it { should == expected }
40
+ end
41
+
42
+ context "Given no form" do
43
+ context "When given a select field with options" do
44
+ let(:expected) { %q!
45
+ %label{ for: "teas", }
46
+ Favourite tea:
47
+ %select{ atts[:teas], tabindex: "#{i += 1}", name: "teas", }
48
+ %option{ value: "", disabled: "disabled", name: "teas", }Choose one:
49
+ %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas", }Ceylon
50
+ %option{ atts[:teas_breakfast], value: "breakfast", id: "teas_breakfast", name: "teas", }Breakfast
51
+ %option{ atts[:teas_earl_grey], value: "earl grey", id: "teas_earl_grey", name: "teas", }Earl grey
52
+ !.strip + "\n"
53
+ }
54
+ let(:tag) {
55
+ select = Campo::Select.new( "teas" )
56
+ tag = select.with_default.option("ceylon").option("breakfast").option("earl grey").labelled("Favourite tea:")
57
+ tag
58
+ }
59
+ subject { Campo.output :partial, tag }
60
+ it { should_not be_nil }
61
+ it { should == expected }
62
+
63
+ end
64
+ end
65
+
66
+ describe "A realish form" do
67
+ context "Given a form" do
68
+ let(:form) do
69
+ form = Campo::Form.new( "personal_details", action: %Q!"uri("/my/personal_details/update/")! )
70
+
71
+ form.fieldset("Your details") do |f|
72
+
73
+ f.text( "full_name", "Full name: ", size: 60 )
74
+ f.text( "dob", "Date of birth: ", size: 10 ) #TODO change this
75
+ f.fieldset( "Gender: " ) do |genders|
76
+ genders.radio( "gender", "Male", value: 1 )
77
+ genders.radio( "gender", "Female", value: 2 )
78
+ end
79
+ f.select( "ethnicorigin_id", {opts: [[1, "White"],[2,"Asian"],[3,"Black"],[4,"Chinese and Other"], [5,"Mixed"] ] }).with_default.labelled( "Ethnic-origin: " )
80
+ f.text( "occupation", "Occupation: ", size: 60 )
81
+ f.text( "phone_landline", "Phone (landline): ", size: 20 )
82
+ f.text( "phone_mobile", "Phone (mobile): ", size: 20 )
83
+ f.fieldset( "May we contact you..." ) do |c|
84
+ c.checkbox( "contactable", "In the day?", value: "day" )
85
+ c.checkbox( "contactable", "In the evening?", value: "evening" )
86
+ end
87
+ f.submit("Save")
88
+
89
+ end # form
90
+ form
91
+ end # let
92
+
93
+
94
+ let(:expected) {
95
+ %Q!- atts = {} if atts.nil?\n- atts.default = {} if atts.default.nil?\n- inners = {} if inners.nil?\n- inners.default = "" if inners.default.nil?\n- i = 0 # for tabindex\n\n%form{ atts[:personal_details], method: "POST", action: uri("/my/personal_details/update/"), name: "personal_details", }\n %fieldset{ }\n %legend{ }Your details\n %label{ for: "full_name", }\n Full name: \n %input{ atts[:full_name], tabindex: "\#{i += 1}", type: "text", id: "full_name", size: "60", name: "full_name", }\n %label{ for: "dob", }\n Date of birth: \n %input{ atts[:dob], tabindex: "\#{i += 1}", type: "text", id: "dob", size: "10", name: "dob", }\n %fieldset{ }\n %legend{ }Gender: \n %label{ for: "gender_1", }\n Male\n %input{ atts[:gender_1], tabindex: "\#{i += 1}", type: "radio", id: "gender_1", value: "1", name: "gender", }\n %label{ for: "gender_2", }\n Female\n %input{ atts[:gender_2], tabindex: "\#{i += 1}", type: "radio", id: "gender_2", value: "2", name: "gender", }\n %label{ for: "ethnicorigin_id", }\n Ethnic-origin: \n %select{ atts[:ethnicorigin_id], tabindex: "\#{i += 1}", name: "ethnicorigin_id", }\n %option{ value: "", disabled: "disabled", name: "ethnicorigin_id", }Choose one:\n %option{ atts[:ethnicorigin_id_1], value: "1", id: "ethnicorigin_id_1", name: "ethnicorigin_id", }White\n %option{ atts[:ethnicorigin_id_2], value: "2", id: "ethnicorigin_id_2", name: "ethnicorigin_id", }Asian\n %option{ atts[:ethnicorigin_id_3], value: "3", id: "ethnicorigin_id_3", name: "ethnicorigin_id", }Black\n %option{ atts[:ethnicorigin_id_4], value: "4", id: "ethnicorigin_id_4", name: "ethnicorigin_id", }Chinese and Other\n %option{ atts[:ethnicorigin_id_5], value: "5", id: "ethnicorigin_id_5", name: "ethnicorigin_id", }Mixed\n %label{ for: "occupation", }\n Occupation: \n %input{ atts[:occupation], tabindex: "\#{i += 1}", type: "text", id: "occupation", size: "60", name: "occupation", }\n %label{ for: "phone_landline", }\n Phone (landline): \n %input{ atts[:phone_landline], tabindex: "\#{i += 1}", type: "text", id: "phone_landline", size: "20", name: "phone_landline", }\n %label{ for: "phone_mobile", }\n Phone (mobile): \n %input{ atts[:phone_mobile], tabindex: "\#{i += 1}", type: "text", id: "phone_mobile", size: "20", name: "phone_mobile", }\n %fieldset{ }\n %legend{ }May we contact you...\n %label{ for: "contactable_day", }\n In the day?\n %input{ atts[:contactable_day], tabindex: "\#{i += 1}", type: "checkbox", id: "contactable_day", value: "day", name: "contactable", }\n %label{ for: "contactable_evening", }\n In the evening?\n %input{ atts[:contactable_evening], tabindex: "\#{i += 1}", type: "checkbox", id: "contactable_evening", value: "evening", name: "contactable", }\n %input{ atts[:Save_Save], tabindex: "\#{i += 1}", type: "submit", id: "Save_Save", value: "Save", }\n!
96
+ } # let expected
97
+
98
+ subject{ Campo.output form }
99
+ it { should == expected }
100
+
101
+
102
+ context "With a div to wrap it in" do
103
+ let(:doc) {
104
+ doc = Campo.literal ".centred.form" do |wrapper|
105
+ wrapper << form
106
+ end
107
+ }
108
+ let(:expected) {
109
+ %Q!- atts = {} if atts.nil?\n- atts.default = {} if atts.default.nil?\n- inners = {} if inners.nil?\n- inners.default = "" if inners.default.nil?\n- i = 0 # for tabindex\n\n.centred.form\n %form{ atts[:personal_details], method: "POST", action: uri("/my/personal_details/update/"), name: "personal_details", }\n %fieldset{ }\n %legend{ }Your details\n %label{ for: "full_name", }\n Full name: \n %input{ atts[:full_name], tabindex: "\#{i += 1}", type: "text", id: "full_name", size: "60", name: "full_name", }\n %label{ for: "dob", }\n Date of birth: \n %input{ atts[:dob], tabindex: "\#{i += 1}", type: "text", id: "dob", size: "10", name: "dob", }\n %fieldset{ }\n %legend{ }Gender: \n %label{ for: "gender_1", }\n Male\n %input{ atts[:gender_1], tabindex: "\#{i += 1}", type: "radio", id: "gender_1", value: "1", name: "gender", }\n %label{ for: "gender_2", }\n Female\n %input{ atts[:gender_2], tabindex: "\#{i += 1}", type: "radio", id: "gender_2", value: "2", name: "gender", }\n %label{ for: "ethnicorigin_id", }\n Ethnic-origin: \n %select{ atts[:ethnicorigin_id], tabindex: "\#{i += 1}", name: "ethnicorigin_id", }\n %option{ value: "", disabled: "disabled", name: "ethnicorigin_id", }Choose one:\n %option{ atts[:ethnicorigin_id_1], value: "1", id: "ethnicorigin_id_1", name: "ethnicorigin_id", }White\n %option{ atts[:ethnicorigin_id_2], value: "2", id: "ethnicorigin_id_2", name: "ethnicorigin_id", }Asian\n %option{ atts[:ethnicorigin_id_3], value: "3", id: "ethnicorigin_id_3", name: "ethnicorigin_id", }Black\n %option{ atts[:ethnicorigin_id_4], value: "4", id: "ethnicorigin_id_4", name: "ethnicorigin_id", }Chinese and Other\n %option{ atts[:ethnicorigin_id_5], value: "5", id: "ethnicorigin_id_5", name: "ethnicorigin_id", }Mixed\n %label{ for: "occupation", }\n Occupation: \n %input{ atts[:occupation], tabindex: "\#{i += 1}", type: "text", id: "occupation", size: "60", name: "occupation", }\n %label{ for: "phone_landline", }\n Phone (landline): \n %input{ atts[:phone_landline], tabindex: "\#{i += 1}", type: "text", id: "phone_landline", size: "20", name: "phone_landline", }\n %label{ for: "phone_mobile", }\n Phone (mobile): \n %input{ atts[:phone_mobile], tabindex: "\#{i += 1}", type: "text", id: "phone_mobile", size: "20", name: "phone_mobile", }\n %fieldset{ }\n %legend{ }May we contact you...\n %label{ for: "contactable_day", }\n In the day?\n %input{ atts[:contactable_day], tabindex: "\#{i += 1}", type: "checkbox", id: "contactable_day", value: "day", name: "contactable", }\n %label{ for: "contactable_evening", }\n In the evening?\n %input{ atts[:contactable_evening], tabindex: "\#{i += 1}", type: "checkbox", id: "contactable_evening", value: "evening", name: "contactable", }\n %input{ atts[:Save_Save], tabindex: "\#{i += 1}", type: "submit", id: "Save_Save", value: "Save", }\n!
110
+ } # let expected
111
+
112
+ subject{ Campo.output doc }
113
+ it { should == expected }
114
+ end # context
115
+ end # context
116
+ end # describe a form
117
+ end
118
+
119
+ describe Convenience do
120
+ let(:obj) {
121
+ class Fakeclass
122
+ include Convenience
123
+ include Childish
124
+ end
125
+ Fakeclass.new
126
+ }
127
+ let(:form) { Campo::Form.new( "myform" ) }
128
+
129
+ describe "input" do
130
+ context "When given type" do
131
+ context "of text" do
132
+ context "with a label" do
133
+ let(:expected) { top_bit + %q!
134
+ %form{ atts[:myform], method: "POST", name: "myform", }
135
+ %label{ for: "blah_blahdeblah", }
136
+ Blahd
137
+ %input{ atts[:blah_blahdeblah], tabindex: "#{i += 1}", type: "text", id: "blah_blahdeblah", value: "blahdeblah", name: "blah", }!.strip + "\n" }
138
+ subject {
139
+ form.input( "blah", :text, "Blahd", value: "blahdeblah" )
140
+ Campo.output form
141
+ }
142
+ it { should_not be_nil }
143
+ it { should == expected }
144
+ context "via convenience method" do
145
+ subject {
146
+ form.text( "blah", "Blahd", value: "blahdeblah" )
147
+ Campo.output form
148
+ }
149
+ it { should_not be_nil }
150
+ it { should == expected }
151
+ end
152
+ end
153
+ context "without a label" do
154
+ let(:expected) { top_bit + %q!
155
+ %form{ atts[:myform], method: "POST", name: "myform", }
156
+ %label{ for: "blah_blahdeblah", }
157
+ Blah
158
+ %input{ atts[:blah_blahdeblah], tabindex: "#{i += 1}", type: "text", id: "blah_blahdeblah", value: "blahdeblah", name: "blah", }!.strip + "\n" }
159
+
160
+ subject {
161
+ form.input( "blah", :text, value: "blahdeblah" )
162
+ Campo.output form
163
+ }
164
+ it { should_not be_nil }
165
+ it { should == expected }
166
+ context "via convenience method" do
167
+ subject {
168
+ form.text( "blah", value: "blahdeblah" )
169
+ Campo.output form
170
+ }
171
+ it { should_not be_nil }
172
+ it { should == expected }
173
+ end
174
+ end
175
+
176
+ end # text
177
+ context "of checkbox" do
178
+ let(:expected) { top_bit + %q!
179
+ %form{ atts[:myform], method: "POST", name: "myform", }
180
+ %label{ for: "blah_blahdeblah", }
181
+ Blahd
182
+ %input{ atts[:blah_blahdeblah], tabindex: "#{i += 1}", type: "checkbox", id: "blah_blahdeblah", value: "blahdeblah", name: "blah", }!.strip + "\n" }
183
+
184
+ subject {
185
+ form.input( "blah", :checkbox, "Blahd", value: "blahdeblah" )
186
+ Campo.output form
187
+ }
188
+ it { should_not be_nil }
189
+ it { should == expected }
190
+
191
+ context "via convenience method" do
192
+ subject {
193
+ form.checkbox( "blah", "Blahd", value: "blahdeblah" )
194
+ Campo.output form
195
+ }
196
+ it { should_not be_nil }
197
+ it { should == expected }
198
+ end # conveniece
199
+ end # checkbox
200
+ end
201
+ end # describe input
202
+
203
+ end # Convenience
204
+
205
+
206
+ describe Label do
207
+ let(:tag) { Label.new( "abc", "A, B, or C?" ) }
208
+ subject { tag }
209
+ it { should_not be_nil }
210
+ it { should be_a_kind_of(Label) }
211
+
212
+ describe :output do
213
+ let(:expected) { s =<<'STR'
214
+ %label{ for: "abc", }
215
+ A, B, or C?
216
+ STR
217
+ s.chomp
218
+ }
219
+ subject { tag.output }
220
+ it { should == expected }
221
+ end
222
+ end
223
+
224
+ describe Form do
225
+
226
+ describe "initialisation" do
227
+ context "When given no args" do
228
+ let(:form) { Campo::Form.new }
229
+ it { should_not be_nil }
230
+ it { should raise_error }
231
+ end
232
+ context "When given a name" do
233
+ let(:form) { Campo::Form.new( "myform" ) }
234
+ subject { form }
235
+ it { should_not be_nil }
236
+ it { should be_a_kind_of(Form) }
237
+
238
+ context "via convenience method" do
239
+ let(:form) { Campo.form( "myform" ) }
240
+ subject { form }
241
+ it { should_not be_nil }
242
+ it { should be_a_kind_of(Form) }
243
+ end
244
+
245
+
246
+ context "simple output" do
247
+ let(:expected) { %q!%form{ atts[:myform], method: "POST", name: "myform", }! }
248
+ subject { form.output }
249
+ it { should == expected }
250
+ end
251
+ end
252
+ context "When given a name and a hash of haml attributes" do
253
+ let(:form) { Campo::Form.new( "myform", action: "/" ) }
254
+ subject { form }
255
+ it { should_not be_nil }
256
+ it { should be_a_kind_of(Form) }
257
+
258
+ context "simple output" do
259
+ let(:expected) { %q!%form{ atts[:myform], method: "POST", action: "/", name: "myform", }! }
260
+ subject { form.output }
261
+ it { should == expected }
262
+ end
263
+ end
264
+ end
265
+
266
+
267
+
268
+ describe :fieldset do
269
+ context "When given a form with only a name" do
270
+ let(:form) { Campo::Form.new( "myform" ) }
271
+ let(:expected) { top_bit + %q!
272
+ %form{ atts[:myform], method: "POST", name: "myform", }
273
+ %fieldset{ }
274
+ %legend{ }Do you like these colours? Tick for yes:
275
+
276
+ !.strip + "\n" }
277
+ subject { form.fieldset("Do you like these colours? Tick for yes:")
278
+ Campo.output form
279
+ }
280
+ it { should_not be_nil }
281
+ it { should == expected }
282
+ end
283
+ context "When given a form with a mix of fields" do
284
+ let(:form) { Campo::Form.new( "myform" ) }
285
+ end
286
+ end
287
+
288
+ context :select do
289
+ let(:form) { Campo::Form.new( "myform" ) }
290
+ context "Given one select tag" do
291
+ let(:expected) { top_bit + %q!
292
+ %form{ atts[:myform], method: "POST", name: "myform", }
293
+ %label{ for: "teas", }
294
+ Favourite tea:
295
+ %select{ atts[:teas], tabindex: "#{i += 1}", name: "teas", }
296
+ %option{ value: "", disabled: "disabled", name: "teas", }Choose one:
297
+ %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas", }Ceylon
298
+ %option{ atts[:teas_breakfast], value: "breakfast", id: "teas_breakfast", name: "teas", }Breakfast
299
+ %option{ atts[:teas_earl_grey], value: "earl grey", id: "teas_earl_grey", name: "teas", }Earl grey
300
+
301
+ !.strip + "\n"
302
+ }
303
+
304
+ subject {
305
+ form.select("teas").with_default.option("ceylon").option("breakfast").option("earl grey").labelled("Favourite tea:")
306
+ Campo.output form
307
+ }
308
+ it { should_not be_nil }
309
+ it { should == expected }
310
+ end
311
+ context "Given several select tags" do
312
+ let(:expected) { top_bit + %q!
313
+ %form{ atts[:myform], method: "POST", name: "myform", }
314
+ %label{ for: "teas", }
315
+ Favourite tea:
316
+ %select{ atts[:teas], tabindex: "#{i += 1}", name: "teas", }
317
+ %option{ value: "", disabled: "disabled", name: "teas", }Choose one:
318
+ %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas", }Ceylon
319
+ %option{ atts[:teas_breakfast], value: "breakfast", id: "teas_breakfast", name: "teas", }Breakfast
320
+ %option{ atts[:teas_earl_grey], value: "earl grey", id: "teas_earl_grey", name: "teas", }Earl grey
321
+ %label{ for: "coffees", }
322
+ Favourite coffee:
323
+ %select{ atts[:coffees], tabindex: "#{i += 1}", name: "coffees", }
324
+ %option{ value: "", disabled: "disabled", name: "coffees", }Choose one:
325
+ %option{ atts[:coffees_blue_mountain], value: "blue mountain", id: "coffees_blue_mountain", name: "coffees", }Blue mountain
326
+ %option{ atts[:coffees_kenyan_peaberry], value: "kenyan peaberry", id: "coffees_kenyan_peaberry", name: "coffees", }Kenyan peaberry
327
+ %option{ atts[:coffees_colombian], value: "colombian", id: "coffees_colombian", name: "coffees", }Colombian
328
+ %option{ atts[:coffees_java], value: "java", id: "coffees_java", name: "coffees", }Java
329
+
330
+ !.strip + "\n" }
331
+ before {
332
+ form.select("teas").with_default.option("ceylon").option("breakfast").option("earl grey").labelled("Favourite tea:")
333
+ form.select("coffees").with_default.option("blue mountain").option("kenyan peaberry").option("colombian").option("java").labelled("Favourite coffee:")
334
+ }
335
+
336
+ subject{ Campo.output form }
337
+ it { should_not be_nil }
338
+ it { should == expected }
339
+ end
340
+ end
341
+
342
+ describe :submit do
343
+ let(:form) { Campo::Form.new( "myform" ) }
344
+ context "Given a submit button" do
345
+ context "With no arguments" do
346
+ let(:expected) { top_bit + %q!
347
+ %form{ atts[:myform], method: "POST", name: "myform", }
348
+ %input{ atts[:Submit_Submit], tabindex: "#{i += 1}", type: "submit", id: "Submit_Submit", value: "Submit", }
349
+
350
+ !.strip + "\n" }
351
+
352
+ subject {
353
+ form.submit
354
+ Campo.output form
355
+ }
356
+
357
+ it { should_not be_nil }
358
+ it { should == expected }
359
+ end
360
+ context "With a name" do
361
+ let(:expected) { top_bit + %q!
362
+ %form{ atts[:myform], method: "POST", name: "myform", }
363
+ %input{ atts[:Save_Save], tabindex: "#{i += 1}", type: "submit", id: "Save_Save", value: "Save", }
364
+
365
+ !.strip + "\n" }
366
+
367
+ subject {
368
+ form.submit( "Save" )
369
+ Campo.output form
370
+ }
371
+
372
+ it { should_not be_nil }
373
+ it { should == expected }
374
+ end
375
+ end
376
+ end
377
+ end
378
+
379
+
380
+ describe Literal do
381
+ let(:tag) { Literal.new "anything at all at all" }
382
+ subject { tag }
383
+ it { should_not be_nil }
384
+ it { should be_a_kind_of( Literal ) }
385
+
386
+ describe :output do
387
+ let(:expected) { "anything at all at all" }
388
+ subject { tag.output }
389
+ it { should == expected }
390
+ end
391
+
392
+ describe "Campo.output" do
393
+ let(:expected) { "anything at all at all\n" }
394
+ subject { Campo.output :partial, tag }
395
+ it { should == expected }
396
+ end
397
+
398
+ context "When using convenience method" do
399
+ let(:form) { Campo::Form.new( "myform" ) }
400
+ subject { form.literal( "Hello, World!" ) }
401
+ it { should_not be_nil }
402
+ it { should be_a_kind_of( Literal ) }
403
+
404
+ describe "the full output" do
405
+ let(:expected) { top_bit + %q$
406
+ %form{ atts[:myform], method: "POST", name: "myform", }
407
+ Hello, World!$.strip + "\n"}
408
+ let(:form){
409
+ form = Campo::Form.new( "myform" )
410
+ form.literal( "Hello, World!" )
411
+ form
412
+ }
413
+ subject { Campo.output form }
414
+ it { should == expected }
415
+ end
416
+ end
417
+ end
418
+
419
+
420
+ describe Haml_Ruby_Insert do
421
+ let(:tag) { Haml_Ruby_Insert.new "= sel_opts" }
422
+ subject { tag }
423
+ it { should_not be_nil }
424
+ it { should be_a_kind_of(Haml_Ruby_Insert) }
425
+
426
+ describe :output do
427
+ let(:expected) { "= sel_opts" }
428
+ subject { tag.output }
429
+ it { should == expected }
430
+ end
431
+
432
+ describe "Campo.output" do
433
+ let(:expected) { %Q!= sel_opts\n! }
434
+ subject { Campo.output :partial, tag }
435
+ it { should == expected }
436
+ end
437
+
438
+ context "When using convenience method" do
439
+ let(:form) { Campo::Form.new( "myform" ) }
440
+ subject { form.bit_of_ruby( "= 5 + 1" ) }
441
+ it { should_not be_nil }
442
+ it { should be_a_kind_of( Haml_Ruby_Insert ) }
443
+
444
+ describe "the full output" do
445
+ let(:expected) { top_bit + %q!
446
+ %form{ atts[:myform], method: "POST", name: "myform", }
447
+ = 5 + 1!.strip + "\n"}
448
+ let(:form){
449
+ form = Campo::Form.new( "myform" )
450
+ form.bit_of_ruby( "= 5 + 1" )
451
+ form
452
+ }
453
+ subject { Campo.output form }
454
+ it { should == expected }
455
+ end
456
+ end
457
+ end
458
+
459
+
460
+ describe Select do
461
+ context "initialisation" do
462
+ context "Given a name" do
463
+ context "and nothing else" do
464
+ let(:tag) { Campo::Select.new( "pqr" ) }
465
+ subject { tag }
466
+ it { should_not be_nil }
467
+ it { should be_a_kind_of(Select) }
468
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
469
+
470
+ context "Campo.output" do
471
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }!.strip + "\n" }
472
+ subject { Campo.output :partial, tag }
473
+ it { should_not be_nil }
474
+ it { should == expected }
475
+ end
476
+
477
+ context "and a default" do
478
+
479
+ subject { tag.with_default }
480
+ it { should_not be_nil }
481
+ it { should be_a_kind_of(Select) }
482
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
483
+
484
+ context "Campo.output" do
485
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
486
+ %option{ value: "", disabled: "disabled", name: "pqr", }Choose one:!.strip + "\n" }
487
+ subject { Campo.output :partial, tag.with_default }
488
+ it { should == expected }
489
+ end
490
+ end
491
+ end
492
+
493
+ context "and a block with options" do
494
+ let(:tag) {
495
+ Campo::Select.new( "pqr" ) do |s|
496
+ s.option "volvo", "Volvo"
497
+ s.option "saab", "Saab"
498
+ s.option "audi", "Audi"
499
+ end
500
+ }
501
+
502
+ subject { tag }
503
+
504
+ it { should_not be_nil }
505
+ it { should be_a_kind_of(Select) }
506
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
507
+
508
+ context "Campo.output" do
509
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
510
+ %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
511
+ %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
512
+ %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
513
+ !.strip + "\n" }
514
+ subject { Campo.output :partial, tag }
515
+ it { should_not be_nil }
516
+ it { should == expected }
517
+ end
518
+
519
+
520
+ context "and a default" do
521
+
522
+ subject { tag.with_default }
523
+ it { should_not be_nil }
524
+ it { should be_a_kind_of(Select) }
525
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
526
+
527
+ context "Campo.output" do
528
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
529
+ %option{ value: "", disabled: "disabled", name: "pqr", }Choose one:
530
+ %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
531
+ %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
532
+ %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
533
+ !.strip + "\n" }
534
+ subject { Campo.output :partial, tag.with_default }
535
+ it { should == expected }
536
+ end
537
+ end
538
+
539
+
540
+ context "and a haml ruby insert" do
541
+ let(:tag) {
542
+ Campo::Select.new( "pqr", {haml_insert: "= opts"} ) do |s|
543
+ s.option "volvo"
544
+ s.option "saab", "Saab"
545
+ s.option "audi", "Audi"
546
+ end
547
+ }
548
+ subject { tag }
549
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
550
+
551
+ context "Campo.output" do
552
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
553
+ %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
554
+ %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
555
+ %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
556
+ = opts!.strip + "\n" }
557
+ subject {
558
+ Campo.output :partial, tag
559
+ }
560
+ it { should_not be_nil }
561
+ it { should == expected }
562
+ end
563
+ end
564
+ end
565
+
566
+ context "and an array" do
567
+ let(:opts) { [["ford", "ford"], ["bmw", "BMW"], ["ferrari", "Ferrari", "checked"]] }
568
+ subject { Campo::Select.new( "pqr", {opts: opts} ) }
569
+
570
+ it { should_not be_nil }
571
+ it { should be_a_kind_of(Select) }
572
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
573
+
574
+ context "with a block with options" do
575
+ let(:opts) { [["ford", "Ford"], ["bmw", "BMW"], ["ferrari", "Ferrari", "checked"]] }
576
+ let(:tag){
577
+ Campo::Select.new( "pqr", {opts: opts} ) do |s|
578
+ s.option "volvo", "Volvo"
579
+ s.option "saab", "Saab"
580
+ s.option "audi", "Audi"
581
+ end
582
+ }
583
+ subject { tag }
584
+
585
+ it { should_not be_nil }
586
+ it { should be_a_kind_of(Select) }
587
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
588
+
589
+ context "Campo.output" do
590
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
591
+ %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
592
+ %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
593
+ %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
594
+ %option{ atts[:pqr_ford], value: "ford", id: "pqr_ford", name: "pqr", }Ford
595
+ %option{ atts[:pqr_bmw], value: "bmw", id: "pqr_bmw", name: "pqr", }BMW
596
+ %option{ atts[:pqr_ferrari], value: "ferrari", selected: "selected", id: "pqr_ferrari", name: "pqr", }Ferrari!.strip + "\n" }
597
+ subject { Campo.output :partial, tag }
598
+ it { should_not be_nil }
599
+ it { should == expected }
600
+ end
601
+
602
+ context "and a default" do
603
+
604
+ subject { tag.with_default }
605
+ it { should_not be_nil }
606
+ it { should be_a_kind_of(Select) }
607
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
608
+
609
+ context "Campo.output" do
610
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
611
+ %option{ value: "", disabled: "disabled", name: "pqr", }Choose one:
612
+ %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
613
+ %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
614
+ %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
615
+ %option{ atts[:pqr_ford], value: "ford", id: "pqr_ford", name: "pqr", }Ford
616
+ %option{ atts[:pqr_bmw], value: "bmw", id: "pqr_bmw", name: "pqr", }BMW
617
+ %option{ atts[:pqr_ferrari], value: "ferrari", selected: "selected", id: "pqr_ferrari", name: "pqr", }Ferrari!.strip + "\n" }
618
+ subject { Campo.output :partial, tag.with_default }
619
+ it { should == expected }
620
+ end
621
+ end
622
+
623
+ end
624
+ end
625
+
626
+ end
627
+
628
+ end # initialisation
629
+
630
+ describe :mark_as_selected do
631
+ pending "write a test for this soon please!"
632
+ end
633
+ end # Select
634
+
635
+ describe Input do
636
+
637
+ context "initialisation" do
638
+ context "Given a name" do
639
+ context "and nothing else" do
640
+ let(:tag) { Campo::Input.new( "abc" ) }
641
+ let(:output) { %q!%input{ atts[:abc], tabindex: "#{i += 1}", type: "text", id: "abc", name: "abc", }! }
642
+ subject { tag }
643
+ it { should_not be_nil }
644
+ it { should be_a_kind_of(Input) }
645
+ specify { subject.attributes[:type].should == "text" }
646
+ specify { subject.output.should == output }
647
+ context "Campo.output" do
648
+ let(:expected) { output + "\n" }
649
+ subject { Campo.output :partial, tag }
650
+ it { should_not be_nil }
651
+ it { should == expected }
652
+ end
653
+ end
654
+
655
+ context "and a type" do
656
+ context "of text" do
657
+ let(:tag) { Campo::Input.new( "abc", :text ) }
658
+ let(:output) { %q!%input{ atts[:abc], tabindex: "#{i += 1}", type: "text", id: "abc", name: "abc", }! }
659
+ subject { tag }
660
+ it { should_not be_nil }
661
+ it { should be_a_kind_of(Input) }
662
+ specify { subject.attributes[:type].should == "text" }
663
+ specify { subject.output.should == output }
664
+ context "Campo.output" do
665
+ let(:expected) { output + "\n" }
666
+ subject { Campo.output :partial, tag }
667
+ it { should_not be_nil }
668
+ it { should == expected }
669
+ end
670
+ end
671
+ context "of password" do
672
+ let(:tag) { Campo::Input.new( "abc", :password ) }
673
+ let(:output) { %q!%input{ atts[:abc], tabindex: "#{i += 1}", type: "password", id: "abc", name: "abc", }! }
674
+ subject { tag }
675
+ it { should_not be_nil }
676
+ it { should be_a_kind_of(Input) }
677
+ specify { subject.attributes[:type].should == "password" }
678
+ specify { subject.output.should == output }
679
+
680
+ context "Campo.output" do
681
+ let(:expected) { output + "\n" }
682
+ subject { Campo.output :partial, tag }
683
+ it { should_not be_nil }
684
+ it { should == expected }
685
+ end
686
+ end
687
+ end
688
+ context "of checkbox" do
689
+ let(:tag) { Campo::Input.new( "abc", :checkbox ) }
690
+ let(:output) { %q!%input{ atts[:abc], tabindex: "#{i += 1}", type: "checkbox", id: "abc", name: "abc", }! }
691
+ subject { tag }
692
+ it { should_not be_nil }
693
+ it { should be_a_kind_of(Input) }
694
+ specify { subject.attributes[:type].should == "checkbox" }
695
+ specify { subject.output.should == output }
696
+
697
+ context "Campo.output" do
698
+ let(:expected) { output + "\n" }
699
+ subject { Campo.output :partial, tag }
700
+ it { should_not be_nil }
701
+ it { should == expected }
702
+ end
703
+
704
+ end
705
+ context "of radio" do
706
+ let(:tag) { Campo::Input.new( "abc", :radio ) }
707
+ let(:output) { %q!%input{ atts[:abc], tabindex: "#{i += 1}", type: "radio", id: "abc", name: "abc", }! }
708
+ subject { tag }
709
+ it { should_not be_nil }
710
+ it { should be_a_kind_of(Input) }
711
+ specify { subject.attributes[:type].should == "radio" }
712
+ specify { subject.output.should == output }
713
+
714
+ context "Campo.output" do
715
+ let(:expected) { output + "\n" }
716
+ subject { Campo.output :partial, tag }
717
+ it { should_not be_nil }
718
+ it { should == expected }
719
+ end
720
+ end
721
+
722
+ end # context
723
+ end # initialisation
724
+
725
+ context "Labelling" do
726
+ let(:expected) {
727
+ top_bit + %q!
728
+ %form{ atts[:myform], method: "POST", name: "myform", }
729
+ %label{ for: "abc", }
730
+ abc
731
+ %input{ atts[:abc], tabindex: "#{i += 1}", type: "text", id: "abc", name: "abc", }
732
+ %label{ for: "deff", }
733
+ deff
734
+ %input{ atts[:deff], tabindex: "#{i += 1}", type: "text", id: "deff", name: "deff", }
735
+ %label{ for: "ghi", }
736
+ ghi
737
+ %input{ atts[:ghi], tabindex: "#{i += 1}", type: "text", id: "ghi", name: "ghi", }
738
+
739
+ !.strip + "\n"
740
+ }
741
+ let(:form) {
742
+ form = Campo::Form.new( "myform" )
743
+ form << Campo::Input.new( "abc", :text ).labelled("abc")
744
+ form << Campo::Input.new( "deff", :text ).labelled("deff")
745
+ form << Campo::Input.new( "ghi", :text ).labelled("ghi")
746
+ form
747
+ }
748
+ subject { Campo.output form }
749
+ it { should_not be_nil }
750
+ it { should == expected }
751
+
752
+ context "Within a fieldset" do
753
+ let(:expected) {
754
+ top_bit + %q!
755
+ %form{ atts[:myform], method: "POST", name: "myform", }
756
+ %fieldset{ }
757
+ %legend{ }Alphabetty spaghetti
758
+ %label{ for: "abc", }
759
+ abc
760
+ %input{ atts[:abc], tabindex: "#{i += 1}", type: "text", id: "abc", name: "abc", }
761
+ %label{ for: "def", }
762
+ def
763
+ %input{ atts[:def], tabindex: "#{i += 1}", type: "text", id: "def", name: "def", }
764
+ %label{ for: "ghi", }
765
+ ghi
766
+ %input{ atts[:ghi], tabindex: "#{i += 1}", type: "text", id: "ghi", name: "ghi", }
767
+
768
+ !.strip + "\n"
769
+ }
770
+ let(:form) {
771
+ form = Campo::Form.new( "myform" )
772
+ myfieldset = form.fieldset( "Alphabetty spaghetti" ) do |f|
773
+ f << Campo::Input.new( "abc", :text ).labelled("abc")
774
+ f << Campo::Input.new( "def", :text ).labelled("def")
775
+ f << Campo::Input.new( "ghi", :text ).labelled("ghi")
776
+ end
777
+ form
778
+ }
779
+ subject { Campo.output form }
780
+ it { should_not be_nil }
781
+ it { should == expected }
782
+ end
783
+ end
784
+
785
+
786
+ describe "A form with a group of radio buttons" do
787
+ let(:expected) {
788
+ top_bit + %q!
789
+ %form{ atts[:myform], method: "POST", name: "myform", }
790
+ %fieldset{ }
791
+ %legend{ }Select the colour you like most:
792
+ %label{ for: "radio1_green", }
793
+ green
794
+ %input{ atts[:radio1_green], tabindex: "#{i += 1}", type: "radio", id: "radio1_green", value: "green", name: "radio1", }
795
+ %label{ for: "radio1_yellow", }
796
+ yellow
797
+ %input{ atts[:radio1_yellow], tabindex: "#{i += 1}", type: "radio", id: "radio1_yellow", value: "yellow", name: "radio1", }
798
+ %label{ for: "radio1_red", }
799
+ red
800
+ %input{ atts[:radio1_red], tabindex: "#{i += 1}", type: "radio", id: "radio1_red", value: "red", name: "radio1", }
801
+ %label{ for: "radio1_blue", }
802
+ blue
803
+ %input{ atts[:radio1_blue], tabindex: "#{i += 1}", type: "radio", id: "radio1_blue", value: "blue", name: "radio1", }
804
+ %label{ for: "radio1_purple", }
805
+ purple
806
+ %input{ atts[:radio1_purple], tabindex: "#{i += 1}", type: "radio", id: "radio1_purple", value: "purple", name: "radio1", }
807
+
808
+ !.strip + "\n"
809
+ }
810
+
811
+ let(:radios) {
812
+ form = Campo::Form.new( "myform" )
813
+ form.fieldset( "Select the colour you like most:" ) do |f|
814
+ f << Campo::Input.new("radio1", :radio, value: "green" ).labelled( "green" )
815
+ f << Campo::Input.new("radio1", :radio, value: "yellow" ).labelled( "yellow" )
816
+ f << Campo::Input.new("radio1", :radio, value: "red" ).labelled( "red" )
817
+ f << Campo::Input.new("radio1", :radio, value: "blue" ).labelled( "blue" )
818
+ f << Campo::Input.new("radio1", :radio, value: "purple" ).labelled( "purple" )
819
+ end
820
+ form
821
+ }
822
+ subject { Campo.output radios }
823
+ it { should_not be_nil }
824
+ it { should == expected }
825
+ end # a group of radio buttons
826
+ end # Input
827
+
828
+ describe Textarea do
829
+ context "Given no arguments" do
830
+ it { should_not be_nil }
831
+ it { should raise_error }
832
+ end
833
+ context "Given a name" do
834
+ subject { Textarea.new( "textie" ) }
835
+ it { should_not be_nil }
836
+ it { should be_a_kind_of(Textarea) }
837
+
838
+ context "and using convenience method" do
839
+ let(:form) { Campo::Form.new( "myform" ) }
840
+ subject { form.textarea( "textie" ) }
841
+ it { should_not be_nil }
842
+ it { should be_a_kind_of(Textarea) }
843
+
844
+ describe "the full output" do
845
+ let(:expected) { top_bit + %q!
846
+ %form{ atts[:myform], method: "POST", name: "myform", }
847
+ %textarea{ atts[:textie], tabindex: "#{i += 1}", cols: "40", rows: "10", name: "textie", }= inners[:textie] !.strip + " \n"}
848
+ let(:form){
849
+ form = Campo::Form.new( "myform" )
850
+ form.textarea( "textie" )
851
+ form
852
+ }
853
+ subject { Campo.output form }
854
+ it { should == expected }
855
+ end
856
+ end
857
+
858
+ context "and an attribute" do
859
+ subject { Textarea.new( "textie", cols: 60 ) }
860
+ it { should_not be_nil }
861
+ it { should be_a_kind_of(Textarea) }
862
+
863
+ context "When using convenience method" do
864
+ let(:form) { Campo::Form.new( "myform" ) }
865
+ subject { form.textarea( "textie" ) }
866
+ it { should_not be_nil }
867
+ it { should be_a_kind_of(Textarea) }
868
+
869
+ describe "the full output" do
870
+ let(:expected) { top_bit + %q!
871
+ %form{ atts[:myform], method: "POST", name: "myform", }
872
+ %textarea{ atts[:textie], tabindex: "#{i += 1}", cols: "60", rows: "10", name: "textie", }= inners[:textie]
873
+ !.strip + " \n"}
874
+ let(:form){
875
+ form = Campo::Form.new( "myform" )
876
+ form.textarea( "textie", cols: 60 )
877
+ form
878
+ }
879
+ subject { Campo.output form }
880
+ it { should == expected }
881
+ end
882
+ end
883
+ end
884
+
885
+ end
886
+ end
887
+
888
+ end # describe Campo
889
+ end # Campo