Skip to content

Commit

Permalink
Merge pull request #887 from tmorris-fiksu/numeric_literals_auto-gen_…
Browse files Browse the repository at this point in the history
…config_off_by_one_884

[#884] Fix --auto-gen-config for `NumericLiterals` so MinDigits is correct.
  • Loading branch information
bbatsov committed Mar 15, 2014
2 parents d95fcf5 + 30cca76 commit d685e26
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Bugs fixed

* [#884](https://github.com/bbatsov/rubocop/issues/884): Fix --auto-gen-config for `NumericLiterals` so MinDigits is correct. ([@tmorris-fiksu][])
* [#879](https://github.com/bbatsov/rubocop/issues/879): Fix --auto-gen-config for `RegexpLiteral` so we don't generate illegal values for `MaxSlashes`. ([@jonas054][])
* Fix the name of the `Include` param in the default config of the Rails cops. ([@bbatsov][])
* [#878](https://github.com/bbatsov/rubocop/pull/878): Blacklist `Rakefile`, `Gemfile` and `Capfile` by default in the `FileName` cop. ([@bbatsov][])
Expand Down Expand Up @@ -766,3 +767,4 @@
[@hannestyden]: https://github.com/hannestyden
[@geniou]: https://github.com/geniou
[@jkogara]: https://github.com/jkogara
[@tmorris-fiksu]: https://github.com/tmorris-fiksu
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/numeric_literals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def check(node)
if int.size >= min_digits
case int
when /^\d+$/
add_offense(node, :expression) { self.max = int.size }
add_offense(node, :expression) { self.max = int.size + 1 }
when /\d{4}/, /_\d{1,2}_/
add_offense(node, :expression) do
self.config_to_allow_offenses = { 'Enabled' => false }
Expand Down
12 changes: 10 additions & 2 deletions spec/rubocop/cop/style/numeric_literals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
subject(:cop) { described_class.new(config) }
let(:cop_config) { { 'MinDigits' => 5 } }

it 'registers an offense for a long integer without underscores' do
inspect_source(cop, ['a = 123456'])
it 'registers an offense for a long undelimited integer' do
inspect_source(cop, ['a = 12345'])
expect(cop.offenses.size).to eq(1)
expect(cop.config_to_allow_offenses).to eq('MinDigits' => 6)
end

it 'registers an offense for a float with a long undelimited integer part' do
pending 'Though the offense message implies that floats are checked, ' \
'currently the cop only detects integers.'
inspect_source(cop, ['a = 123456.789'])
expect(cop.offenses.size).to eq(1)
expect(cop.config_to_allow_offenses).to eq('MinDigits' => 7)
end

it 'registers an offense for an integer with misplaced underscore' do
inspect_source(cop, ['a = 123_456_78_90_00'])
expect(cop.offenses.size).to eq(1)
Expand Down

0 comments on commit d685e26

Please sign in to comment.