Add specs for warning behavior on Class#name violations

This commit is contained in:
Markus Schirp 2014-11-17 21:03:40 +00:00
parent 4f24280efe
commit 14e95080ed
3 changed files with 52 additions and 3 deletions

View file

@ -1,3 +1,3 @@
---
threshold: 18
total_score: 1119
total_score: 1132

View file

@ -86,7 +86,7 @@ module Mutant
def scope_name(scope)
scope.name
rescue => exception
warn("While obtaining #{scope.class}#name from: #{scope.inspect} It raised an error: #{exception.inspect} fix your lib!")
warn("#{scope.class}#name from: #{scope.inspect} raised an error: #{exception.inspect} fix your lib to follow normal ruby semantics!")
nil
end
@ -106,7 +106,7 @@ module Mutant
name = scope_name(scope) or return
unless name.is_a?(String)
warn("#{scope.class}#name from: #{scope.inspect} did not return a String or nil. Fix your lib to support normal ruby semantics!")
warn("#{scope.class}#name from: #{scope.inspect} returned #{name.inspect} instead String or nil. Fix your lib to follow normal ruby semantics!")
return
end

View file

@ -0,0 +1,49 @@
RSpec.describe Mutant::Env do
let(:config) { Mutant::Config::DEFAULT.update(jobs: 1, reporter: Mutant::Reporter::Trace.new) }
context '.new' do
subject { described_class.new(config) }
context 'when Module#name calls result in exceptions' do
it 'warns via reporter' do
klass = Class.new do
def self.name
raise
end
end
expected_warnings = ["Class#name from: #{klass} raised an error: RuntimeError fix your lib to follow normal ruby semantics!"]
expect { subject }.to change { config.reporter.warn_calls }.from([]).to(expected_warnings)
# Fix Class#name so other specs do not see this one
class << klass
undef :name
def name
end
end
end
end
context 'when Module#name does not return a String or nil' do
it 'warns via reporter' do
klass = Class.new do
def self.name
Object
end
end
expected_warnings = ["Class#name from: #{klass.inspect} returned #{Object.inspect} instead String or nil. Fix your lib to follow normal ruby semantics!"]
expect { subject }.to change { config.reporter.warn_calls }.from([]).to(expected_warnings)
# Fix Class#name so other specs do not see this one
class << klass
undef :name
def name
end
end
end
end
end
end