# Copyright (c) 2020 Jerome Arbez-Gindre # frozen_string_literal: true require('defmastership') RSpec.describe(DefMastership::UpdateDefChecksumModifier) do subject(:modifier) do described_class.new( 'fake config' ) end let(:adoc_texts) do { 'file1.adoc' => "file1 line1\nfile1 line2", 'file2.adoc' => "file2 line1\nfile2 line2" } end describe '.new' do it { is_expected.not_to(be(nil)) } it { is_expected.to(have_attributes(config: 'fake config')) } it { is_expected.to(have_attributes(changes: [])) } end describe '#do_modifications' do let(:line_modifier) { instance_double(DefMastership::UpdateDefChecksumLineModifier, 'lineModifier') } let(:document) { instance_double(DefMastership::Document, 'document') } before do allow(DefMastership::UpdateDefChecksumLineModifier).to( receive(:from_config).with('fake config').and_return(line_modifier) ) allow(DefMastership::Document).to(receive(:new).and_return(document)) allow(document).to(receive(:parse_file_with_preprocessor)) allow(line_modifier).to(receive(:'document=').with(document)) allow(line_modifier).to(receive(:replace).with("file1 line1\n").and_return("new file1 line1\n")) allow(line_modifier).to(receive(:replace).with('file1 line2').and_return('new file1 line2')) allow(line_modifier).to(receive(:replace).with("file2 line1\n").and_return("new file2 line1\n")) allow(line_modifier).to(receive(:replace).with('file2 line2').and_return('new file2 line2')) allow(line_modifier).to(receive(:config).and_return('new fake config')) allow(line_modifier).to(receive(:changes).and_return([%w[from1 to1], %w[from2 to2]])) end context 'when detailed expectations' do before { modifier.do_modifications(adoc_texts) } it do expect(DefMastership::UpdateDefChecksumLineModifier).to( have_received(:from_config).with('fake config') ) end it { expect(document).to(have_received(:parse_file_with_preprocessor).with('file1.adoc')) } it { expect(document).to(have_received(:parse_file_with_preprocessor).with('file2.adoc')) } it { expect(line_modifier).to(have_received(:'document=').with(document)) } it { expect(line_modifier).to(have_received(:replace).with("file1 line1\n")) } it { expect(line_modifier).to(have_received(:replace).with('file1 line2')) } it { expect(line_modifier).to(have_received(:replace).with("file2 line1\n")) } it { expect(line_modifier).to(have_received(:replace).with('file2 line2')) } it { expect(line_modifier).to(have_received(:config)) } it { expect(line_modifier).to(have_received(:changes)) } it { is_expected.to(have_attributes(config: 'new fake config')) } it { is_expected.to(have_attributes(changes: [%w[from1 to1], %w[from2 to2]])) } end it do expected_adoc = { 'file1.adoc' => "new file1 line1\nnew file1 line2", 'file2.adoc' => "new file2 line1\nnew file2 line2" } expect(modifier.do_modifications(adoc_texts)).to(eq(expected_adoc)) end end end