forked from covermymeds/rubocop-thread_safety
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Actualize
ThreadSafety::NewThread
cop specs
- Loading branch information
1 parent
72b8b4f
commit bb6cc37
Showing
1 changed file
with
53 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::ThreadSafety::NewThread, :config do | ||
let(:msg) { 'Avoid starting new threads.' } | ||
|
||
it 'registers an offense for starting a new thread' do | ||
expect_offense(<<~RUBY) | ||
Thread.new { do_work } | ||
^^^^^^^^^^ #{msg} | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for starting a new thread with positional arguments' do | ||
expect_offense(<<~RUBY) | ||
Thread.new(1) { do_work } | ||
^^^^^^^^^^^^^ #{msg} | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for starting a new thread with keyword arguments' do | ||
expect_offense(<<~RUBY) | ||
Thread.new(a: 42) { do_work } | ||
^^^^^^^^^^^^^^^^^ #{msg} | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for starting a new thread with `fork` method' do | ||
expect_offense(<<~RUBY) | ||
Thread.fork { do_work } | ||
^^^^^^^^^^^ #{msg} | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for starting a new thread with `start` method' do | ||
expect_offense(<<~RUBY) | ||
Thread.start { do_work } | ||
^^^^^^^^^^^^ #{msg} | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for starting a new thread with top-level constant' do | ||
expect_offense(<<~RUBY) | ||
::Thread.new { do_work } | ||
^^^^^^^^^^^^ #{msg} | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for starting a new thread with safe navigation' do | ||
expect_offense(<<~RUBY) | ||
Thread&.new { do_work } | ||
^^^^^^^^^^^ #{msg} | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for calling new on other classes' do | ||
expect_no_offenses('Other.new { do_work }') | ||
%w[new fork start].each do |method_name| | ||
it "registers an offense for `#{Thread}.#{method_name}`" do | ||
expect_offense(<<~RUBY, method_name: method_name) | ||
Thread.%{method_name} { do_work } | ||
^^^^^^^^{method_name} Avoid starting new threads. | ||
RUBY | ||
end | ||
|
||
it "registers an offense for `#{Thread}.#{method_name}` with positional arguments" do | ||
expect_offense(<<~RUBY, method_name: method_name) | ||
Thread.%{method_name}(1) { do_work } | ||
^^^^^^^^{method_name}^^^ Avoid starting new threads. | ||
RUBY | ||
end | ||
|
||
it "registers an offense for `#{Thread}.#{method_name}` with keyword arguments" do | ||
expect_offense(<<~RUBY, method_name: method_name) | ||
Thread.%{method_name}(a: 42) { do_work } | ||
^^^^^^^^{method_name}^^^^^^^ Avoid starting new threads. | ||
RUBY | ||
end | ||
|
||
it "registers an offense for `#{Thread}.#{method_name}` with fully qualified constant name" do | ||
expect_offense(<<~RUBY, method_name: method_name) | ||
::Thread.%{method_name}(a: 42) { do_work } | ||
^^^^^^^^^^{method_name}^^^^^^^ Avoid starting new threads. | ||
RUBY | ||
end | ||
|
||
it "registers an offense for `#{Thread}.#{method_name}` with safe navigation" do | ||
expect_offense(<<~RUBY, method_name: method_name) | ||
Thread&.%{method_name}(a: 42) { do_work } | ||
^^^^^^^^^{method_name}^^^^^^^ Avoid starting new threads. | ||
RUBY | ||
end | ||
|
||
it "registers an offense for `#{Thread}.#{method_name}` with block argument" do | ||
expect_offense(<<~RUBY, method_name: method_name) | ||
Thread&.%{method_name}(&block) | ||
^^^^^^^^^{method_name}^^^^^^^^ Avoid starting new threads. | ||
RUBY | ||
end | ||
|
||
it "registers an offense for `#{Thread}.#{method_name}` with block and other arguments" do | ||
expect_offense(<<~RUBY, method_name: method_name) | ||
Thread&.%{method_name}(1, a: 42, &block) | ||
^^^^^^^^^{method_name}^^^^^^^^^^^^^^^^^^ Avoid starting new threads. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for unrelated receiver' do | ||
expect_no_offenses("Other.#{method_name} { do_work }") | ||
end | ||
end | ||
end |