Skip to content

Commit

Permalink
beef up link_to_filename to handle input from different generators
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Jun 20, 2009
1 parent dee9b54 commit 296048f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
14 changes: 9 additions & 5 deletions lib/base/base_template.rb
Expand Up @@ -99,13 +99,17 @@ def inline_css(css)
#
# @return String
# An anchor link to a textmate reference or a file reference
def link_to_filename(name, line = nil)
filename = File.expand_path(name)
def link_to_filename(name, line = nil, link_content = nil)
link_content ||= "#{name}:#{line}"
"<a href='#{file_url(name, line)}'>#{link_content}</a>"
end

def file_url(name, line)
filename = File.expand_path(name.gsub(/^\//, ''))
if MetricFu.configuration.platform.include?('darwin')
"<a href='txmt://open/?url=file://" \
+"#{filename}&line=#{line}'>#{name}:#{line}</a>"
"txmt://open/?url=file://#{filename}&line=#{line}"
else
"<a href='file://#{filename}'>#{name}:#{line}</a>"
"file://#{filename}"
end
end

Expand Down
47 changes: 34 additions & 13 deletions spec/base/base_template_spec.rb
Expand Up @@ -36,7 +36,7 @@
end

describe 'if the template does not exist' do
it 'should return false' do
it 'should return false' do
File.should_receive(:exist?).with(@section).and_return(false)
result = @template.send(:template_exists?, @section)
result.should be_false
Expand Down Expand Up @@ -82,37 +82,58 @@
result.should == 'css contents'
end
end

describe "#link_to_filename " do
describe "when on OS X" do
before(:each) do
config = mock("configuration")
config.should_receive(:platform).and_return('universal-darwin-9.0')
config.stub!(:platform).and_return('universal-darwin-9.0')
MetricFu.stub!(:configuration).and_return(config)
File.should_receive(:expand_path).and_return('filename')
end

it 'should return a textmate protocol link' do
name = "filename"
result = @template.send(:link_to_filename, name)
File.stub(:expand_path).with('filename').and_return('/expanded/filename')
result = @template.send(:link_to_filename, 'filename')
result.should eql("<a href='txmt://open/?url=file://" \
+ "/expanded/filename&line='>filename:</a>")
end

it "should do the right thing with a filename that starts with a slash" do
File.stub(:expand_path).with('filename').and_return('/expanded/filename')
result = @template.send(:link_to_filename, '/filename')
result.should eql("<a href='txmt://open/?url=file://" \
+ "filename&line='>filename:</a>")
+ "/expanded/filename&line='>/filename:</a>")
end

it "should include a line number" do
File.stub(:expand_path).with('filename').and_return('/expanded/filename')
result = @template.send(:link_to_filename, 'filename', 6)
result.should eql("<a href='txmt://open/?url=file://" \
+ "/expanded/filename&line=6'>filename:6</a>")
end

describe "and given link text" do
it "should use the submitted link text" do
File.stub(:expand_path).with('filename').and_return('/expanded/filename')
result = @template.send(:link_to_filename, 'filename', 6, 'link content')
result.should eql("<a href='txmt://open/?url=file://" \
+ "/expanded/filename&line=6'>link content</a>")
end
end
end

describe "when on other platforms" do
before(:each) do
before(:each) do
config = mock("configuration")
config.should_receive(:platform).and_return('other')
MetricFu.stub!(:configuration).and_return(config)
File.should_receive(:expand_path).and_return('filename')
end

it 'should return a file protocol link' do
name = "filename"
name = "filename"
result = @template.send(:link_to_filename, name)
result.should == "<a href='file://filename'>filename:</a>"
result.should == "<a href='file://filename'>filename:</a>"
end
end
end
Expand All @@ -122,15 +143,15 @@
first_val = "first"
second_val = "second"
iter = 2
result = @template.send(:cycle, first_val, second_val, iter)
result = @template.send(:cycle, first_val, second_val, iter)
result.should == first_val
end

it 'should return the second_value passed if iteration passed is odd' do
first_val = "first"
second_val = "second"
iter = 1
result = @template.send(:cycle, first_val, second_val, iter)
iter = 1
result = @template.send(:cycle, first_val, second_val, iter)
result.should == second_val
end
end
Expand Down

0 comments on commit 296048f

Please sign in to comment.