# frozen_string_literal: true
require 'fast_spec_helper'
require 'rspec-parameterized'
require 'set'
MOCK_LINE = Struct.new(:text, :type, :index, :old_pos, :new_pos)
def make_lines(old_lines, new_lines, texts = nil, types = nil)
old_lines.each_with_index.map do |old, i|
MOCK_LINE.new(texts ? texts[i] : '', types ? types[i] : nil, i, old, new_lines[i])
end
end
RSpec.describe Gitlab::Diff::Rendered::Notebook::DiffFileHelper do
let(:dummy) { Class.new { include Gitlab::Diff::Rendered::Notebook::DiffFileHelper }.new }
describe '#strip_diff_frontmatter' do
using RSpec::Parameterized::TableSyntax
subject { dummy.strip_diff_frontmatter(diff) }
where(:diff, :result) do
"FileLine1\nFileLine2\n@@ -1,76 +1,74 @@\nhello\n" | "@@ -1,76 +1,74 @@\nhello\n"
"" | nil
nil | nil
end
with_them do
it { is_expected.to eq(result) }
end
end
describe '#map_transformed_line_to_source' do
using RSpec::Parameterized::TableSyntax
subject { dummy.source_line_from_block(1, transformed_blocks) }
where(:case, :transformed_blocks, :result) do
'if transformed diff is empty' | [] | 0
'if the transformed line does not map to any in the original file' | [{ source_line: nil }] | 0
'if the transformed line maps to a line in the source file' | [{ source_line: 3 }] | 3
end
with_them do
it { is_expected.to eq(result) }
end
end
describe '#image_as_rich_text' do
let(:img) { 'data:image/png;base64,some_image_here' }
let(:line_text) { " ![](#{img})"}
subject { dummy.image_as_rich_text(line_text) }
context 'text does not contain image' do
let(:img) { "not an image" }
it { is_expected.to be_nil }
end
context 'text contains image' do
it { is_expected.to eq("") }
end
context 'text contains image that has malicious html' do
let(:img) { 'data:image/png;base64,some_image_here"