|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Performance::MapMethodChain, :config do |
| 4 | + it 'registers an offense when using `map` method chain and receiver is a method call' do |
| 5 | + expect_offense(<<~RUBY) |
| 6 | + array.map(&:foo).map(&:bar) |
| 7 | + ^^^^^^^^^^^^^^^^^^^^^ Use `map { |x| x.foo.bar }` instead of `map` method chain. |
| 8 | + RUBY |
| 9 | + end |
| 10 | + |
| 11 | + it 'registers an offense when using `collect` method chain and receiver is a method call' do |
| 12 | + expect_offense(<<~RUBY) |
| 13 | + array.collect(&:foo).collect(&:bar) |
| 14 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `collect { |x| x.foo.bar }` instead of `collect` method chain. |
| 15 | + RUBY |
| 16 | + end |
| 17 | + |
| 18 | + it 'registers an offense when using `map` and `collect` method chain and receiver is a method call' do |
| 19 | + expect_offense(<<~RUBY) |
| 20 | + array.map(&:foo).collect(&:bar) |
| 21 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Use `map { |x| x.foo.bar }` instead of `map` method chain. |
| 22 | + RUBY |
| 23 | + end |
| 24 | + |
| 25 | + it 'registers an offense when using `map` method chain and receiver is a variable' do |
| 26 | + expect_offense(<<~RUBY) |
| 27 | + array = create_array |
| 28 | + array.map(&:foo).map(&:bar) |
| 29 | + ^^^^^^^^^^^^^^^^^^^^^ Use `map { |x| x.foo.bar }` instead of `map` method chain. |
| 30 | + RUBY |
| 31 | + end |
| 32 | + |
| 33 | + it 'registers an offense when using `map` method chain repeated three times' do |
| 34 | + expect_offense(<<~RUBY) |
| 35 | + array.map(&:foo).map(&:bar).map(&:baz) |
| 36 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `map { |x| x.foo.bar.baz }` instead of `map` method chain. |
| 37 | + RUBY |
| 38 | + end |
| 39 | + |
| 40 | + it 'registers an offense when using `map` method chain repeated three times with safe navigation' do |
| 41 | + expect_offense(<<~RUBY) |
| 42 | + array&.map(&:foo).map(&:bar).map(&:baz) |
| 43 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `map { |x| x.foo.bar.baz }` instead of `map` method chain. |
| 44 | + RUBY |
| 45 | + end |
| 46 | + |
| 47 | + it 'does not register an offense when using `do_something` method chain and receiver is a method call' do |
| 48 | + expect_no_offenses(<<~RUBY) |
| 49 | + array.do_something(&:foo).do_something(&:bar) |
| 50 | + RUBY |
| 51 | + end |
| 52 | + |
| 53 | + it 'does not register an offense when there is a method call between `map` method chain' do |
| 54 | + expect_no_offenses(<<~RUBY) |
| 55 | + array.map(&:foo).do_something.map(&:bar) |
| 56 | + RUBY |
| 57 | + end |
| 58 | + |
| 59 | + it 'does not register an offense when using `flat_map` and `map` method chain and receiver is a method call' do |
| 60 | + expect_no_offenses(<<~RUBY) |
| 61 | + array.flat_map(&:foo).map(&:bar) |
| 62 | + RUBY |
| 63 | + end |
| 64 | + |
| 65 | + it 'does not register an offense when using `map(&:foo).join(', ')`' do |
| 66 | + expect_no_offenses(<<~RUBY) |
| 67 | + array = create_array |
| 68 | + array.map(&:foo).join(', ') |
| 69 | + RUBY |
| 70 | + end |
| 71 | + |
| 72 | + it 'does not register an offense when using `map(&:foo).join(', ')` with safe navigation' do |
| 73 | + expect_no_offenses(<<~RUBY) |
| 74 | + array = create_array |
| 75 | + array.map(&:foo).join(', ') |
| 76 | + RUBY |
| 77 | + end |
| 78 | +end |
0 commit comments