diff options
Diffstat (limited to 'ci/custom_cops/test')
-rw-r--r-- | ci/custom_cops/test/custom_cops/assert_not_test.rb | 42 | ||||
-rw-r--r-- | ci/custom_cops/test/custom_cops/refute_not_test.rb | 66 | ||||
-rw-r--r-- | ci/custom_cops/test/support/cop_helper.rb | 47 |
3 files changed, 0 insertions, 155 deletions
diff --git a/ci/custom_cops/test/custom_cops/assert_not_test.rb b/ci/custom_cops/test/custom_cops/assert_not_test.rb deleted file mode 100644 index abb151aeb4..0000000000 --- a/ci/custom_cops/test/custom_cops/assert_not_test.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -require "support/cop_helper" -require_relative "../../lib/custom_cops/assert_not" - -class AssertNotTest < ActiveSupport::TestCase - include CopHelper - - setup do - @cop = CustomCops::AssertNot.new - end - - test "rejects 'assert !'" do - inspect_source @cop, "assert !x" - assert_offense @cop, "^^^^^^^^^ Prefer `assert_not` over `assert !`" - end - - test "rejects 'assert !' with a complex value" do - inspect_source @cop, "assert !a.b(c)" - assert_offense @cop, "^^^^^^^^^^^^^^ Prefer `assert_not` over `assert !`" - end - - test "autocorrects `assert !`" do - corrected = autocorrect_source(@cop, "assert !false") - assert_equal "assert_not false", corrected - end - - test "autocorrects `assert !` with extra spaces" do - corrected = autocorrect_source(@cop, "assert ! false") - assert_equal "assert_not false", corrected - end - - test "autocorrects `assert !` with parentheses" do - corrected = autocorrect_source(@cop, "assert(!false)") - assert_equal "assert_not(false)", corrected - end - - test "accepts `assert_not`" do - inspect_source @cop, "assert_not x" - assert_empty @cop.offenses - end -end diff --git a/ci/custom_cops/test/custom_cops/refute_not_test.rb b/ci/custom_cops/test/custom_cops/refute_not_test.rb deleted file mode 100644 index f0f6eaeda0..0000000000 --- a/ci/custom_cops/test/custom_cops/refute_not_test.rb +++ /dev/null @@ -1,66 +0,0 @@ -# frozen_string_literal: true - -require "support/cop_helper" -require_relative "../../lib/custom_cops/refute_not" - -class RefuteNotTest < ActiveSupport::TestCase - include CopHelper - - setup do - @cop = CustomCops::RefuteNot.new - end - - { - refute: :assert_not, - refute_empty: :assert_not_empty, - refute_equal: :assert_not_equal, - refute_in_delta: :assert_not_in_delta, - refute_in_epsilon: :assert_not_in_epsilon, - refute_includes: :assert_not_includes, - refute_instance_of: :assert_not_instance_of, - refute_kind_of: :assert_not_kind_of, - refute_nil: :assert_not_nil, - refute_operator: :assert_not_operator, - refute_predicate: :assert_not_predicate, - refute_respond_to: :assert_not_respond_to, - refute_same: :assert_not_same, - refute_match: :assert_no_match - }.each do |refute_method, assert_method| - test "rejects `#{refute_method}` with a single argument" do - inspect_source(@cop, "#{refute_method} a") - assert_offense @cop, offense_message(refute_method, assert_method) - end - - test "rejects `#{refute_method}` with multiple arguments" do - inspect_source(@cop, "#{refute_method} a, b, c") - assert_offense @cop, offense_message(refute_method, assert_method) - end - - test "autocorrects `#{refute_method}` with a single argument" do - corrected = autocorrect_source(@cop, "#{refute_method} a") - assert_equal "#{assert_method} a", corrected - end - - test "autocorrects `#{refute_method}` with multiple arguments" do - corrected = autocorrect_source(@cop, "#{refute_method} a, b, c") - assert_equal "#{assert_method} a, b, c", corrected - end - - test "accepts `#{assert_method}` with a single argument" do - inspect_source(@cop, "#{assert_method} a") - assert_empty @cop.offenses - end - - test "accepts `#{assert_method}` with multiple arguments" do - inspect_source(@cop, "#{assert_method} a, b, c") - assert_empty @cop.offenses - end - end - - private - - def offense_message(refute_method, assert_method) - carets = "^" * refute_method.to_s.length - "#{carets} Prefer `#{assert_method}` over `#{refute_method}`" - end -end diff --git a/ci/custom_cops/test/support/cop_helper.rb b/ci/custom_cops/test/support/cop_helper.rb deleted file mode 100644 index c2c6b969dd..0000000000 --- a/ci/custom_cops/test/support/cop_helper.rb +++ /dev/null @@ -1,47 +0,0 @@ -# frozen_string_literal: true - -require "rubocop" - -module CopHelper - def inspect_source(cop, source) - processed_source = parse_source(source) - raise "Error parsing example code" unless processed_source.valid_syntax? - investigate(cop, processed_source) - processed_source - end - - def autocorrect_source(cop, source) - cop.instance_variable_get(:@options)[:auto_correct] = true - processed_source = inspect_source(cop, source) - rewrite(cop, processed_source) - end - - def assert_offense(cop, expected_message) - assert_not_empty( - cop.offenses, - "Expected offense with message \"#{expected_message}\", but got no offense" - ) - - offense = cop.offenses.first - carets = "^" * offense.column_length - - assert_equal expected_message, "#{carets} #{offense.message}" - end - - private - TARGET_RUBY_VERSION = 2.4 - - def parse_source(source) - RuboCop::ProcessedSource.new(source, TARGET_RUBY_VERSION) - end - - def rewrite(cop, processed_source) - RuboCop::Cop::Corrector.new(processed_source.buffer, cop.corrections) - .rewrite - end - - def investigate(cop, processed_source) - RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) - .investigate(processed_source) - end -end |