9191 must_equal %(<a href="qux" title="Foo > Bar">Bar < Foo</a>)
9292 end
9393
94- it "returns the escaped text when URL argument is nil" do
95- _ ( @helpers . link_to ( "Bar < Foo" , nil , title : "Foo > Bar" ) ) .
96- must_equal %(Bar < Foo)
94+ it "uses the first argument as the URL when no URL is specified" do
95+ _ ( @helpers . link_to ( "foo/bar/qux.html" ) ) .
96+ must_equal %(<a href="foo/bar/qux.html">foo/bar/qux.html</a>)
97+
98+ _ ( @helpers . link_to ( "foo/bar/qux.html" , "data-hoge" : "fuga" ) ) .
99+ must_equal %(<a href="foo/bar/qux.html" data-hoge="fuga">foo/bar/qux.html</a>)
97100 end
98101
99- it "returns an appropriate link when URL argument is an RDoc::CodeObject that responds to #path " do
102+ it "uses #full_name when the text argument is an RDoc::CodeObject" do
100103 top_level = rdoc_top_level_for <<~RUBY
101104 module Foo; class Bar; def qux; end; end; end
102105 RUBY
103106
104- _ ( @helpers . link_to ( "perma" , top_level . find_module_named ( "Foo" ) ) ) .
105- must_equal %(<a href="/classes/Foo.html">perma</a>)
107+ [
108+ top_level ,
109+ top_level . find_module_named ( "Foo" ) ,
110+ top_level . find_module_named ( "Foo::Bar" ) ,
111+ top_level . find_module_named ( "Foo::Bar" ) . find_method ( "qux" , false ) ,
112+ ] . each do |code_object |
113+ _ ( @helpers . link_to ( code_object , "url" ) ) .
114+ must_equal %(<a href="url">#{ @helpers . full_name ( code_object ) } </a>)
115+ end
116+ end
117+
118+ it "uses RDoc::CodeObject#path as the URL when URL argument is an RDoc::CodeObject" do
119+ top_level = rdoc_top_level_for <<~RUBY
120+ module Foo; class Bar; def qux; end; end; end
121+ RUBY
122+
123+ [
124+ top_level ,
125+ top_level . find_module_named ( "Foo" ) ,
126+ top_level . find_module_named ( "Foo::Bar" ) ,
127+ top_level . find_module_named ( "Foo::Bar" ) . find_method ( "qux" , false ) ,
128+ ] . each do |code_object |
129+ _ ( @helpers . link_to ( "text" , code_object ) ) .
130+ must_equal %(<a href="/#{ code_object . path } ">text</a>)
131+ end
132+ end
133+ end
134+
135+ describe "#link_to_if" do
136+ it "returns the link's HTML when the condition is true" do
137+ args = [ "Bar < Foo" , "qux" , title : "Foo > Bar" ]
138+ _ ( @helpers . link_to_if ( true , *args ) ) . must_equal @helpers . link_to ( *args )
139+ end
140+
141+ it "returns the link's inner HTML when the condition is false" do
142+ _ ( @helpers . link_to_if ( false , "Bar < Foo" , "url" ) ) . must_equal ERB ::Util . h ( "Bar < Foo" )
106143
107- _ ( @helpers . link_to ( "perma" , top_level . find_module_named ( "Foo::Bar" ) ) ) .
108- must_equal %(<a href="/classes/Foo/Bar.html">perma</a>)
144+ rdoc_module = rdoc_top_level_for ( <<~RUBY ) . find_module_named ( "Foo::Bar" )
145+ module Foo; class Bar; end; end
146+ RUBY
109147
110- _ ( @helpers . link_to ( "perma" , top_level . find_module_named ( "Foo::Bar" ) . find_method ( "qux" , false ) ) ) .
111- must_equal %(<a href="/classes/Foo/Bar.html#method-i-qux">perma</a>)
148+ _ ( @helpers . link_to_if ( false , rdoc_module , "url" ) ) . must_equal @helpers . full_name ( rdoc_module )
112149 end
113150 end
114151
@@ -134,6 +171,30 @@ module Foo; class Bar; def qux; end; end; end
134171 end
135172 end
136173
174+ describe "#full_name" do
175+ it "inserts word-break opportunities into module names" do
176+ _ ( @helpers . full_name ( "Foo::Bar::Qux" ) ) . must_equal "Foo::<wbr>Bar::<wbr>Qux"
177+ _ ( @helpers . full_name ( "::Foo::Bar::Qux" ) ) . must_equal "::Foo::<wbr>Bar::<wbr>Qux"
178+ end
179+
180+ it "inserts word-break opportunities into file paths" do
181+ _ ( @helpers . full_name ( "path/to/file.rb" ) ) . must_equal "path/<wbr>to/<wbr>file.rb"
182+ _ ( @helpers . full_name ( "/path/to/file.rb" ) ) . must_equal "/path/<wbr>to/<wbr>file.rb"
183+ end
184+
185+ it "escapes name parts" do
186+ _ ( @helpers . full_name ( "ruby & rails/file.rb" ) ) . must_equal "ruby & rails/<wbr>file.rb"
187+ end
188+
189+ it "uses RDoc::CodeObject#full_name when argument is an RDoc::CodeObject" do
190+ rdoc_module = rdoc_top_level_for ( <<~RUBY ) . find_module_named ( "Foo::Bar::Qux" )
191+ module Foo; module Bar; class Qux; end; end; end
192+ RUBY
193+
194+ _ ( @helpers . full_name ( rdoc_module ) ) . must_equal "Foo::<wbr>Bar::<wbr>Qux"
195+ end
196+ end
197+
137198 describe "#base_tag_for_context" do
138199 it "returns an idempotent <base> tag for nil context" do
139200 _ ( @helpers . base_tag_for_context ( nil ) ) .
0 commit comments