diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 13:34:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 13:34:06 +0300 |
commit | 859a6fb938bb9ee2a317c46dfa4fcc1af49608f0 (patch) | |
tree | d7f2700abe6b4ffcb2dcfc80631b2d87d0609239 /spec/lib/gitlab/diff/char_diff_spec.rb | |
parent | 446d496a6d000c73a304be52587cd9bbc7493136 (diff) |
Add latest changes from gitlab-org/gitlab@13-9-stable-eev13.9.0-rc42
Diffstat (limited to 'spec/lib/gitlab/diff/char_diff_spec.rb')
-rw-r--r-- | spec/lib/gitlab/diff/char_diff_spec.rb | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/spec/lib/gitlab/diff/char_diff_spec.rb b/spec/lib/gitlab/diff/char_diff_spec.rb new file mode 100644 index 00000000000..e4e2a3ba050 --- /dev/null +++ b/spec/lib/gitlab/diff/char_diff_spec.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'diff_match_patch' + +RSpec.describe Gitlab::Diff::CharDiff do + let(:old_string) { "Helo \n Worlld" } + let(:new_string) { "Hello \n World" } + + subject(:diff) { described_class.new(old_string, new_string) } + + describe '#generate_diff' do + context 'when old string is nil' do + let(:old_string) { nil } + + it 'does not raise an error' do + expect { subject.generate_diff }.not_to raise_error + end + + it 'treats nil values as blank strings' do + changes = subject.generate_diff + + expect(changes).to eq([ + [:insert, "Hello \n World"] + ]) + end + end + + it 'generates an array of changes' do + changes = subject.generate_diff + + expect(changes).to eq([ + [:equal, "Hel"], + [:insert, "l"], + [:equal, "o \n Worl"], + [:delete, "l"], + [:equal, "d"] + ]) + end + end + + describe '#changed_ranges' do + subject { diff.changed_ranges } + + context 'when old string is nil' do + let(:old_string) { nil } + + it 'returns lists of changes' do + old_diffs, new_diffs = subject + + expect(old_diffs).to eq([]) + expect(new_diffs).to eq([0..12]) + end + end + + it 'returns ranges of changes' do + old_diffs, new_diffs = subject + + expect(old_diffs).to eq([11..11]) + expect(new_diffs).to eq([3..3]) + end + end + + describe '#to_html' do + it 'returns an HTML representation of the diff' do + subject.generate_diff + + expect(subject.to_html).to eq( + '<span class="idiff">Hel</span>' \ + '<span class="idiff addition">l</span>' \ + "<span class=\"idiff\">o \n Worl</span>" \ + '<span class="idiff deletion">l</span>' \ + '<span class="idiff">d</span>' + ) + end + end +end |