lib/rubocop/cop/rspec/rails/http_status.rb in rubocop-rspec-2.23.2 vs lib/rubocop/cop/rspec/rails/http_status.rb in rubocop-rspec-2.24.0
- old
+ new
@@ -15,34 +15,40 @@
#
# @example `EnforcedStyle: symbolic` (default)
# # bad
# it { is_expected.to have_http_status 200 }
# it { is_expected.to have_http_status 404 }
+ # it { is_expected.to have_http_status "403" }
#
# # good
# it { is_expected.to have_http_status :ok }
# it { is_expected.to have_http_status :not_found }
+ # it { is_expected.to have_http_status :forbidden }
# it { is_expected.to have_http_status :success }
# it { is_expected.to have_http_status :error }
#
# @example `EnforcedStyle: numeric`
# # bad
# it { is_expected.to have_http_status :ok }
# it { is_expected.to have_http_status :not_found }
+ # it { is_expected.to have_http_status "forbidden" }
#
# # good
# it { is_expected.to have_http_status 200 }
# it { is_expected.to have_http_status 404 }
+ # it { is_expected.to have_http_status 403 }
# it { is_expected.to have_http_status :success }
# it { is_expected.to have_http_status :error }
#
# @example `EnforcedStyle: be_status`
# # bad
# it { is_expected.to have_http_status :ok }
# it { is_expected.to have_http_status :not_found }
+ # it { is_expected.to have_http_status "forbidden" }
# it { is_expected.to have_http_status 200 }
# it { is_expected.to have_http_status 404 }
+ # it { is_expected.to have_http_status "403" }
#
# # good
# it { is_expected.to be_ok }
# it { is_expected.to be_not_found }
# it { is_expected.to have_http_status :success }
@@ -53,11 +59,11 @@
include ConfigurableEnforcedStyle
RESTRICT_ON_SEND = %i[have_http_status].freeze
# @!method http_status(node)
def_node_matcher :http_status, <<-PATTERN
- (send nil? :have_http_status ${int sym})
+ (send nil? :have_http_status ${int sym str})
PATTERN
def on_send(node)
http_status(node) do |arg|
checker = checker_class.new(arg)
@@ -122,21 +128,21 @@
def prefer
symbol.inspect
end
def current
- number.inspect
+ node.value.inspect
end
private
def symbol
::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(number)
end
def number
- node.source.to_i
+ node.source.delete('"').to_i
end
end
# :nodoc:
class NumericStyleChecker < StyleCheckerBase
@@ -157,11 +163,11 @@
def symbol
node.value
end
def number
- ::Rack::Utils::SYMBOL_TO_STATUS_CODE[symbol]
+ ::Rack::Utils::SYMBOL_TO_STATUS_CODE[symbol.to_sym]
end
end
# :nodoc:
class BeStatusStyleChecker < StyleCheckerBase
@@ -175,12 +181,14 @@
end
def prefer
if node.sym_type?
"be_#{node.value}"
- else
+ elsif node.int_type?
"be_#{symbol}"
+ elsif node.str_type?
+ "be_#{normalize_str}"
end
end
def current
offense_range.source
@@ -192,9 +200,18 @@
::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(number)
end
def number
node.source.to_i
+ end
+
+ def normalize_str
+ normalized = node.source.delete('"')
+ if normalized.match?(/\A\d+\z/)
+ ::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(normalized.to_i)
+ else
+ normalized
+ end
end
end
end
end
end