2014-09-09 07:17:42 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe DiffHelper do
|
|
|
|
include RepoHelpers
|
|
|
|
|
|
|
|
let(:project) { create(:project) }
|
2016-01-08 19:57:51 -05:00
|
|
|
let(:repository) { project.repository }
|
2015-04-21 09:13:40 -04:00
|
|
|
let(:commit) { project.commit(sample_commit.id) }
|
2016-07-27 13:00:34 -04:00
|
|
|
let(:diffs) { commit.raw_diffs }
|
2015-04-27 12:06:51 -04:00
|
|
|
let(:diff) { diffs.first }
|
2016-01-14 16:38:37 -05:00
|
|
|
let(:diff_refs) { [commit.parent, commit] }
|
2016-06-20 12:51:48 -04:00
|
|
|
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: diff_refs, repository: repository) }
|
2014-09-09 07:17:42 -04:00
|
|
|
|
2016-04-14 17:32:46 -04:00
|
|
|
describe 'diff_view' do
|
|
|
|
it 'returns a valid value when cookie is set' do
|
|
|
|
helper.request.cookies[:diff_view] = 'parallel'
|
|
|
|
|
2016-07-21 14:44:12 -04:00
|
|
|
expect(helper.diff_view).to eq :parallel
|
2016-04-14 17:32:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a default value when cookie is invalid' do
|
|
|
|
helper.request.cookies[:diff_view] = 'invalid'
|
|
|
|
|
2016-07-21 14:44:12 -04:00
|
|
|
expect(helper.diff_view).to eq :inline
|
2016-04-14 17:32:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a default value when cookie is nil' do
|
|
|
|
expect(helper.request.cookies).to be_empty
|
|
|
|
|
2016-07-21 14:44:12 -04:00
|
|
|
expect(helper.diff_view).to eq :inline
|
2016-04-14 17:32:46 -04:00
|
|
|
end
|
|
|
|
end
|
2016-07-21 14:44:12 -04:00
|
|
|
|
2016-03-03 12:38:44 -05:00
|
|
|
describe 'diff_options' do
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'returns no collapse false' do
|
2016-07-15 03:04:18 -04:00
|
|
|
expect(diff_options).to include(no_collapse: false)
|
|
|
|
end
|
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'returns no collapse true if expand_all_diffs' do
|
2016-07-15 03:04:18 -04:00
|
|
|
allow(controller).to receive(:params) { { expand_all_diffs: true } }
|
|
|
|
expect(diff_options).to include(no_collapse: true)
|
|
|
|
end
|
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'returns no collapse true if action name diff_for_path' do
|
2016-07-15 03:04:18 -04:00
|
|
|
allow(controller).to receive(:action_name) { 'diff_for_path' }
|
|
|
|
expect(diff_options).to include(no_collapse: true)
|
|
|
|
end
|
2016-07-27 13:00:34 -04:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns paths if action name diff_for_path and param old path' do
|
2016-07-27 13:00:34 -04:00
|
|
|
allow(controller).to receive(:params) { { old_path: 'lib/wadus.rb' } }
|
|
|
|
allow(controller).to receive(:action_name) { 'diff_for_path' }
|
|
|
|
expect(diff_options[:paths]).to include('lib/wadus.rb')
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns paths if action name diff_for_path and param new path' do
|
2016-07-27 13:00:34 -04:00
|
|
|
allow(controller).to receive(:params) { { new_path: 'lib/wadus.rb' } }
|
|
|
|
allow(controller).to receive(:action_name) { 'diff_for_path' }
|
|
|
|
expect(diff_options[:paths]).to include('lib/wadus.rb')
|
|
|
|
end
|
2015-04-27 12:06:51 -04:00
|
|
|
end
|
|
|
|
|
2016-01-30 06:53:12 -05:00
|
|
|
describe '#diff_line_content' do
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'returns non breaking space when line is empty' do
|
2015-02-12 13:53:23 -05:00
|
|
|
expect(diff_line_content(nil)).to eq(' ')
|
2014-09-09 07:17:42 -04:00
|
|
|
end
|
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'returns the line itself' do
|
2015-02-12 13:53:23 -05:00
|
|
|
expect(diff_line_content(diff_file.diff_lines.first.text)).
|
|
|
|
to eq('@@ -6,12 +6,18 @@ module Popen')
|
|
|
|
expect(diff_line_content(diff_file.diff_lines.first.type)).to eq('match')
|
2016-01-14 16:38:37 -05:00
|
|
|
expect(diff_file.diff_lines.first.new_pos).to eq(6)
|
|
|
|
end
|
2016-01-30 06:53:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#mark_inline_diffs" do
|
|
|
|
let(:old_line) { %{abc 'def'} }
|
|
|
|
let(:new_line) { %{abc "def"} }
|
|
|
|
|
|
|
|
it "returns strings with marked inline diffs" do
|
|
|
|
marked_old_line, marked_new_line = mark_inline_diffs(old_line, new_line)
|
2016-01-14 16:38:37 -05:00
|
|
|
|
2016-04-06 15:37:09 -04:00
|
|
|
expect(marked_old_line).to eq("abc <span class='idiff left right deletion'>'def'</span>")
|
2016-01-30 06:53:12 -05:00
|
|
|
expect(marked_old_line).to be_html_safe
|
2016-04-06 15:37:09 -04:00
|
|
|
expect(marked_new_line).to eq("abc <span class='idiff left right addition'>"def"</span>")
|
2016-01-30 06:53:12 -05:00
|
|
|
expect(marked_new_line).to be_html_safe
|
2014-09-09 07:17:42 -04:00
|
|
|
end
|
|
|
|
end
|
2016-07-21 14:44:12 -04:00
|
|
|
|
|
|
|
describe "#diff_match_line" do
|
|
|
|
let(:old_pos) { 40 }
|
|
|
|
let(:new_pos) { 50 }
|
|
|
|
let(:text) { 'some_text' }
|
|
|
|
|
|
|
|
it "should generate foldable top match line for inline view with empty text by default" do
|
|
|
|
output = diff_match_line old_pos, new_pos
|
|
|
|
|
|
|
|
expect(output).to be_html_safe
|
|
|
|
expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.old_line[data-linenumber='#{old_pos}']", text: '...'
|
|
|
|
expect(output).to have_css "td:nth-child(2):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.new_line[data-linenumber='#{new_pos}']", text: '...'
|
|
|
|
expect(output).to have_css 'td:nth-child(3):not(.parallel).line_content.match', text: ''
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow to define text and bottom option" do
|
|
|
|
output = diff_match_line old_pos, new_pos, text: text, bottom: true
|
|
|
|
|
|
|
|
expect(output).to be_html_safe
|
|
|
|
expect(output).to have_css "td:nth-child(1).diff-line-num.unfold.js-unfold.js-unfold-bottom.old_line[data-linenumber='#{old_pos}']", text: '...'
|
|
|
|
expect(output).to have_css "td:nth-child(2).diff-line-num.unfold.js-unfold.js-unfold-bottom.new_line[data-linenumber='#{new_pos}']", text: '...'
|
|
|
|
expect(output).to have_css 'td:nth-child(3):not(.parallel).line_content.match', text: text
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should generate match line for parallel view" do
|
|
|
|
output = diff_match_line old_pos, new_pos, text: text, view: :parallel
|
|
|
|
|
|
|
|
expect(output).to be_html_safe
|
|
|
|
expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.old_line[data-linenumber='#{old_pos}']", text: '...'
|
|
|
|
expect(output).to have_css 'td:nth-child(2).line_content.match.parallel', text: text
|
|
|
|
expect(output).to have_css "td:nth-child(3):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.new_line[data-linenumber='#{new_pos}']", text: '...'
|
|
|
|
expect(output).to have_css 'td:nth-child(4).line_content.match.parallel', text: text
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow to generate only left match line for parallel view" do
|
|
|
|
output = diff_match_line old_pos, nil, text: text, view: :parallel
|
|
|
|
|
|
|
|
expect(output).to be_html_safe
|
|
|
|
expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.old_line[data-linenumber='#{old_pos}']", text: '...'
|
|
|
|
expect(output).to have_css 'td:nth-child(2).line_content.match.parallel', text: text
|
|
|
|
expect(output).not_to have_css 'td:nth-child(3)'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow to generate only right match line for parallel view" do
|
|
|
|
output = diff_match_line nil, new_pos, text: text, view: :parallel
|
|
|
|
|
|
|
|
expect(output).to be_html_safe
|
|
|
|
expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.new_line[data-linenumber='#{new_pos}']", text: '...'
|
|
|
|
expect(output).to have_css 'td:nth-child(2).line_content.match.parallel', text: text
|
|
|
|
expect(output).not_to have_css 'td:nth-child(3)'
|
|
|
|
end
|
|
|
|
end
|
2014-09-09 07:17:42 -04:00
|
|
|
end
|