|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Rails::EnumHash do |
| 4 | + subject(:cop) { described_class.new(config) } |
| 5 | + |
| 6 | + let(:config) { RuboCop::Config.new } |
| 7 | + |
| 8 | + context 'when array syntax is used' do |
| 9 | + context 'with %i[] syntax' do |
| 10 | + it 'registers an offense' do |
| 11 | + expect_offense(<<~RUBY) |
| 12 | + enum status: %i[active archived] |
| 13 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead. |
| 14 | + RUBY |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + context 'with %w[] syntax' do |
| 19 | + it 'registers an offense' do |
| 20 | + expect_offense(<<~RUBY) |
| 21 | + enum status: %w[active archived] |
| 22 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead. |
| 23 | + RUBY |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + context 'with %i() syntax' do |
| 28 | + it 'registers an offense' do |
| 29 | + expect_offense(<<~RUBY) |
| 30 | + enum status: %i(active archived) |
| 31 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead. |
| 32 | + RUBY |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + context 'with %w() syntax' do |
| 37 | + it 'registers an offense' do |
| 38 | + expect_offense(<<~RUBY) |
| 39 | + enum status: %w(active archived) |
| 40 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead. |
| 41 | + RUBY |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + context 'with [] syntax' do |
| 46 | + it 'registers an offense' do |
| 47 | + expect_offense(<<~RUBY) |
| 48 | + enum status: [:active, :archived] |
| 49 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead. |
| 50 | + RUBY |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'when hash syntax is used' do |
| 56 | + it 'does not register an offense' do |
| 57 | + expect_no_offenses('enum status: { active: 0, archived: 1 }') |
| 58 | + end |
| 59 | + end |
| 60 | +end |
0 commit comments