summaryrefslogtreecommitdiff
path: root/spec/hunk_spec.rb
blob: c52a6fd777c6f5ba769fd297ee9602da8b7d3b74 (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
# frozen_string_literal: true

require "spec_helper"

if String.method_defined?(:encoding)
  require "diff/lcs/hunk"

  describe Diff::LCS::Hunk do
    let(:old_data) { ["Tu a un carté avec {count} itéms".encode("UTF-16LE")] }
    let(:new_data) { ["Tu a un carte avec {count} items".encode("UTF-16LE")] }
    let(:pieces) { Diff::LCS.diff old_data, new_data }
    let(:hunk) { Diff::LCS::Hunk.new(old_data, new_data, pieces[0], 3, 0) }

    it "produces a unified diff from the two pieces" do
      expected = <<-EXPECTED.gsub(/^\s+/, "").encode("UTF-16LE").chomp
        @@ -1 +1 @@
        -Tu a un carté avec {count} itéms
        +Tu a un carte avec {count} items
      EXPECTED

      expect(hunk.diff(:unified)).to eq(expected)
    end

    it "produces a unified diff from the two pieces (last entry)" do
      expected = <<-EXPECTED.gsub(/^\s+/, "").encode("UTF-16LE").chomp
        @@ -1 +1 @@
        -Tu a un carté avec {count} itéms
        +Tu a un carte avec {count} items
        \\ No newline at end of file
      EXPECTED

      expect(hunk.diff(:unified, true)).to eq(expected)
    end

    it "produces a context diff from the two pieces" do
      expected = <<-EXPECTED.gsub(/^\s+/, "").encode("UTF-16LE").chomp
        ***************
        *** 1 ****
        ! Tu a un carté avec {count} itéms
        --- 1 ----
        ! Tu a un carte avec {count} items
      EXPECTED

      expect(hunk.diff(:context)).to eq(expected)
    end

    it "produces an old diff from the two pieces" do
      expected = <<-EXPECTED.gsub(/^ +/, "").encode("UTF-16LE").chomp
        1c1
        < Tu a un carté avec {count} itéms
        ---
        > Tu a un carte avec {count} items

      EXPECTED

      expect(hunk.diff(:old)).to eq(expected)
    end

    it "produces a reverse ed diff from the two pieces" do
      expected = <<-EXPECTED.gsub(/^ +/, "").encode("UTF-16LE").chomp
        c1
        Tu a un carte avec {count} items
        .

      EXPECTED

      expect(hunk.diff(:reverse_ed)).to eq(expected)
    end

    context "with empty first data set" do
      let(:old_data) { [] }

      it "produces a unified diff" do
        expected = <<-EXPECTED.gsub(/^\s+/, "").encode("UTF-16LE").chomp
          @@ -1 +1,2 @@
          +Tu a un carte avec {count} items
        EXPECTED

        expect(hunk.diff(:unified)).to eq(expected)
      end
    end
  end
end