Skip to content

Commit

Permalink
MutableConstant: Extract string literal examples
Browse files Browse the repository at this point in the history
  • Loading branch information
splattael committed Aug 23, 2021
1 parent 5a12fad commit d8cae21
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 58 deletions.
102 changes: 45 additions & 57 deletions spec/rubocop/cop/style/mutable_constant_spec.rb
Expand Up @@ -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' } }

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/rubocop/cop/style/redundant_freeze_spec.rb
Expand Up @@ -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.
Expand Down

0 comments on commit d8cae21

Please sign in to comment.