Skip to content

Commit e1b0aef

Browse files
committed
Add regression tests for <code>#method</code> preservation
When `<code>#method</code>` (or `<tt>#method</tt>`) appears in markup and the method does not resolve to a known method, the inline code formatting and `#` prefix must be preserved verbatim. caad3b9 fixed the underlying behavior by returning nil from `link` and falling back to the original text in `cross_reference`, but the bug from #1684 had no direct test coverage for the `<code>#unresolved</code>` form that the issue reported. These tests lock in the fix using the exact wording from `doc/syntax/calling_methods.rdoc` that triggered the bug, plus the `<tt>` alias, methods ending in `?`/`!`, and a mixed case where an unresolved `<code>#nope</code>` sits next to a resolvable class reference.
1 parent bc0baee commit e1b0aef

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

test/rdoc/markup/to_html_crossref_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,40 @@ def test_cross_reference_preserves_explicit_text_for_unresolved
435435
assert_equal "Foo", @to.cross_reference("Missing", "Foo")
436436
end
437437

438+
# Regression test for https://github.com/ruby/rdoc/issues/1684
439+
# `<code>#method</code>` should keep its inline code formatting and `#`
440+
# prefix when `#method` does not resolve to a known method.
441+
def test_handle_TT_unresolved_hash_method_preserves_code
442+
@to = RDoc::Markup::ToHtmlCrossref.new 'index.html', @top_level,
443+
hyperlink_all: false, warn_missing_rdoc_ref: true
444+
445+
# The exact wording from doc/syntax/calling_methods.rdoc that triggered the bug
446+
result = @to.convert 'If the object responds to a <code>#to_hash</code> method'
447+
assert_equal para('If the object responds to a <code>#to_hash</code> method'), result
448+
449+
# The `<tt>` form is the legacy alias for `<code>` and must behave identically
450+
result = @to.convert 'If the object responds to a <tt>#to_hash</tt> method'
451+
assert_equal para('If the object responds to a <code>#to_hash</code> method'), result
452+
453+
# Methods ending with `?` and `!` are also unresolved here and must be preserved
454+
result = @to.convert '<code>#empty?</code> and <code>#freeze!</code>'
455+
assert_equal para('<code>#empty?</code> and <code>#freeze!</code>'), result
456+
end
457+
458+
def test_handle_TT_unresolved_hash_method_preserves_code_with_hyperlink_all
459+
# Same scenario but with --hyperlink-all enabled (the default for @to)
460+
result = @to.convert 'If the object responds to a <code>#to_hash</code> method'
461+
assert_equal para('If the object responds to a <code>#to_hash</code> method'), result
462+
end
463+
464+
def test_handle_TT_unresolved_hash_method_alongside_resolved_class
465+
# Mixes the bug scenario with a resolvable class reference (`C1`).
466+
# The unresolved `<code>#nope</code>` must stay verbatim while `C1`
467+
# still becomes an auto-link, mirroring the live `Hash` linking from the issue.
468+
result = @to.convert '<code>#nope</code> on C1'
469+
assert_equal para('<code>#nope</code> on <a href="C1.html"><code>C1</code></a>'), result
470+
end
471+
438472
private
439473

440474
def para(text)

0 commit comments

Comments
 (0)