lib/yardcheck/violation.rb in yardcheck-0.0.2 vs lib/yardcheck/violation.rb in yardcheck-0.0.3

- old
+ new

@@ -3,51 +3,63 @@ module Yardcheck class Violation extend Color include Color - def initialize(observation, test_locations = [observation.test_location]) + def initialize( + observation, + test_locations = [observation.test_location], + test_ids = [observation.test_id] + ) @observation = observation @test_locations = test_locations.sort.uniq + @test_ids = test_ids.sort.uniq end def offense indented_source = indent(observation.source_code) source = "\n#{CodeRay.encode(indented_source, :ruby, :terminal)}\n" location_hint = indent(grey("source: #{observation.source_location}")) test_lines = test_locations.map { |l| " - #{l}" }.join("\n") + tests_block = "tests:\n#{test_lines}" test_hint = indent(grey(tests_block)) - "#{explanation}\n\n#{location_hint}\n#{test_hint}\n#{source}\n" + rerun_block = indent(grey("rerun with:\n #{rerun_command}")) + + "#{explanation}\n\n#{location_hint}\n#{test_hint}\n#{rerun_block}\n#{source}\n" end def combine(other) fail 'Cannot combine' unless combine_with?(other) - with_tests(other.test_locations) + with_tests(other) end def combination_identifier combine_requirements.map(&method(:__send__)) end - attr_reader :test_locations + attr_reader :test_locations, :test_ids private attr_reader :observation def combine_with?(other) combination_identifier == other.combination_identifier end def with_tests(other_test_locations) - self.class.new(observation, test_locations + other_test_locations) + self.class.new(observation, test_locations + other_test_locations, test_ids + other.test_ids) end + def rerun_command + "rspec #{test_ids.map { |id| "'#{id}'" }.join(' ')}" + end + def indent(string) string.gsub(/^/, ' ') end def shorthand @@ -68,11 +80,15 @@ FORMAT = "Expected #{blue('%<shorthand>s')} to return " \ "#{yellow('%<signature>s')} but observed " \ "#{red('%<observed_type>s')}" - def initialize(observation, test_locations = [observation.test_location]) + def initialize( + observation, + test_locations = [observation.test_location], + test_ids = [observation.test_id] + ) super end def explanation format( @@ -101,14 +117,19 @@ end # Return class Param < self include Equalizer.new(:name, :observation) - def initialize(name, observation, test_locations = [observation.test_location]) + def initialize( + name, + observation, + test_locations = [observation.test_location], + test_ids = [observation.test_id] + ) @name = name - super(observation, test_locations) + super(observation, test_locations, test_ids) end FORMAT = "Expected #{blue('%<shorthand>s')} to " \ "receive #{yellow('%<signature>s')} for #{blue('%<name>s')} " \ @@ -134,14 +155,15 @@ def combine_requirements %i[name shorthand signature observed_type] end - def with_tests(other_test_locations) + def with_tests(other) self.class.new( name, observation, - test_locations + other_test_locations + test_locations + other.test_locations, + test_ids + other.test_ids ) end def test_value observation.observed_param(name)