]> git.wincent.com - wikitext.git/blob - spec/link_encoding_spec.rb
e1344fe7a5a44d7bf64e9700b5af0577409d9716
[wikitext.git] / spec / link_encoding_spec.rb
1 #!/usr/bin/env ruby
2 # encoding: utf-8
3 # Copyright 2007-2009 Wincent Colaiuta. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
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.
13
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.
25
26 require File.join(File.dirname(__FILE__), 'spec_helper.rb')
27 require 'wikitext'
28 require 'uri'
29
30 describe Wikitext, 'encoding a link target' do
31   it 'should complain if passed nil' do
32     lambda { Wikitext::Parser.encode_link_target(nil) }.should raise_error
33   end
34
35   it 'should do nothing on zero-length input' do
36     Wikitext::Parser.encode_link_target('').should == ''
37   end
38
39   it 'should convert embedded spaces into "%20"' do
40     Wikitext::Parser.encode_link_target('hello world').should == 'hello%20world'
41   end
42
43   it 'should eat leading spaces' do
44     Wikitext::Parser.encode_link_target(' hello world').should == 'hello%20world'
45     Wikitext::Parser.encode_link_target('  hello world').should == 'hello%20world'
46     Wikitext::Parser.encode_link_target('   hello world').should == 'hello%20world'
47     Wikitext::Parser.encode_link_target('    hello world').should == 'hello%20world'
48     Wikitext::Parser.encode_link_target('     hello world').should == 'hello%20world'
49     Wikitext::Parser.encode_link_target('      hello world').should == 'hello%20world'
50   end
51
52   it 'should eat trailing spaces' do
53     Wikitext::Parser.encode_link_target('hello world ').should == 'hello%20world'
54     Wikitext::Parser.encode_link_target('hello world  ').should == 'hello%20world'
55     Wikitext::Parser.encode_link_target('hello world   ').should == 'hello%20world'
56     Wikitext::Parser.encode_link_target('hello world    ').should == 'hello%20world'
57     Wikitext::Parser.encode_link_target('hello world     ').should == 'hello%20world'
58     Wikitext::Parser.encode_link_target('hello world      ').should == 'hello%20world'
59   end
60
61   it 'should eat leading and trailing spaces combined' do
62     Wikitext::Parser.encode_link_target('     hello world ').should == 'hello%20world'
63     Wikitext::Parser.encode_link_target('     hello world ').should == 'hello%20world'
64     Wikitext::Parser.encode_link_target('    hello world  ').should == 'hello%20world'
65     Wikitext::Parser.encode_link_target('   hello world   ').should == 'hello%20world'
66     Wikitext::Parser.encode_link_target('  hello world    ').should == 'hello%20world'
67     Wikitext::Parser.encode_link_target(' hello world     ').should == 'hello%20world'
68   end
69
70   it 'should return nothing for input consisting entirely of spaces' do
71     Wikitext::Parser.encode_link_target(' ').should == ''
72     Wikitext::Parser.encode_link_target('  ').should == ''
73     Wikitext::Parser.encode_link_target('   ').should == ''
74     Wikitext::Parser.encode_link_target('    ').should == ''
75     Wikitext::Parser.encode_link_target('     ').should == ''
76     Wikitext::Parser.encode_link_target('      ').should == ''
77   end
78
79   it 'should convert reserved symbols into percent escapes' do
80     Wikitext::Parser.encode_link_target('http://www.apple.com/q?foo').should == 'http%3a%2f%2fwww.apple.com%2fq%3ffoo'
81   end
82
83   it 'should convert non-ASCII into UTF-8 and then apply percent escapes' do
84     Wikitext::Parser.encode_link_target('cañon').should == 'ca%c3%b1on'
85   end
86
87   it 'should handle mixed scenarios (commas, double-quotes and UTF-8)' do
88     Wikitext::Parser.encode_link_target('foo, "bar" & baz €').should == 'foo%2c%20%22bar%22%20%26%20baz%20%e2%82%ac'
89   end
90
91   it 'should get the same answer as URI.escape' do
92     reserved = Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
93     ['foo bar', 'http://www.google.com/search?q=hello&foo=bar', '€'].each do |string|
94       Wikitext::Parser.encode_link_target(string).should == URI.escape(string, reserved).downcase
95     end
96   end
97
98   # "special" links don't get transformed in any way
99   describe 'special links' do
100
101     # as of version 1.4.0 the encode_link_target function no longer handles special links
102     it 'should (no longer) recognize links which match /\A[a-z]+\/\d+\z/ as being special' do
103       string = 'foo/10'
104       Wikitext::Parser.encode_special_link_target(string).should == 'foo%2f10'
105       Wikitext::Parser.encode_link_target(string).should == 'foo%2f10'
106     end
107
108     it "should not recognize links which don't match at /\A/ as being special" do
109       string = '  foo/10'
110       Wikitext::Parser.encode_special_link_target(string).should_not == string
111       string = '..foo/10'
112       Wikitext::Parser.encode_special_link_target(string).should_not == string
113       string = '12foo/10'
114       Wikitext::Parser.encode_special_link_target(string).should_not == string
115     end
116
117     it "should not recognize links which don't match at /\z/ as being special" do
118       string = 'foo/10  '
119       Wikitext::Parser.encode_special_link_target(string).should_not == string
120       string = 'foo/10__'
121       Wikitext::Parser.encode_special_link_target(string).should_not == string
122       string = 'foo/10##'
123       Wikitext::Parser.encode_special_link_target(string).should_not == string
124       string = 'foo/10ab'
125       Wikitext::Parser.encode_special_link_target(string).should_not == string
126     end
127
128     it "should not recognize links which don't match at /[a-z]/ (case differences) as being special" do
129       string = 'FOO/10'
130       Wikitext::Parser.encode_special_link_target(string).should_not == string
131     end
132
133     it "should not recognize links which don't match at /[0-9]/ (case differences) as being special" do
134       string = 'foo/xx'
135       Wikitext::Parser.encode_special_link_target(string).should_not == string
136     end
137
138     it "should not recognize links which don't match at /\// as being special" do
139       string = 'foo 10'
140       Wikitext::Parser.encode_special_link_target(string).should_not == string
141     end
142   end
143 end
144