forked from rubocop/rubocop-rails
/
hash_enum_spec.rb
66 lines (57 loc) · 2.08 KB
/
hash_enum_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::HashEnum do
subject(:cop) { described_class.new(config) }
let(:config) { RuboCop::Config.new }
context 'when array syntax is used' do
context 'with %i[] syntax' do
it 'registers an offense' do
expect_offense(<<~RUBY)
enum status: %i[active archived]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead.
RUBY
end
end
context 'with %w[] syntax' do
it 'registers an offense' do
expect_offense(<<~RUBY)
enum status: %w[active archived]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead.
RUBY
end
end
context 'with %i() syntax' do
it 'registers an offense' do
expect_offense(<<~RUBY)
enum status: %i(active archived)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead.
RUBY
end
end
context 'with %w() syntax' do
it 'registers an offense' do
expect_offense(<<~RUBY)
enum status: %w(active archived)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead.
RUBY
end
end
context 'with [] syntax' do
it 'registers an offense' do
expect_offense(<<~RUBY)
enum status: [:active, :archived]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead.
RUBY
end
end
end
context 'when hash syntax is used' do
it 'does not register an offense' do
expect_no_offenses('enum status: { active: 0, archived: 1 }')
end
end
it 'autocorrects a enum with array syntax' do
expect(
autocorrect_source('enum status: [:active, "completely archived", 42]')
).to eq("enum status: { active: 0, 'completely archived' => 1, 42 => 2 }")
end
end