|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "./cop_test" |
| 4 | +require "minitest/autorun" |
| 5 | +require "rubocop/cop/github/avoid_object_send_with_dynamic_method" |
| 6 | + |
| 7 | +class TestAvoidObjectSendWithDynamicMethod < CopTest |
| 8 | + def cop_class |
| 9 | + RuboCop::Cop::GitHub::AvoidObjectSendWithDynamicMethod |
| 10 | + end |
| 11 | + |
| 12 | + def test_offended_by_send_call |
| 13 | + offenses = investigate cop, <<-RUBY |
| 14 | + def my_method(foo) |
| 15 | + foo.send(@some_ivar) |
| 16 | + end |
| 17 | + RUBY |
| 18 | + assert_equal 1, offenses.size |
| 19 | + assert_equal "Avoid using Object#send with a dynamic method name.", offenses.first.message |
| 20 | + end |
| 21 | + |
| 22 | + def test_offended_by_public_send_call |
| 23 | + offenses = investigate cop, <<-RUBY |
| 24 | + foo.public_send(bar) |
| 25 | + RUBY |
| 26 | + assert_equal 1, offenses.size |
| 27 | + assert_equal "Avoid using Object#public_send with a dynamic method name.", offenses.first.message |
| 28 | + end |
| 29 | + |
| 30 | + def test_offended_by_call_to___send__ |
| 31 | + offenses = investigate cop, <<-RUBY |
| 32 | + foo.__send__(bar) |
| 33 | + RUBY |
| 34 | + assert_equal 1, offenses.size |
| 35 | + assert_equal "Avoid using Object#__send__ with a dynamic method name.", offenses.first.message |
| 36 | + end |
| 37 | + |
| 38 | + def test_offended_by_send_calls_without_receiver |
| 39 | + offenses = investigate cop, <<-RUBY |
| 40 | + send(some_method) |
| 41 | + public_send(@some_ivar) |
| 42 | + __send__(a_variable, "foo", "bar") |
| 43 | + RUBY |
| 44 | + assert_equal 3, offenses.size |
| 45 | + assert_equal "Avoid using Object#send with a dynamic method name.", offenses[0].message |
| 46 | + assert_equal "Avoid using Object#public_send with a dynamic method name.", offenses[1].message |
| 47 | + assert_equal "Avoid using Object#__send__ with a dynamic method name.", offenses[2].message |
| 48 | + end |
| 49 | + |
| 50 | + def test_unoffended_by_other_method_calls |
| 51 | + offenses = investigate cop, <<-RUBY |
| 52 | + foo.bar(arg1, arg2) |
| 53 | + case @some_ivar |
| 54 | + when :foo |
| 55 | + baz.foo |
| 56 | + when :bar |
| 57 | + baz.bar |
| 58 | + end |
| 59 | + puts "public_send" if send? |
| 60 | + RUBY |
| 61 | + assert_equal 0, offenses.size |
| 62 | + end |
| 63 | + |
| 64 | + def test_unoffended_by_send_calls_to_dynamic_methods_that_include_hardcoded_strings |
| 65 | + offenses = investigate cop, <<-'RUBY' |
| 66 | + foo.send("can_#{action}?") |
| 67 | + foo.public_send("make_#{SOME_CONSTANT}") |
| 68 | + RUBY |
| 69 | + assert_equal 0, offenses.size |
| 70 | + end |
| 71 | + |
| 72 | + def test_unoffended_by_send_calls_without_dynamic_methods |
| 73 | + offenses = investigate cop, <<-RUBY |
| 74 | + base.send :extend, ClassMethods |
| 75 | + foo.public_send(:bar) |
| 76 | + foo.__send__("bar", arg1, arg2) |
| 77 | + RUBY |
| 78 | + assert_equal 0, offenses.size |
| 79 | + end |
| 80 | +end |
0 commit comments