Sha256: 438288874178d1806a94823d390ec81203055b4663046fa7c952b1f8a4d375c7

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Discourse::Plugins::NamespaceMethods, :config do
  subject(:cop) { described_class.new(config) }

  let(:config) { RuboCop::Config.new }

  context "when defining a method outside any namespace" do
    it "registers an offense" do
      expect_offense(<<~RUBY)
        def my_method
        ^^^^^^^^^^^^^ Discourse/Plugins/NamespaceMethods: Don’t define methods outside a class or a module.
          "my_value"
        end

        class MyClass
          def my_method
            "my_method"
          end
        end
      RUBY
    end
  end

  context "when defining a method inside a class" do
    context "when defining an instance method" do
      it "does not register an offense" do
        expect_no_offenses(<<~RUBY)
          class MyClass
            def my_method
              "my_value"
            end
          end
        RUBY
      end
    end

    context "when defining a class method" do
      it "does not register an offense" do
        expect_no_offenses(<<~RUBY)
          class MyClass
            class << self
              def my_method
                "my_value"
              end

              def another_method
                "plop"
              end
            end
          end
        RUBY
      end
    end
  end

  context "when defining a method inside a module" do
    context "when defining an instance method" do
      it "does not register an offense" do
        expect_no_offenses(<<~RUBY)
          module MyModule
            def my_method
              "my_value"
            end
          end
        RUBY
      end
    end

    context "when defining a class method" do
      it "does not register an offense" do
        expect_no_offenses(<<~RUBY)
          module MyModule
            class << self
              def my_method
                "my_value"
              end
            end
          end
        RUBY
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-discourse-3.9.3 spec/lib/rubocop/cop/plugins/namespace_methods_spec.rb
rubocop-discourse-3.9.2 spec/lib/rubocop/cop/plugins/namespace_methods_spec.rb