Use a more error tolerant way to access {Module,Class}#name

"Many" library authors override Class or Modules #name not to return a
String or nil. They are not aware these are integral APIs for
reflection / tools. This change prints an ugly warning message in case
the API was violated.

Mutant used to crash often because of this LSP violation. This change
should provide a generic workaround.
This commit is contained in:
Markus Schirp 2014-01-18 23:58:14 +01:00
parent 4c26dc740b
commit f2297e3472

View file

@ -54,6 +54,29 @@ module Mutant
end
end
# Return scope name
#
# @param [Class,Module] scope
#
# @return [String]
# if scope has a name and does not raise exceptions optaining it
#
# @return [nil]
# otherwise
#
# @api private
#
def self.scope_name(scope)
scope.name
rescue => exception
$stderr.puts <<-MESSAGE
WARNING:
While optaining #{scope.class}#name from: #{scope.inspect}
It raised an error: #{exception.inspect} fix your lib!
MESSAGE
nil
end
# Yield scope if name matches pattern
#
# @param [Module,Class] scope
@ -63,9 +86,15 @@ module Mutant
# @api private
#
def emit_scope(scope)
name = scope.name
name = self.class.scope_name(scope)
# FIXME: Fix nokogiri to return a string here
return unless name.kind_of?(String)
unless name.nil? or name.kind_of?(String)
$stderr.puts <<-MESSAGE
WARNING:
#{scope.class}#name did not return a string or nil.
Fix your lib!
MESSAGE
end
if pattern =~ name
yield scope
end