summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/diff/formatters/text_formatter_spec.rb
blob: 290585d0991b30fadc31a55e1c6f15ef1540d5ec (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Diff::Formatters::TextFormatter do
  let!(:base) do
    {
      base_sha: 123,
      start_sha: 456,
      head_sha: 789,
      old_path: 'old_path.txt',
      new_path: 'new_path.txt',
      line_range: nil
    }
  end

  let!(:complete) do
    base.merge(old_line: 1, new_line: 2)
  end

  it_behaves_like "position formatter" do
    let(:base_attrs) { base }

    let(:attrs) { complete }
  end

  # Specific text formatter examples
  let!(:formatter) { described_class.new(attrs) }
  let(:attrs) { base }

  describe '#line_age' do
    subject { formatter.line_age }

    context ' when there is only new_line' do
      let(:attrs) { base.merge(new_line: 1) }

      it { is_expected.to eq('new') }
    end

    context ' when there is only old_line' do
      let(:attrs) { base.merge(old_line: 1) }

      it { is_expected.to eq('old') }
    end
  end

  describe "#==" do
    it "is false when the line_range changes" do
      formatter_1 = described_class.new(base.merge(line_range: { "start": { "line_code" => "foo" }, "end": { "line_code" => "bar" } }))
      formatter_2 = described_class.new(base.merge(line_range: { "start": { "line_code" => "foo" }, "end": { "line_code" => "baz" } }))

      expect(formatter_1).not_to eq(formatter_2)
    end

    it "is true when the line_range doesn't change" do
      attrs = base.merge({ line_range: { start: { line_code: "foo" }, end: { line_code: "baz" } } })
      formatter_1 = described_class.new(attrs)
      formatter_2 = described_class.new(attrs)

      expect(formatter_1).to eq(formatter_2)
    end
  end
end