Sha256: 81e8eb3e9f6a5c4dd0da80e2d8db9429109a32aff4ed429f252c1d53ce2ea841

Contents?: true

Size: 1.84 KB

Versions: 32

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

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

module Rubocop
  class MetricReturnValueTest < Minitest::Test
    include RubocopHelper

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

    def test_ok_for_non_metric_method
      assert_no_offenses("backend = StatsD.backend")
    end

    def test_ok_as_naked_statement
      assert_no_offenses("StatsD.increment('foo')")
      assert_no_offenses("StatsD.measure('foo') { foo }")
    end

    def test_ok_as_multiple_statement
      assert_no_offenses(<<~RUBY)
        StatsD.increment 'foo'
        StatsD.increment 'bar'
      RUBY
    end

    def test_ok_inside_block
      assert_no_offenses(<<~RUBY)
        block do
          StatsD.measure
        end
      RUBY
    end

    def test_ok_when_passing_a_block_as_param
      assert_no_offenses("block_result = StatsD.measure('foo', &block)")
    end

    def test_ok_when_passing_a_curly_braces_block
      assert_no_offenses("block_result = StatsD.measure('foo') { measure_me }")
    end

    def test_ok_when_passing_do_end_block
      assert_no_offenses(<<~RUBY)
        block_result = StatsD.measure('foo') do
          return_something_useful
        end
      RUBY
    end

    def test_offense_in_assignment
      assert_offense("metric = StatsD.increment('foo')")
    end

    def test_offense_in_multi_assignment
      assert_offense("foo, metric = bar, StatsD.increment('foo')")
    end

    def test_offense_in_hash
      assert_offense("{ metric: StatsD.increment('foo') }")
    end

    def test_offense_in_method_call
      assert_offense("process(StatsD.increment('foo'))")
    end

    def test_offense_when_returning
      assert_offense("return StatsD.increment('foo')")
    end

    def test_offense_when_yielding
      assert_offense("yield StatsD.increment('foo')")
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

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