# Copyright (c) 2020 Jerome Arbez-Gindre # frozen_string_literal: true require('defmastership') RSpec.describe(DefMastership::UpdateDefVersionLineModifier) 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', first_version: 'a' ) end it { is_expected.not_to(be_nil) } it { is_expected.to(have_attributes(def_type: 'requirement')) } it { is_expected.to(have_attributes(first_version: 'a')) } it { is_expected.to(have_attributes(document: nil)) } it { is_expected.to(have_attributes(ref_document: nil)) } end describe '#replace' do subject(:linemodifier) do described_class.from_config( def_type: 'requirement', first_version: 'a' ) end let(:document) { instance_double(DefMastership::Document, 'document') } let(:ref_document) { instance_double(DefMastership::Document, 'ref_document') } let(:definition) { instance_double(DefMastership::Definition, 'definition') } let(:ref_definition) { instance_double(DefMastership::Definition, 'ref_definitions') } before do linemodifier.document = document linemodifier.ref_document = ref_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 context 'when definition has NOT changed' do before do allow(ref_document).to(receive(:ref_to_def).with('REFERENCE').and_return(ref_definition)) allow(ref_definition).to(receive(:sha256).and_return('~abcd1234')) end it do allow(ref_definition).to(receive(:explicit_version).and_return(nil)) expect(linemodifier.replace('[define,requirement,REFERENCE]')) .to(eq('[define,requirement,REFERENCE]')) end it do allow(ref_definition).to(receive(:explicit_version).and_return('c')) expect(linemodifier.replace('[define,requirement,REFERENCE]')) .to(eq('[define,requirement,REFERENCE(c)]')) end it do allow(ref_definition).to(receive(:explicit_version).and_return('c')) expect(linemodifier.replace('[define,requirement,REFERENCE(tyty~1234)]')) .to(eq('[define,requirement,REFERENCE(c~1234)]')) end end context 'when definition has changed' do before do allow(ref_document).to(receive(:ref_to_def).with('REFERENCE').and_return(ref_definition)) allow(ref_definition).to(receive(:sha256).and_return('~4321aaaa')) end [ [nil, '', '(a)'], ['c', '', '(d)'], ['c', '(tyty~1234)', '(d~1234)'], ['2', '', '(3)'], ['1222', '', '(1223)'], ['abb', '', '(abc)'] ].each do |ref, from, to| it do allow(ref_definition).to(receive(:explicit_version).and_return(ref)) expect(linemodifier.replace("[define,requirement,REFERENCE#{from}]")) .to(eq("[define,requirement,REFERENCE#{to}]")) end end end context 'when definition is new' do before do allow(ref_document).to(receive(:ref_to_def).with('REFERENCE').and_return(nil)) end it do expect(linemodifier.replace('[define,requirement,REFERENCE(whatever)]')) .to(eq('[define,requirement,REFERENCE]')) end it do expect(linemodifier.replace('[define,requirement,REFERENCE(~1234)]')) .to(eq('[define,requirement,REFERENCE(~1234)]')) end end end end end