From 14e95080edc66cc61bf44f1987207ae204d8e175 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Mon, 17 Nov 2014 21:03:40 +0000 Subject: [PATCH] Add specs for warning behavior on Class#name violations --- config/flay.yml | 2 +- lib/mutant/env.rb | 4 +-- spec/unit/mutant/env_spec.rb | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 spec/unit/mutant/env_spec.rb diff --git a/config/flay.yml b/config/flay.yml index d9dc814e..48be7fe8 100644 --- a/config/flay.yml +++ b/config/flay.yml @@ -1,3 +1,3 @@ --- threshold: 18 -total_score: 1119 +total_score: 1132 diff --git a/lib/mutant/env.rb b/lib/mutant/env.rb index 71a31643..94cf77b5 100644 --- a/lib/mutant/env.rb +++ b/lib/mutant/env.rb @@ -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 diff --git a/spec/unit/mutant/env_spec.rb b/spec/unit/mutant/env_spec.rb new file mode 100644 index 00000000..b91f1490 --- /dev/null +++ b/spec/unit/mutant/env_spec.rb @@ -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