acts_as_markup_on 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,669 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ActsAsMarkupOnTest < ActsAsMarkupOnTestCase
4
+ def setup
5
+ setup_db
6
+ end
7
+
8
+ context 'acts_as_markup_on' do
9
+ setup do
10
+ ActsAsMarkupOn.markdown_library = :rdiscount
11
+
12
+ @textile_text = "h2. Textile Test Text"
13
+ class ::TextilePost < ActiveRecord::Base
14
+ acts_as_markup_on :language => :textile, :columns => [:body]
15
+ end
16
+ @textile_post = TextilePost.create!(:title => 'Blah', :body => @textile_text)
17
+
18
+ @markdown_text = '## Markdown Test Text'
19
+ class ::MarkdownPost < ActiveRecord::Base
20
+ acts_as_markup_on :language => :markdown, :columns => [:body]
21
+ end
22
+ @markdown_post = MarkdownPost.create!(:title => 'Blah', :body => @markdown_text)
23
+ end
24
+
25
+ should "have a markup object returned for the column value" do
26
+ assert_kind_of RedCloth::TextileDoc, @textile_post.body
27
+ assert_kind_of RDiscount, @markdown_post.body
28
+ end
29
+
30
+ should "return original markup text for a `to_s` method call on the column value" do
31
+ assert_equal @markdown_text, @markdown_post.body.to_s
32
+ assert_equal @textile_text, @textile_post.body.to_s
33
+ end
34
+
35
+ should "return formated html for a `to_html` method call on the column value" do
36
+ assert_match(/<h2(\s\w+\=['"]\w*['"])*\s*>\s*Markdown Test Text\s*<\/h2>/, @markdown_post.body.to_html)
37
+ assert_match(/<h2>Textile Test Text<\/h2>/, @textile_post.body.to_html)
38
+ end
39
+
40
+ context "changing value of markup field should return new markup object" do
41
+ setup do
42
+ @markdown_old_body = @markdown_post.body
43
+ @markdown_post.body = "`@count = 20`"
44
+ @textile_old_body = @textile_post.body
45
+ @textile_post.body = "@@count = 20@"
46
+ end
47
+
48
+ should "still have an markup object but not the same object" do
49
+ assert_kind_of RedCloth::TextileDoc, @textile_post.body
50
+ assert_not_same @markdown_post.body, @markdown_old_body
51
+ assert_kind_of RDiscount, @markdown_post.body
52
+ assert_not_same @textile_post.body, @textile_old_body
53
+ end
54
+
55
+ should "return correct text for `to_s`" do
56
+ assert_equal "`@count = 20`", @markdown_post.body.to_s
57
+ assert_equal "@@count = 20@", @textile_post.body.to_s
58
+ end
59
+
60
+ should "return correct HTML for the `to_html` method" do
61
+ assert_match(/<code>\s*\@count\s\=\s20\s*<\/code>/, @markdown_post.body.to_html)
62
+ assert_match(/<code>\@count\s\=\s20<\/code>/, @textile_post.body.to_html)
63
+ end
64
+
65
+ teardown do
66
+ @markdown_old_body, @textile_old_body = nil
67
+ end
68
+ end
69
+
70
+ teardown do
71
+ @textile_text, @textile_post, @markdown_text, @markdown_post = nil
72
+ TextilePost.delete_all
73
+ MarkdownPost.delete_all
74
+ end
75
+ end
76
+
77
+ context 'acts_as_markup_on with variable language' do
78
+ setup do
79
+ ActsAsMarkupOn.markdown_library = :rdiscount
80
+ ActsAsMarkupOn.mediawiki_library = :wikicloth
81
+ class ::VariablePost < ActiveRecord::Base
82
+ acts_as_markup_on :language => :variable, :columns => [:body]
83
+ end
84
+ end
85
+
86
+ context "with a Markdown post" do
87
+ setup do
88
+ @markdown_text = '## Markdown Test Text'
89
+ @markdown_post = VariablePost.create!(:title => 'Blah', :body => @markdown_text, :markup_language => 'Markdown')
90
+ end
91
+
92
+ should "have a markup object returned for the column value" do
93
+ assert_kind_of RDiscount, @markdown_post.body
94
+ end
95
+
96
+ should "return original markup text for a `to_s` method call on the column value" do
97
+ assert_equal @markdown_text, @markdown_post.body.to_s
98
+ end
99
+
100
+ should "return formated html for a `to_html` method call on the column value" do
101
+ assert_match(/<h2(\s\w+\=['"]\w*['"])*\s*>\s*Markdown Test Text\s*<\/h2>/, @markdown_post.body.to_html)
102
+ end
103
+
104
+ context "changing value of markup field should return new markup object" do
105
+ setup do
106
+ @markdown_old_body = @markdown_post.body
107
+ @markdown_post.body = "`@count = 20`"
108
+ end
109
+
110
+ should "still have an markup object but not the same object" do
111
+ assert_not_same @markdown_post.body, @markdown_old_body
112
+ assert_kind_of RDiscount, @markdown_post.body
113
+ end
114
+
115
+ should "return correct text for `to_s`" do
116
+ assert_equal "`@count = 20`", @markdown_post.body.to_s
117
+ end
118
+
119
+ should "return correct HTML for the `to_html` method" do
120
+ assert_match(/<code>\s*\@count\s\=\s20\s*<\/code>/, @markdown_post.body.to_html)
121
+ end
122
+
123
+ teardown do
124
+ @markdown_old_body = nil
125
+ end
126
+ end
127
+
128
+ teardown do
129
+ @markdown_text, @markup_post = nil
130
+ end
131
+ end
132
+
133
+ context "with a Textile post" do
134
+ setup do
135
+ @textile_text = "h2. Textile Test Text"
136
+ @textile_post = VariablePost.create!(:title => 'Blah', :body => @textile_text, :markup_language => 'Textile')
137
+ end
138
+
139
+ should "have a markup object returned for the column value" do
140
+ assert_kind_of RedCloth::TextileDoc, @textile_post.body
141
+ end
142
+
143
+ should "return original markup text for a `to_s` method call on the column value" do
144
+ assert_equal @textile_text, @textile_post.body.to_s
145
+ end
146
+
147
+ should "return formated html for a `to_html` method call on the column value" do
148
+ assert_match(/<h2>Textile Test Text<\/h2>/, @textile_post.body.to_html)
149
+ end
150
+
151
+ context "changing value of markup field should return new markup object" do
152
+ setup do
153
+ @textile_old_body = @textile_post.body
154
+ @textile_post.body = "@@count = 20@"
155
+ end
156
+
157
+ should "still have an markup object but not the same object" do
158
+ assert_kind_of RedCloth::TextileDoc, @textile_post.body
159
+ assert_not_same @textile_post.body, @textile_old_body
160
+ end
161
+
162
+ should "return correct text for `to_s`" do
163
+ assert_equal "@@count = 20@", @textile_post.body.to_s
164
+ end
165
+
166
+ should "return correct HTML for the `to_html` method" do
167
+ assert_match(/<code>\@count\s\=\s20<\/code>/, @textile_post.body.to_html)
168
+ end
169
+
170
+ teardown do
171
+ @textile_old_body = nil
172
+ end
173
+ end
174
+
175
+ teardown do
176
+ @textile_text, @textile_post = nil
177
+ end
178
+ end
179
+
180
+ context 'with a Mediawiki post' do
181
+ setup do
182
+ @wikitext = "== Wikitext Test Text =="
183
+ @wikitext_post = VariablePost.create!(:title => 'Blah', :body => @wikitext, :markup_language => 'Mediawiki')
184
+ end
185
+
186
+ should "have a WikitextString object returned for the column value" do
187
+ assert_kind_of WikiClothText, @wikitext_post.body
188
+ end
189
+
190
+ should "return original wikitext text for a `to_s` method call on the column value" do
191
+ assert_equal @wikitext, @wikitext_post.body.to_s
192
+ end
193
+
194
+ should "return formated html for a `to_html` method call on the column value" do
195
+ assert_match(/<h2>.*Wikitext Test Text.*<\/h2>/, @wikitext_post.body.to_html)
196
+ end
197
+
198
+ context "changing value of wikitext field should return new wikitext object" do
199
+ setup do
200
+ @old_body = @wikitext_post.body
201
+ @wikitext_post.body = "'''This is very important'''"
202
+ end
203
+
204
+ should "still have an WikitextString object but not the same object" do
205
+ assert_kind_of WikiClothText, @wikitext_post.body
206
+ assert_not_same @wikitext_post.body, @old_body
207
+ end
208
+
209
+ should "return correct text for `to_s`" do
210
+ assert_equal "'''This is very important'''", @wikitext_post.body.to_s
211
+ end
212
+
213
+ should "return correct HTML for the `to_html` method" do
214
+ assert_match(/<p><b>This is very important<\/b><\/p>/, @wikitext_post.body.to_html)
215
+ end
216
+
217
+ teardown do
218
+ @old_body = nil
219
+ end
220
+ end
221
+
222
+ teardown do
223
+ @wikitext, @wikitext_post = nil
224
+ Post.delete_all
225
+ end
226
+ end
227
+
228
+ context 'with a RDoc post' do
229
+ setup do
230
+ @rdoctext = "== RDoc Test Text"
231
+ @rdoc_post = VariablePost.create!(:title => 'Blah', :body => @rdoctext, :markup_language => 'RDoc')
232
+ end
233
+
234
+ should "have a RDocText object returned for the column value" do
235
+ assert_kind_of RDocText, @rdoc_post.body
236
+ end
237
+
238
+ should "return original RDoc text for a `to_s` method call on the column value" do
239
+ assert_equal @rdoctext, @rdoc_post.body.to_s
240
+ end
241
+
242
+ should "return formated html for a `to_html` method call on the column value" do
243
+ assert_match(/<h2[^>]*>\s*RDoc Test Text\s*<\/h2>/, @rdoc_post.body.to_html)
244
+ end
245
+
246
+ context "changing value of RDoc field should return new RDoc object" do
247
+ setup do
248
+ @old_body = @rdoc_post.body
249
+ @rdoc_post.body = "http://www.example.com/"
250
+ end
251
+
252
+ should "still have an RDocText object but not the same object" do
253
+ assert_kind_of RDocText, @rdoc_post.body
254
+ assert_not_same @rdoc_post.body, @old_body
255
+ end
256
+
257
+ should "return correct text for `to_s`" do
258
+ assert_equal "http://www.example.com/", @rdoc_post.body.to_s
259
+ end
260
+
261
+ should "return correct HTML for the `to_html` method" do
262
+ assert_match(/<a href="http:\/\/www.example.com">www.example.com<\/a>/, @rdoc_post.body.to_html)
263
+ end
264
+
265
+ teardown do
266
+ @old_body = nil
267
+ end
268
+ end
269
+
270
+ teardown do
271
+ @rdoctext, @rdoc_post = nil
272
+ Post.delete_all
273
+ end
274
+ end
275
+
276
+ context "with a plain text post" do
277
+ setup do
278
+ @plain_text = "Hahaha!!!"
279
+ @plain_text_post = VariablePost.create!(:title => 'Blah', :body => @plain_text, :markup_language => 'text')
280
+ end
281
+
282
+ should "have a string object returned for the column value" do
283
+ assert_kind_of String, @plain_text_post.body
284
+ end
285
+
286
+ should "return the original string with a `to_s` method call on the column value" do
287
+ assert_equal @plain_text, @plain_text_post.body.to_s
288
+ end
289
+
290
+ # FIXME: why is this failing??? both objects are String, both have EXACTLY the same value when output
291
+ # in failure message. assert_equal does not require same object. This is very odd!
292
+ should "return the original string with a `to_html` method call on the column value" do
293
+ assert_equal @plain_text, @plain_text_post.body.to_html
294
+ end
295
+
296
+ context "changing value of markup field should return new markup object" do
297
+ setup do
298
+ @plaintext_old_body = @plain_text_post.body
299
+ @plain_text_post.body = "Lorem ipsum dolor sit amet"
300
+ end
301
+
302
+ should "still have an markup object but not the same object" do
303
+ assert_kind_of String, @plain_text_post.body
304
+ assert_not_same @plain_text_post.body, @plaintext_old_body
305
+ end
306
+
307
+ should "return correct text for `to_s`" do
308
+ assert_equal "Lorem ipsum dolor sit amet", @plain_text_post.body.to_s
309
+ end
310
+
311
+ teardown do
312
+ @textile_old_body = nil
313
+ end
314
+ end
315
+
316
+ teardown do
317
+ @textile_text, @textile_post = nil
318
+ end
319
+ end
320
+
321
+
322
+ teardown do
323
+ VariablePost.delete_all
324
+ end
325
+ end
326
+
327
+ context 'acts_as_markup_on with variable language setting the language column' do
328
+ setup do
329
+ ActsAsMarkupOn.markdown_library = :rdiscount
330
+ ActsAsMarkupOn.mediawiki_library = :wikicloth
331
+ class ::VariableLanguagePost < ActiveRecord::Base
332
+ acts_as_markup_on :language => :variable, :columns => [:body], :language_column => :language_name
333
+ end
334
+ end
335
+
336
+ should "use the correct language column" do
337
+ markdown_text = '## Markdown Test Text'
338
+ markdown_post = VariableLanguagePost.create!(:title => 'Blah', :body => markdown_text, :language_name => 'Markdown')
339
+
340
+ assert_kind_of RDiscount, markdown_post.body
341
+ assert_equal markdown_text, markdown_post.body.to_s
342
+ assert_match(/<h2(\s\w+\=['"]\w*['"])*\s*>\s*Markdown Test Text\s*<\/h2>/, markdown_post.body.to_html)
343
+ end
344
+
345
+ teardown do
346
+ VariableLanguagePost.delete_all
347
+ end
348
+ end
349
+
350
+ context 'with a nil value for the text' do
351
+ setup do
352
+ @text = nil
353
+ end
354
+
355
+ context 'with textile' do
356
+ setup do
357
+ class ::Post < ActiveRecord::Base
358
+ acts_as_textile_on :body
359
+ end
360
+ @post = Post.create!(:title => 'Blah', :body => @text)
361
+ end
362
+
363
+ should 'return a blank string for `to_s` method' do
364
+ assert_equal @post.body.to_s, ''
365
+ end
366
+
367
+ should 'return true for .blank?' do
368
+ assert @post.body.blank?
369
+ end
370
+
371
+ should 'return a blank string for `to_html` method' do
372
+ assert_match(/[\n\s]*/, @post.body.to_html)
373
+ end
374
+
375
+ should "have a RedCloth object returned for the column value" do
376
+ assert_kind_of RedCloth::TextileDoc, @post.body
377
+ end
378
+
379
+ teardown do
380
+ @post = nil
381
+ Post.delete_all
382
+ end
383
+ end
384
+
385
+ context 'with WikiCloth mediawiki' do
386
+ setup do
387
+ ActsAsMarkupOn.mediawiki_library = :wikicloth
388
+ class ::Post < ActiveRecord::Base
389
+ acts_as_mediawiki_on :body
390
+ end
391
+ @post = Post.create!(:title => 'Blah', :body => @text)
392
+ end
393
+
394
+ should 'return a blank string for `to_s` method' do
395
+ assert_equal @post.body.to_s, ''
396
+ end
397
+
398
+ should 'return true for .blank?' do
399
+ assert @post.body.blank?
400
+ end
401
+
402
+ should 'return a blank string for `to_html` method' do
403
+ assert_match(/[\n\s]*/, @post.body.to_html)
404
+ end
405
+
406
+ should "have a WikitextString object returned for the column value" do
407
+ assert_kind_of WikiClothText, @post.body
408
+ end
409
+
410
+ teardown do
411
+ @post = nil
412
+ Post.delete_all
413
+ end
414
+ end
415
+
416
+ context 'with Wikitext mediawiki' do
417
+ setup do
418
+ ActsAsMarkupOn.mediawiki_library = :wikitext
419
+ class ::Post < ActiveRecord::Base
420
+ acts_as_mediawiki_on :body
421
+ end
422
+ @post = Post.create!(:title => 'Blah', :body => @text)
423
+ end
424
+
425
+ should 'return a blank string for `to_s` method' do
426
+ assert_equal @post.body.to_s, ''
427
+ end
428
+
429
+ should 'return true for .blank?' do
430
+ assert @post.body.blank?
431
+ end
432
+
433
+ should 'return a blank string for `to_html` method' do
434
+ assert_match(/[\n\s]*/, @post.body.to_html)
435
+ end
436
+
437
+ should "have a WikitextString object returned for the column value" do
438
+ assert_kind_of WikitextString, @post.body
439
+ end
440
+
441
+ teardown do
442
+ @post = nil
443
+ Post.delete_all
444
+ end
445
+ end
446
+
447
+ context 'with RDoc' do
448
+ setup do
449
+ class ::Post < ActiveRecord::Base
450
+ acts_as_rdoc_on :body
451
+ end
452
+ @post = Post.create!(:title => 'Blah', :body => @text)
453
+ end
454
+
455
+ should 'return a blank string for `to_s` method' do
456
+ assert_equal @post.body.to_s, ''
457
+ end
458
+
459
+ should 'return true for .blank?' do
460
+ assert @post.body.blank?
461
+ end
462
+
463
+ should 'return a blank string for `to_html` method' do
464
+ assert_match(/[\n\s]*/, @post.body.to_html)
465
+ end
466
+
467
+ should "have a RDocText object returned for the column value" do
468
+ assert_kind_of RDocText, @post.body
469
+ end
470
+
471
+ teardown do
472
+ @post = nil
473
+ Post.delete_all
474
+ end
475
+ end
476
+
477
+ context 'with RDiscount Markdown' do
478
+ setup do
479
+ ActsAsMarkupOn.markdown_library = :rdiscount
480
+ class ::Post < ActiveRecord::Base
481
+ acts_as_markdown_on :body
482
+ end
483
+ @post = Post.create!(:title => 'Blah', :body => @text)
484
+ end
485
+
486
+ should 'return a blank string for `to_s` method' do
487
+ assert_equal @post.body.to_s, ''
488
+ end
489
+
490
+ should 'return true for .blank?' do
491
+ assert @post.body.blank?
492
+ end
493
+
494
+ should 'return a blank string for `to_html` method' do
495
+ assert_match(/[\n\s]*/, @post.body.to_html)
496
+ end
497
+
498
+ should "have a RDiscount object returned for the column value" do
499
+ assert_kind_of RDiscount, @post.body
500
+ end
501
+
502
+ teardown do
503
+ @post = nil
504
+ Post.delete_all
505
+ end
506
+ end
507
+
508
+ context 'with BlueCloth Markdown' do
509
+ setup do
510
+ ActsAsMarkupOn.markdown_library = :bluecloth
511
+ class ::Post < ActiveRecord::Base
512
+ acts_as_markdown_on :body
513
+ end
514
+ @post = Post.create!(:title => 'Blah', :body => @text)
515
+ end
516
+
517
+ should 'return a blank string for `to_s` method' do
518
+ assert_equal '', @post.body.to_s
519
+ end
520
+
521
+ should 'return true for .blank?' do
522
+ assert @post.body.blank?
523
+ end
524
+
525
+ should 'return a blank string for `to_html` method' do
526
+ assert_match(/[\n\s]*/, @post.body.to_html)
527
+ end
528
+
529
+ should "have a BlueCloth object returned for the column value" do
530
+ assert_kind_of BlueCloth, @post.body
531
+ end
532
+
533
+ teardown do
534
+ @post = nil
535
+ Post.delete_all
536
+ end
537
+ end
538
+
539
+ context 'with Ruby PEG Markdown' do
540
+ setup do
541
+ ActsAsMarkupOn.markdown_library = :rpeg
542
+ class ::Post < ActiveRecord::Base
543
+ acts_as_markdown_on :body
544
+ end
545
+ @post = Post.create!(:title => 'Blah', :body => @text)
546
+ end
547
+
548
+ should 'return a blank string for `to_s` method' do
549
+ assert_equal @post.body.to_s, ''
550
+ end
551
+
552
+ should 'return true for .blank?' do
553
+ assert @post.body.blank?
554
+ end
555
+
556
+ should 'return a blank string for `to_html` method' do
557
+ assert_match(/[\n\s]*/, @post.body.to_html)
558
+ end
559
+
560
+ should "have a PEGMarkdown object returned for the column value" do
561
+ assert_kind_of PEGMarkdown, @post.body
562
+ end
563
+
564
+ teardown do
565
+ @post = nil
566
+ Post.delete_all
567
+ end
568
+ end
569
+
570
+ context 'with Maruku Markdown' do
571
+ setup do
572
+ ActsAsMarkupOn.markdown_library = :maruku
573
+ class ::Post < ActiveRecord::Base
574
+ acts_as_markdown_on :body
575
+ end
576
+ @post = Post.create!(:title => 'Blah', :body => @text)
577
+ end
578
+
579
+ should 'return a blank string for `to_s` method' do
580
+ assert_equal @post.body.to_s, ''
581
+ end
582
+
583
+ should 'return true for .blank?' do
584
+ assert @post.body.blank?
585
+ end
586
+
587
+ should 'return a blank string for `to_html` method' do
588
+ assert_match(/[\n\s]*/, @post.body.to_html)
589
+ end
590
+
591
+ should "have a Maruku object returned for the column value" do
592
+ assert_kind_of Maruku, @post.body
593
+ end
594
+
595
+ teardown do
596
+ @post = nil
597
+ Post.delete_all
598
+ end
599
+ end
600
+
601
+ context 'with Redcarpet Markdown' do
602
+ setup do
603
+ ActsAsMarkupOn.markdown_library = :redcarpet
604
+ class ::Post < ActiveRecord::Base
605
+ acts_as_markdown_on :body
606
+ end
607
+ @post = Post.create!(:title => 'Blah', :body => @text)
608
+ end
609
+
610
+ should 'return a blank string for `to_s` method' do
611
+ assert_equal @post.body.to_s, ''
612
+ end
613
+
614
+ should 'return true for .blank?' do
615
+ assert @post.body.blank?
616
+ end
617
+
618
+ should 'return a blank string for `to_html` method' do
619
+ assert_match(/[\n\s]*/, @post.body.to_html)
620
+ end
621
+
622
+ should "have a Maruku object returned for the column value" do
623
+ assert_kind_of Redcarpet, @post.body
624
+ end
625
+
626
+ teardown do
627
+ @post = nil
628
+ Post.delete_all
629
+ end
630
+ end
631
+ end
632
+
633
+ context 'acts_as_markup_on with bad language name' do
634
+ should 'raise exception when a non-supported language is passed to acts_as_markup_on' do
635
+ assert_raise ActsAsMarkupOn::UnsupportedMarkupLanguage do
636
+ class ::Post < ActiveRecord::Base
637
+ acts_as_markup_on :language => :fake, :columns => [:body]
638
+ end
639
+ end
640
+ end
641
+ end
642
+
643
+ context 'acts_as_markup_on with bad markdown library' do
644
+ should 'raise exception when a non-supported library is set as the markdown library attribute on ActsAsMarkupOn' do
645
+ assert_raise ActsAsMarkupOn::UnsportedMarkdownLibrary do
646
+ ActsAsMarkupOn.markdown_library = :fake
647
+ class ::Post < ActiveRecord::Base
648
+ acts_as_markup_on :language => :markdown, :columns => [:body]
649
+ end
650
+ end
651
+ end
652
+ end
653
+
654
+ context 'acts_as_markup_on with bad mediawiki library' do
655
+ should 'raise exception when a non-supported library is set as the mediawiki library attribute on ActsAsMarkupOn' do
656
+ assert_raise ActsAsMarkupOn::UnsportedMediawikiLibrary do
657
+ ActsAsMarkupOn.mediawiki_library = :fake
658
+ class ::Post < ActiveRecord::Base
659
+ acts_as_markup_on :language => :mediawiki, :columns => [:body]
660
+ end
661
+ end
662
+ end
663
+ end
664
+
665
+ def teardown
666
+ teardown_db
667
+ end
668
+ end
669
+