# Copyright (c) 2020 Jerome Arbez-Gindre # frozen_string_literal: true require('defmastership') RSpec.describe(DefMastership::UpdateDefChecksumLineModifier) do subject(:linemodifier) { described_class.new } describe '.new' do it { is_expected.not_to(be_nil) } it { is_expected.to(have_attributes(def_type: '')) } it { is_expected.to(have_attributes(changes: [])) } it do expect { linemodifier.user_defined_attribute } .to(raise_error(NoMethodError)) end end describe '.from_config' do subject(:linemodifier) do described_class.from_config( def_type: 'requirement' ) end it { is_expected.not_to(be_nil) } it { is_expected.to(have_attributes(def_type: 'requirement')) } it { is_expected.to(have_attributes(document: nil)) } end describe '#replace' do subject(:linemodifier) do described_class.from_config( def_type: 'requirement' ) end let(:definition) { instance_double(DefMastership::Definition, 'definition') } let(:document) { instance_double(DefMastership::Document, 'document') } let(:definitions) { { 'REFERENCE' => definition } } before do linemodifier.document = document allow(File).to(receive(:rename)) end context 'when definition has not the good type' do it do expect(linemodifier.replace('[define,req,REFERENCE]')) .to(eq('[define,req,REFERENCE]')) end end context 'when definition has the good type' do before do allow(document).to(receive(:ref_to_def).with('REFERENCE').and_return(definition)) allow(definition).to(receive(:sha256).and_return('~abcd1234')) end it do expect(linemodifier.replace('[define,requirement,REFERENCE]')) .to(eq('[define,requirement,REFERENCE(~abcd1234)]')) end it do expect(linemodifier.replace('[define,requirement,REFERENCE(~bad)]')) .to(eq('[define,requirement,REFERENCE(~abcd1234)]')) end it do expect(linemodifier.replace('[define,requirement,REFERENCE(toto~bad)]')) .to(eq('[define,requirement,REFERENCE(toto~abcd1234)]')) end it do expect(linemodifier.replace('[define,requirement,REFERENCE(toto)]')) .to(eq('[define,requirement,REFERENCE(toto~abcd1234)]')) end end end end