3 # Copyright 2007-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')
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
35 it 'should do nothing on zero-length input' do
36 Wikitext::Parser.encode_link_target('').should == ''
39 it 'should convert embedded spaces into "%20"' do
40 Wikitext::Parser.encode_link_target('hello world').should == 'hello%20world'
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'
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'
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'
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 == ''
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'
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'
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'
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
98 # "special" links don't get transformed in any way
99 describe 'special links' do
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
104 Wikitext::Parser.encode_special_link_target(string).should == 'foo%2f10'
105 Wikitext::Parser.encode_link_target(string).should == 'foo%2f10'
108 it "should not recognize links which don't match at /\A/ as being special" do
110 Wikitext::Parser.encode_special_link_target(string).should_not == string
112 Wikitext::Parser.encode_special_link_target(string).should_not == string
114 Wikitext::Parser.encode_special_link_target(string).should_not == string
117 it "should not recognize links which don't match at /\z/ as being special" do
119 Wikitext::Parser.encode_special_link_target(string).should_not == string
121 Wikitext::Parser.encode_special_link_target(string).should_not == string
123 Wikitext::Parser.encode_special_link_target(string).should_not == string
125 Wikitext::Parser.encode_special_link_target(string).should_not == string
128 it "should not recognize links which don't match at /[a-z]/ (case differences) as being special" do
130 Wikitext::Parser.encode_special_link_target(string).should_not == string
133 it "should not recognize links which don't match at /[0-9]/ (case differences) as being special" do
135 Wikitext::Parser.encode_special_link_target(string).should_not == string
138 it "should not recognize links which don't match at /\// as being special" do
140 Wikitext::Parser.encode_special_link_target(string).should_not == string