3 # Copyright 2008-2009 Wincent Colaiuta. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
8 # 1. Redistributions of source code must retain the above copyright notice,
9 # this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright notice,
11 # this list of conditions and the following disclaimer in the documentation
12 # and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
18 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 # POSSIBILITY OF SUCH DAMAGE.
26 require File.join(File.dirname(__FILE__), 'spec_helper.rb')
29 # this is a general-purpose file in which I'll add specs for former bugs to make sure that they don't regress
30 describe Wikitext::Parser, 'regressions' do
32 @parser = Wikitext::Parser.new
35 # turns out that this was never a bug in wikitext -- it was a bug in the host application -- but keeping the test does no harm
36 it 'should correctly transform example #1' do
45 expected = dedent <<-END
51 <p>Y <a href="/wiki/otro_articulo">otro articulo</a>.</p>
53 @parser.parse(input).should == expected
56 # discovered at: http://rails.wincent.com/wiki/nginx_log_rotation
58 it 'should allow empty lines in PRE blocks marked up with a leading space' do
60 # -d turns on debug mode: output is verbose, no actual changes are made to the log files
61 sudo logrotate -d /etc/logrotate.d/nginx
63 # if the debug output looks good, proceed with a real rotation (-v turns on verbose output)
64 sudo logrotate -v /etc/logrotate.d/nginx
66 expected = dedent <<-END
67 <pre># -d turns on debug mode: output is verbose, no actual changes are made to the log files
68 sudo logrotate -d /etc/logrotate.d/nginx
70 # if the debug output looks good, proceed with a real rotation (-v turns on verbose output)
71 sudo logrotate -v /etc/logrotate.d/nginx</pre>
73 @parser.parse(input).should == expected
76 # discovered at: http://rails.wincent.com/wiki/Installing_Ragel_5.2.0_on_Mac_OS_X_Tiger
78 it 'should handle PRE_START blocks which follow unordered lists' do
80 * Get link to latest source code from: http://www.cs.queensu.ca/~thurston/ragel/
82 <pre>wget http://www.cs.queensu.ca/~thurston/ragel/ragel-5.20.tar.gz
83 tar xzvf ragel-5.20.tar.gz
86 expected = dedent <<-END
88 <li>Get link to latest source code from: <a href="http://www.cs.queensu.ca/~thurston/ragel/" class="external">http://www.cs.queensu.ca/~thurston/ragel/</a></li>
90 <pre>wget <a href="http://www.cs.queensu.ca/~thurston/ragel/ragel-5.20.tar.gz" class="external">http://www.cs.queensu.ca/~thurston/ragel/ragel-5.20.tar.gz</a>
91 tar xzvf ragel-5.20.tar.gz
94 @parser.parse(input).should == expected
97 # discovered at: http://rails.wincent.com/wiki/Movable_Type_security_notes
99 it 'should handle PRE_START blocks which follow ordered lists' do
100 input = dedent <<-END
101 # Turn off the [[Movable Type]] search function; use Google instead (it's better anyway) with a form something like this:
103 <pre><form method="get"...></pre>
105 expected = dedent <<-END
107 <li>Turn off the <a href="/wiki/Movable_Type">Movable Type</a> search function; use Google instead (it's better anyway) with a form something like this:</li>
109 <pre><form method="get"...></pre>
111 @parser.parse(input).should == expected
114 # discovered at: http://rails.wincent.com/wiki/Movable_Type_security_notes
116 it 'should respect additional indentation found inside PRE blocks' do
117 # note the two extra spaces on each line
118 input = dedent <<-END
119 <input type="text" name="q" size="20" maxlength="255" value="" />
120 <input type="hidden" name="hl" value="en" />
123 # problem is the spaces were being emitted _before_ the CRLF
124 expected = dedent <<-END
125 <pre> <input type="text" name="q" size="20" maxlength="255" value="" />
126 <input type="hidden" name="hl" value="en" /></pre>
128 @parser.parse(input).should == expected
131 # this is the general case of the bug covered in the previous spec
132 # any token that appears as the first token after a PRE token can manifest this bug
133 # PRINTABLE didn't only because it called _Wikitext_start_para_if_necessary(), which handled the pending CRLF
134 it 'should emit pending newlines for all token types found inside PRE and PRE_START blocks' do
136 input = dedent <<-END
140 expected = dedent <<-END
144 @parser.parse(input).should == expected
147 input = dedent <<-END
151 expected = dedent <<-END
153 </pre>bar</pre>
155 @parser.parse(input).should == expected
158 input = dedent <<-END
162 expected = dedent <<-END
164 <blockquote>bar</pre>
166 @parser.parse(input).should == expected
169 input = dedent <<-END
173 expected = dedent <<-END
175 </blockquote>bar</pre>
177 @parser.parse(input).should == expected
180 input = dedent <<-END
184 expected = dedent <<-END
186 <nowiki>bar</pre>
188 @parser.parse(input).should == expected
191 input = dedent <<-END
195 expected = dedent <<-END
199 @parser.parse(input).should == expected
202 input = dedent <<-END
206 expected = dedent <<-END
210 @parser.parse(input).should == expected
213 input = dedent <<-END
217 expected = dedent <<-END
219 <strong>bar</pre>
221 @parser.parse(input).should == expected
224 input = dedent <<-END
228 expected = dedent <<-END
230 </strong>bar</pre>
232 @parser.parse(input).should == expected
235 input = dedent <<-END
239 expected = dedent <<-END
243 @parser.parse(input).should == expected
246 input = dedent <<-END
250 expected = dedent <<-END
254 @parser.parse(input).should == expected
257 input = dedent <<-END
261 expected = dedent <<-END
265 @parser.parse(input).should == expected
268 input = dedent <<-END
272 expected = dedent <<-END
276 @parser.parse(input).should == expected
279 input = dedent <<-END
283 expected = dedent <<-END
287 @parser.parse(input).should == expected
290 input = dedent <<-END
294 expected = dedent <<-END
298 @parser.parse(input).should == expected
301 input = dedent <<-END
306 expected = dedent <<-END
311 @parser.parse(input).should == expected
314 input = dedent <<-END
319 expected = dedent <<-END
324 @parser.parse(input).should == expected
327 input = dedent <<-END
332 expected = dedent <<-END
337 @parser.parse(input).should == expected
340 input = dedent <<-END
345 expected = dedent <<-END
350 @parser.parse(input).should == expected
353 input = dedent <<-END
358 expected = dedent <<-END
363 @parser.parse(input).should == expected
366 input = dedent <<-END
371 expected = dedent <<-END
376 @parser.parse(input).should == expected
379 input = dedent <<-END
383 expected = dedent <<-END
387 @parser.parse(input).should == expected
390 input = dedent <<-END
394 expected = dedent <<-END
398 @parser.parse(input).should == expected
401 input = dedent <<-END
405 expected = dedent <<-END
409 @parser.parse(input).should == expected
412 input = dedent <<-END
416 expected = dedent <<-END
420 @parser.parse(input).should == expected
423 input = dedent <<-END
427 expected = dedent <<-END
431 @parser.parse(input).should == expected
434 input = dedent <<-END
438 expected = dedent <<-END
442 @parser.parse(input).should == expected
444 # these tokens weren't affected by the bug, seeing as they either call _Wikitext_start_para_if_necessary()
445 # or they can only appear in PRE_START (not PRE) thanks to the tokenizer
446 # but we add specs for them to make sure that the issue never crops up for them in the future
449 input = dedent <<-END
453 expected = dedent <<-END
457 @parser.parse(input).should == expected
459 # BLOCKQUOTE (in PRE_START)
460 input = dedent <<-END
464 expected = dedent <<-END
468 @parser.parse(input).should == expected
471 input = dedent <<-END
475 expected = dedent <<-END
479 @parser.parse(input).should == expected
482 input = dedent <<-END
486 expected = dedent <<-END
490 @parser.parse(input).should == expected
492 # H6_START (in PRE_START)
493 input = dedent <<-END
498 expected = dedent <<-END
503 @parser.parse(input).should == expected
505 # H5_START (in PRE_START)
506 input = dedent <<-END
511 expected = dedent <<-END
516 @parser.parse(input).should == expected
518 # H4_START (in PRE_START)
519 input = dedent <<-END
524 expected = dedent <<-END
529 @parser.parse(input).should == expected
531 # H3_START (in PRE_START)
532 input = dedent <<-END
537 expected = dedent <<-END
542 @parser.parse(input).should == expected
544 # H2_START (in PRE_START)
545 input = dedent <<-END
550 expected = dedent <<-END
555 @parser.parse(input).should == expected
557 # H1_START (in PRE_START)
558 input = dedent <<-END
563 expected = dedent <<-END
568 @parser.parse(input).should == expected
571 input = dedent <<-END
575 expected = dedent <<-END
577 </nowiki>bar</pre>
579 @parser.parse(input).should == expected
582 input = dedent <<-END
586 expected = dedent <<-END
590 @parser.parse(input).should == expected
593 input = dedent <<-END
597 expected = dedent <<-END
601 @parser.parse(input).should == expected
604 input = dedent <<-END
608 expected = dedent <<-END
612 @parser.parse(input).should == expected
615 input = dedent <<-END
619 expected = dedent <<-END
623 @parser.parse(input).should == expected
626 input = dedent <<-END
630 expected = dedent <<-END
634 @parser.parse(input).should == expected
637 input = dedent <<-END
641 expected = dedent <<-END
645 @parser.parse(input).should == expected
648 input = dedent <<-END
652 expected = dedent <<-END
656 @parser.parse(input).should == expected
659 input = dedent <<-END
663 expected = dedent <<-END
667 @parser.parse(input).should == expected
670 input = dedent <<-END
674 expected = dedent <<-END
678 @parser.parse(input).should == expected
681 input = dedent <<-END
685 expected = dedent <<-END
689 @parser.parse(input).should == expected
692 input = dedent <<-END
696 expected = dedent <<-END
698 <a href="http://example.com/" class="external">http://example.com/</a></pre>
700 @parser.parse(input).should == expected
703 input = dedent <<-END
707 expected = dedent <<-END
711 @parser.parse(input).should == expected
714 input = dedent <<-END
718 expected = dedent <<-END
722 @parser.parse(input).should == expected
725 input = dedent <<-END
729 expected = dedent <<-END
733 @parser.parse(input).should == expected
736 input = dedent <<-END
740 expected = dedent <<-END
744 @parser.parse(input).should == expected
747 input = dedent <<-END
751 expected = dedent <<-END
755 @parser.parse(input).should == expected
758 # discovered at: http://rails.wincent.com/wiki/Testing_cookies_in_Rails
759 it 'should handle BLOCKQUOTE_START blocks which follow lists' do
760 # example text taken from wiki article and edited for brevity
761 input = dedent <<-END
763 <blockquote>The cookies</blockquote>
765 expected = dedent <<-END
767 <li>This article</li>
773 @parser.parse(input).should == expected
776 # https://wincent.com/issues/818
777 it 'should handle BLOCKQUOTE_START blocks which follow BLOCKQUOTE shorthand' do
778 input = dedent <<-END
780 <blockquote>bar</blockquote>
782 expected = dedent <<-END
790 @parser.parse(input).should == expected
793 # https://wincent.com/issues/818
794 it 'should handle PRE_START blocks which follow BLOCKQUOTE shorthand' do
795 input = dedent <<-END
799 expected = dedent <<-END
805 @parser.parse(input).should == expected
808 # https://wincent.com/issues/818
809 it 'should handle BLOCKQUOTE_START blocks which follow nested BLOCKQUOTE shorthand' do
810 input = dedent <<-END
812 <blockquote>bar</blockquote>
814 expected = dedent <<-END
826 @parser.parse(input).should == expected
829 # https://wincent.com/issues/818
830 it 'should handle PRE_START blocks which follow nested BLOCKQUOTE shorthand' do
831 input = dedent <<-END
835 expected = dedent <<-END
845 @parser.parse(input).should == expected
848 # https://wincent.com/issues/1289
849 it 'should handle empty (zero-width) link targets' do
850 # these were badly broken (caused exceptions to be raised)
851 @parser.parse('[[]]').should == "<p>[[]]</p>\n"
852 @parser.parse('[[|]]').should == "<p>[[|]]</p>\n"
853 @parser.parse('[[|foo]]').should == "<p>[[|foo]]</p>\n"
855 # was working, but check here anyway to guard against regressions
856 @parser.parse('[[foo|]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
859 it 'should handle empty (whitespace only) link targets' do
860 # no exception raised, but clearly not desirable behaviour
861 # formerly these all returned: <p><a href="/wiki/"></a></p>\n
862 @parser.parse('[[ ]]').should == "<p>[[ ]]</p>\n"
863 @parser.parse('[[ ]]').should == "<p>[[ ]]</p>\n"
864 @parser.parse('[[ |]]').should == "<p>[[ |]]</p>\n"
866 # and this one returned: <p><a href="/wiki/">foo</a></p>\n
867 @parser.parse('[[ |foo]]').should == "<p>[[ |foo]]</p>\n"