poefy-sqlite3 0.1.0 → 2.0.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.
@@ -1,686 +1,701 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: UTF-8
3
-
4
- ################################################################################
5
-
6
- describe Poefy::Poem, "-- SQLite" do
7
-
8
- before(:all) do
9
- require 'poefy/sqlite3'
10
- @root = Poefy.root
11
- dbs = %w{spec_test_tiny spec_shakespeare spec_whitman}
12
- dbs.each do |db_name|
13
- db_file = "#{@root}/data/#{db_name}.db"
14
- File.delete(db_file) if File.exists?(db_file)
15
- end
16
- end
17
-
18
- after(:all) do
19
- dbs = %w{spec_test_tiny spec_shakespeare spec_whitman}
20
- dbs.each do |db_name|
21
- db_file = "#{@root}/data/#{db_name}.db"
22
- File.delete(db_file) if File.exists?(db_file)
23
- end
24
- end
25
-
26
- ##############################################################################
27
-
28
- describe "using tiny dataset 'spec_test_tiny'" do
29
- corpus = "spec_test_tiny"
30
- db_file = "#{Poefy.root}/data/#{corpus}.db"
31
-
32
- # Create a small corpus of a few rhymes.
33
- text_array = %w{man plan flan can dan fish dish wish bee sea tree flea}
34
- text_array.map!{ |i| 'a ' + i }
35
- text_string = text_array.join("\n")
36
- row_count = text_array.count
37
-
38
- before(:each) do
39
- @poefy = Poefy::Poem.new(corpus, { proper: false })
40
- end
41
- after(:each) do
42
- @poefy.close
43
- end
44
- it "initialised object not nil" do
45
- expect(@poefy).to_not be_nil
46
- end
47
-
48
- # Create corpora in the three different ways.
49
- describe "@poefy#make_database!" do
50
-
51
- it "Use array of strings" do
52
- @poefy.make_database! text_array
53
- expect(@poefy.corpus.exists?).to be true
54
- expect(File.exists?(db_file)).to be true
55
- expect(@poefy.corpus.count).to be row_count
56
- poem = @poefy.poem({ rhyme: 'aabb' })
57
- expect(poem.count).to be 4
58
- end
59
-
60
- it "Use one long newline delimited string" do
61
- @poefy.make_database! text_string
62
- expect(@poefy.corpus.exists?).to be true
63
- expect(File.exists?(db_file)).to be true
64
- expect(@poefy.corpus.count).to be row_count
65
- poem = @poefy.poem({ rhyme: 'aabb' })
66
- expect(poem.count).to be 4
67
- end
68
-
69
- it "Use text lines from a file" do
70
-
71
- # Create a temp file.
72
- tmp = Tempfile.new('spec-', Poefy.root + '/spec')
73
- text_path = tmp.path
74
- tmp.write text_string
75
- tmp.close
76
-
77
- @poefy.make_database! text_path
78
- expect(@poefy.corpus.exists?).to be true
79
- expect(File.exists?(db_file)).to be true
80
- expect(@poefy.corpus.count).to be row_count
81
- poem = @poefy.poem({ rhyme: 'aabb' })
82
- expect(poem.count).to be 4
83
-
84
- # Delete the temp file.
85
- tmp.delete
86
- end
87
- end
88
-
89
- # Make sure that the description can be updated as specified
90
- # and that it doesn't cause SQL injection.
91
- describe "corpus description using #desc=" do
92
- it "@poefy.corpus.desc is initially empty" do
93
- expect(@poefy.corpus.desc).to eq ''
94
- end
95
-
96
- values = [
97
- "test",
98
- " -- test",
99
- "; -- test",
100
- "test' -- ",
101
- "test'' -- ",
102
- "'test' -- ",
103
- "'test'' -- ",
104
- "Shakespeare's sonnets",
105
- "Shakespeare's -- sonnets",
106
- "Shakespeare's; -- sonnets",
107
- "test' ; INSERT INTO spec_test_tiny VALUES('foo') -- ",
108
- "105 OR 1=1",
109
- "' or ''='"
110
- ]
111
- values.each do |value|
112
- it "@poefy.corpus.desc = #{value}" do
113
- @poefy.corpus.desc = value
114
- expect(@poefy.corpus.desc).to eq value
115
- expect(@poefy.corpus.count).to be row_count
116
- end
117
- end
118
- end
119
-
120
- describe ":rhyme option" do
121
-
122
- describe "should return nil" do
123
- it "blank, no argument" do
124
- poem = @poefy.poem
125
- expect(poem).to be_nil
126
- end
127
- it "({ })" do
128
- poem = @poefy.poem ({ })
129
- expect(poem).to be_nil
130
- end
131
- it "({ rhyme: nil })" do
132
- poem = @poefy.poem ({ rhyme: nil })
133
- expect(poem).to be_nil
134
- end
135
- it "({ rhyme: ' ' })" do
136
- poem = @poefy.poem ({ rhyme: ' ' })
137
- expect(poem).to be_nil
138
- end
139
- it "({ rhyme: '' })" do
140
- poem = @poefy.poem ({ rhyme: '' })
141
- expect(poem).to be_nil
142
- end
143
- end
144
-
145
- describe "should return correct number of lines" do
146
- rhymes = %w{a b z A aa ab zz AA AB AA1 A1 B1 Z1 AB1 A1A1A1A1B1B1B1B1B1}
147
- rhymes += ['A1A1A1 A1A1A1 B1B1B1B1B1B1','a b c a b c']
148
- rhymes += [' abc','abc ',' abc ']
149
- rhymes += ['n aaa n','n aXXXa N1']
150
- rhymes.each do |i|
151
- it "({ rhyme: '#{i}' })" do
152
- poem = @poefy.poem ({ rhyme: i })
153
- expect(poem.count).to be i.gsub(/[0-9]/,'').length
154
- end
155
- end
156
- end
157
-
158
- describe "should accept characters other than number" do
159
- rhymes = %w{. , : .. ., ,, :: (()) @ ~ <<>< A1A1A1...a;}
160
- rhymes.each do |i|
161
- it "({ rhyme: '#{i}' })" do
162
- poem = @poefy.poem ({ rhyme: i })
163
- expect(poem.count).to be i.gsub(/[0-9]/,'').length
164
- end
165
- end
166
- end
167
-
168
- describe "should be nil if can't parse rhyme string" do
169
- rhymes = %w{a1 b1 ab1 Ab1 AAAAABb1 1 1111 1122 11221 ;;::1. }
170
- rhymes += ['AA Bb1','11 11','11 1 1','..1.']
171
- rhymes.each do |i|
172
- it "({ rhyme: '#{i}' })" do
173
- poem = @poefy.poem ({ rhyme: i })
174
- expect(poem).to be_nil
175
- end
176
- end
177
- end
178
-
179
- describe "should be nil if can't complete rhyme string" do
180
- rhymes = %w{aaaaaa abcd aaaaabbbbb}
181
- rhymes.each do |i|
182
- it "({ rhyme: '#{i}' })" do
183
- poem = @poefy.poem ({ rhyme: i })
184
- expect(poem).to be_nil
185
- end
186
- end
187
- end
188
-
189
- describe "should correctly repeat uppercase lines" do
190
- lines = 200
191
- it "({ rhyme: 'A' * #{lines} })" do
192
- poem = @poefy.poem ({ rhyme: 'A' * lines })
193
- expect(poem.count).to be lines
194
- expect(poem.uniq.count).to be 1
195
- end
196
- it "({ rhyme: ('A'..'C').to_a.map { |i| i * #{lines} }.join })" do
197
- rhyme = ('A'..'C').to_a.map { |i| i * lines }.join
198
- poem = @poefy.poem ({ rhyme: rhyme })
199
- expect(poem.count).to be lines * 3
200
- expect(poem.uniq.count).to be 3
201
- end
202
- end
203
-
204
- describe "should be nil if can't complete repeating rhyme string" do
205
- lines = 200
206
- it "({ rhyme: ('A'..'D').to_a.map { |i| i * #{lines} }.join })" do
207
- rhyme = ('A'..'D').to_a.map { |i| i * lines }.join
208
- poem = @poefy.poem ({ rhyme: rhyme })
209
- expect(poem).to be_nil
210
- end
211
- end
212
-
213
- end
214
-
215
- describe ":form option" do
216
-
217
- describe "should return correct number of lines" do
218
- it "({ form: :default })" do
219
- poem = @poefy.poem ({ form: :default })
220
- expect(poem.count).to be 1
221
- end
222
- end
223
-
224
- describe "should be nil if given a named form it can't fulfil" do
225
- it "({ form: 'sonnet' })" do
226
- poem = @poefy.poem ({ form: 'sonnet' })
227
- expect(poem).to be_nil
228
- end
229
- it "({ form: :villanelle })" do
230
- poem = @poefy.poem ({ form: :villanelle })
231
- expect(poem).to be_nil
232
- end
233
- end
234
-
235
- describe "should be nil if given a junk named form" do
236
- it "({ form: 'sonnet_junk' })" do
237
- poem = @poefy.poem ({ form: 'sonnet_junk' })
238
- expect(poem).to be_nil
239
- end
240
- it "({ form: :not_a_form })" do
241
- poem = @poefy.poem ({ form: :not_a_form })
242
- expect(poem).to be_nil
243
- end
244
- it "({ form: :not_a_form, indent: '0010' })" do
245
- poem = @poefy.poem ({ form: :not_a_form, indent: '0010' })
246
- expect(poem).to be_nil
247
- end
248
- end
249
-
250
- describe "should be valid if given a junk named form, and a rhyme" do
251
- it "({ form: :not_a_form, rhyme: 'abcb' })" do
252
- poem = @poefy.poem ({ form: :not_a_form, rhyme: 'abcb' })
253
- expect(poem.count).to be 4
254
- end
255
- end
256
-
257
- describe "should overwrite a named form if another option is specified" do
258
- it "({ form: 'default', rhyme: 'ab' })" do
259
- poem = @poefy.poem ({ form: 'default', rhyme: 'ab' })
260
- expect(poem.count).to be 2
261
- end
262
- it "({ form: :villanelle, rhyme: 'abcb' })" do
263
- poem = @poefy.poem ({ form: :villanelle, rhyme: 'abcb' })
264
- expect(poem.count).to be 4
265
- end
266
- end
267
- end
268
- end
269
-
270
- ##############################################################################
271
-
272
- describe "using dataset 'spec_shakespeare'" do
273
-
274
- file_txt = "shakespeare_sonnets.txt"
275
- file_db = "spec_shakespeare.db"
276
-
277
- # All the Shakespeare lines are pentameter, so some forms should fail.
278
- forms = Poefy::PoeticForms::POETIC_FORMS
279
- forms_fail = [:limerick, :haiku, :common, :ballad, :double_dactyl]
280
- forms_pass = forms.keys - forms_fail
281
-
282
- before(:each) do
283
- @poefy = Poefy::Poem.new(file_db, { proper: false })
284
- end
285
- after(:each) do
286
- @poefy.close
287
- end
288
-
289
- it "initialised object not nil" do
290
- expect(@poefy).to_not be_nil
291
- end
292
-
293
- describe "#make_database( '#{@root}/data/#{file_txt}', true )" do
294
- it "should make the database '#{@root}/data/#{file_db}" do
295
- db_file = "#{@root}/data/#{file_db}"
296
- input = `sed '/[a-z]/!d' #{@root}/data/#{file_txt}`
297
- @poefy.make_database input
298
- expect(@poefy.corpus.exists?).to be true
299
- expect(File.exists?(db_file)).to be true
300
- end
301
- end
302
-
303
- describe "using acrostic option" do
304
- describe "should return correct number of lines" do
305
- it "({ form: :sonnet, acrostic: 'pauldpthompson' })" do
306
- poem = @poefy.poem ({ form: :sonnet,
307
- acrostic: 'pauldpthompson' })
308
- expect(poem.count).to be 14
309
- end
310
- end
311
- describe "should fail to be created" do
312
- it "({ form: :sonnet, acrostic: 'qqqqqqqqqqqqqq' })" do
313
- poem = @poefy.poem ({ form: :sonnet,
314
- acrostic: 'qqqqqqqqqqqqqq' })
315
- expect(poem).to be_nil
316
- end
317
- end
318
- end
319
-
320
- describe "using form string" do
321
- describe "should return correct number of lines" do
322
-
323
- # Make sure each form's lines match the expected output.
324
- # Generate a few to be sure.
325
- forms_pass.each do |form|
326
- it "({ form: #{form} })" do
327
- 10.times do
328
- poem = @poefy.poem ({ form: form })
329
- expect(poem.count).to satisfy do |c|
330
- [*forms[form][:rhyme]].map do |r|
331
- r.gsub(/[0-9]/,'').length
332
- end.include?(c)
333
- end
334
- end
335
- end
336
- end
337
- end
338
-
339
- describe "should fail to be created" do
340
- forms_fail.each do |form|
341
- it "({ form: #{form} })" do
342
- 4.times do
343
- poem = @poefy.poem ({ form: form })
344
- expect(poem).to be_nil
345
- end
346
- end
347
- end
348
- end
349
- end
350
-
351
- describe "make sonnets" do
352
- sonnet_options = [
353
- { rhyme: 'ababcdcdefefgg' },
354
- { rhyme: 'abab cdcd efef gg', indent: '0101 0101 0011 01' },
355
- { form: 'sonnet' },
356
- { form: :sonnet, syllable: 0 },
357
- { form: :sonnet, syllable: 10 },
358
- { form: :sonnet, regex: /^[A-Z].*$/ },
359
- { form: :sonnet, regex: '^[A-Z].*$' },
360
- { form: :sonnet, acrostic: 'pauldpthompson' },
361
- { form: 'sonnet', indent: '01010101001101' },
362
- { form: 'sonnet', proper: false }
363
- ]
364
- sonnet_options.each do |option|
365
- it "#{option}" do
366
- 4.times do
367
- poem = @poefy.poem(option)
368
- expect(poem).to_not be_nil
369
- end
370
- end
371
- end
372
- end
373
- end
374
-
375
- ##############################################################################
376
-
377
- describe "using dataset 'spec_whitman'" do
378
-
379
- file_txt = "whitman_leaves.txt"
380
- file_db = "spec_whitman.db"
381
-
382
- # There's a good mix of syllable count, so all forms should pass.
383
- forms = Poefy::PoeticForms::POETIC_FORMS
384
- forms_pass = forms.keys
385
-
386
- before(:each) do
387
- @poefy = Poefy::Poem.new(file_db, { proper: false })
388
- end
389
- after(:each) do
390
- @poefy.close
391
- end
392
-
393
- it "initialised object not nil" do
394
- expect(@poefy).to_not be_nil
395
- end
396
-
397
- describe "#make_database( '#{@root}/data/#{file_txt}', true )" do
398
- it "should make the database '#{@root}/data/#{file_db}" do
399
- db_file = "#{@root}/data/#{file_db}"
400
- input = `sed '/[a-z]/!d' #{@root}/data/#{file_txt}`
401
- @poefy.make_database input
402
- expect(@poefy.corpus.exists?).to be true
403
- expect(File.exists?(db_file)).to be true
404
- end
405
- end
406
-
407
- describe "using form string" do
408
- describe "should return correct number of lines" do
409
-
410
- # Make sure each form's lines match the expected output.
411
- # Generate a few to be sure.
412
- forms_pass.each do |form|
413
- it "({ form: #{form} })" do
414
- 10.times do
415
- poem = @poefy.poem ({ form: form })
416
- expect(poem.count).to satisfy do |c|
417
- [*forms[form][:rhyme]].map do |r|
418
- r.gsub(/[0-9]/,'').length
419
- end.include?(c)
420
- end
421
- end
422
- end
423
- end
424
- end
425
- end
426
-
427
- describe "make sonnets" do
428
- sonnet_options = [
429
- { rhyme: 'ababcdcdefefgg' },
430
- { rhyme: 'abab cdcd efef gg', indent: '0101 0101 0011 01' },
431
- { form: 'sonnet' },
432
- { form: :sonnet, syllable: 0 },
433
- { form: :sonnet, syllable: 10 },
434
- { form: :sonnet, regex: /^[A-Z].*$/ },
435
- { form: :sonnet, regex: '^[A-Z].*$' },
436
- { form: :sonnet, acrostic: 'pauldpthompson' },
437
- { form: 'sonnet', indent: '01010101001101' },
438
- { form: 'sonnet', proper: false }
439
- ]
440
- sonnet_options.each do |option|
441
- it "#{option}" do
442
- 4.times do
443
- poem = @poefy.poem(option)
444
- expect(poem).to_not be_nil
445
- end
446
- end
447
- end
448
- end
449
-
450
- describe "using syllable string" do
451
-
452
- it "({ rhyme: 'abcb defe', syllable: '[8,6,8,6,0,8,6,8,6]' })" do
453
- options = {
454
- rhyme: 'abcb defe',
455
- syllable: '[8,6,8,6,0,8,6,8,6]'
456
- }
457
- poem = @poefy.poem (options)
458
- expect(poem.count).to be options[:rhyme].length
459
- end
460
-
461
- it "({ rhyme: 'abcb defe', syllable: '[8,6,8,6,8,6,8,6]' })" do
462
- options = {
463
- rhyme: 'abcb defe',
464
- syllable: '[8,6,8,6,8,6,8,6]'
465
- }
466
- poem = @poefy.poem (options)
467
- expect(poem.count).to be options[:rhyme].length
468
- end
469
- end
470
- end
471
-
472
- ##############################################################################
473
-
474
- describe "reusing the same Poem instance" do
475
- it "should correctly merge the option hashes" do
476
-
477
- # Default to use rondeau poetic form, and proper sentence validation
478
- poefy = Poefy::Poem.new(
479
- 'spec_shakespeare',
480
- { form: 'rondeau', proper: true }
481
- )
482
-
483
- # Generate a properly sentenced rondeau
484
- poem = poefy.poem
485
- expect(poem.count).to be 17
486
-
487
- # Generate a rondeau without proper validation
488
- poem = poefy.poem ({ proper: false })
489
- expect(poem.count).to be 17
490
-
491
- # Generate a proper rondeau with a certain indentation
492
- poem = poefy.poem ({ indent: '01012 0012 010112' })
493
- expect(poem.count).to be 17
494
-
495
- # Generate other forms
496
- poem = poefy.poem ({ rhyme: 'abbaabbacdecde' })
497
- expect(poem.count).to be 14
498
- poem = poefy.poem ({ form: 'sonnet' })
499
- expect(poem.count).to be 14
500
- poem = poefy.poem ({ form: 'ballade' })
501
- expect(poem.count).to be 31
502
-
503
- # Generate a default rondeau again
504
- poem = poefy.poem
505
- expect(poem.count).to be 17
506
-
507
- poefy.close
508
- end
509
- end
510
-
511
- ##############################################################################
512
-
513
- describe "using the transform option" do
514
-
515
- it "should correctly transform the output 1" do
516
- poefy = Poefy::Poem.new :spec_shakespeare
517
- transform_hash = {
518
- 4 => proc { |line, num, poem| line.upcase },
519
- 12 => proc { |line, num, poem| line.upcase }
520
- }
521
- poem = poefy.poem({ form: :sonnet, transform: transform_hash })
522
- expect(poem.count).to be 14
523
- expect(poem[3]).to eq poem[3].upcase
524
- expect(poem[11]).to eq poem[11].upcase
525
- poefy.close
526
- end
527
-
528
- it "should correctly transform the output 2" do
529
- poefy = Poefy::Poem.new :spec_shakespeare
530
- transform_hash = {
531
- 4 => proc { |line, num, poem| poem.count },
532
- -3 => proc { |line, num, poem| poem.count },
533
- 7 => proc { |line, num, poem| 'test string' }
534
- }
535
- poem = poefy.poem({ form: :sonnet, transform: transform_hash })
536
- expect(poem.count).to be 14
537
- expect(poem[3]).to eq '14'
538
- expect(poem[11]).to eq '14'
539
- expect(poem[6]).to eq 'test string'
540
- poefy.close
541
- end
542
-
543
- it "should correctly transform the output 3" do
544
- poefy = Poefy::Poem.new :spec_shakespeare
545
- transform_proc = proc { |line, num, poem| line.downcase }
546
- poem = poefy.poem({ form: :sonnet, transform: transform_proc })
547
- expect(poem.count).to be 14
548
- poem.each do |i|
549
- expect(i).to eq i.downcase
550
- end
551
- poefy.close
552
- end
553
-
554
- it "should correctly transform the output 4" do
555
- poefy = Poefy::Poem.new :spec_shakespeare
556
- transform_proc = proc { |line, num, poem| "#{num} #{line.downcase}" }
557
- poem = poefy.poem({ form: :sonnet, transform: transform_proc })
558
- expect(poem.count).to be 14
559
- poem.each.with_index do |line, index|
560
- expect(line).to eq line.downcase
561
- first_word = line.split(' ').first
562
- expect(first_word).to eq (index + 1).to_s
563
- end
564
- poefy.close
565
- end
566
- end
567
-
568
- ##############################################################################
569
-
570
- describe "using the form_from_text option" do
571
- before(:all) do
572
- @text = <<-TEXT
573
- [Chorus 1]
574
- Oh yeah, I'll tell you something
575
- I think you'll understand
576
- When I'll say that something
577
- I want to hold your hand
578
- I want to hold your hand
579
- I want to hold your hand
580
-
581
- [Verse 1]
582
- Oh please, say to me
583
- You'll let me be your man
584
- And please, say to me
585
- You'll let me hold your hand
586
- I'll let me hold your hand
587
- I want to hold your hand
588
- TEXT
589
- @line_count = @text.split("\n").count
590
- end
591
-
592
- it "should use the exact poetic form 1" do
593
- poefy = Poefy::Poem.new(:spec_whitman, {
594
- form_from_text: @text
595
- })
596
- poem = poefy.poem
597
- poem.map!(&:strip!)
598
- expect(poem.count).to be @line_count
599
- expect(poem[0]).to eq "[Chorus 1]"
600
- expect(poem[8]).to eq "[Verse 1]"
601
- expect(poem[5]).to eq poem[4]
602
- expect(poem[6]).to eq poem[4]
603
- poefy.close
604
- end
605
-
606
- it "should use the exact poetic form 2" do
607
- poefy = Poefy::Poem.new :spec_whitman
608
- poem = poefy.poem({
609
- form_from_text: @text
610
- })
611
- poem.map!(&:strip!)
612
- expect(poem.count).to be @line_count
613
- expect(poem[0]).to eq "[Chorus 1]"
614
- expect(poem[8]).to eq "[Verse 1]"
615
- expect(poem[5]).to eq poem[4]
616
- expect(poem[6]).to eq poem[4]
617
- poefy.close
618
- end
619
-
620
- it "should correctly modify the poetic form 1" do
621
- poefy = Poefy::Poem.new(:spec_whitman, {
622
- form_from_text: @text,
623
- syllable: 6
624
- })
625
- poem = poefy.poem
626
- poem.map!(&:strip!)
627
- expect(poem.count).to be @line_count
628
- expect(poem[0]).to eq "[Chorus 1]"
629
- expect(poem[8]).to eq "[Verse 1]"
630
- expect(poem[5]).to eq poem[4]
631
- expect(poem[6]).to eq poem[4]
632
- poefy.close
633
- end
634
-
635
- it "should correctly modify the poetic form 2" do
636
- poefy = Poefy::Poem.new :spec_whitman
637
- poem = poefy.poem({
638
- form_from_text: @text,
639
- syllable: 6
640
- })
641
- poem.map!(&:strip!)
642
- expect(poem.count).to be @line_count
643
- expect(poem[0]).to eq "[Chorus 1]"
644
- expect(poem[8]).to eq "[Verse 1]"
645
- expect(poem[5]).to eq poem[4]
646
- expect(poem[6]).to eq poem[4]
647
- poefy.close
648
- end
649
-
650
- it "should correctly modify the poetic form 3" do
651
- poefy = Poefy::Poem.new(:spec_whitman, {
652
- form_from_text: @text
653
- })
654
- poem = poefy.poem({
655
- syllable: 6
656
- })
657
- poem.map!(&:strip!)
658
- expect(poem.count).to be @line_count
659
- expect(poem[0]).to eq "[Chorus 1]"
660
- expect(poem[8]).to eq "[Verse 1]"
661
- expect(poem[5]).to eq poem[4]
662
- expect(poem[6]).to eq poem[4]
663
- poefy.close
664
- end
665
-
666
- it "should correctly replace the poetic form" do
667
- poefy = Poefy::Poem.new(:spec_whitman, {
668
- syllable: 6
669
- })
670
- poem = poefy.poem({
671
- form_from_text: @text
672
- })
673
- poem.map!(&:strip!)
674
- expect(poem.count).to be @line_count
675
- expect(poem[0]).to eq "[Chorus 1]"
676
- expect(poem[8]).to eq "[Verse 1]"
677
- expect(poem[5]).to eq poem[4]
678
- expect(poem[6]).to eq poem[4]
679
- poefy.close
680
- end
681
-
682
- end
683
-
684
- end
685
-
686
- ################################################################################
1
+ #!/usr/bin/env ruby
2
+ # Encoding: UTF-8
3
+
4
+ ################################################################################
5
+
6
+ describe Poefy::Poem, "-- SQLite" do
7
+
8
+ before(:all) do
9
+ require 'poefy/sqlite3'
10
+ @root = Poefy.root
11
+ dbs = %w{spec_test_tiny spec_shakespeare spec_whitman}
12
+ dbs.each do |db_name|
13
+ db_file = "#{@root}/data/#{db_name}.db"
14
+ File.delete(db_file) if File.exist?(db_file)
15
+ end
16
+ end
17
+
18
+ after(:all) do
19
+ dbs = %w{spec_test_tiny spec_shakespeare spec_whitman}
20
+ dbs.each do |db_name|
21
+ db_file = "#{@root}/data/#{db_name}.db"
22
+ File.delete(db_file) if File.exist?(db_file)
23
+ end
24
+ end
25
+
26
+ ##############################################################################
27
+
28
+ describe "using tiny dataset 'spec_test_tiny'" do
29
+ corpus = "spec_test_tiny"
30
+ db_file = "#{Poefy.root}/data/#{corpus}.db"
31
+
32
+ # Create a small corpus of a few rhymes.
33
+ text_array = %w{man plan flan can dan fish dish wish bee sea tree flea}
34
+ text_array.map!{ |i| 'a ' + i }
35
+ text_string = text_array.join("\n")
36
+ row_count = text_array.count
37
+
38
+ before(:each) do
39
+ @poefy = Poefy::Poem.new(corpus, { proper: false })
40
+ end
41
+ after(:each) do
42
+ @poefy.close
43
+ end
44
+ it "initialised object not nil" do
45
+ expect(@poefy).to_not be_nil
46
+ end
47
+
48
+ # Create corpora in the three different ways.
49
+ describe "@poefy#make_database!" do
50
+
51
+ it "Use array of strings" do
52
+ @poefy.make_database! text_array
53
+ expect(@poefy.corpus.exist?).to be true
54
+ expect(File.exist?(db_file)).to be true
55
+ expect(@poefy.corpus.count).to be row_count
56
+ poem = @poefy.poem({ rhyme: 'aabb' })
57
+ expect(poem.count).to be 4
58
+ end
59
+
60
+ it "Use one long newline delimited string" do
61
+ @poefy.make_database! text_string
62
+ expect(@poefy.corpus.exist?).to be true
63
+ expect(File.exist?(db_file)).to be true
64
+ expect(@poefy.corpus.count).to be row_count
65
+ poem = @poefy.poem({ rhyme: 'aabb' })
66
+ expect(poem.count).to be 4
67
+ end
68
+
69
+ it "Use text lines from a file" do
70
+
71
+ # Create a temp file.
72
+ tmp = Tempfile.new('spec-', Poefy.root + '/spec')
73
+ text_path = tmp.path
74
+ tmp.write text_string
75
+ tmp.close
76
+
77
+ @poefy.make_database! text_path
78
+ expect(@poefy.corpus.exist?).to be true
79
+ expect(File.exist?(db_file)).to be true
80
+ expect(@poefy.corpus.count).to be row_count
81
+ poem = @poefy.poem({ rhyme: 'aabb' })
82
+ expect(poem.count).to be 4
83
+
84
+ # Delete the temp file.
85
+ tmp.delete
86
+ end
87
+ end
88
+
89
+ # Make sure that the description can be updated as specified
90
+ # and that it doesn't cause SQL injection.
91
+ describe "corpus description using #desc=" do
92
+ it "@poefy.corpus.desc is initially empty" do
93
+ expect(@poefy.corpus.desc).to eq ''
94
+ end
95
+
96
+ values = [
97
+ "test",
98
+ " -- test",
99
+ "; -- test",
100
+ "test' -- ",
101
+ "test'' -- ",
102
+ "'test' -- ",
103
+ "'test'' -- ",
104
+ "Shakespeare's sonnets",
105
+ "Shakespeare's -- sonnets",
106
+ "Shakespeare's; -- sonnets",
107
+ "test' ; INSERT INTO spec_test_tiny VALUES('foo') -- ",
108
+ "105 OR 1=1",
109
+ "' or ''='"
110
+ ]
111
+ values.each do |value|
112
+ it "@poefy.corpus.desc = #{value}" do
113
+ @poefy.corpus.desc = value
114
+ expect(@poefy.corpus.desc).to eq value
115
+ expect(@poefy.corpus.count).to be row_count
116
+ end
117
+ end
118
+ end
119
+
120
+ describe ":rhyme option" do
121
+
122
+ describe "should raise Poefy::MissingFormOrRhyme" do
123
+ it "blank, no argument" do
124
+ expect {
125
+ @poefy.poem
126
+ }.to raise_error(Poefy::MissingFormOrRhyme)
127
+ end
128
+ it "({ })" do
129
+ expect {
130
+ @poefy.poem ({ })
131
+ }.to raise_error(Poefy::MissingFormOrRhyme)
132
+ end
133
+ it "({ rhyme: nil })" do
134
+ expect {
135
+ @poefy.poem ({ rhyme: nil })
136
+ }.to raise_error(Poefy::MissingFormOrRhyme)
137
+ end
138
+ it "({ rhyme: ' ' })" do
139
+ expect {
140
+ @poefy.poem ({ rhyme: ' ' })
141
+ }.to raise_error(Poefy::MissingFormOrRhyme)
142
+ end
143
+ it "({ rhyme: '' })" do
144
+ expect {
145
+ @poefy.poem ({ rhyme: '' })
146
+ }.to raise_error(Poefy::MissingFormOrRhyme)
147
+ end
148
+ end
149
+
150
+ describe "should return correct number of lines" do
151
+ rhymes = %w{a b z A aa ab zz AA AB AA1 A1 B1 Z1 AB1 A1A1A1A1B1B1B1B1B1}
152
+ rhymes += ['A1A1A1 A1A1A1 B1B1B1B1B1B1','a b c a b c']
153
+ rhymes += [' abc','abc ',' abc ']
154
+ rhymes += ['n aaa n','n aXXXa N1']
155
+ rhymes.each do |i|
156
+ it "({ rhyme: '#{i}' })" do
157
+ poem = @poefy.poem ({ rhyme: i })
158
+ expect(poem.count).to be i.gsub(/[0-9]/,'').length
159
+ end
160
+ end
161
+ end
162
+
163
+ describe "should accept characters other than number" do
164
+ rhymes = %w{. , : .. ., ,, :: (()) @ ~ <<>< A1A1A1...a;}
165
+ rhymes.each do |i|
166
+ it "({ rhyme: '#{i}' })" do
167
+ poem = @poefy.poem ({ rhyme: i })
168
+ expect(poem.count).to be i.gsub(/[0-9]/,'').length
169
+ end
170
+ end
171
+ end
172
+
173
+ describe "should raise error if can't parse rhyme string" do
174
+ rhymes = %w{a1 b1 ab1 Ab1 AAAAABb1 1 1111 1122 11221 ;;::1. }
175
+ rhymes += ['AA Bb1','11 11','11 1 1','..1.']
176
+ rhymes.each do |i|
177
+ it "({ rhyme: '#{i}' })" do
178
+ expect {
179
+ @poefy.poem ({ rhyme: i })
180
+ }.to raise_error(Poefy::RhymeError)
181
+ end
182
+ end
183
+ end
184
+
185
+ describe "should raise error if can't complete rhyme string" do
186
+ rhymes = %w{aaaaaa abcd aaaaabbbbb}
187
+ rhymes.each do |i|
188
+ it "({ rhyme: '#{i}' })" do
189
+ expect {
190
+ @poefy.poem ({ rhyme: i })
191
+ }.to raise_error(Poefy::NotEnoughData)
192
+ end
193
+ end
194
+ end
195
+
196
+ describe "should correctly repeat uppercase lines" do
197
+ lines = 200
198
+ it "({ rhyme: 'A' * #{lines} })" do
199
+ poem = @poefy.poem ({ rhyme: 'A' * lines })
200
+ expect(poem.count).to be lines
201
+ expect(poem.uniq.count).to be 1
202
+ end
203
+ it "({ rhyme: ('A'..'C').to_a.map { |i| i * #{lines} }.join })" do
204
+ rhyme = ('A'..'C').to_a.map { |i| i * lines }.join
205
+ poem = @poefy.poem ({ rhyme: rhyme })
206
+ expect(poem.count).to be lines * 3
207
+ expect(poem.uniq.count).to be 3
208
+ end
209
+ end
210
+
211
+ describe "should raise error if can't complete repeating rhyme string" do
212
+ lines = 200
213
+ it "({ rhyme: ('A'..'D').to_a.map { |i| i * #{lines} }.join })" do
214
+ rhyme = ('A'..'D').to_a.map { |i| i * lines }.join
215
+ expect {
216
+ @poefy.poem ({ rhyme: rhyme })
217
+ }.to raise_error(Poefy::NotEnoughData)
218
+ end
219
+ end
220
+
221
+ end
222
+
223
+ describe ":form option" do
224
+
225
+ describe "should return correct number of lines" do
226
+ it "({ form: :default })" do
227
+ poem = @poefy.poem ({ form: :default })
228
+ expect(poem.count).to be 1
229
+ end
230
+ end
231
+
232
+ describe "should raise error if given a named form it can't fulfil" do
233
+ it "({ form: 'sonnet' })" do
234
+ expect {
235
+ @poefy.poem ({ form: 'sonnet' })
236
+ }.to raise_error(Poefy::NotEnoughData)
237
+ end
238
+ it "({ form: :villanelle })" do
239
+ expect {
240
+ @poefy.poem ({ form: 'villanelle' })
241
+ }.to raise_error(Poefy::NotEnoughData)
242
+ end
243
+ end
244
+
245
+ describe "should raise error if given a junk named form" do
246
+ it "({ form: 'sonnet_junk' })" do
247
+ expect {
248
+ @poefy.poem ({ form: 'sonnet_junk' })
249
+ }.to raise_error(Poefy::MissingFormOrRhyme)
250
+ end
251
+ it "({ form: :not_a_form })" do
252
+ expect {
253
+ @poefy.poem ({ form: :not_a_form })
254
+ }.to raise_error(Poefy::MissingFormOrRhyme)
255
+ end
256
+ it "({ form: :not_a_form, indent: '0010' })" do
257
+ expect {
258
+ @poefy.poem ({ form: :not_a_form, indent: '0010' })
259
+ }.to raise_error(Poefy::MissingFormOrRhyme)
260
+ end
261
+ end
262
+
263
+ describe "should be valid if given a junk named form, and a rhyme" do
264
+ it "({ form: :not_a_form, rhyme: 'abcb' })" do
265
+ poem = @poefy.poem ({ form: :not_a_form, rhyme: 'abcb' })
266
+ expect(poem.count).to be 4
267
+ end
268
+ end
269
+
270
+ describe "should overwrite a named form if another option is specified" do
271
+ it "({ form: 'default', rhyme: 'ab' })" do
272
+ poem = @poefy.poem ({ form: 'default', rhyme: 'ab' })
273
+ expect(poem.count).to be 2
274
+ end
275
+ it "({ form: :villanelle, rhyme: 'abcb' })" do
276
+ poem = @poefy.poem ({ form: :villanelle, rhyme: 'abcb' })
277
+ expect(poem.count).to be 4
278
+ end
279
+ end
280
+ end
281
+ end
282
+
283
+ ##############################################################################
284
+
285
+ describe "using dataset 'spec_shakespeare'" do
286
+
287
+ file_txt = "shakespeare_sonnets.txt"
288
+ file_db = "spec_shakespeare.db"
289
+
290
+ # All the Shakespeare lines are pentameter, so some forms should fail.
291
+ forms = Poefy::PoeticForms::POETIC_FORMS
292
+ forms_fail = [:limerick, :haiku, :common, :ballad, :double_dactyl]
293
+ forms_pass = forms.keys - forms_fail
294
+
295
+ before(:each) do
296
+ @poefy = Poefy::Poem.new(file_db, { proper: false })
297
+ end
298
+ after(:each) do
299
+ @poefy.close
300
+ end
301
+
302
+ it "initialised object not nil" do
303
+ expect(@poefy).to_not be_nil
304
+ end
305
+
306
+ describe "#make_database( '#{@root}/data/#{file_txt}', true )" do
307
+ it "should make the database '#{@root}/data/#{file_db}" do
308
+ db_file = "#{@root}/data/#{file_db}"
309
+ input = `sed '/[a-z]/!d' #{@root}/data/#{file_txt}`
310
+ @poefy.make_database input
311
+ expect(@poefy.corpus.exist?).to be true
312
+ expect(File.exist?(db_file)).to be true
313
+ end
314
+ end
315
+
316
+ describe "using acrostic option" do
317
+ describe "should return correct number of lines" do
318
+ it "({ form: :sonnet, acrostic: 'pauldpthompson' })" do
319
+ poem = @poefy.poem ({ form: :sonnet,
320
+ acrostic: 'pauldpthompson' })
321
+ expect(poem.count).to be 14
322
+ end
323
+ end
324
+ describe "should raise Poefy::NotEnoughData" do
325
+ it "({ form: :sonnet, acrostic: 'qqqqqqqqqqqqqq' })" do
326
+ expect {
327
+ @poefy.poem ({ form: :sonnet,
328
+ acrostic: 'qqqqqqqqqqqqqq' })
329
+ }.to raise_error(Poefy::NotEnoughData)
330
+ end
331
+ end
332
+ end
333
+
334
+ describe "using form string" do
335
+ describe "should return correct number of lines" do
336
+
337
+ # Make sure each form's lines match the expected output.
338
+ # Generate a few to be sure.
339
+ forms_pass.each do |form|
340
+ it "({ form: #{form} })" do
341
+ 10.times do
342
+ poem = @poefy.poem ({ form: form })
343
+ expect(poem.count).to satisfy do |c|
344
+ [*forms[form][:rhyme]].map do |r|
345
+ r.gsub(/[0-9]/,'').length
346
+ end.include?(c)
347
+ end
348
+ end
349
+ end
350
+ end
351
+ end
352
+
353
+ describe "should raise Poefy::NotEnoughData" do
354
+ forms_fail.each do |form|
355
+ it "({ form: #{form} })" do
356
+ 4.times do
357
+ expect {
358
+ @poefy.poem ({ form: form })
359
+ }.to raise_error(Poefy::NotEnoughData)
360
+ end
361
+ end
362
+ end
363
+ end
364
+ end
365
+
366
+ describe "make sonnets" do
367
+ sonnet_options = [
368
+ { rhyme: 'ababcdcdefefgg' },
369
+ { rhyme: 'abab cdcd efef gg', indent: '0101 0101 0011 01' },
370
+ { form: 'sonnet' },
371
+ { form: :sonnet, syllable: 0 },
372
+ { form: :sonnet, syllable: 10 },
373
+ { form: :sonnet, regex: /^[A-Z].*$/ },
374
+ { form: :sonnet, regex: '^[A-Z].*$' },
375
+ { form: :sonnet, acrostic: 'pauldpthompson' },
376
+ { form: 'sonnet', indent: '01010101001101' },
377
+ { form: 'sonnet', proper: false }
378
+ ]
379
+ sonnet_options.each do |option|
380
+ it "#{option}" do
381
+ 4.times do
382
+ poem = @poefy.poem(option)
383
+ expect(poem).to_not be_nil
384
+ end
385
+ end
386
+ end
387
+ end
388
+ end
389
+
390
+ ##############################################################################
391
+
392
+ describe "using dataset 'spec_whitman'" do
393
+
394
+ file_txt = "whitman_leaves.txt"
395
+ file_db = "spec_whitman.db"
396
+
397
+ # There's a good mix of syllable count, so all forms should pass.
398
+ forms = Poefy::PoeticForms::POETIC_FORMS
399
+ forms_pass = forms.keys
400
+
401
+ before(:each) do
402
+ @poefy = Poefy::Poem.new(file_db, { proper: false })
403
+ end
404
+ after(:each) do
405
+ @poefy.close
406
+ end
407
+
408
+ it "initialised object not nil" do
409
+ expect(@poefy).to_not be_nil
410
+ end
411
+
412
+ describe "#make_database( '#{@root}/data/#{file_txt}', true )" do
413
+ it "should make the database '#{@root}/data/#{file_db}" do
414
+ db_file = "#{@root}/data/#{file_db}"
415
+ input = `sed '/[a-z]/!d' #{@root}/data/#{file_txt}`
416
+ @poefy.make_database input
417
+ expect(@poefy.corpus.exist?).to be true
418
+ expect(File.exist?(db_file)).to be true
419
+ end
420
+ end
421
+
422
+ describe "using form string" do
423
+ describe "should return correct number of lines" do
424
+
425
+ # Make sure each form's lines match the expected output.
426
+ # Generate a few to be sure.
427
+ forms_pass.each do |form|
428
+ it "({ form: #{form} })" do
429
+ 10.times do
430
+ poem = @poefy.poem ({ form: form })
431
+ expect(poem.count).to satisfy do |c|
432
+ [*forms[form][:rhyme]].map do |r|
433
+ r.gsub(/[0-9]/,'').length
434
+ end.include?(c)
435
+ end
436
+ end
437
+ end
438
+ end
439
+ end
440
+ end
441
+
442
+ describe "make sonnets" do
443
+ sonnet_options = [
444
+ { rhyme: 'ababcdcdefefgg' },
445
+ { rhyme: 'abab cdcd efef gg', indent: '0101 0101 0011 01' },
446
+ { form: 'sonnet' },
447
+ { form: :sonnet, syllable: 0 },
448
+ { form: :sonnet, syllable: 10 },
449
+ { form: :sonnet, regex: /^[A-Z].*$/ },
450
+ { form: :sonnet, regex: '^[A-Z].*$' },
451
+ { form: :sonnet, acrostic: 'pauldpthompson' },
452
+ { form: 'sonnet', indent: '01010101001101' },
453
+ { form: 'sonnet', proper: false }
454
+ ]
455
+ sonnet_options.each do |option|
456
+ it "#{option}" do
457
+ 4.times do
458
+ poem = @poefy.poem(option)
459
+ expect(poem).to_not be_nil
460
+ end
461
+ end
462
+ end
463
+ end
464
+
465
+ describe "using syllable string" do
466
+
467
+ it "({ rhyme: 'abcb defe', syllable: '[8,6,8,6,0,8,6,8,6]' })" do
468
+ options = {
469
+ rhyme: 'abcb defe',
470
+ syllable: '[8,6,8,6,0,8,6,8,6]'
471
+ }
472
+ poem = @poefy.poem (options)
473
+ expect(poem.count).to be options[:rhyme].length
474
+ end
475
+
476
+ it "({ rhyme: 'abcb defe', syllable: '[8,6,8,6,8,6,8,6]' })" do
477
+ options = {
478
+ rhyme: 'abcb defe',
479
+ syllable: '[8,6,8,6,8,6,8,6]'
480
+ }
481
+ poem = @poefy.poem (options)
482
+ expect(poem.count).to be options[:rhyme].length
483
+ end
484
+ end
485
+ end
486
+
487
+ ##############################################################################
488
+
489
+ describe "reusing the same Poem instance" do
490
+ it "should correctly merge the option hashes" do
491
+
492
+ # Default to use rondeau poetic form, and proper sentence validation
493
+ poefy = Poefy::Poem.new(
494
+ 'spec_shakespeare',
495
+ { form: 'rondeau', proper: true }
496
+ )
497
+
498
+ # Generate a properly sentenced rondeau
499
+ poem = poefy.poem
500
+ expect(poem.count).to be 17
501
+
502
+ # Generate a rondeau without proper validation
503
+ poem = poefy.poem ({ proper: false })
504
+ expect(poem.count).to be 17
505
+
506
+ # Generate a proper rondeau with a certain indentation
507
+ poem = poefy.poem ({ indent: '01012 0012 010112' })
508
+ expect(poem.count).to be 17
509
+
510
+ # Generate other forms
511
+ poem = poefy.poem ({ rhyme: 'abbaabbacdecde' })
512
+ expect(poem.count).to be 14
513
+ poem = poefy.poem ({ form: 'sonnet' })
514
+ expect(poem.count).to be 14
515
+ poem = poefy.poem ({ form: 'ballade' })
516
+ expect(poem.count).to be 31
517
+
518
+ # Generate a default rondeau again
519
+ poem = poefy.poem
520
+ expect(poem.count).to be 17
521
+
522
+ poefy.close
523
+ end
524
+ end
525
+
526
+ ##############################################################################
527
+
528
+ describe "using the transform option" do
529
+
530
+ it "should correctly transform the output 1" do
531
+ poefy = Poefy::Poem.new :spec_shakespeare
532
+ transform_hash = {
533
+ 4 => proc { |line, num, poem| line.upcase },
534
+ 12 => proc { |line, num, poem| line.upcase }
535
+ }
536
+ poem = poefy.poem({ form: :sonnet, transform: transform_hash })
537
+ expect(poem.count).to be 14
538
+ expect(poem[3]).to eq poem[3].upcase
539
+ expect(poem[11]).to eq poem[11].upcase
540
+ poefy.close
541
+ end
542
+
543
+ it "should correctly transform the output 2" do
544
+ poefy = Poefy::Poem.new :spec_shakespeare
545
+ transform_hash = {
546
+ 4 => proc { |line, num, poem| poem.count },
547
+ -3 => proc { |line, num, poem| poem.count },
548
+ 7 => proc { |line, num, poem| 'test string' }
549
+ }
550
+ poem = poefy.poem({ form: :sonnet, transform: transform_hash })
551
+ expect(poem.count).to be 14
552
+ expect(poem[3]).to eq '14'
553
+ expect(poem[11]).to eq '14'
554
+ expect(poem[6]).to eq 'test string'
555
+ poefy.close
556
+ end
557
+
558
+ it "should correctly transform the output 3" do
559
+ poefy = Poefy::Poem.new :spec_shakespeare
560
+ transform_proc = proc { |line, num, poem| line.downcase }
561
+ poem = poefy.poem({ form: :sonnet, transform: transform_proc })
562
+ expect(poem.count).to be 14
563
+ poem.each do |i|
564
+ expect(i).to eq i.downcase
565
+ end
566
+ poefy.close
567
+ end
568
+
569
+ it "should correctly transform the output 4" do
570
+ poefy = Poefy::Poem.new :spec_shakespeare
571
+ transform_proc = proc { |line, num, poem| "#{num} #{line.downcase}" }
572
+ poem = poefy.poem({ form: :sonnet, transform: transform_proc })
573
+ expect(poem.count).to be 14
574
+ poem.each.with_index do |line, index|
575
+ expect(line).to eq line.downcase
576
+ first_word = line.split(' ').first
577
+ expect(first_word).to eq (index + 1).to_s
578
+ end
579
+ poefy.close
580
+ end
581
+ end
582
+
583
+ ##############################################################################
584
+
585
+ describe "using the form_from_text option" do
586
+ before(:all) do
587
+ @text = <<-TEXT
588
+ [Chorus 1]
589
+ Oh yeah, I'll tell you something
590
+ I think you'll understand
591
+ When I'll say that something
592
+ I want to hold your hand
593
+ I want to hold your hand
594
+ I want to hold your hand
595
+
596
+ [Verse 1]
597
+ Oh please, say to me
598
+ You'll let me be your man
599
+ And please, say to me
600
+ You'll let me hold your hand
601
+ I'll let me hold your hand
602
+ I want to hold your hand
603
+ TEXT
604
+ @line_count = @text.split("\n").count
605
+ end
606
+
607
+ it "should use the exact poetic form 1" do
608
+ poefy = Poefy::Poem.new(:spec_whitman, {
609
+ form_from_text: @text
610
+ })
611
+ poem = poefy.poem
612
+ poem.map!(&:strip!)
613
+ expect(poem.count).to be @line_count
614
+ expect(poem[0]).to eq "[Chorus 1]"
615
+ expect(poem[8]).to eq "[Verse 1]"
616
+ expect(poem[5]).to eq poem[4]
617
+ expect(poem[6]).to eq poem[4]
618
+ poefy.close
619
+ end
620
+
621
+ it "should use the exact poetic form 2" do
622
+ poefy = Poefy::Poem.new :spec_whitman
623
+ poem = poefy.poem({
624
+ form_from_text: @text
625
+ })
626
+ poem.map!(&:strip!)
627
+ expect(poem.count).to be @line_count
628
+ expect(poem[0]).to eq "[Chorus 1]"
629
+ expect(poem[8]).to eq "[Verse 1]"
630
+ expect(poem[5]).to eq poem[4]
631
+ expect(poem[6]).to eq poem[4]
632
+ poefy.close
633
+ end
634
+
635
+ it "should correctly modify the poetic form 1" do
636
+ poefy = Poefy::Poem.new(:spec_whitman, {
637
+ form_from_text: @text,
638
+ syllable: 6
639
+ })
640
+ poem = poefy.poem
641
+ poem.map!(&:strip!)
642
+ expect(poem.count).to be @line_count
643
+ expect(poem[0]).to eq "[Chorus 1]"
644
+ expect(poem[8]).to eq "[Verse 1]"
645
+ expect(poem[5]).to eq poem[4]
646
+ expect(poem[6]).to eq poem[4]
647
+ poefy.close
648
+ end
649
+
650
+ it "should correctly modify the poetic form 2" do
651
+ poefy = Poefy::Poem.new :spec_whitman
652
+ poem = poefy.poem({
653
+ form_from_text: @text,
654
+ syllable: 6
655
+ })
656
+ poem.map!(&:strip!)
657
+ expect(poem.count).to be @line_count
658
+ expect(poem[0]).to eq "[Chorus 1]"
659
+ expect(poem[8]).to eq "[Verse 1]"
660
+ expect(poem[5]).to eq poem[4]
661
+ expect(poem[6]).to eq poem[4]
662
+ poefy.close
663
+ end
664
+
665
+ it "should correctly modify the poetic form 3" do
666
+ poefy = Poefy::Poem.new(:spec_whitman, {
667
+ form_from_text: @text
668
+ })
669
+ poem = poefy.poem({
670
+ syllable: 6
671
+ })
672
+ poem.map!(&:strip!)
673
+ expect(poem.count).to be @line_count
674
+ expect(poem[0]).to eq "[Chorus 1]"
675
+ expect(poem[8]).to eq "[Verse 1]"
676
+ expect(poem[5]).to eq poem[4]
677
+ expect(poem[6]).to eq poem[4]
678
+ poefy.close
679
+ end
680
+
681
+ it "should correctly replace the poetic form" do
682
+ poefy = Poefy::Poem.new(:spec_whitman, {
683
+ syllable: 6
684
+ })
685
+ poem = poefy.poem({
686
+ form_from_text: @text
687
+ })
688
+ poem.map!(&:strip!)
689
+ expect(poem.count).to be @line_count
690
+ expect(poem[0]).to eq "[Chorus 1]"
691
+ expect(poem[8]).to eq "[Verse 1]"
692
+ expect(poem[5]).to eq poem[4]
693
+ expect(poem[6]).to eq poem[4]
694
+ poefy.close
695
+ end
696
+
697
+ end
698
+
699
+ end
700
+
701
+ ################################################################################