require File.expand_path(File.join(File.dirname(__FILE__),'test_helper'))
class WikiParser < WikiCloth::Parser
template do |template|
case template
when "noinclude"
"hello worldtesting"
when "test"
"busted"
when "nowiki"
"hello world"
when "testparams"
"{{{def|hello world}}} {{{1}}} {{{test}}} {{{nested|{{{2}}}}}}"
when "moreparamtest"
"{{{{{test|bla}}|wtf}}}"
when "loop"
"{{loop}}"
end
end
external_link do |url,text|
"#{text.blank? ? url : text}"
end
end
class WikiClothTest < ActiveSupport::TestCase
test "links and references" do
wiki = WikiCloth::Parser.new(:data => File.open(File.join(File.dirname(__FILE__), '../sample_documents/george_washington.wiki'), READ_MODE) { |f| f.read })
data = wiki.to_html
assert wiki.external_links.size == 38
assert wiki.references.size == 76
assert wiki.internal_links.size == 450
end
test "Embedded images with no explicit title" do
wiki = WikiParser.new(:data => "[[Image:Rectangular coordinates.svg|left|thumb|250px]]")
test = true
begin
data = wiki.to_html
rescue
test = false
end
assert test == true
end
test "First item in list not created when list is preceded by a heading" do
wiki = WikiParser.new(:data => "=Heading=\n* One\n* Two\n* Three")
data = wiki.to_html
assert data !~ /\*/
end
test "behavior switch should not show up in the html output" do
wiki = WikiParser.new(:data => "__NOTOC__hello world")
data = wiki.to_html
assert data !~ /TOC/
end
test "template vars should not be parsed inside a pre tag" do
wiki = WikiCloth::Parser.new(:data => "
{{{1}}}
")
data = wiki.to_html
assert data =~ /{{{1}}}/
end
test "[[ links ]] should not work inside pre tags" do
data = < @wiki = WikiParser.new({
:params => { "PAGENAME" => "Testing123" },
:data => "[[test]] {{hello|world}} From {{ PAGENAME }} -- [www.google.com]"
})
@wiki.to_html
EOS
wiki = WikiCloth::Parser.new(:data => data)
data = wiki.to_html
assert data !~ /href/
assert data !~ /\{/
assert data !~ /\]/
end
test "auto pre at end of document" do
wiki = WikiParser.new(:data => "test\n\n hello\n world\nend")
data = wiki.to_html
assert data =~ /hello/
assert data =~ /world/
wiki = WikiParser.new(:data => "test\n\n hello\n world")
data = wiki.to_html
assert data =~ /hello/
assert data =~ /world/
end
test "template params" do
wiki = WikiParser.new(:data => "{{testparams|test|test=bla|it worked|bla=whoo}}\n")
data = wiki.to_html
assert data =~ /hello world/
assert data =~ /test/
assert data =~ /bla/
assert data =~ /it worked/ # nested default param
wiki = WikiParser.new(:data => "{{moreparamtest|p=othervar}}")
data = wiki.to_html
assert data =~ /wtf/
wiki = WikiParser.new(:data => "{{moreparamtest|p=othervar|busted=whoo}}")
data = wiki.to_html
assert data =~ /whoo/
end
test "horizontal rule" do
wiki = WikiParser.new(:data => "----\n")
data = wiki.to_html
assert data =~ /hr/
end
test "template loops" do
wiki = WikiParser.new(:data => "{{#iferror:{{loop}}|loop detected|wtf}}")
data = wiki.to_html
assert data =~ /loop detected/
end
test "input with no newline" do
wiki = WikiParser.new(:data => "{{test}}")
data = wiki.to_html
assert data =~ /busted/
end
test "lists" do
wiki = WikiParser.new(:data => "* item 1\n* item 2\n* item 3\n")
data = wiki.to_html
assert data =~ /ul/
count = 0
# should == 6.. 3 's and 3 's
data.gsub(/li/) { |ret|
count += 1
ret
}
assert count == 6
end
test "noinclude and includeonly tags" do
wiki = WikiParser.new(:data => "main pagenever seen{{noinclude}}\n")
data = wiki.to_html
assert data =~ /testing/
assert data =~ /main page/
assert !(data =~ /never seen/)
assert !(data =~ /hello world/)
end
test "bold/italics" do
wiki = WikiParser.new(:data => "test ''testing'' '''123''' '''''echo'''''\n")
data = wiki.to_html
assert data =~ /testing<\/i>/
assert data =~ /123<\/b>/
assert data =~ /echo<\/i><\/b>/
end
test "sanitize html" do
wiki = WikiParser.new(:data => "\ntest\n")
data = wiki.to_html
assert !(data =~ /