|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Rails::OrderArguments, :config do |
| 4 | + it 'registers an offense for `order` with a string argument' do |
| 5 | + expect_offense(<<~RUBY) |
| 6 | + User.order('first_name') |
| 7 | + ^^^^^^^^^^^^ Prefer `:first_name` instead. |
| 8 | + RUBY |
| 9 | + |
| 10 | + expect_correction(<<~RUBY) |
| 11 | + User.order(:first_name) |
| 12 | + RUBY |
| 13 | + end |
| 14 | + |
| 15 | + it 'registers an offense for `order` with multiple string arguments' do |
| 16 | + expect_offense(<<~RUBY) |
| 17 | + User.order('first_name', 'last_name') |
| 18 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `:first_name, :last_name` instead. |
| 19 | + RUBY |
| 20 | + |
| 21 | + expect_correction(<<~RUBY) |
| 22 | + User.order(:first_name, :last_name) |
| 23 | + RUBY |
| 24 | + end |
| 25 | + |
| 26 | + it 'registers an offense for `order` with a string argument with DESC direction' do |
| 27 | + expect_offense(<<~RUBY) |
| 28 | + User.order('name DESC') |
| 29 | + ^^^^^^^^^^^ Prefer `name: :desc` instead. |
| 30 | + RUBY |
| 31 | + |
| 32 | + expect_correction(<<~RUBY) |
| 33 | + User.order(name: :desc) |
| 34 | + RUBY |
| 35 | + end |
| 36 | + |
| 37 | + it 'registers an offense for `order` with a string argument with ASC order' do |
| 38 | + expect_offense(<<~RUBY) |
| 39 | + User.order('name ASC') |
| 40 | + ^^^^^^^^^^ Prefer `:name` instead. |
| 41 | + RUBY |
| 42 | + |
| 43 | + expect_correction(<<~RUBY) |
| 44 | + User.order(:name) |
| 45 | + RUBY |
| 46 | + end |
| 47 | + |
| 48 | + it 'registers an offense for `order` with DESC column followed by implicit ASC column' do |
| 49 | + expect_offense(<<~RUBY) |
| 50 | + User.order('first_name DESC, last_name') |
| 51 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `first_name: :desc, last_name: :asc` instead. |
| 52 | + RUBY |
| 53 | + |
| 54 | + expect_correction(<<~RUBY) |
| 55 | + User.order(first_name: :desc, last_name: :asc) |
| 56 | + RUBY |
| 57 | + end |
| 58 | + |
| 59 | + it 'registers an offense for `order` with explicit ASC column followed by implicit ASC column' do |
| 60 | + expect_offense(<<~RUBY) |
| 61 | + User.order('first_name ASC, last_name') |
| 62 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `:first_name, :last_name` instead. |
| 63 | + RUBY |
| 64 | + |
| 65 | + expect_correction(<<~RUBY) |
| 66 | + User.order(:first_name, :last_name) |
| 67 | + RUBY |
| 68 | + end |
| 69 | + |
| 70 | + it 'registers an offense for safe navigation `order` with a string argument' do |
| 71 | + expect_offense(<<~RUBY) |
| 72 | + User&.order('first_name') |
| 73 | + ^^^^^^^^^^^^ Prefer `:first_name` instead. |
| 74 | + RUBY |
| 75 | + |
| 76 | + expect_correction(<<~RUBY) |
| 77 | + User&.order(:first_name) |
| 78 | + RUBY |
| 79 | + end |
| 80 | + |
| 81 | + it 'registers an offense for `order` with multiple string arguments, one of which has multiple sorts' do |
| 82 | + expect_offense(<<~RUBY) |
| 83 | + User.order('first_name, middle_name', 'last_name') |
| 84 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `:first_name, :middle_name, :last_name` instead. |
| 85 | + RUBY |
| 86 | + |
| 87 | + expect_correction(<<~RUBY) |
| 88 | + User.order(:first_name, :middle_name, :last_name) |
| 89 | + RUBY |
| 90 | + end |
| 91 | + |
| 92 | + it 'registers an offense for `order` with lowercase direction' do |
| 93 | + expect_offense(<<~RUBY) |
| 94 | + User.order('name desc') |
| 95 | + ^^^^^^^^^^^ Prefer `name: :desc` instead. |
| 96 | + RUBY |
| 97 | + |
| 98 | + expect_correction(<<~RUBY) |
| 99 | + User.order(name: :desc) |
| 100 | + RUBY |
| 101 | + end |
| 102 | + |
| 103 | + it 'registers an offense for `order` with uppercase column' do |
| 104 | + expect_offense(<<~RUBY) |
| 105 | + User.order('NAME DESC') |
| 106 | + ^^^^^^^^^^^ Prefer `name: :desc` instead. |
| 107 | + RUBY |
| 108 | + |
| 109 | + expect_correction(<<~RUBY) |
| 110 | + User.order(name: :desc) |
| 111 | + RUBY |
| 112 | + end |
| 113 | + |
| 114 | + it 'does not register an offense for `order` with symbol argument' do |
| 115 | + expect_no_offenses(<<~RUBY) |
| 116 | + User.order(:first_name) |
| 117 | + RUBY |
| 118 | + end |
| 119 | + |
| 120 | + it 'does not register an offense for `order` with a string and a symbol argument' do |
| 121 | + expect_no_offenses(<<~RUBY) |
| 122 | + User.order(:first_name, 'last_name') |
| 123 | + RUBY |
| 124 | + end |
| 125 | + |
| 126 | + it 'does not register an offense for `order` with string argument expression' do |
| 127 | + expect_no_offenses(<<~RUBY) |
| 128 | + User.order('LEFT(first_name, 1)') |
| 129 | + RUBY |
| 130 | + end |
| 131 | +end |
0 commit comments