require 'spec_helper' describe Gitlab::Diff::Highlight, lib: true do include RepoHelpers let(:project) { create(:project) } let(:commit) { project.commit(sample_commit.id) } let(:diff) { commit.diffs.first } let(:diff_file) { Gitlab::Diff::File.new(diff, [commit.parent.id, commit.id], project.repository) } describe '.process_diff_lines' do context 'when processing Gitlab::Diff::Line objects' do let(:diff_lines) { Gitlab::Diff::Highlight.process_diff_lines(diff_file) } it 'should return Gitlab::Diff::Line elements' do expect(diff_lines.first).to be_an_instance_of(Gitlab::Diff::Line) end it 'should highlight the code' do code = %Q{ def popen(cmd, path=nil)\n} expect(diff_lines[2].text).to eq(code) end it 'should not generate the inline diff markup' do expect(diff_lines[5].text).not_to match(Regexp.new(Regexp.escape(''))) end it 'should not modify "match" lines' do expect(diff_lines[0].text).to eq('@@ -6,12 +6,18 @@ module Popen') expect(diff_lines[22].text).to eq('@@ -19,6 +25,7 @@ module Popen') end it 'should highlight unchanged lines' do code = %Q{ def popen(cmd, path=nil)\n} expect(diff_lines[2].text).to eq(code) end it 'should highlight added lines' do code = %Q{+ raise RuntimeError, "System commands must be given as an array of strings"\n} expect(diff_lines[5].text).to eq(code) end it 'should highlight removed lines' do code = %Q{- raise "System commands must be given as an array of strings"\n} expect(diff_lines[4].text).to eq(code) end end end describe '.process_file' do let(:lines) do Gitlab::Diff::Highlight.process_file(project.repository, commit.id, 'files/ruby/popen.rb') end it 'should properly highlight all the lines' do expect(lines[4]).to eq(%Q{ extend self\n}) expect(lines[21]).to eq(%Q{ unless File.directory?(path)\n}) expect(lines[26]).to eq(%Q{ @cmd_status = 0\n}) end end end