BigW Consortium Gitlab

highlight_spec.rb 3.09 KB
Newer Older
1 2 3 4 5 6 7
require 'spec_helper'

describe Gitlab::Diff::Highlight, lib: true do
  include RepoHelpers

  let(:project) { create(:project) }
  let(:commit) { project.commit(sample_commit.id) }
8
  let(:diff) { commit.raw_diffs.first }
9
  let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: project.repository) }
10

11
  describe '#highlight' do
12
    context "with a diff file" do
13
      let(:subject) { Gitlab::Diff::Highlight.new(diff_file, repository: project.repository).highlight }
14

15 16 17
      it 'should return Gitlab::Diff::Line elements' do
        expect(subject.first).to be_an_instance_of(Gitlab::Diff::Line)
      end
Rubén Dávila committed
18

19 20 21 22
      it 'should not modify "match" lines' do
        expect(subject[0].text).to eq('@@ -6,12 +6,18 @@ module Popen')
        expect(subject[22].text).to eq('@@ -19,6 +25,7 @@ module Popen')
      end
Rubén Dávila committed
23

24 25
      it 'highlights and marks unchanged lines' do
        code = %Q{ <span id="LC7" class="line">  <span class="k">def</span> <span class="nf">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="kp">nil</span><span class="p">)</span></span>\n}
Rubén Dávila committed
26

27 28
        expect(subject[2].text).to eq(code)
      end
Rubén Dávila committed
29

30
      it 'highlights and marks removed lines' do
31
        code = %Q{-<span id="LC9" class="line">      <span class="k">raise</span> <span class="s2">"System commands must be given as an array of strings"</span></span>\n}
Rubén Dávila committed
32

33 34
        expect(subject[4].text).to eq(code)
      end
35

36
      it 'highlights and marks added lines' do
37
        code = %Q{+<span id="LC9" class="line">      <span class="k">raise</span> <span class="no"><span class='idiff left'>RuntimeError</span></span><span class="p"><span class='idiff'>,</span></span><span class='idiff right'> </span><span class="s2">"System commands must be given as an array of strings"</span></span>\n}
38

39 40
        expect(subject[5].text).to eq(code)
      end
Rubén Dávila committed
41
    end
42

43
    context "with diff lines" do
44
      let(:subject) { Gitlab::Diff::Highlight.new(diff_file.diff_lines, repository: project.repository).highlight }
45

46 47 48
      it 'should return Gitlab::Diff::Line elements' do
        expect(subject.first).to be_an_instance_of(Gitlab::Diff::Line)
      end
49

50 51 52 53
      it 'should not modify "match" lines' do
        expect(subject[0].text).to eq('@@ -6,12 +6,18 @@ module Popen')
        expect(subject[22].text).to eq('@@ -19,6 +25,7 @@ module Popen')
      end
54

55 56
      it 'marks unchanged lines' do
        code = %Q{   def popen(cmd, path=nil)}
57

58 59 60
        expect(subject[2].text).to eq(code)
        expect(subject[2].text).not_to be_html_safe
      end
61

62 63
      it 'marks removed lines' do
        code = %Q{-      raise "System commands must be given as an array of strings"}
64

65 66 67
        expect(subject[4].text).to eq(code)
        expect(subject[4].text).not_to be_html_safe
      end
68

69
      it 'marks added lines' do
70
        code = %Q{+      raise <span class='idiff left right'>RuntimeError, </span>&quot;System commands must be given as an array of strings&quot;}
71

72 73
        expect(subject[5].text).to eq(code)
        expect(subject[5].text).to be_html_safe
74
      end
75
    end
76 77
  end
end