From d8cae2170fb7212a89ab012054d15eae6562c138 Mon Sep 17 00:00:00 2001 From: Peter Leitzen Date: Thu, 12 Aug 2021 17:37:09 +0200 Subject: [PATCH] MutableConstant: Extract string literal examples --- .../cop/style/mutable_constant_spec.rb | 102 ++++++++---------- .../cop/style/redundant_freeze_spec.rb | 2 +- 2 files changed, 46 insertions(+), 58 deletions(-) diff --git a/spec/rubocop/cop/style/mutable_constant_spec.rb b/spec/rubocop/cop/style/mutable_constant_spec.rb index 4c60c24f3ba..4c7cec74e4f 100644 --- a/spec/rubocop/cop/style/mutable_constant_spec.rb +++ b/spec/rubocop/cop/style/mutable_constant_spec.rb @@ -80,6 +80,49 @@ it_behaves_like 'immutable objects', "::ENV['foo']" end + shared_examples 'string literal' do + # TODO : It is not yet decided when frozen string will be the default. + # It has been abandoned in the Ruby 3.0 period, but may default in + # the long run. So these tests are left with a provisional value of 4.0. + if RuboCop::TargetRuby.supported_versions.include?(4.0) + context 'when the target ruby version >= 4.0' do + let(:ruby_version) { 4.0 } + + context 'when the frozen string literal comment is missing' do + it_behaves_like 'immutable objects', '"#{a}"' + end + + context 'when the frozen string literal comment is true' do + let(:prefix) { '# frozen_string_literal: true' } + + it_behaves_like 'immutable objects', '"#{a}"' + end + + context 'when the frozen string literal comment is false' do + let(:prefix) { '# frozen_string_literal: false' } + + it_behaves_like 'immutable objects', '"#{a}"' + end + end + end + + context 'when the frozen string literal comment is missing' do + it_behaves_like 'mutable objects', '"#{a}"' + end + + context 'when the frozen string literal comment is true' do + let(:prefix) { '# frozen_string_literal: true' } + + it_behaves_like 'immutable objects', '"#{a}"' + end + + context 'when the frozen string literal comment is false' do + let(:prefix) { '# frozen_string_literal: false' } + + it_behaves_like 'mutable objects', '"#{a}"' + end + end + context 'Strict: false' do let(:cop_config) { { 'EnforcedStyle' => 'literals' } } @@ -294,48 +337,7 @@ end end - context 'when the constant is a frozen string literal' do - # TODO : It is not yet decided when frozen string will be the default. - # It has been abandoned in the Ruby 3.0 period, but may default in - # the long run. So these tests are left with a provisional value of 4.0. - if RuboCop::TargetRuby.supported_versions.include?(4.0) - context 'when the target ruby version >= 4.0' do - let(:ruby_version) { 4.0 } - - context 'when the frozen string literal comment is missing' do - it_behaves_like 'immutable objects', '"#{a}"' - end - - context 'when the frozen string literal comment is true' do - let(:prefix) { '# frozen_string_literal: true' } - - it_behaves_like 'immutable objects', '"#{a}"' - end - - context 'when the frozen string literal comment is false' do - let(:prefix) { '# frozen_string_literal: false' } - - it_behaves_like 'immutable objects', '"#{a}"' - end - end - end - - context 'when the frozen string literal comment is missing' do - it_behaves_like 'mutable objects', '"#{a}"' - end - - context 'when the frozen string literal comment is true' do - let(:prefix) { '# frozen_string_literal: true' } - - it_behaves_like 'immutable objects', '"#{a}"' - end - - context 'when the frozen string literal comment is false' do - let(:prefix) { '# frozen_string_literal: false' } - - it_behaves_like 'mutable objects', '"#{a}"' - end - end + it_behaves_like 'string literal' end context 'Strict: true' do @@ -558,20 +560,6 @@ def assignment? RUBY end - context 'when the frozen string literal comment is missing' do - it_behaves_like 'mutable objects', '"#{a}"' - end - - context 'when the frozen string literal comment is true' do - let(:prefix) { '# frozen_string_literal: true' } - - it_behaves_like 'immutable objects', '"#{a}"' - end - - context 'when the frozen string literal comment is false' do - let(:prefix) { '# frozen_string_literal: false' } - - it_behaves_like 'mutable objects', '"#{a}"' - end + it_behaves_like 'string literal' end end diff --git a/spec/rubocop/cop/style/redundant_freeze_spec.rb b/spec/rubocop/cop/style/redundant_freeze_spec.rb index df624241a96..9595dc434f2 100644 --- a/spec/rubocop/cop/style/redundant_freeze_spec.rb +++ b/spec/rubocop/cop/style/redundant_freeze_spec.rb @@ -49,7 +49,7 @@ expect_no_offenses('TOP_TEST = Something.new.freeze') end - context 'when the receiver is a frozen string literal' do + context 'when the receiver is a string literal' do # TODO : It is not yet decided when frozen string will be the default. # It has been abandoned in the Ruby 3.0 period, but may default in # the long run. So these tests are left with a provisional value of 4.0.