defmastership 1.0.17 → 1.0.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.gitlab-ci.yml +37 -11
  4. data/Gemfile +71 -1
  5. data/Guardfile +44 -0
  6. data/Rakefile +16 -61
  7. data/bin/defmastership +9 -6
  8. data/config/cucumber.yml +3 -0
  9. data/config/mutant.yml +27 -3
  10. data/config/reek.yml +129 -105
  11. data/config/rubocop.yml +72 -28
  12. data/defmastership.gemspec +5 -13
  13. data/features/changeref.feature +0 -8
  14. data/features/definition_checksum.feature +30 -10
  15. data/features/definition_version.feature +168 -10
  16. data/features/export.feature +64 -17
  17. data/features/modify.feature +1 -5
  18. data/features/rename_included_files.feature +27 -4
  19. data/features/step_definitions/git_steps.rb +19 -0
  20. data/lib/defmastership/batch_modifier.rb +17 -12
  21. data/lib/defmastership/change_ref_modifier.rb +88 -6
  22. data/lib/defmastership/comment_filter.rb +1 -1
  23. data/lib/defmastership/constants.rb +10 -7
  24. data/lib/defmastership/csv_formatter.rb +16 -12
  25. data/lib/defmastership/csv_formatter_body.rb +18 -15
  26. data/lib/defmastership/csv_formatter_header.rb +1 -1
  27. data/lib/defmastership/definition.rb +59 -20
  28. data/lib/defmastership/document.rb +101 -74
  29. data/lib/defmastership/matching_line.rb +17 -0
  30. data/lib/defmastership/modifier.rb +42 -0
  31. data/lib/defmastership/modifier_factory.rb +12 -0
  32. data/lib/defmastership/parsing_state.rb +15 -9
  33. data/lib/defmastership/rename_included_files_modifier.rb +172 -5
  34. data/lib/defmastership/set_join_hack.rb +11 -0
  35. data/lib/defmastership/update_def_checksum_modifier.rb +8 -13
  36. data/lib/defmastership/update_def_modifier.rb +49 -0
  37. data/lib/defmastership/update_def_version_modifier.rb +81 -15
  38. data/lib/defmastership/version.rb +1 -1
  39. data/lib/defmastership.rb +1 -6
  40. data/spec/spec_helper.rb +3 -1
  41. data/spec/unit/def_mastership/batch_modifier_spec.rb +40 -36
  42. data/spec/unit/def_mastership/change_ref_modifier_spec.rb +196 -51
  43. data/spec/unit/def_mastership/csv_formatter_body_spec.rb +60 -31
  44. data/spec/unit/def_mastership/csv_formatter_header_spec.rb +1 -1
  45. data/spec/unit/def_mastership/csv_formatter_spec.rb +79 -87
  46. data/spec/unit/def_mastership/definition_parser_spec.rb +1 -1
  47. data/spec/unit/def_mastership/definition_spec.rb +16 -6
  48. data/spec/unit/def_mastership/document_spec.rb +81 -38
  49. data/spec/unit/def_mastership/matching_line_spec.rb +37 -0
  50. data/spec/unit/def_mastership/modifier_factory_spec.rb +38 -0
  51. data/spec/unit/def_mastership/modifier_spec.rb +85 -0
  52. data/spec/unit/def_mastership/parsing_state_spec.rb +1 -1
  53. data/spec/unit/def_mastership/rename_included_files_modifier_spec.rb +219 -47
  54. data/spec/unit/def_mastership/string_spec.rb +1 -1
  55. data/spec/unit/def_mastership/update_def_checksum_modifier_spec.rb +82 -50
  56. data/spec/unit/def_mastership/update_def_modifier_spec.rb +121 -0
  57. data/spec/unit/def_mastership/update_def_version_modifier_spec.rb +327 -56
  58. data/tasks/code_quality.rake +74 -0
  59. data/tasks/console.rake +8 -0
  60. data/tasks/package.task +9 -0
  61. data/tasks/test.rake +30 -0
  62. metadata +33 -145
  63. data/.rubocop.yml +0 -76
  64. data/config/devtools.yml +0 -2
  65. data/config/flay.yml +0 -3
  66. data/config/flog.yml +0 -2
  67. data/config/yardstick.yml +0 -2
  68. data/cucumber.yml +0 -2
  69. data/lib/defmastership/change_ref_line_modifier.rb +0 -85
  70. data/lib/defmastership/line_modifier_base.rb +0 -29
  71. data/lib/defmastership/modifier_base.rb +0 -36
  72. data/lib/defmastership/rename_included_files_line_modifier.rb +0 -126
  73. data/lib/defmastership/update_def_checksum_line_modifier.rb +0 -38
  74. data/lib/defmastership/update_def_version_line_modifier.rb +0 -58
  75. data/spec/unit/def_mastership/change_ref_line_modifier_spec.rb +0 -250
  76. data/spec/unit/def_mastership/rename_included_files_line_modifier_spec.rb +0 -207
  77. data/spec/unit/def_mastership/update_def_checksum_line_modifier_spec.rb +0 -82
  78. data/spec/unit/def_mastership/update_def_version_line_modifier_spec.rb +0 -131
  79. /data/{.rspec → config/rspec} +0 -0
@@ -0,0 +1,49 @@
1
+ # Copyright (c) 2024 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ module DefMastership
5
+ # absttrac class for modififier that need o update defintion references
6
+ class UpdateDefModifier
7
+ include Modifier
8
+
9
+ attr_reader :document
10
+
11
+ def self.default_config
12
+ {
13
+ def_type: ''
14
+ }
15
+ end
16
+
17
+ def self.replacement_methods
18
+ %i[replace_reference]
19
+ end
20
+
21
+ def self.reference_regexp(reference)
22
+ Regexp.new("#{reference}#{DMRegexp::DEF_VERSION_AND_CHECKSUM}")
23
+ end
24
+
25
+ def initialize(config)
26
+ @document = Document.new
27
+
28
+ setup_modifier_module(config)
29
+ end
30
+
31
+ def do_modifications(adoc_sources)
32
+ adoc_sources.each_key do |adoc_file|
33
+ document.parse_file_with_preprocessor(adoc_file)
34
+ end
35
+
36
+ super
37
+ end
38
+
39
+ def replace_reference(line)
40
+ match = line.match(DMRegexp::DEFINITION)
41
+
42
+ return line unless match
43
+ return line unless match[:type] == def_type
44
+
45
+ reference = match[:reference]
46
+ line.sub(self.class.reference_regexp(reference), reference_replacement(reference, match))
47
+ end
48
+ end
49
+ end
@@ -1,25 +1,91 @@
1
1
  # Copyright (c) 2020 Jerome Arbez-Gindre
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'defmastership/constants'
5
+ require 'defmastership/update_def_modifier'
6
+ require 'git'
7
+ require 'tmpdir'
8
+
4
9
  module DefMastership
5
- # Update definition version if the definition has change since
6
- # reference document
7
- class UpdateDefVersionModifier < ModifierBase
8
- def replacements
9
- %i[replace]
10
+ # modify one line after another
11
+ class UpdateDefVersionModifier < UpdateDefModifier
12
+ def self.default_config
13
+ {
14
+ def_type: '',
15
+ ref_document: [],
16
+ ref_tag: '',
17
+ ref_repo: '.',
18
+ first_version: ''
19
+ }
20
+ end
21
+
22
+ def initialize(config)
23
+ @ref_document = Document.new
24
+
25
+ Helper.normalilize_config(config) if config.key?(:ref_document)
26
+
27
+ super
28
+ end
29
+
30
+ def do_modifications(adoc_sources)
31
+ if ref_tag == ''
32
+ ref_document.each { |ref_doc| @ref_document.parse_file_with_preprocessor(ref_doc) }
33
+ else
34
+ Dir.mktmpdir('defmastership') do |tmpdir|
35
+ parse_ref_files_from_git(adoc_sources, tmpdir)
36
+ end
37
+ end
38
+
39
+ super
40
+ end
41
+
42
+ private
43
+
44
+ def parse_ref_files_from_git(adoc_sources, tmpdir)
45
+ Git.clone(ref_repo, tmpdir, branch: ref_tag)
46
+ ref_sources = ref_document.empty? ? adoc_sources.keys : ref_document
47
+ ref_sources.each do |adoc_file|
48
+ @ref_document.parse_file_with_preprocessor("#{tmpdir}/#{adoc_file}")
49
+ end
10
50
  end
11
51
 
12
- def new_line_modifier(config, adoc_texts)
13
- document = Document.new
14
- adoc_texts.each do |adoc_file, _|
15
- document.parse_file_with_preprocessor(adoc_file)
52
+ def reference_replacement(reference, match)
53
+ "#{reference}#{version_and_checksum_str(match)}"
54
+ end
55
+
56
+ def version_and_checksum_str(match)
57
+ explicit_checksum = match[:explicit_checksum]
58
+ version = version_string(match)
59
+ return unless explicit_checksum || version
60
+
61
+ "(#{version}#{explicit_checksum})"
62
+ end
63
+
64
+ def version_string(match)
65
+ ref_definition = Helper.def_from_match(@ref_document, match)
66
+ definition = Helper.def_from_match(document, match)
67
+ return unless ref_definition
68
+
69
+ Helper.ref_version(ref_definition, definition, first_version)
70
+ end
71
+
72
+ # Helper functions
73
+ module Helper
74
+ def self.def_from_match(doc, match)
75
+ doc.ref_to_def(match[:reference])
76
+ end
77
+
78
+ def self.ref_version(ref_definition, definition, first_version)
79
+ new_ref_version = ref_definition.explicit_version
80
+ return new_ref_version if definition.sha256_short == ref_definition.sha256_short
81
+
82
+ new_ref_version ? new_ref_version.next : first_version
83
+ end
84
+
85
+ def self.normalilize_config(config)
86
+ ref_docs = config.fetch(:ref_document)
87
+ config[:ref_document] = [ref_docs] if ref_docs.instance_of?(String)
16
88
  end
17
- ref_document = Document.new
18
- ref_document.parse_file_with_preprocessor(config[:ref_document])
19
- line_modifier = UpdateDefVersionLineModifier.from_config(config)
20
- line_modifier.document = document
21
- line_modifier.ref_document = ref_document
22
- line_modifier
23
89
  end
24
90
  end
25
91
  end
@@ -2,6 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module DefMastership
5
- VERSION = '1.0.17'
5
+ VERSION = '1.0.19'
6
6
  public_constant :VERSION
7
7
  end
data/lib/defmastership.rb CHANGED
@@ -6,7 +6,6 @@ require('defmastership/version')
6
6
  # Add requires for other files you add to your project here, so
7
7
  # you just need to require this one file in your bin file
8
8
  require('defmastership/batch_modifier')
9
- require('defmastership/change_ref_line_modifier')
10
9
  require('defmastership/change_ref_modifier')
11
10
  require('defmastership/comment_filter')
12
11
  require('defmastership/constants')
@@ -15,12 +14,8 @@ require('defmastership/definition')
15
14
  require('defmastership/definition_parser')
16
15
  require('defmastership/document')
17
16
  require('defmastership/filters')
18
- require('defmastership/line_modifier_base')
19
- require('defmastership/modifier_base')
17
+ require('defmastership/modifier_factory')
20
18
  require('defmastership/parsing_state')
21
- require('defmastership/rename_included_files_line_modifier')
22
19
  require('defmastership/rename_included_files_modifier')
23
- require('defmastership/update_def_checksum_line_modifier')
24
20
  require('defmastership/update_def_checksum_modifier')
25
- require('defmastership/update_def_version_line_modifier')
26
21
  require('defmastership/update_def_version_modifier')
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,6 @@
3
3
 
4
4
  require('aasm/rspec')
5
5
  require('aruba/rspec')
6
- require('bundler/setup')
7
6
 
8
7
  # formatter = [SimpleCov::Formatter::HTMLFormatter]
9
8
  # SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new(formatter)
@@ -18,8 +17,11 @@ SimpleCov.start do
18
17
 
19
18
  add_filter 'config'
20
19
  add_filter 'vendor'
20
+ add_filter 'set_join_hack'
21
21
 
22
22
  minimum_coverage 100
23
+
24
+ enable_coverage :branch
23
25
  end
24
26
 
25
27
  RSpec.configure do |config|
@@ -1,29 +1,33 @@
1
1
  # Copyright (c) 2020 Jerome Arbez-Gindre
2
2
  # frozen_string_literal: true
3
3
 
4
- require('defmastership')
4
+ require('defmastership/batch_modifier')
5
5
 
6
6
  module DefMastership
7
- class TotoClassModifier < ModifierBase
7
+ # Modifier example
8
+ class TotoModifier
9
+ include Modifier
8
10
  end
9
11
 
10
- class TutuClassModifier < ModifierBase
12
+ # Modifier example
13
+ class TuTuModifier
14
+ include Modifier
11
15
  end
12
16
  end
13
17
 
14
18
  RSpec.describe(DefMastership::BatchModifier) do
15
19
  subject(:batchmodifier) do
16
- described_class.new(config, adoc_texts)
20
+ described_class.new(config, adoc_sources)
17
21
  end
18
22
 
19
23
  let(:config) do
20
24
  {
21
- modifier1: { type: 'toto_class', config: { p: 1 } },
22
- modifier2: { type: 'toto_class', config: { p: 'whatever1' } },
23
- modifier3: { type: 'tutu_class', config: { p1: 'whatever2', p2: 'whatever3' } }
25
+ modifier1: { type: 'toto', config: { p: 1 } },
26
+ modifier2: { type: 'toto', config: { p: 'whatever1' } },
27
+ modifier3: { type: 'tu_tu', config: { p1: 'whatever2', p2: 'whatever3' } }
24
28
  }
25
29
  end
26
- let(:adoc_texts) do
30
+ let(:adoc_sources) do
27
31
  {
28
32
  'file1.adoc' => 'some text',
29
33
  'file2.adoc' => 'another text'
@@ -35,14 +39,14 @@ RSpec.describe(DefMastership::BatchModifier) do
35
39
  it { is_expected.to(have_attributes(config: config)) }
36
40
 
37
41
  it do
38
- expect(batchmodifier).to(have_attributes(adoc_texts: adoc_texts))
42
+ expect(batchmodifier).to(have_attributes(adoc_sources: adoc_sources))
39
43
  end
40
44
  end
41
45
 
42
46
  describe '#apply' do
43
47
  context 'with only one modification' do
44
- let(:toto1) { instance_double(DefMastership::TotoClassModifier, 'toto1') }
45
- let(:adoc_texts_modified) do
48
+ let(:toto1) { instance_double(DefMastership::TotoModifier, 'toto1') }
49
+ let(:adoc_sources_modified) do
46
50
  {
47
51
  'file1.adoc' => 'some text modified',
48
52
  'file2.adoc' => 'another text'
@@ -51,26 +55,26 @@ RSpec.describe(DefMastership::BatchModifier) do
51
55
 
52
56
  let(:config_modified) do
53
57
  {
54
- modifier1: { type: 'toto_class', config: { p: 'modified_param' } },
55
- modifier2: { type: 'toto_class', config: { p: 'whatever1' } },
56
- modifier3: { type: 'tutu_class', config: { p1: 'whatever2', p2: 'whatever3' } }
58
+ modifier1: { type: 'toto', config: { p: 'modified_param' } },
59
+ modifier2: { type: 'toto', config: { p: 'whatever1' } },
60
+ modifier3: { type: 'tu_tu', config: { p1: 'whatever2', p2: 'whatever3' } }
57
61
  }
58
62
  end
59
63
 
60
64
  before do
61
- allow(DefMastership::TotoClassModifier).to(receive(:new).once.and_return(toto1))
62
- allow(toto1).to(receive(:do_modifications).once.and_return(adoc_texts_modified))
65
+ allow(DefMastership::TotoModifier).to(receive(:new).once.and_return(toto1))
66
+ allow(toto1).to(receive(:do_modifications).once.and_return(adoc_sources_modified))
63
67
  allow(toto1).to(receive(:config).once.and_return(p: 'modified_param'))
64
68
  allow(toto1).to(receive(:changes).once.and_return([%w[from1 to1], %w[from2 to2]]))
65
- batchmodifier.apply('modifier1')
69
+ batchmodifier.apply(%i[modifier1])
66
70
  end
67
71
 
68
72
  it do
69
- expect(DefMastership::TotoClassModifier).to(have_received(:new).with(p: 1))
73
+ expect(DefMastership::TotoModifier).to(have_received(:new).with(p: 1))
70
74
  end
71
75
 
72
- it { expect(toto1).to(have_received(:do_modifications).with(adoc_texts)) }
73
- it { is_expected.to(have_attributes(adoc_texts: adoc_texts_modified)) }
76
+ it { expect(toto1).to(have_received(:do_modifications).with(adoc_sources)) }
77
+ it { is_expected.to(have_attributes(adoc_sources: adoc_sources_modified)) }
74
78
  it { expect(toto1).to(have_received(:config).with(no_args)) }
75
79
 
76
80
  it { is_expected.to(have_attributes(config: config_modified)) }
@@ -78,43 +82,43 @@ RSpec.describe(DefMastership::BatchModifier) do
78
82
  end
79
83
 
80
84
  context 'with two modifications' do
81
- let(:toto2) { instance_double(DefMastership::TotoClassModifier, 'toto2') }
82
- let(:tutu3) { instance_double(DefMastership::TutuClassModifier, 'tutu3') }
85
+ let(:toto2) { instance_double(DefMastership::TotoModifier, 'toto2') }
86
+ let(:tutu3) { instance_double(DefMastership::TuTuModifier, 'tutu3') }
83
87
 
84
88
  let(:config_modified) do
85
89
  {
86
- modifier1: { type: 'toto_class', config: { p: 1 } },
87
- modifier2: { type: 'toto_class', config: :whatever },
88
- modifier3: { type: 'tutu_class', config: :pouet }
90
+ modifier1: { type: 'toto', config: { p: 1 } },
91
+ modifier2: { type: 'toto', config: :whatever },
92
+ modifier3: { type: 'tu_tu', config: :pouet }
89
93
  }
90
94
  end
91
95
 
92
96
  before do
93
- allow(DefMastership::TotoClassModifier).to(receive(:new).once.and_return(toto2))
94
- allow(toto2).to(receive(:do_modifications).once.and_return(:adoc_texts_modified_mod2))
97
+ allow(DefMastership::TotoModifier).to(receive(:new).once.and_return(toto2))
98
+ allow(toto2).to(receive(:do_modifications).once.and_return(:adoc_sources_modified_mod2))
95
99
  allow(toto2).to(receive(:config).once.and_return(:whatever))
96
- allow(DefMastership::TutuClassModifier).to(receive(:new).once.and_return(tutu3))
97
- allow(tutu3).to(receive(:do_modifications).once.and_return(:adoc_texts_modified_mod3))
100
+ allow(DefMastership::TuTuModifier).to(receive(:new).once.and_return(tutu3))
101
+ allow(tutu3).to(receive(:do_modifications).once.and_return(:adoc_sources_modified_mod3))
98
102
  allow(tutu3).to(receive(:config).once.and_return(:pouet))
99
103
  allow(toto2).to(receive(:changes).once.and_return([%w[from1 to1]]))
100
104
  allow(tutu3).to(receive(:changes).once.and_return([%w[from2 to2]]))
101
- batchmodifier.apply('modifier2, modifier3')
105
+ batchmodifier.apply(%i[modifier2 modifier3])
102
106
  end
103
107
 
104
- it { expect(DefMastership::TotoClassModifier).to(have_received(:new).with(p: 'whatever1')) }
105
- it { expect(DefMastership::TutuClassModifier).to(have_received(:new).with(p1: 'whatever2', p2: 'whatever3')) }
106
- it { expect(toto2).to(have_received(:do_modifications).with(adoc_texts)) }
108
+ it { expect(DefMastership::TotoModifier).to(have_received(:new).with(p: 'whatever1')) }
109
+ it { expect(DefMastership::TuTuModifier).to(have_received(:new).with(p1: 'whatever2', p2: 'whatever3')) }
110
+ it { expect(toto2).to(have_received(:do_modifications).with(adoc_sources)) }
107
111
  it { expect(toto2).to(have_received(:config).with(no_args)) }
108
- it { expect(tutu3).to(have_received(:do_modifications).with(:adoc_texts_modified_mod2)) }
112
+ it { expect(tutu3).to(have_received(:do_modifications).with(:adoc_sources_modified_mod2)) }
109
113
  it { expect(tutu3).to(have_received(:config).with(no_args)) }
110
- it { is_expected.to(have_attributes(adoc_texts: :adoc_texts_modified_mod3)) }
114
+ it { is_expected.to(have_attributes(adoc_sources: :adoc_sources_modified_mod3)) }
111
115
  it { is_expected.to(have_attributes(config: config_modified)) }
112
116
  it { is_expected.to(have_attributes(changes: [%w[modifier2 from1 to1], %w[modifier3 from2 to2]])) }
113
117
  end
114
118
 
115
119
  context 'with wrong modification' do
116
120
  it do
117
- expect { batchmodifier.apply('wrong-modification') }
121
+ expect { batchmodifier.apply(%i[wrong-modification]) }
118
122
  .to(
119
123
  raise_error(ArgumentError, 'wrong-modification is not a known modification')
120
124
  )
@@ -1,76 +1,221 @@
1
1
  # Copyright (c) 2020 Jerome Arbez-Gindre
2
2
  # frozen_string_literal: true
3
3
 
4
- require('defmastership')
4
+ require('defmastership/change_ref_modifier')
5
5
 
6
6
  RSpec.describe(DefMastership::ChangeRefModifier) do
7
- subject(:modifier) do
8
- described_class.new(
9
- 'fake config'
10
- )
7
+ subject(:refchanger) { described_class.new({}) }
8
+
9
+ describe '.new' do
10
+ it { expect(described_class.ancestors).to(include(DefMastership::Modifier)) }
11
+ it { is_expected.not_to(be_nil) }
12
+ it { is_expected.to(have_attributes(from_regexp: '')) }
13
+ it { is_expected.to(have_attributes(to_template: '')) }
14
+ it { is_expected.to(have_attributes(next_ref: 0)) }
11
15
  end
12
16
 
13
- let(:adoc_texts) do
14
- {
15
- 'file1.adoc' => "file1 line1\nfile1 line2",
16
- 'file2.adoc' => "file2 line1\nfile2 line2"
17
- }
17
+ describe '.replacement_methods' do
18
+ it { expect(described_class.replacement_methods).to(eq(%i[replace_refdef replace_irefs])) }
18
19
  end
19
20
 
20
- describe '.new' do
21
- 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: [])) }
21
+ describe '#config' do
22
+ context 'when not initalized' do
23
+ it do
24
+ expect(refchanger.config).to(
25
+ include(from_regexp: '', to_template: '', next_ref: 0)
26
+ )
27
+ end
28
+ end
24
29
  end
25
30
 
26
- describe '#do_modifications' do
27
- let(:line_modifier) { instance_double(DefMastership::ChangeRefLineModifier, 'lineModifier') }
31
+ describe '#replace_def' do
32
+ context 'when really simple rule' do
33
+ subject(:refchanger) do
34
+ described_class.new(
35
+ from_regexp: 'TEMP',
36
+ to_template: 'TUTU'
37
+ )
38
+ end
28
39
 
29
- before do
30
- allow(DefMastership::ChangeRefLineModifier).to(
31
- receive(:from_config).with('fake config').and_return(line_modifier)
32
- )
33
- allow(line_modifier).to(receive(:replace_refdef).with("file1 line1\n").and_return("new file1 line1\n"))
34
- allow(line_modifier).to(receive(:replace_refdef).with('file1 line2').and_return('new file1 line2'))
35
- allow(line_modifier).to(receive(:replace_refdef).with("file2 line1\n").and_return("new file2 line1\n"))
36
- allow(line_modifier).to(receive(:replace_refdef).with('file2 line2').and_return('new file2 line2'))
37
- allow(line_modifier).to(receive(:replace_irefs).with("new file1 line1\n").and_return("new2 file1 line1\n"))
38
- allow(line_modifier).to(receive(:replace_irefs).with('new file1 line2').and_return('new2 file1 line2'))
39
- allow(line_modifier).to(receive(:replace_irefs).with("new file2 line1\n").and_return("new2 file2 line1\n"))
40
- allow(line_modifier).to(receive(:replace_irefs).with('new file2 line2').and_return('new2 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]]))
40
+ context 'when valid definition' do
41
+ it do
42
+ expect(refchanger.replace_refdef('[define, whatever, TEMP]'))
43
+ .to(eq('[define, whatever, TUTU]'))
44
+ end
45
+ end
46
+
47
+ context 'when no valid definition' do
48
+ it do
49
+ expect(refchanger.replace_refdef('[pouet, whatever, TEMP]'))
50
+ .to(eq('[pouet, whatever, TEMP]'))
51
+ end
52
+ end
43
53
  end
44
54
 
45
- context 'when detailed expectations' do
46
- before { modifier.do_modifications(adoc_texts) }
55
+ context 'when template is variable' do
56
+ subject(:refchanger) do
57
+ described_class.new(
58
+ from_regexp: 'TEMP',
59
+ to_template: 'TOTO-%<next_ref>04d',
60
+ next_ref: 124
61
+ )
62
+ end
47
63
 
48
64
  it do
49
- expect(DefMastership::ChangeRefLineModifier).to(
50
- have_received(:from_config).with('fake config')
65
+ expect(refchanger.replace_refdef('[define, whatever, TEMP]'))
66
+ .to(eq('[define, whatever, TOTO-0124]'))
67
+ end
68
+
69
+ it do
70
+ refchanger.replace_refdef('[define, whatever, TEMP]')
71
+ expect(refchanger).to(have_attributes(next_ref: 125))
72
+ end
73
+ end
74
+
75
+ context 'when capture group in regexp' do
76
+ subject(:refchanger) do
77
+ described_class.new(
78
+ from_regexp: '(?<cg>TOTO|TITI)-TEMP[\d]*',
79
+ to_template: '%<cg>s-%<next_ref>04d',
80
+ next_ref: 132
81
+ )
82
+ end
83
+
84
+ it do
85
+ expect(refchanger.replace_refdef('[define, whatever, TOTO-TEMP]'))
86
+ .to(eq('[define, whatever, TOTO-0132]'))
87
+ end
88
+
89
+ it do
90
+ refchanger.replace_refdef('[define, whatever, TOTO-TEMP]')
91
+ expect(refchanger.replace_refdef('[define, whatever, TITI-TEMP]'))
92
+ .to(eq('[define, whatever, TITI-0133]'))
93
+ end
94
+
95
+ it do
96
+ refchanger.replace_refdef('[define, whatever, TOTO-TEMP1]')
97
+ refchanger.replace_refdef('[define, whatever, TITI-TEMP2]')
98
+ expect(refchanger).to(have_attributes(changes: [%w[TOTO-TEMP1 TOTO-0132], %w[TITI-TEMP2 TITI-0133]]))
99
+ end
100
+ end
101
+
102
+ context 'when definition is in literal block' do
103
+ subject(:refchanger) do
104
+ described_class.new(
105
+ from_regexp: 'TEMP',
106
+ to_template: 'TUTU'
107
+ )
108
+ end
109
+
110
+ before do
111
+ refchanger.replace_refdef('....')
112
+ end
113
+
114
+ it do
115
+ expect(refchanger.replace_refdef('[define, whatever, TEMP]'))
116
+ .to(eq('[define, whatever, TEMP]'))
117
+ end
118
+ end
119
+
120
+ context 'when defintion is after literal block' do
121
+ subject(:refchanger) do
122
+ described_class.new(
123
+ from_regexp: 'TEMP',
124
+ to_template: 'TUTU'
51
125
  )
52
126
  end
53
127
 
54
- it { expect(line_modifier).to(have_received(:replace_refdef).with("file1 line1\n")) }
55
- it { expect(line_modifier).to(have_received(:replace_refdef).with('file1 line2')) }
56
- it { expect(line_modifier).to(have_received(:replace_refdef).with("file2 line1\n")) }
57
- it { expect(line_modifier).to(have_received(:replace_refdef).with('file2 line2')) }
58
- it { expect(line_modifier).to(have_received(:replace_irefs).with("new file1 line1\n")) }
59
- it { expect(line_modifier).to(have_received(:replace_irefs).with('new file1 line2')) }
60
- it { expect(line_modifier).to(have_received(:replace_irefs).with("new file2 line1\n")) }
61
- it { expect(line_modifier).to(have_received(:replace_irefs).with('new file2 line2')) }
62
- it { expect(line_modifier).to(have_received(:config)) }
63
- it { expect(line_modifier).to(have_received(:changes)) }
64
- it { is_expected.to(have_attributes(config: 'new fake config')) }
65
- it { is_expected.to(have_attributes(changes: [%w[from1 to1], %w[from2 to2]])) }
128
+ before do
129
+ refchanger.replace_refdef("....\n")
130
+ refchanger.replace_refdef('[define, whatever, TEMP]')
131
+ refchanger.replace_refdef("....\n")
132
+ end
133
+
134
+ it do
135
+ expect(refchanger.replace_refdef('[define, whatever, TEMP]'))
136
+ .to(eq('[define, whatever, TUTU]'))
137
+ end
138
+ end
139
+ end
140
+
141
+ describe '#replace_irefs' do
142
+ subject(:refchanger) do
143
+ described_class.new(
144
+ from_regexp: '(?<cg>TOTO|TITI)-TEMP[\d]*',
145
+ to_template: '%<cg>s-%<next_ref>04d',
146
+ next_ref: 132
147
+ )
148
+ end
149
+
150
+ before do
151
+ refchanger.replace_refdef('[define, whatever, TOTO-TEMP123]')
152
+ refchanger.replace_refdef('[define, whatever, TITI-TEMP421]')
153
+ end
154
+
155
+ it do
156
+ expect(refchanger.replace_irefs('defs:iref[TOTO-TEMP1234]'))
157
+ .to(eq('defs:iref[TOTO-TEMP1234]'))
158
+ end
159
+
160
+ it do
161
+ expect(refchanger.replace_irefs('defs:iref[TOTO-TEMP123]'))
162
+ .to(eq('defs:iref[TOTO-0132]'))
163
+ end
164
+
165
+ it do
166
+ expect(
167
+ refchanger.replace_irefs(
168
+ 'defs:iref[TOTO-TEMP123] defs:iref[TITI-TEMP421] bla'
169
+ )
170
+ ).to(eq('defs:iref[TOTO-0132] defs:iref[TITI-0133] bla'))
171
+ end
172
+
173
+ it do
174
+ expect(
175
+ refchanger.replace_irefs(
176
+ 'defs:iref[TOTO-TEMP123] defs:iref[TOTO-TEMP123] bla'
177
+ )
178
+ ).to(eq('defs:iref[TOTO-0132] defs:iref[TOTO-0132] bla'))
179
+ end
180
+ end
181
+
182
+ describe '#replace' do
183
+ subject(:refchanger) do
184
+ described_class.new(
185
+ from_regexp: 'TEMP',
186
+ to_template: 'TUTU'
187
+ )
188
+ end
189
+
190
+ it do
191
+ expect(refchanger.replace(:refdef, '[define, whatever, TEMP]'))
192
+ .to(eq('[define, whatever, TUTU]'))
193
+ end
194
+
195
+ it do
196
+ expect(refchanger.replace(:refdef, '[define, whatever, TEMP(a~12345678)]'))
197
+ .to(eq('[define, whatever, TUTU(a~12345678)]'))
198
+ end
199
+
200
+ it do
201
+ expect(refchanger.replace(:refdef, '[define, whatever, TEMP(~12345678)]'))
202
+ .to(eq('[define, whatever, TUTU(~12345678)]'))
203
+ end
204
+
205
+ it do
206
+ expect(refchanger.replace(:refdef, '[define, whatever, TEMP(a)]'))
207
+ .to(eq('[define, whatever, TUTU(a)]'))
208
+ end
209
+
210
+ it do
211
+ refchanger.replace(:refdef, '[define, whatever, TEMP]')
212
+ expect(refchanger.replace(:irefs, 'defs:iref[TEMP] defs:iref[TEMP]'))
213
+ .to(eq('defs:iref[TUTU] defs:iref[TUTU]'))
66
214
  end
67
215
 
68
216
  it do
69
- expected_adoc = {
70
- 'file1.adoc' => "new2 file1 line1\nnew2 file1 line2",
71
- 'file2.adoc' => "new2 file2 line1\nnew2 file2 line2"
72
- }
73
- expect(modifier.do_modifications(adoc_texts)).to(eq(expected_adoc))
217
+ expect { refchanger.replace(:pouet, 'whatever') }
218
+ .to(raise_error(NoMethodError))
74
219
  end
75
220
  end
76
221
  end