Sha256: 7fc15ec97d040fc4c22fe93c4806973719d2da386246344ada9db6fd5f92c192

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

module SpecCombos

  module MatcherCombiner

    module And
      def match_result
        matches(NOT_MATCHING).empty?
      end
    end

    module Or
      def match_result
        !matches(MATCHING).empty?
      end
    end

    def matches?(actual)
      @actual = actual
      @match_details = perform_matches(actual)
      match_result
    end

    def failure_message_for_should
      failure_message(MATCHING, failure_summary_for_should)
    end

    def failure_message_for_should_not
      failure_message(NOT_MATCHING, failure_summary_for_should_not)
    end

    private

    attr_reader :actual

    MATCHING = true
    NOT_MATCHING = false
    INDENT_WIDTH = 2

    def failure_message(expected_match, summary)
      summary + "\n" + indent(full_explanation(expected_match), INDENT_WIDTH)
    end
    def full_explanation(expected_match)
      explanations(!expected_match).join("\n")
    end
    def explanations(expected_match)
      matches(expected_match).map {|failure| explain(failure)}
    end
    def explain(failure)
      failure_message_prefix(failure) + message(failure)
    end
    def failure_message_prefix(failure)
      ""
    end
    def message(failure)
      failure[:matcher].send(message_selector(failure))
    end
    def message_selector(failure)
      if failure[:match]
        :failure_message_for_should_not
      else
        :failure_message_for_should
      end
    end

    def matches(expected_match)
      @match_details.find_all {|detail| detail[:match] == expected_match}
    end

    def indent(text, tabs)
      text.gsub(/^/, ' ' * tabs)
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spec_combos-0.3.0 lib/spec_combos/matcher_combiner.rb
spec_combos-0.2.0 lib/spec_combos/matcher_combiner.rb