defmastership 1.0.17 → 1.0.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/.gitlab-ci.yml +22 -10
  3. data/Gemfile +51 -1
  4. data/Rakefile +16 -61
  5. data/bin/defmastership +9 -6
  6. data/config/mutant.yml +23 -3
  7. data/defmastership.gemspec +0 -10
  8. data/features/definition_checksum.feature +31 -1
  9. data/features/export.feature +43 -1
  10. data/features/rename_included_files.feature +28 -0
  11. data/lib/defmastership/batch_modifier.rb +17 -12
  12. data/lib/defmastership/change_ref_modifier.rb +88 -6
  13. data/lib/defmastership/comment_filter.rb +1 -1
  14. data/lib/defmastership/constants.rb +5 -4
  15. data/lib/defmastership/csv_formatter.rb +16 -12
  16. data/lib/defmastership/csv_formatter_body.rb +18 -15
  17. data/lib/defmastership/csv_formatter_header.rb +1 -1
  18. data/lib/defmastership/definition.rb +58 -19
  19. data/lib/defmastership/document.rb +109 -74
  20. data/lib/defmastership/matching_line.rb +17 -0
  21. data/lib/defmastership/modifier.rb +42 -0
  22. data/lib/defmastership/modifier_factory.rb +12 -0
  23. data/lib/defmastership/parsing_state.rb +15 -9
  24. data/lib/defmastership/rename_included_files_modifier.rb +172 -5
  25. data/lib/defmastership/set_join_hack.rb +11 -0
  26. data/lib/defmastership/update_def_checksum_modifier.rb +8 -13
  27. data/lib/defmastership/update_def_modifier.rb +49 -0
  28. data/lib/defmastership/update_def_version_modifier.rb +56 -15
  29. data/lib/defmastership/version.rb +1 -1
  30. data/lib/defmastership.rb +1 -6
  31. data/spec/spec_helper.rb +3 -1
  32. data/spec/unit/def_mastership/batch_modifier_spec.rb +38 -36
  33. data/spec/unit/def_mastership/change_ref_modifier_spec.rb +196 -51
  34. data/spec/unit/def_mastership/csv_formatter_body_spec.rb +60 -31
  35. data/spec/unit/def_mastership/csv_formatter_header_spec.rb +1 -1
  36. data/spec/unit/def_mastership/csv_formatter_spec.rb +79 -87
  37. data/spec/unit/def_mastership/definition_parser_spec.rb +1 -1
  38. data/spec/unit/def_mastership/definition_spec.rb +16 -6
  39. data/spec/unit/def_mastership/document_spec.rb +81 -38
  40. data/spec/unit/def_mastership/matching_line_spec.rb +37 -0
  41. data/spec/unit/def_mastership/modifier_factory_spec.rb +37 -0
  42. data/spec/unit/def_mastership/modifier_spec.rb +83 -0
  43. data/spec/unit/def_mastership/parsing_state_spec.rb +1 -1
  44. data/spec/unit/def_mastership/rename_included_files_modifier_spec.rb +219 -47
  45. data/spec/unit/def_mastership/string_spec.rb +1 -1
  46. data/spec/unit/def_mastership/update_def_checksum_modifier_spec.rb +82 -50
  47. data/spec/unit/def_mastership/update_def_modifier_spec.rb +119 -0
  48. data/spec/unit/def_mastership/update_def_version_modifier_spec.rb +135 -56
  49. data/tasks/console.rake +8 -0
  50. data/tasks/package.task +9 -0
  51. data/tasks/smelling_code.rake +38 -0
  52. data/tasks/test.rake +45 -0
  53. metadata +16 -153
  54. data/lib/defmastership/change_ref_line_modifier.rb +0 -85
  55. data/lib/defmastership/line_modifier_base.rb +0 -29
  56. data/lib/defmastership/modifier_base.rb +0 -36
  57. data/lib/defmastership/rename_included_files_line_modifier.rb +0 -126
  58. data/lib/defmastership/update_def_checksum_line_modifier.rb +0 -38
  59. data/lib/defmastership/update_def_version_line_modifier.rb +0 -58
  60. data/spec/unit/def_mastership/change_ref_line_modifier_spec.rb +0 -250
  61. data/spec/unit/def_mastership/rename_included_files_line_modifier_spec.rb +0 -207
  62. data/spec/unit/def_mastership/update_def_checksum_line_modifier_spec.rb +0 -82
  63. data/spec/unit/def_mastership/update_def_version_line_modifier_spec.rb +0 -131
@@ -0,0 +1,37 @@
1
+ # Copyright (c) 2023 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ require('defmastership/modifier_factory')
5
+
6
+ module DefMastership
7
+ class TotoModifier
8
+ include Modifier
9
+ end
10
+ end
11
+
12
+ RSpec.describe(DefMastership::ModifierFactory) do
13
+ describe('.from_config') do
14
+ let(:toto) { instance_double(DefMastership::TotoModifier, 'toto') }
15
+ let(:toto_config) { { type: 'toto', config: { p: 'whatever' } } }
16
+ let(:tutu) { instance_double(DefMastership::TotoModifier, 'tutu') }
17
+ let(:tutu_config) { { type: 'tu_tu', config: { p: 'whatever' } } }
18
+
19
+ before do
20
+ allow(DefMastership::TotoModifier).to(receive(:new).with(toto_config[:config]).and_return(toto))
21
+ allow(DefMastership::TuTuModifier).to(receive(:new).with(tutu_config[:config]).and_return(tutu))
22
+ end
23
+
24
+ it { expect(described_class.from_config(toto_config)).to(be(toto)) }
25
+ it { expect(described_class.from_config(tutu_config)).to(be(tutu)) }
26
+
27
+ it do
28
+ described_class.from_config(toto_config)
29
+ expect(DefMastership::TotoModifier).to(have_received(:new).with(toto_config[:config]))
30
+ end
31
+
32
+ it do
33
+ described_class.from_config(tutu_config)
34
+ expect(DefMastership::TuTuModifier).to(have_received(:new).with(tutu_config[:config]))
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,83 @@
1
+ # Copyright (c) 2023 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ require('defmastership/modifier')
5
+
6
+ class DummyclassParrent
7
+ def respond_to_missing?(method_name, *_)
8
+ return true if method_name == :ploup
9
+
10
+ false
11
+ end
12
+ end
13
+
14
+ class DummyClass < DummyclassParrent
15
+ include DefMastership::Modifier
16
+
17
+ def self.replacement_methods
18
+ %i[replace_pouet_by_foo replace_foo_by_zoo]
19
+ end
20
+
21
+ def self.default_config
22
+ { something: :default }
23
+ end
24
+
25
+ def initialize(config)
26
+ setup_modifier_module(config)
27
+ super()
28
+ end
29
+
30
+ def replace_pouet_by_foo(line)
31
+ line.gsub('pouet', 'foo')
32
+ end
33
+
34
+ def replace_foo_by_zoo(line)
35
+ line.gsub('foo', 'zoo')
36
+ end
37
+ end
38
+
39
+ class BadDummyClass < DummyclassParrent
40
+ include DefMastership::Modifier
41
+ end
42
+
43
+ RSpec.describe(DefMastership::Modifier) do
44
+ subject(:modifier) { DummyClass.new({}) }
45
+
46
+ describe '.new' do
47
+ it { is_expected.not_to(be_nil) }
48
+ it { is_expected.to(have_attributes(config: { something: :default })) }
49
+ it { is_expected.to(have_attributes(changes: [])) }
50
+ end
51
+
52
+ context 'when want to use config smoothly' do
53
+ subject(:modifier) { DummyClass.new(plop: 'Whatever', something: :not_default) }
54
+
55
+ it { is_expected.to(have_attributes(config: { plop: 'Whatever', something: :not_default })) }
56
+ it { is_expected.to(have_attributes(plop: 'Whatever')) }
57
+ it { is_expected.not_to(respond_to(:pleeep)) }
58
+ it { is_expected.to(respond_to(:ploup)) }
59
+
60
+ it do
61
+ expect { modifier.plooooop }
62
+ .to(raise_error(NoMethodError))
63
+ end
64
+ end
65
+
66
+ describe '#apply_to_all' do
67
+ it do
68
+ texts = { first: "pouet\ntoto\npouet", second: "toto\npouet\ntoto" }
69
+
70
+ expect(modifier.apply_to_all(texts, :replace_pouet_by_foo))
71
+ .to(eq({ first: "foo\ntoto\nfoo", second: "toto\nfoo\ntoto" }))
72
+ end
73
+ end
74
+
75
+ describe '#do_modifications' do
76
+ it do
77
+ adoc_sources = { first: "pouet\ntoto\npouet", second: "toto\npouet\ntoto" }
78
+
79
+ expect(modifier.do_modifications(adoc_sources))
80
+ .to(eq({ first: "zoo\ntoto\nzoo", second: "toto\nzoo\ntoto" }))
81
+ end
82
+ end
83
+ end
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2020 Jerome Arbez-Gindre
2
2
  # frozen_string_literal: true
3
3
 
4
- require('defmastership')
4
+ require('defmastership/parsing_state')
5
5
 
6
6
  RSpec.describe(DefMastership::ParsingState) do
7
7
  subject(:parsing_state) do
@@ -1,67 +1,239 @@
1
1
  # Copyright (c) 2021 Jerome Arbez-Gindre
2
2
  # frozen_string_literal: true
3
3
 
4
- require('defmastership')
4
+ require('defmastership/rename_included_files_modifier')
5
5
 
6
6
  RSpec.describe(DefMastership::RenameIncludedFilesModifier) do
7
- subject(:modifier) do
8
- described_class.new(
9
- 'fake config'
10
- )
11
- end
12
-
13
- let(:adoc_texts) do
14
- {
15
- 'file1.adoc' => "file1 line1\nfile1 line2",
16
- 'file2.adoc' => "file2 line1\nfile2 line2"
17
- }
18
- end
7
+ subject(:includeschanger) { described_class.new({}) }
19
8
 
20
9
  describe '.new' do
10
+ it { expect(described_class.ancestors).to(include(DefMastership::Modifier)) }
21
11
  it { is_expected.not_to(be_nil) }
22
- it { is_expected.to(have_attributes(config: 'fake config')) }
23
- it { is_expected.to(have_attributes(changes: [])) }
12
+ it { is_expected.to(have_attributes(from_regexp: '')) }
13
+ it { is_expected.to(have_attributes(to_template: '')) }
24
14
  end
25
15
 
26
- describe '#do_modifications' do
27
- let(:line_modifier) { instance_double(DefMastership::RenameIncludedFilesLineModifier, 'lineModifier') }
28
-
29
- before do
30
- allow(DefMastership::RenameIncludedFilesLineModifier).to(
31
- receive(:from_config).with('fake config').and_return(line_modifier)
32
- )
33
- allow(line_modifier).to(receive(:replace).with("file1 line1\n").and_return("new file1 line1\n"))
34
- allow(line_modifier).to(receive(:replace).with('file1 line2').and_return('new file1 line2'))
35
- allow(line_modifier).to(receive(:replace).with("file2 line1\n").and_return("new file2 line1\n"))
36
- allow(line_modifier).to(receive(:replace).with('file2 line2').and_return('new file2 line2'))
37
- allow(line_modifier).to(receive(:changes).and_return([%w[from1 to1], %w[from2 to2]]))
38
- allow(line_modifier).to(receive(:config).and_return('fake config'))
39
- end
16
+ describe '.replacement_methods' do
17
+ it { expect(described_class.replacement_methods).to(eq(%i[replace])) }
18
+ end
40
19
 
41
- context 'when detailed expectations' do
42
- before { modifier.do_modifications(adoc_texts) }
20
+ describe '.respond_to_missing?' do
21
+ it { expect(includeschanger.respond_to?(:add_new_definition)).to(be(true)) }
22
+ it { expect(includeschanger.respond_to?(:not_implemented_random_method)).to(be(false)) }
23
+ end
43
24
 
44
- it do
45
- expect(DefMastership::RenameIncludedFilesLineModifier).to(
46
- have_received(:from_config).with('fake config')
25
+ describe '#replace' do
26
+ context 'when NOT valid include' do
27
+ subject(:includeschanger) do
28
+ described_class.new(
29
+ from_regexp: 'orig',
30
+ to_template: 'dest'
47
31
  )
48
32
  end
49
33
 
50
- it { expect(line_modifier).to(have_received(:replace).with("file1 line1\n")) }
51
- it { expect(line_modifier).to(have_received(:replace).with('file1 line2')) }
52
- it { expect(line_modifier).to(have_received(:replace).with("file2 line1\n")) }
53
- it { expect(line_modifier).to(have_received(:replace).with('file2 line2')) }
54
- it { expect(line_modifier).to(have_received(:changes)) }
55
- it { is_expected.to(have_attributes(config: 'fake config')) }
56
- it { is_expected.to(have_attributes(changes: [%w[from1 to1], %w[from2 to2]])) }
34
+ before do
35
+ allow(File).to(receive(:rename))
36
+ end
37
+
38
+ context 'when the include statement do not match' do
39
+ before do
40
+ ['[define,requirement,REFERENCE]', '--'].each do |line|
41
+ includeschanger.replace(line)
42
+ end
43
+ end
44
+
45
+ it do
46
+ expect(includeschanger.replace('badinclude::orig[]'))
47
+ .to(eq('badinclude::orig[]'))
48
+ end
49
+
50
+ it do
51
+ includeschanger.replace('badinclude::orig[]')
52
+ expect(includeschanger).to(have_attributes(changes: []))
53
+ end
54
+ end
55
+
56
+ context 'when the include is not in a definition' do
57
+ before do
58
+ ['[define,requirement,REFERENCE]', '--', 'text', '--'].each do |line|
59
+ includeschanger.replace(line)
60
+ end
61
+ end
62
+
63
+ it do
64
+ expect(includeschanger.replace('include::orig[]'))
65
+ .to(eq('include::orig[]'))
66
+ end
67
+
68
+ it do
69
+ includeschanger.replace('include::orig[]')
70
+ expect(includeschanger).to(have_attributes(changes: []))
71
+ end
72
+ end
73
+
74
+ context 'when the line is commented' do
75
+ before do
76
+ includeschanger.replace('[define,requirement,REFERENCE]')
77
+ includeschanger.replace('--')
78
+ end
79
+
80
+ it do
81
+ expect(includeschanger.replace('// include::orig[]'))
82
+ .to(eq('// include::orig[]'))
83
+ end
84
+ end
85
+
86
+ context 'when the cancel regexp is met' do
87
+ subject(:includeschanger) do
88
+ described_class.new(
89
+ from_regexp: 'orig',
90
+ cancel_if_match: 'orig',
91
+ to_template: 'dest'
92
+ )
93
+ end
94
+
95
+ before do
96
+ includeschanger.replace('[define,requirement,REFERENCE]')
97
+ includeschanger.replace('--')
98
+ end
99
+
100
+ it do
101
+ expect(includeschanger.replace("include::orig[]\n"))
102
+ .to(eq("include::orig[]\n"))
103
+ end
104
+ end
57
105
  end
58
106
 
59
- it do
60
- expected_adoc = {
61
- 'file1.adoc' => "new file1 line1\nnew file1 line2",
62
- 'file2.adoc' => "new file2 line1\nnew file2 line2"
63
- }
64
- expect(modifier.do_modifications(adoc_texts)).to(eq(expected_adoc))
107
+ context 'when valid include' do
108
+ before do
109
+ allow(File).to(receive(:rename))
110
+ end
111
+
112
+ context 'when really simple rule' do
113
+ subject(:includeschanger) do
114
+ described_class.new(
115
+ from_regexp: 'orig',
116
+ cancel_if_match: '_no_cancel_',
117
+ to_template: 'dest'
118
+ )
119
+ end
120
+
121
+ before do
122
+ includeschanger.replace('[define,requirement,REFERENCE]')
123
+ includeschanger.replace('--')
124
+ end
125
+
126
+ it do
127
+ expect(includeschanger.replace("include::orig[]\n"))
128
+ .to(eq("include::dest[]\n"))
129
+ end
130
+
131
+ it do
132
+ includeschanger.replace("include::orig[]\n")
133
+ expect(File).to(have_received(:rename).with('orig', 'dest'))
134
+ end
135
+
136
+ it do
137
+ expect(includeschanger.replace("include::orig[leveloffset=offset,lines=ranges]\n"))
138
+ .to(eq("include::dest[leveloffset=offset,lines=ranges]\n"))
139
+ end
140
+
141
+ it do
142
+ includeschanger.replace("include::orig[leveloffset=offset,lines=ranges]\n")
143
+ expect(File).to(have_received(:rename).with('orig', 'dest'))
144
+ end
145
+
146
+ it do
147
+ includeschanger.replace("include::orig[]\n")
148
+ expect(includeschanger).to(have_attributes(changes: [%w[orig dest]]))
149
+ end
150
+
151
+ it do
152
+ expect(includeschanger.replace("include::toto/orig[]\n"))
153
+ .to(eq("include::toto/dest[]\n"))
154
+ end
155
+
156
+ it do
157
+ includeschanger.replace("include::toto/orig[]\n")
158
+ expect(File).to(have_received(:rename).with('toto/orig', 'toto/dest'))
159
+ end
160
+
161
+ it do
162
+ includeschanger.replace("include::toto/orig[]\n")
163
+ expect(includeschanger).to(have_attributes(changes: [%w[toto/orig toto/dest]]))
164
+ end
165
+ end
166
+
167
+ context 'when complex from_regexp' do
168
+ subject(:includeschanger) do
169
+ described_class.new(
170
+ from_regexp: '(?<origin>.*\.extension)',
171
+ to_template: '%<reference>s_%<origin>s'
172
+ )
173
+ end
174
+
175
+ before do
176
+ includeschanger.replace('[define,requirement,REF]')
177
+ end
178
+
179
+ it do
180
+ expect(includeschanger.replace('include::any_path/one_file.extension[]'))
181
+ .to(eq('include::any_path/REF_one_file.extension[]'))
182
+ end
183
+
184
+ it do
185
+ includeschanger.replace('include::any_path/one_file.extension[]')
186
+ expect(File).to(have_received(:rename).with('any_path/one_file.extension', 'any_path/REF_one_file.extension'))
187
+ end
188
+
189
+ it do
190
+ changes = [%w[any_path/one_file.extension any_path/REF_one_file.extension]]
191
+ includeschanger.replace('include::any_path/one_file.extension[]')
192
+ expect(includeschanger).to(have_attributes(changes: changes))
193
+ end
194
+ end
195
+
196
+ context 'when path with variable' do
197
+ subject(:includeschanger) do
198
+ described_class.new(
199
+ from_regexp: 'orig',
200
+ to_template: 'dest'
201
+ )
202
+ end
203
+
204
+ before do
205
+ includeschanger.replace(':any: one')
206
+ includeschanger.replace(':path: two')
207
+ includeschanger.replace(':var: three')
208
+ includeschanger.replace('[define,requirement,REFERENCE]')
209
+ includeschanger.replace('--')
210
+ end
211
+
212
+ it do
213
+ expect(includeschanger.replace('include::{any}/orig[]'))
214
+ .to(eq('include::{any}/dest[]'))
215
+ end
216
+
217
+ it do
218
+ includeschanger.replace('include::{any}{var}/orig[]')
219
+ expect(File).to(have_received(:rename).with('onethree/orig', 'onethree/dest'))
220
+ end
221
+
222
+ it do
223
+ includeschanger.replace('include::{any}_{path}/orig[]')
224
+ expect(File).to(have_received(:rename).with('one_two/orig', 'one_two/dest'))
225
+ end
226
+
227
+ it do
228
+ expect { includeschanger.replace('include::{bad_variable}/orig[]') }
229
+ .to(raise_error(KeyError, 'key not found: :bad_variable'))
230
+ end
231
+
232
+ it do
233
+ includeschanger.replace('include::{any}_{path}/orig[]')
234
+ expect(includeschanger).to(have_attributes(changes: [%w[one_two/orig one_two/dest]]))
235
+ end
236
+ end
65
237
  end
66
238
  end
67
239
  end
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2020 Jerome Arbez-Gindre
2
2
  # frozen_string_literal: true
3
3
 
4
- require('defmastership')
4
+ require('defmastership/comment_filter')
5
5
 
6
6
  RSpec.describe(String) do
7
7
  context 'when .commented?' do
@@ -1,75 +1,107 @@
1
1
  # Copyright (c) 2020 Jerome Arbez-Gindre
2
2
  # frozen_string_literal: true
3
3
 
4
- require('defmastership')
4
+ require('defmastership/update_def_checksum_modifier')
5
5
 
6
6
  RSpec.describe(DefMastership::UpdateDefChecksumModifier) do
7
- subject(:modifier) do
8
- described_class.new(
9
- 'fake config'
10
- )
11
- end
12
-
13
- let(:adoc_texts) do
14
- {
15
- 'file1.adoc' => "file1 line1\nfile1 line2",
16
- 'file2.adoc' => "file2 line1\nfile2 line2"
17
- }
18
- end
7
+ subject(:modifier) { described_class.new({}) }
19
8
 
20
9
  describe '.new' do
10
+ it { expect(described_class.ancestors).to(include(DefMastership::Modifier)) }
21
11
  it { is_expected.not_to(be_nil) }
22
- it { is_expected.to(have_attributes(config: 'fake config')) }
23
- it { is_expected.to(have_attributes(changes: [])) }
12
+ it { is_expected.to(have_attributes(def_type: '')) }
13
+ end
14
+
15
+ describe '.replacement_methods' do
16
+ it { expect(described_class.replacement_methods).to(eq(%i[replace_reference])) }
24
17
  end
25
18
 
26
19
  describe '#do_modifications' do
27
- let(:line_modifier) { instance_double(DefMastership::UpdateDefChecksumLineModifier, 'lineModifier') }
28
- let(:document) { instance_double(DefMastership::Document, 'document') }
20
+ subject(:modifier) { described_class.new({ def_type: 'req' }) }
21
+
22
+ let(:document) { instance_double(DefMastership::Document, 'document') }
23
+ let(:definition) { instance_double(DefMastership::Definition, 'definition') }
24
+ let(:definitions) { { 'REFERENCE' => definition } }
25
+ let(:adoc_sources) do
26
+ {
27
+ 'file1.adoc' => "[define,req,REFERENCE]\nfile1 line2",
28
+ 'file2.adoc' => "file2 line1\nfile2 line2"
29
+ }
30
+ end
31
+ let(:new_adoc_sources) { nil }
29
32
 
30
33
  before do
31
- allow(DefMastership::UpdateDefChecksumLineModifier).to(
32
- receive(:from_config).with('fake config').and_return(line_modifier)
33
- )
34
34
  allow(DefMastership::Document).to(receive(:new).and_return(document))
35
35
  allow(document).to(receive(:parse_file_with_preprocessor))
36
- allow(line_modifier).to(receive(:'document=').with(document))
37
- allow(line_modifier).to(receive(:replace).with("file1 line1\n").and_return("new file1 line1\n"))
38
- allow(line_modifier).to(receive(:replace).with('file1 line2').and_return('new file1 line2'))
39
- allow(line_modifier).to(receive(:replace).with("file2 line1\n").and_return("new file2 line1\n"))
40
- allow(line_modifier).to(receive(:replace).with('file2 line2').and_return('new file2 line2'))
41
- allow(line_modifier).to(receive(:config).and_return('new fake config'))
42
- allow(line_modifier).to(receive(:changes).and_return([%w[from1 to1], %w[from2 to2]]))
36
+ allow(document).to(receive(:ref_to_def).with('REFERENCE').and_return(definition))
37
+ allow(definition).to(receive(:sha256_short).and_return('~abcd1234'))
38
+
39
+ modifier.do_modifications(adoc_sources)
43
40
  end
44
41
 
45
- context 'when detailed expectations' do
46
- before { modifier.do_modifications(adoc_texts) }
42
+ it { expect(DefMastership::Document).to(have_received(:new)) }
43
+
44
+ it do
45
+ expect(document).to(
46
+ have_received(:parse_file_with_preprocessor)
47
+ .once.with('file1.adoc')
48
+ .once.with('file2.adoc')
49
+ )
50
+ end
51
+
52
+ it do
53
+ expect(modifier.do_modifications(adoc_sources).fetch('file1.adoc')).to(include('abcd1234'))
54
+ end
55
+ end
56
+
57
+ describe '#replace' do
58
+ subject(:modifier) do
59
+ described_class.new(
60
+ def_type: 'requirement'
61
+ )
62
+ end
47
63
 
64
+ let(:definition) { instance_double(DefMastership::Definition, 'definition') }
65
+ let(:document) { instance_double(DefMastership::Document, 'document') }
66
+ let(:definitions) { { 'REFERENCE' => definition } }
67
+
68
+ before do
69
+ allow(File).to(receive(:rename))
70
+ end
71
+
72
+ context 'when definition has not the good type' do
48
73
  it do
49
- expect(DefMastership::UpdateDefChecksumLineModifier).to(
50
- have_received(:from_config).with('fake config')
51
- )
74
+ expect(modifier.replace_reference('[define,req,REFERENCE]'))
75
+ .to(eq('[define,req,REFERENCE]'))
52
76
  end
53
-
54
- it { expect(document).to(have_received(:parse_file_with_preprocessor).with('file1.adoc')) }
55
- it { expect(document).to(have_received(:parse_file_with_preprocessor).with('file2.adoc')) }
56
- it { expect(line_modifier).to(have_received(:'document=').with(document)) }
57
- it { expect(line_modifier).to(have_received(:replace).with("file1 line1\n")) }
58
- it { expect(line_modifier).to(have_received(:replace).with('file1 line2')) }
59
- it { expect(line_modifier).to(have_received(:replace).with("file2 line1\n")) }
60
- it { expect(line_modifier).to(have_received(:replace).with('file2 line2')) }
61
- it { expect(line_modifier).to(have_received(:config)) }
62
- it { expect(line_modifier).to(have_received(:changes)) }
63
- it { is_expected.to(have_attributes(config: 'new fake config')) }
64
- it { is_expected.to(have_attributes(changes: [%w[from1 to1], %w[from2 to2]])) }
65
77
  end
66
78
 
67
- it do
68
- expected_adoc = {
69
- 'file1.adoc' => "new file1 line1\nnew file1 line2",
70
- 'file2.adoc' => "new file2 line1\nnew file2 line2"
71
- }
72
- expect(modifier.do_modifications(adoc_texts)).to(eq(expected_adoc))
79
+ context 'when definition has the good type' do
80
+ before do
81
+ allow(DefMastership::Document).to(receive(:new).and_return(document))
82
+ allow(document).to(receive(:ref_to_def).with('REFERENCE').and_return(definition))
83
+ allow(definition).to(receive(:sha256_short).and_return('~abcd1234'))
84
+ end
85
+
86
+ it do
87
+ expect(modifier.replace_reference('[define,requirement,REFERENCE]'))
88
+ .to(eq('[define,requirement,REFERENCE(~abcd1234)]'))
89
+ end
90
+
91
+ it do
92
+ expect(modifier.replace_reference('[define,requirement,REFERENCE(~bad)]'))
93
+ .to(eq('[define,requirement,REFERENCE(~abcd1234)]'))
94
+ end
95
+
96
+ it do
97
+ expect(modifier.replace_reference('[define,requirement,REFERENCE(toto~bad)]'))
98
+ .to(eq('[define,requirement,REFERENCE(toto~abcd1234)]'))
99
+ end
100
+
101
+ it do
102
+ expect(modifier.replace_reference('[define,requirement,REFERENCE(toto)]'))
103
+ .to(eq('[define,requirement,REFERENCE(toto~abcd1234)]'))
104
+ end
73
105
  end
74
106
  end
75
107
  end