Skip to content

Commit

Permalink
Added greater/less than spec filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Henrich committed Apr 13, 2015
1 parent c98f58c commit 93d4c12
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 60 deletions.
1 change: 0 additions & 1 deletion lib/rubocop/cop/style/hash_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ def word_symbol_pair?(pair)
end

def valid_19_syntax_symbol?(sym_name)
return false if sym_name =~ /\A:["']/
sym_name.sub!(/\A:/, '')
RuboCop::ProcessedSource.new("{ #{sym_name}: :foo }").valid_syntax?
end
Expand Down
167 changes: 108 additions & 59 deletions spec/rubocop/cop/style/hash_syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@
expect(cop.messages).to be_empty
end

it 'accepts hash rockets when symbol keys have string in them' do
inspect_source(cop, 'x = { :"string" => 0 }')
expect(cop.messages).to be_empty
context 'ruby < 2.2', ruby_less_than: 2.2 do
it 'accepts hash rockets when symbol keys have string in them' do
inspect_source(cop, 'x = { :"string" => 0 }')
expect(cop.messages).to be_empty
end
end

context 'ruby >= 2.2', ruby_greater_than_or_equal: 2.2 do
it 'registers an offense when symbol keys have strings in them' do
inspect_source(cop, 'x = { :"string" => 0 }')
expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.'])
end
end

it 'accepts hash rockets when symbol keys end with =' do
Expand Down Expand Up @@ -283,37 +292,57 @@
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'accepts hash rockets when keys have whitespaces in them' do
inspect_source(cop, 'x = { :"t o" => 0, :b => 1 }')
expect(cop.messages).to be_empty
end
context 'ruby < 2.2', ruby_less_than: 2.2 do
it 'accepts hash rockets when keys have whitespaces in them' do
inspect_source(cop, 'x = { :"t o" => 0, :b => 1 }')
expect(cop.messages).to be_empty
end

it 'registers an offense when keys have whitespaces and mix styles' do
inspect_source(cop, 'x = { :"t o" => 0, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end
it 'registers an offense when keys have whitespaces and mix styles' do
inspect_source(cop, 'x = { :"t o" => 0, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'accepts hash rockets when keys have special symbols in them' do
inspect_source(cop, 'x = { :"\tab" => 1, :b => 1 }')
expect(cop.messages).to be_empty
end
it 'accepts hash rockets when keys have special symbols in them' do
inspect_source(cop, 'x = { :"\tab" => 1, :b => 1 }')
expect(cop.messages).to be_empty
end

it 'registers an offense when keys have special symbols and mix styles' do
inspect_source(cop, 'x = { :"\tab" => 1, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end
it 'registers an offense when keys have special symbols and '\
'mix styles' do
inspect_source(cop, 'x = { :"\tab" => 1, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'accepts hash rockets when keys start with a digit' do
inspect_source(cop, 'x = { :"1" => 1, :b => 1 }')
expect(cop.messages).to be_empty
it 'accepts hash rockets when keys start with a digit' do
inspect_source(cop, 'x = { :"1" => 1, :b => 1 }')
expect(cop.messages).to be_empty
end

it 'registers an offense when keys start with a digit and mix styles' do
inspect_source(cop, 'x = { :"1" => 1, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end
end

it 'registers an offense when keys start with a digit and mix styles' do
inspect_source(cop, 'x = { :"1" => 1, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
context 'ruby >= 2.2', ruby_greater_than_or_equal: 2.2 do
it 'registers an offense when keys have whitespaces in them' do
inspect_source(cop, 'x = { :"t o" => 0 }')
expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.'])
end

it 'registers an offense when keys have special symbols in them' do
inspect_source(cop, 'x = { :"\tab" => 1 }')
expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.'])
end

it 'accepts hash rockets when keys start with a digit' do
inspect_source(cop, 'x = { :"1" => 1 }')
expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.'])
end
end

it 'auto-corrects old to new style' do
Expand Down Expand Up @@ -399,37 +428,57 @@
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'accepts hash rockets when keys have whitespaces in them' do
inspect_source(cop, 'x = { :"t o" => 0, :b => 1 }')
expect(cop.messages).to be_empty
end

it 'registers an offense when keys have whitespaces and mix styles' do
inspect_source(cop, 'x = { :"t o" => 0, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'accepts hash rockets when keys have special symbols in them' do
inspect_source(cop, 'x = { :"\tab" => 1, :b => 1 }')
expect(cop.messages).to be_empty
end

it 'registers an offense when keys have special symbols and mix styles' do
inspect_source(cop, 'x = { :"\tab" => 1, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'accepts hash rockets when keys start with a digit' do
inspect_source(cop, 'x = { :"1" => 1, :b => 1 }')
expect(cop.messages).to be_empty
end

it 'registers an offense when keys start with a digit and mix styles' do
inspect_source(cop, 'x = { :"1" => 1, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
context 'ruby < 2.2', ruby_less_than: 2.2 do
it 'accepts hash rockets when keys have whitespaces in them' do
inspect_source(cop, 'x = { :"t o" => 0, :b => 1 }')
expect(cop.messages).to be_empty
end

it 'registers an offense when keys have whitespaces and mix styles' do
inspect_source(cop, 'x = { :"t o" => 0, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'accepts hash rockets when keys have special symbols in them' do
inspect_source(cop, 'x = { :"\tab" => 1, :b => 1 }')
expect(cop.messages).to be_empty
end

it 'registers an offense when keys have special symbols and ' \
'mix styles' do
inspect_source(cop, 'x = { :"\tab" => 1, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'accepts hash rockets when keys start with a digit' do
inspect_source(cop, 'x = { :"1" => 1, :b => 1 }')
expect(cop.messages).to be_empty
end

it 'registers an offense when keys start with a digit and mix styles' do
inspect_source(cop, 'x = { :"1" => 1, b: 1 }')
expect(cop.messages).to eq(["Don't mix styles in the same hash."])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end
end

context 'ruby >= 2.2', ruby_greater_than_or_equal: 2.2 do
it 'registers an offense when keys have whitespaces in them' do
inspect_source(cop, 'x = { :"t o" => 0 }')
expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.'])
end

it 'registers an offense when keys have special symbols in them' do
inspect_source(cop, 'x = { :"\tab" => 1 }')
expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.'])
end

it 'accepts hash rockets when keys start with a digit' do
inspect_source(cop, 'x = { :"1" => 1 }')
expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.'])
end
end

it 'auto-corrects old to new style' do
Expand Down
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@

config.filter_run_excluding ruby: ->(v) { !RUBY_VERSION.start_with?(v.to_s) }

ruby_version = Gem::Version.new(RUBY_VERSION)
config.filter_run_excluding ruby_less_than: (lambda do |v|
ruby_version >= Gem::Version.new(v)
end)
config.filter_run_excluding ruby_less_than: (lambda do |v|
ruby_version >= Gem::Version.new(v)
end)
config.filter_run_excluding ruby_greater_than_or_equal: (lambda do |v|
ruby_version < Gem::Version.new(v)
end)
config.filter_run_excluding ruby_greater_than_or_equal: (lambda do |v|
ruby_version < Gem::Version.new(v)
end)

broken_filter = lambda do |v|
v.is_a?(Symbol) ? RUBY_ENGINE == v.to_s : v
end
Expand Down

0 comments on commit 93d4c12

Please sign in to comment.