summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/diff/line_mapper_spec.rb
blob: 1739bcd14a888ffadb46935f98fec353393dd6ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Diff::LineMapper do
  include RepoHelpers

  let(:project) { create(:project, :repository) }
  let(:repository) { project.repository }
  let(:commit) { project.commit(sample_commit.id) }
  let(:diffs) { commit.raw_diffs }
  let(:diff) { diffs.first }
  let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: repository) }
  subject { described_class.new(diff_file) }

  describe '#old_to_new' do
    context "with a diff file" do
      let(:mapping) do
        {
          1 => 1,
          2 => 2,
          3 => 3,
          4 => 4,
          5 => 5,
          6 => 6,
          7 => 7,
          8 => 8,
          9 => nil,
          # nil => 9,
          10 => 10,
          11 => 11,
          12 => 12,
          13 => nil,
          14 => nil,
          # nil => 15,
          # nil => 16,
          # nil => 17,
          # nil => 18,
          # nil => 19,
          # nil => 20,
          15 => 21,
          16 => 22,
          17 => 23,
          18 => 24,
          19 => 25,
          20 => 26,
          21 => 27,
          # nil => 28,
          22 => 29,
          23 => 30,
          24 => 31,
          25 => 32,
          26 => 33,
          27 => 34,
          28 => 35,
          29 => 36,
          30 => 37
        }
      end

      it 'returns the new line number for the old line number' do
        mapping.each do |old_line, new_line|
          expect(subject.old_to_new(old_line)).to eq(new_line)
        end
      end
    end

    context "without a diff file" do
      let(:diff_file) { nil }

      it "returns the same line number" do
        expect(subject.old_to_new(100)).to eq(100)
      end
    end
  end

  describe '#new_to_old' do
    context "with a diff file" do
      let(:mapping) do
        {
          1 => 1,
          2 => 2,
          3 => 3,
          4 => 4,
          5 => 5,
          6 => 6,
          7 => 7,
          8 => 8,
          # nil => 9,
          9 => nil,
          10 => 10,
          11 => 11,
          12 => 12,
          # nil => 13,
          # nil => 14,
          13 => nil,
          14 => nil,
          15 => nil,
          16 => nil,
          17 => nil,
          18 => nil,
          19 => nil,
          20 => nil,
          21 => 15,
          22 => 16,
          23 => 17,
          24 => 18,
          25 => 19,
          26 => 20,
          27 => 21,
          28 => nil,
          29 => 22,
          30 => 23,
          31 => 24,
          32 => 25,
          33 => 26,
          34 => 27,
          35 => 28,
          36 => 29,
          37 => 30
        }
      end

      it 'returns the old line number for the new line number' do
        mapping.each do |new_line, old_line|
          expect(subject.new_to_old(new_line)).to eq(old_line)
        end
      end
    end

    context "without a diff file" do
      let(:diff_file) { nil }

      it "returns the same line number" do
        expect(subject.new_to_old(100)).to eq(100)
      end
    end
  end
end