require File.dirname(__FILE__) + '/../../spec_helper' require File.dirname(__FILE__) + "/shared_signature_examples" describe YARD::Templates::Helpers::HtmlHelper do include YARD::Templates::Helpers::HtmlHelper include YARD::Templates::Helpers::MethodHelper describe '#h' do it "should use #h to escape HTML" do h('Usage: foo "bar" ').should == "Usage: foo "bar" <baz>" end end describe '#fix_typewriter' do it "should use #fix_typewriter to convert +text+ to text" do fix_typewriter("Some +typewriter text+.").should == "Some t\x04y\x04p\x04e\x04w\x04r\x04i\x04t\x04e\x04r\x04" + " \x04t\x04e\x04x\x04t\x04." fix_typewriter("Not +typewriter text.").should == "Not +typewriter text." fix_typewriter("Alternating +type writer+ text +here+.").should == "Alternating t\x04y\x04p\x04e\x04 \x04w\x04r\x04i\x04t\x04e\x04r" + "\x04 text h\x04e\x04r\x04e\x04." fix_typewriter("No ++problem.").should == "No ++problem." fix_typewriter("Math + stuff +is ok+").should == "Math + stuff i\x04s\x04 \x04o\x04k\x04" end end describe '#format_types' do it "should include brackets by default" do text = ["String"] should_receive(:linkify).at_least(1).times.with("String", "String").and_return("String") format_types(text).should == format_types(text, true) format_types(text).should == "(String)" end it "should avoid brackets if brackets=false" do should_receive(:linkify).with("String", "String").and_return("String") should_receive(:linkify).with("Symbol", "Symbol").and_return("Symbol") format_types(["String", "Symbol"], false).should == "String, Symbol" end { "String" => [["String"], "String"], "A::B::C" => [["A::B::C"], "A::B::C"], "Array" => [["Array", "String"], "Array<String>"], "Array" => [["Array", "String", "Symbol"], "Array<String, Symbol>"], "Array<{String => Array}>" => [["Array", "String", "Array", "Symbol"], "Array<{String => " + "Array<Symbol>}>"] }.each do |text, values| it "should link all classes in #{text}" do should_receive(:h).with('<').at_least(text.count('<')).times.and_return("<") should_receive(:h).with('>').at_least(text.count('>')).times.and_return(">") values[0].each {|v| should_receive(:linkify).with(v, v).and_return("#{v}") } format_types([text], false).should == values[1] end end end describe '#htmlify' do it "should not use hard breaks for textile markup (RedCloth specific)" do htmlify("A\nB", :textile).should_not include("(.+?)<\/a>/ params, results[:inner_text] = $1, $2 params.split(/\s+/).each do |match| key, value = *match.split('=') results[key.to_sym] = value.gsub(/^["'](.+)["']$/, '\1') end results end it "should link static files with file: prefix" do stub!(:serializer).and_return Serializers::FileSystemSerializer.new stub!(:object).and_return Registry.root parse_link(resolve_links("{file:TEST.txt#abc}")).should == { :inner_text => "TEST.txt", :title => "TEST.txt", :href => "file.TEST.html#abc" } parse_link(resolve_links("{file:TEST.txt title}")).should == { :inner_text => "title", :title => "title", :href => "file.TEST.html" } end it "should create regular links with http:// or https:// prefixes" do parse_link(resolve_links("{http://example.com}")).should == { :inner_text => "http://example.com", :target => "_parent", :href => "http://example.com", :title => "http://example.com" } parse_link(resolve_links("{http://example.com title}")).should == { :inner_text => "title", :target => "_parent", :href => "http://example.com", :title => "title" } end it "should create mailto links with mailto: prefixes" do parse_link(resolve_links('{mailto:joanna@example.com}')).should == { :inner_text => 'mailto:joanna@example.com', :target => '_parent', :href => 'mailto:joanna@example.com', :title => 'mailto:joanna@example.com' } parse_link(resolve_links('{mailto:steve@example.com Steve}')).should == { :inner_text => 'Steve', :target => '_parent', :href => 'mailto:steve@example.com', :title => 'Steve' } end end describe '#signature' do before do @results = { :regular => "- (Object) foo", :default_return => "- (Hello) foo", :no_default_return => "- foo", :private_class => "+ (Object) foo (private)", :single => "- (String) foo", :two_types => "- (String, Symbol) foo", :two_types_multitag => "- (String, Symbol) foo", :type_nil => "- (Type?) foo", :type_array => "- (Type+) foo", :multitype => "- (Type, ...) foo", :void => "- (void) foo", :hide_void => "- foo", :block => "- (Object) foo {|a, b, c| ... }" } end def format_types(types, brackets = false) types.join(", ") end def signature(obj) super(obj, false).strip end it_should_behave_like "signature" end describe '#html_syntax_highlight' do before do stub!(:options).and_return(:no_highlight => false) end it "should return empty string on nil input" do html_syntax_highlight(nil).should == '' end it "should call #html_syntax_highlight_ruby by default" do should_receive(:html_syntax_highlight_ruby).with('def x; end') html_syntax_highlight('def x; end') end it "should call html_syntax_highlight_NAME if source starts with !!!NAME" do should_receive(:respond_to?).with('html_syntax_highlight_NAME').and_return(true) should_receive(:html_syntax_highlight_NAME).and_return("foobar") html_syntax_highlight(<<-eof !!!NAME def x; end eof ).should == "foobar" end it "should not highlight if :no_highlight option is true" do stub!(:options).and_return(:no_highlight => true) should_not_receive(:html_syntax_highlight_ruby) html_syntax_highlight('def x; end').should == 'def x; end' end it "should not highlight if there is no highlight method specified by !!!NAME" do should_receive(:respond_to?).with('html_syntax_highlight_NAME').and_return(false) should_not_receive(:html_syntax_highlight_NAME) html_syntax_highlight("!!!NAME\ndef x; end").should == "def x; end" end end end