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

require 'spec_helper'

describe 'Diff::LCS.traverse_sequences' do
  describe 'callback with no finishers' do
    describe 'over (seq1, seq2)' do
      before(:each) do
        @callback_s1_s2 = simple_callback_no_finishers
        Diff::LCS.traverse_sequences(seq1, seq2, @callback_s1_s2)

        @callback_s2_s1 = simple_callback_no_finishers
        Diff::LCS.traverse_sequences(seq2, seq1, @callback_s2_s1)
      end

      it 'has the correct LCS result on left-matches' do
        expect(@callback_s1_s2.matched_a).to eq(correct_lcs)
        expect(@callback_s2_s1.matched_a).to eq(correct_lcs)
      end

      it 'has the correct LCS result on right-matches' do
        expect(@callback_s1_s2.matched_b).to eq(correct_lcs)
        expect(@callback_s2_s1.matched_b).to eq(correct_lcs)
      end

      it 'has the correct skipped sequences with the left sequence' do
        expect(@callback_s1_s2.discards_a).to eq(skipped_seq1)
        expect(@callback_s2_s1.discards_a).to eq(skipped_seq2)
      end

      it 'has the correct skipped sequences with the right sequence' do
        expect(@callback_s1_s2.discards_b).to eq(skipped_seq2)
        expect(@callback_s2_s1.discards_b).to eq(skipped_seq1)
      end

      it 'does not have anything done markers from the left or right sequences' do
        expect(@callback_s1_s2.done_a).to be_empty
        expect(@callback_s1_s2.done_b).to be_empty
        expect(@callback_s2_s1.done_a).to be_empty
        expect(@callback_s2_s1.done_b).to be_empty
      end
    end

    describe 'over (hello, hello)' do
      before(:each) do
        @callback = simple_callback_no_finishers
        Diff::LCS.traverse_sequences(hello, hello, @callback)
      end

      it 'has the correct LCS result on left-matches' do
        expect(@callback.matched_a).to eq(hello.split(//))
      end

      it 'has the correct LCS result on right-matches' do
        expect(@callback.matched_b).to eq(hello.split(//))
      end

      it 'has the correct skipped sequences with the left sequence', :only => true do
        expect(@callback.discards_a).to be_empty
      end

      it 'has the correct skipped sequences with the right sequence' do
        expect(@callback.discards_b).to be_empty
      end

      it 'does not have anything done markers from the left or right sequences' do
        expect(@callback.done_a).to be_empty
        expect(@callback.done_b).to be_empty
      end
    end

    describe 'over (hello_ary, hello_ary)' do
      before(:each) do
        @callback = simple_callback_no_finishers
        Diff::LCS.traverse_sequences(hello_ary, hello_ary, @callback)
      end

      it 'has the correct LCS result on left-matches' do
        expect(@callback.matched_a).to eq(hello_ary)
      end

      it 'has the correct LCS result on right-matches' do
        expect(@callback.matched_b).to eq(hello_ary)
      end

      it 'has the correct skipped sequences with the left sequence' do
        expect(@callback.discards_a).to be_empty
      end

      it 'has the correct skipped sequences with the right sequence' do
        expect(@callback.discards_b).to be_empty
      end

      it 'does not have anything done markers from the left or right sequences' do
        expect(@callback.done_a).to be_empty
        expect(@callback.done_b).to be_empty
      end
    end
  end

  describe 'callback with finisher' do
    before(:each) do
      @callback_s1_s2 = simple_callback
      Diff::LCS.traverse_sequences(seq1, seq2, @callback_s1_s2)
      @callback_s2_s1 = simple_callback
      Diff::LCS.traverse_sequences(seq2, seq1, @callback_s2_s1)
    end

    it 'has the correct LCS result on left-matches' do
      expect(@callback_s1_s2.matched_a).to eq(correct_lcs)
      expect(@callback_s2_s1.matched_a).to eq(correct_lcs)
    end

    it 'has the correct LCS result on right-matches' do
      expect(@callback_s1_s2.matched_b).to eq(correct_lcs)
      expect(@callback_s2_s1.matched_b).to eq(correct_lcs)
    end

    it 'has the correct skipped sequences for the left sequence' do
      expect(@callback_s1_s2.discards_a).to eq(skipped_seq1)
      expect(@callback_s2_s1.discards_a).to eq(skipped_seq2)
    end

    it 'has the correct skipped sequences for the right sequence' do
      expect(@callback_s1_s2.discards_b).to eq(skipped_seq2)
      expect(@callback_s2_s1.discards_b).to eq(skipped_seq1)
    end

    it 'has done markers differently-sized sequences' do
      expect(@callback_s1_s2.done_a).to eq([['p', 9, 't', 11]])
      expect(@callback_s1_s2.done_b).to be_empty

      expect(@callback_s2_s1.done_a).to be_empty
      expect(@callback_s2_s1.done_b).to eq([['t', 11, 'p', 9]])
    end
  end
end