Skip to content

Commit

Permalink
Update tests for html_syntax_highlight and minor refactor
Browse files Browse the repository at this point in the history
Backports #478, #479, referencing #467

Conflicts:

	lib/yard/templates/helpers/html_helper.rb
  • Loading branch information
lsegal committed Jan 31, 2012
1 parent 6464b38 commit 0407f51
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 38 deletions.
14 changes: 9 additions & 5 deletions lib/yard/templates/helpers/html_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ def htmlify(text, markup = options[:markup])
end
html = resolve_links(html)
html = html.gsub(/<pre\s*(?:lang="(.+?)")?>(?:\s*<code\s*(?:class="(.+?)")?\s*>)?(.+?)(?:<\/code>\s*)?<\/pre>/m) do
string = $3
string = $3
# handle !!!LANG prefix to send to html_syntax_highlight_LANG
language, string = parse_lang_for_codeblock(string)
language ||= $1 || $2 || object.source_type || :ruby
language, _ = parse_lang_for_codeblock(string)
language ||= $1 || $2 || object.source_type

string = html_syntax_highlight(CGI.unescapeHTML(string), language) unless options[:no_highlight]
unless options[:no_highlight]
string = html_syntax_highlight(CGI.unescapeHTML(string), language)
end
classes = ['code', language].compact.join(' ')
%Q{<pre class="#{classes}"><code>#{string}</code></pre>}
end unless [:text, :none, :pre].include?(markup)
Expand Down Expand Up @@ -155,13 +157,15 @@ def htmlify_line(*args)
# +html_syntax_highlight_TYPE+ in this class.
#
# @param [String] source the source code to highlight
# @param [Symbol] type the language type (:ruby, :plain, etc). Use
# @param [Symbol, String] type the language type (:ruby, :plain, etc). Use
# :plain for no syntax highlighting.
# @return [String] the highlighted source
def html_syntax_highlight(source, type = nil)
return "" unless source
return h(source) if options[:no_highlight]

new_type, source = parse_lang_for_codeblock(source)
type ||= new_type || :ruby
meth = "html_syntax_highlight_#{type}"
respond_to?(meth) ? send(meth, source) : h(source)
end
Expand Down
77 changes: 44 additions & 33 deletions spec/templates/helpers/html_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,76 +471,87 @@ def foo; end
end

describe '#html_syntax_highlight' do
before do
stub!(:options).and_return(:no_highlight => false)
stub!(:object).and_return(Registry.root)
end

def fix_rspec2_mock_teardown
return unless defined?(RSpec)
should_receive(:respond_to?).with(:teardown_mocks_for_rspec).and_return(false)
should_receive(:respond_to?).with(:verify_mocks_for_rspec).and_return(false)
subject do
obj = OpenStruct.new
obj.options = {:no_highlight => false}
obj.object = Registry.root
obj.extend(Templates::Helpers::HtmlHelper)
obj
end

it "should return empty string on nil input" do
html_syntax_highlight(nil).should == ''
subject.html_syntax_highlight(nil).should == ''
end

it "should call #html_syntax_highlight_ruby by default" do
Registry.root.source_type = nil
should_receive(:html_syntax_highlight_ruby).with('def x; end')
html_syntax_highlight('def x; end')
subject.should_receive(:html_syntax_highlight_ruby).with('def x; end')
subject.html_syntax_highlight('def x; end')
end

it "should call #html_syntax_highlight_NAME if there's an object with a #source_type" do
Registry.root.source_type = :NAME
fix_rspec2_mock_teardown
should_receive(:respond_to?).with('html_syntax_highlight_NAME').and_return(true)
should_receive(:html_syntax_highlight_NAME).and_return("foobar")
html_syntax_highlight('def x; end').should == 'foobar'
subject.object = OpenStruct.new(:source_type => :NAME)
subject.should_receive(:respond_to?).with('html_markup_html').and_return(true)
subject.should_receive(:respond_to?).with('html_syntax_highlight_NAME').and_return(true)
subject.should_receive(:html_syntax_highlight_NAME).and_return("foobar")
subject.htmlify('<pre><code>def x; end</code></pre>', :html).should ==
'<pre class="code NAME"><code>foobar</code></pre>'
end

it "should add !!!LANG to className in outputted pre tag" do
subject.object = OpenStruct.new(:source_type => :LANG)
subject.should_receive(:respond_to?).with('html_markup_html').and_return(true)
subject.should_receive(:respond_to?).with('html_syntax_highlight_LANG').and_return(true)
subject.should_receive(:html_syntax_highlight_LANG).and_return("foobar")
subject.htmlify("<pre><code>!!!LANG\ndef x; end</code></pre>", :html).should ==
'<pre class="code LANG"><code>foobar</code></pre>'
end

it "should call html_syntax_highlight_NAME if source starts with !!!NAME" do
fix_rspec2_mock_teardown
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
subject.should_receive(:respond_to?).with('html_syntax_highlight_NAME').and_return(true)
subject.should_receive(:html_syntax_highlight_NAME).and_return("foobar")
subject.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'
subject.options[:no_highlight] = true
subject.should_not_receive(:html_syntax_highlight_ruby)
subject.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
fix_rspec2_mock_teardown
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"
subject.should_receive(:respond_to?).with('html_syntax_highlight_NAME').and_return(false)
subject.should_not_receive(:html_syntax_highlight_NAME)
subject.html_syntax_highlight("!!!NAME\ndef x; end").should == "def x; end"
end

it "should highlight as ruby if htmlify(text, :ruby) is called" do
should_receive(:html_syntax_highlight_ruby).with('def x; end').and_return('x')
htmlify('def x; end', :ruby).should == '<pre class="code ruby">x</pre>'
subject.should_receive(:html_syntax_highlight_ruby).with('def x; end').and_return('x')
subject.htmlify('def x; end', :ruby).should == '<pre class="code ruby">x</pre>'
end

it "should not prioritize object source type when called directly" do
subject.should_receive(:html_syntax_highlight_ruby).with('def x; end').and_return('x')
subject.object = OpenStruct.new(:source_type => :c)
subject.html_syntax_highlight("def x; end").should == "x"
end

it "shouldn't escape code snippets twice" do
htmlify('<pre lang="foo"><code>{"foo" => 1}</code></pre>', :html).should ==
subject.htmlify('<pre lang="foo"><code>{"foo" => 1}</code></pre>', :html).should ==
'<pre class="code foo"><code>{&quot;foo&quot; =&gt; 1}</code></pre>'
end

it "should highlight source when matching a pre lang= tag" do
htmlify('<pre lang="foo"><code>x = 1</code></pre>', :html).should ==
subject.htmlify('<pre lang="foo"><code>x = 1</code></pre>', :html).should ==
'<pre class="code foo"><code>x = 1</code></pre>'
end

it "should highlight source when matching a code class= tag" do
htmlify('<pre><code class="foo">x = 1</code></pre>', :html).should ==
subject.htmlify('<pre><code class="foo">x = 1</code></pre>', :html).should ==
'<pre class="code foo"><code>x = 1</code></pre>'
end
end
Expand Down

0 comments on commit 0407f51

Please sign in to comment.