]> git.wincent.com - wikitext.git/blob - spec/link_encoding_spec.rb
Merge branch 'master' into ragel
[wikitext.git] / spec / link_encoding_spec.rb
1 #!/usr/bin/env ruby
2 # Copyright 2007-2008 Wincent Colaiuta
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 require File.join(File.dirname(__FILE__), 'spec_helper.rb')
17 require 'wikitext'
18 require 'uri'
19
20 describe Wikitext, 'encoding a link target' do
21   it 'should complain if passed nil' do
22     lambda { Wikitext.encode_link_target(nil) }.should raise_error
23   end
24
25   it 'should do nothing on zero-length input' do
26     Wikitext.encode_link_target('').should == ''
27   end
28
29   it 'should convert spaces into "%20"' do
30     Wikitext.encode_link_target('hello world').should == 'hello%20world'
31   end
32
33   it 'should convert reserved symbols into percent escapes' do
34     Wikitext.encode_link_target('http://www.apple.com/q?foo').should == 'http%3a%2f%2fwww.apple.com%2fq%3ffoo'
35   end
36
37   it 'should convert non-ASCII into UTF-8 and then apply percent escapes' do
38     Wikitext.encode_link_target('cañon').should == 'ca%c3%b1on'
39   end
40
41   it 'should handle mixed scenarios (commas, double-quotes and UTF-8)' do
42     Wikitext.encode_link_target('foo, "bar" & baz €').should == 'foo%2c%20%22bar%22%20%26%20baz%20%e2%82%ac'
43   end
44
45   it 'should get the same answer as URI.escape' do
46     reserved = Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
47     ['foo bar', 'http://www.google.com/search?q=hello&foo=bar', '€'].each do |string|
48       Wikitext.encode_link_target(string).should == URI.escape(string, reserved).downcase
49     end
50   end
51 end
52