require "spec_helper"
describe StringMaster do
it "closes unclosed tags" do
parser = StringMaster.new("Hello,world")
parser.close_tags.string.should == 'Hello,world'
end
it "escapes html except for some of the allowed tags" do
parser = StringMaster.new('Hello')
parser.html_escape(:except => %w(img)).string.should == '<b>Hello</b>'
parser = StringMaster.new('hello world')
parser.html_escape(:except => %w(b)).to_s.should == '<i>hello</i> world'
parser = StringMaster.new('hello world')
parser.html_escape(:except => %w(a)).to_s.should == 'hello <b>world</b>'
parser = StringMaster.new('hello world')
parser.html_escape(:except => %w(a)).to_s.should == 'hello <b>world</b>'
parser = StringMaster.new('hello world
')
parser.html_escape(:except => %w(a br)).to_s.should == 'hello <b>world</b>
'
parser = StringMaster.new('xsstest')
parser.html_escape.to_s.should == 'xsstest<input/onfocus=prompt(document.cookie) autofocus>'
parser = StringMaster.new('xsstest')
parser.html_escape.to_s.should == 'xsstest<input/onfocus=prompt(document.cookie)autofocus>'
end
it "makes images of urls that end with .jpg and other image extensions" do
parser = StringMaster.new('Hello, this is my photo http://image.com/image.jpg, yeah baby')
parser.urls_to_images(:wrap_with => ['
', '
'], :html_options => 'class="ico"').string.should == 'Hello, this is my photoyeah baby' # use https parser = StringMaster.new('Hello, this is my photo https://image.com/image.jpg, yeah baby') parser.urls_to_images(:wrap_with => ['
', '
'], :html_options => 'class="ico"').string.should == 'Hello, this is my photoyeah baby' end it "makes links of urls" do # example 1 parser = StringMaster.new('Hello, this is my homepage http://url.com, yeah baby') parser.urls_to_links.string.should == 'Hello, this is my homepage http://url.com, yeah baby' # example 2 parser = StringMaster.new("http://localhost:3000/\nhttp://localhost:3000/") parser.urls_to_links.string.should == "http://localhost:3000/\nhttp://localhost:3000/" # example 3 parser = StringMaster.new('http://gyazo.com/a4c16e7a6009f40f29248ad4fed41bd3.png
tags" do
code = <def say_hello
puts "hello world"
return true
end
and here's what my code looks like.
WRAPPED_CODE
end
it "wraps code in and adds a closing tag even if regexp for closing tag doesn't match" do
code = <def say_hello
puts "hello world"
return true
end
WRAPPED_CODE
end
it "wraps inline code into tags" do
code = "I have a variable called `a` and it has a `nil` value"
parser = StringMaster.new(code)
parser.wrap_inline_code.to_s.should == "I have a variable called a and it has a nil value"
end
it "wraps code in backticks stretched across multiple lines" do
code = "`hello\nworld`"
parser = StringMaster.new(code)
parser.wrap_backticks_code.to_s.should == "hello\nworld
"
end
it "breaks long words" do
long_string = 'l'; 1.upto(80) { |i| long_string << "o" }; long_string << "ng"
parser = StringMaster.new(long_string)
# fix extra space at the end of the line
parser.break_long_words.string.should == "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo oooooong"
end
it "handles 'a' tags when attempt to break long words" do
long_string = 'loooo oong'
end
it "handles 'img' tags when attempt to break long words" do
long_string = ''
parser = StringMaster.new(long_string)
parser.break_long_words(5).string.should == ''
end
it "cuts too long string and appends (if specified) characters to its end" do
parser = StringMaster.new("This is quite a long text")
parser.cut(11, 7, :append => '...').string.should == "This is..."
end
it "allows to use block notation" do
parser = StringMaster.new('http://images.com/image.jpg Hello http://url.com ') do |p|
p.urls_to_images.urls_to_links(:html_options => 'target="_blank"')
end
parser.string.should ==
" Hello http://url.com "
parser = StringMaster.new('https://images.com/image.jpg Hello https://url.com ') do |p|
p.urls_to_images.urls_to_links(:html_options => 'target="_blank"')
end
parser.string.should ==
" Hello https://url.com "
end
it "replaces newline characters with
tags" do
parser = StringMaster.new("This is quite a\n long text")
parser.wrap_code.newlines_to_br.to_s.should == "This is quite a
long text"
end
end