Skip to content

Commit

Permalink
Fix bug in TkUnknownChar that would crash #markup_code
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Nov 23, 2010
1 parent 7edf86c commit dcbfee5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
1 change: 1 addition & 0 deletions History.txt
Expand Up @@ -68,6 +68,7 @@
* __send__ and friends no longer get their underscores removed. Patch by
Thierry Lambert.
* The C parser now makes new public when promoting initialize.
* Fix crash in #markup_code for TkUnknownChar.

=== 2.5.11 / 2010-08-20

Expand Down
32 changes: 16 additions & 16 deletions lib/rdoc/generator/markup.rb
Expand Up @@ -58,6 +58,19 @@ def cvs_url(url, full_path)

class RDoc::AnyMethod

STYLE_MAP = {
RDoc::RubyToken::TkCONSTANT => "ruby-constant",
RDoc::RubyToken::TkKW => "ruby-keyword",
RDoc::RubyToken::TkIVAR => "ruby-ivar",
RDoc::RubyToken::TkOp => "ruby-operator",
RDoc::RubyToken::TkId => "ruby-identifier",
RDoc::RubyToken::TkNode => "ruby-node",
RDoc::RubyToken::TkCOMMENT => "ruby-comment",
RDoc::RubyToken::TkREGEXP => "ruby-regexp",
RDoc::RubyToken::TkSTRING => "ruby-string",
RDoc::RubyToken::TkVal => "ruby-value",
}

include RDoc::Generator::Markup

@add_line_numbers = false
Expand Down Expand Up @@ -109,25 +122,12 @@ def markup_code

@token_stream.each do |t|
next unless t
# style = STYLE_MAP[t.class]
style = case t
when RDoc::RubyToken::TkCONSTANT then "ruby-constant"
when RDoc::RubyToken::TkKW then "ruby-keyword"
when RDoc::RubyToken::TkIVAR then "ruby-ivar"
when RDoc::RubyToken::TkOp then "ruby-operator"
when RDoc::RubyToken::TkId then "ruby-identifier"
when RDoc::RubyToken::TkNode then "ruby-node"
when RDoc::RubyToken::TkCOMMENT then "ruby-comment"
when RDoc::RubyToken::TkREGEXP then "ruby-regexp"
when RDoc::RubyToken::TkSTRING then "ruby-string"
when RDoc::RubyToken::TkVal then "ruby-value"
else
nil
end

style = STYLE_MAP[t.class]

text = CGI.escapeHTML t.text

if style
if style then
src << "<span class=\"#{style}\">#{text}</span>"
else
src << text
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/ruby_token.rb
Expand Up @@ -178,7 +178,7 @@ def text
end

class TkUnknownChar < Token
def initialize(seek, line_no, char_no, id)
def initialize(seek, line_no, char_no, name)
super(seek, line_no, char_no)
@name = name
end
Expand Down
41 changes: 41 additions & 0 deletions test/test_rdoc_any_method.rb
@@ -1,4 +1,6 @@
require File.expand_path '../xref_test_case', __FILE__
require 'rdoc/code_objects'
require 'rdoc/generator/markup'

class RDocAnyMethodTest < XrefTestCase

Expand Down Expand Up @@ -36,6 +38,45 @@ def test_full_name
assert_equal 'C1::m', @c1.method_list.first.full_name
end

def test_markup_code
tokens = [
RDoc::RubyToken::TkCONSTANT. new(0, 0, 0, 'CONSTANT'),
RDoc::RubyToken::TkKW. new(0, 0, 0, 'KW'),
RDoc::RubyToken::TkIVAR. new(0, 0, 0, 'IVAR'),
RDoc::RubyToken::TkOp. new(0, 0, 0, 'Op'),
RDoc::RubyToken::TkId. new(0, 0, 0, 'Id'),
RDoc::RubyToken::TkNode. new(0, 0, 0, 'Node'),
RDoc::RubyToken::TkCOMMENT. new(0, 0, 0, 'COMMENT'),
RDoc::RubyToken::TkREGEXP. new(0, 0, 0, 'REGEXP'),
RDoc::RubyToken::TkSTRING. new(0, 0, 0, 'STRING'),
RDoc::RubyToken::TkVal. new(0, 0, 0, 'Val'),
RDoc::RubyToken::TkBACKSLASH.new(0, 0, 0, '\\'),
]

@c2_a.collect_tokens
@c2_a.add_tokens *tokens

expected = [
'<span class="ruby-constant">CONSTANT</span>',
'<span class="ruby-keyword">KW</span>',
'<span class="ruby-ivar">IVAR</span>',
'<span class="ruby-operator">Op</span>',
'<span class="ruby-identifier">Id</span>',
'<span class="ruby-node">Node</span>',
'<span class="ruby-comment">COMMENT</span>',
'<span class="ruby-regexp">REGEXP</span>',
'<span class="ruby-string">STRING</span>',
'<span class="ruby-value">Val</span>',
'\\'
].join

assert_equal expected, @c2_a.markup_code
end

def test_markup_code_empty
assert_equal '', @c2_a.markup_code
end

def test_marshal_load
instance_method = Marshal.load Marshal.dump(@c1.method_list.last)

Expand Down

0 comments on commit dcbfee5

Please sign in to comment.