Welcome to mirror list, hosted at ThFree Co, Russian Federation.

inline_diff_spec.rb « diff « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dce655d5690a4ae0a050507803ed581bf533a6ee (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Diff::InlineDiff do
  describe '.for_lines' do
    let(:diff) do
      <<-EOF.strip_heredoc
         class Test
        -  def initialize(test = true)
        +  def initialize(test = false)
             @test = test
        -    if true
        -      @foo = "bar"
        +    unless false
        +      @foo = "baz"
             end
           end
         end
      EOF
    end

    let(:subject) { described_class.for_lines(diff.lines) }

    it 'finds all inline diffs' do
      expect(subject[0]).to be_nil
      expect(subject[1]).to eq([25..27])
      expect(subject[2]).to eq([25..28])
      expect(subject[3]).to be_nil
      expect(subject[4]).to eq([5..10])
      expect(subject[5]).to eq([17..17])
      expect(subject[6]).to eq([5..15])
      expect(subject[7]).to eq([17..17])
      expect(subject[8]).to be_nil
    end

    it 'can handle unchanged empty lines' do
      expect { described_class.for_lines(['- bar', '+ baz', '']) }.not_to raise_error
    end

    context 'when lines have multiple changes' do
      let(:diff) do
        <<~EOF
        - Hello, how are you?
        + Hi, how are you doing?
        EOF
      end

      let(:subject) { described_class.for_lines(diff.lines) }

      it 'finds all inline diffs' do
        expect(subject[0]).to eq([3..6])
        expect(subject[1]).to eq([3..3, 17..22])
      end

      context 'when feature flag is disabled' do
        before do
          stub_feature_flags(improved_merge_diff_highlighting: false)
        end

        it 'finds all inline diffs' do
          expect(subject[0]).to eq([3..19])
          expect(subject[1]).to eq([3..22])
        end
      end
    end
  end

  describe "#inline_diffs" do
    let(:old_line) { "XXX def initialize(test = true)" }
    let(:new_line) { "YYY def initialize(test = false)" }
    let(:subject) { described_class.new(old_line, new_line, offset: 3).inline_diffs }

    it "finds the inline diff" do
      old_diffs, new_diffs = subject

      expect(old_diffs).to eq([26..28])
      expect(new_diffs).to eq([26..29])
    end
  end
end