Skip to content

Commit

Permalink
Remove redundant constants from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum committed Sep 18, 2015
1 parent dddad34 commit ba8eb6b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
24 changes: 10 additions & 14 deletions spec/rubocop/cop/style/mutable_constant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
describe RuboCop::Cop::Style::MutableConstant do
subject(:cop) { described_class.new }

MUTABLE_OBJECTS = [
'[1, 2, 3]',
'{ a: 1, b: 2 }',
"'str'",
'"top#{1 + 2}"'
]

MUTABLE_OBJECTS.each do |o|
shared_examples :mutable_objects do |o|
it "registers an offense for #{o} assigned to a constant" do
inspect_source(cop, "CONST = #{o}")
expect(cop.offenses.size).to eq(1)
Expand All @@ -24,19 +17,22 @@
end
end

IMMUTABLE_OBJECTS = [
'1',
'1.5',
':sym'
]
it_behaves_like :mutable_objects, '[1, 2, 3]'
it_behaves_like :mutable_objects, '{ a: 1, b: 2 }'
it_behaves_like :mutable_objects, "'str'"
it_behaves_like :mutable_objects, '"top#{1 + 2}"'

IMMUTABLE_OBJECTS.each do |o|
shared_examples :immutable_objects do |o|
it "allows #{o} to be assigned to a constant" do
inspect_source(cop, "CONST = #{o}")
expect(cop.offenses).to be_empty
end
end

it_behaves_like :immutable_objects, '1'
it_behaves_like :immutable_objects, '1.5'
it_behaves_like :immutable_objects, ':sym'

it 'allows method call assignments' do
inspect_source(cop, 'TOP_TEST = Something.new')
expect(cop.offenses).to be_empty
Expand Down
28 changes: 11 additions & 17 deletions spec/rubocop/cop/style/redundant_freeze_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,7 @@
describe RuboCop::Cop::Style::RedundantFreeze do
subject(:cop) { described_class.new }

# TODO: Turns out RSpec defines all constants in the same namespace.
# I guess we should remove all usages of constants from out specs.
MUTABLE_OBJECTS = [
'[1, 2, 3]',
'{ a: 1, b: 2 }',
"'str'",
'"top#{1 + 2}"'
]

IMMUTABLE_OBJECTS = [
'1',
'1.5',
':sym'
]

IMMUTABLE_OBJECTS.each do |o|
shared_examples :immutable_objects do |o|
it "registers an offense for frozen #{o}" do
inspect_source(cop, "CONST = #{o}.freeze")
expect(cop.offenses.size).to eq(1)
Expand All @@ -32,13 +17,22 @@
end
end

MUTABLE_OBJECTS.each do |o|
it_behaves_like :immutable_objects, '1'
it_behaves_like :immutable_objects, '1.5'
it_behaves_like :immutable_objects, ':sym'

shared_examples :mutable_objects do |o|
it "allows #{o} with freeze" do
inspect_source(cop, "CONST = #{o}.freeze")
expect(cop.offenses).to be_empty
end
end

it_behaves_like :mutable_objects, '[1, 2, 3]'
it_behaves_like :mutable_objects, '{ a: 1, b: 2 }'
it_behaves_like :mutable_objects, "'str'"
it_behaves_like :mutable_objects, '"top#{1 + 2}"'

it 'allows .freeze on method call' do
inspect_source(cop, 'TOP_TEST = Something.new.freeze')
expect(cop.offenses).to be_empty
Expand Down

0 comments on commit ba8eb6b

Please sign in to comment.