summaryrefslogtreecommitdiff
path: root/spec/hunk_spec.rb
blob: dc6f5327c9458300f5273ff7dd1e4ffe9c6c6a89 (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
# -*- ruby encoding: utf-8 -*-

require 'spec_helper'

def h(v)
  v.to_s.bytes.to_a.map { |e| "%02x" % e }.join
end

describe "Diff::LCS::Hunk" do
  if String.method_defined?(:encoding)

    let(:old_data) { ["Tu avec carté {count} itém has".encode('UTF-16LE')] }
    let(:new_data) { ["Tu avec carte {count} item has".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 'should be able to produce a unified diff from the two pieces' do
      expected = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
        @@ -1,2 +1,2 @@
        -Tu avec carté {count} itém has
        +Tu avec carte {count} item has
      EOD
      expect(hunk.diff(:unified).to_s == expected).to eql true
    end

    it 'should be able to produce a context diff from the two pieces' do
      expected = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
        ***************
        *** 1,2 ****
        !Tu avec carté {count} itém has
        --- 1,2 ----
        !Tu avec carte {count} item has
      EOD

      expect(hunk.diff(:context).to_s == expected).to eql true
    end

    it 'should be able to produce an old diff from the two pieces' do
      expected = (<<-EOD.gsub(/^ +/,'').encode('UTF-16LE').chomp)
        1,2c1,2
        < Tu avec carté {count} itém has
        ---
        > Tu avec carte {count} item has

      EOD
      expect(hunk.diff(:old).to_s == expected).to eql true
    end

    it 'should be able to produce a reverse ed diff from the two pieces' do
      expected = (<<-EOD.gsub(/^ +/,'').encode('UTF-16LE').chomp)
        c1,2
        Tu avec carte {count} item has
        .

      EOD
      expect(hunk.diff(:reverse_ed).to_s == expected).to eql true
    end

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

      it 'should be able to produce a unified diff' do
        expected = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
          @@ -1 +1,2 @@
          +Tu avec carte {count} item has
        EOD
        expect(hunk.diff(:unified).to_s == expected).to eql true
      end
    end

  end
end