Sha256: 28f787722890a45622122de2be25a285081aa3fc505832d733f38a31c41e1c8a

Contents?: true

Size: 1.91 KB

Versions: 34

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require "test_helper"
require "statsd/instrument/rubocop"

module Rubocop
  class MetaprogrammingPositionalArgumentsTest < Minitest::Test
    include RubocopHelper

    def setup
      @cop = RuboCop::Cop::StatsD::MetaprogrammingPositionalArguments.new
    end

    def test_ok_with_two_arguments
      assert_no_offenses("ClassName.statsd_count_if(:method, 'metric') { foo }")
      assert_no_offenses("ClassName.statsd_measure :method, 'metric'")
      assert_no_offenses(<<~RUBY)
        class Foo
          statsd_count :method, 'metric'
        end
      RUBY
    end

    def test_ok_with_keyword_arguments_and_blocks
      assert_no_offenses("ClassName.statsd_measure :method, 'metric', foo: 'bar'")
      assert_no_offenses("ClassName.statsd_count_success(:method, 'metric', **kwargs)")
      assert_no_offenses("ClassName.statsd_measure(:method, 'metric', foo: 'bar', &block)")
      assert_no_offenses(<<~RUBY)
        class Foo
          statsd_count_if(:method, 'metric', foo: 'bar', baz: 'quc') do |result|
            result == 'ok'
          end
        end
      RUBY
    end

    def test_offense_with_positional_arguments
      assert_offense("ClassName.statsd_measure(:method, 'metric', 1)")
      assert_offense("ClassName.statsd_measure(:method, 'metric', 1, ['tag'])")
      assert_offense("ClassName.statsd_measure(:method, 'metric', 1, tag: 'value')")
      assert_offense(<<~RUBY)
        class Foo
          extend StatsD::Instrument
          statsd_measure(:method, 'metric', 1)
        end
      RUBY
    end

    def test_offense_with_splat
      assert_offense("ClassName.statsd_measure(:method, 'metric', *options)")
    end

    def test_offense_with_constant_or_method_as_third_argument
      assert_offense("ClassName.statsd_measure(:method, 'metric', SAMPLE_RATE_CONSTANT)")
      assert_offense("ClassName.statsd_measure(:method, 'metric', method_returning_a_hash)")
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
statsd-instrument-3.9.9 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.9.8 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.9.7 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.9.6 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.9.5 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.9.4 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.9.3 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.9.2 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.9.1 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.9.0 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.8.0 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.7.0 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.6.1 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.6.0 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.5.12 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.5.11 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.5.10 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.5.9 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.5.8 test/rubocop/metaprogramming_positional_arguments_test.rb
statsd-instrument-3.5.7 test/rubocop/metaprogramming_positional_arguments_test.rb