Sha256: 4faedbcd38ea55ea4c3417da24828b7e36482b3544ef861bae2063f05943b8c7

Contents?: true

Size: 1.65 KB

Versions: 14

Compression:

Stored size: 1.65 KB

Contents

module Spec
  module Matchers
    
    class EqualTmail
      def initialize(expected)
        @expected = expected
      end
      
      def matches?(tmail)
        @given = tmail
        headers_match?
      end
      
      def failure_message
        @failure_message
      end
      
      private
        def comparable_value(value)
          case value
          when Array
            value.collect {|e| e.to_s.strip}.join(" ").strip
          when String
            value.strip
          end
        end
        
        def headers_match?
          missing = @expected.header.collect { |name, value| @given[name].nil? ? name : nil }.compact
          extra = @given.header.collect { |name, value| @expected[name].nil? ? name : nil }.compact
          if extra.empty? && missing.empty?
            unequal = []
            @expected.header.each do |name, value|
              expected_value, given_value = comparable_value(value), comparable_value(@given[name])
              unless expected_value == given_value
                unequal << "expected header #{name.inspect} to be #{expected_value.inspect} but was #{given_value.inspect}"
              end
            end
            @failure_message = unequal.join("\n") unless unequal.empty?
          else
            @failure_message = "expected tmail instances to be equal but headers were not"
            @failure_message << "\nmissing in given: #{missing.inspect}" unless missing.empty?
            @failure_message << "\nextra in given: #{extra.inspect}" unless extra.empty?
          end
          @failure_message.nil?
        end
    end
    
    def equal_tmail(tmail)
      EqualTmail.new(tmail)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
mlist-0.1.23 spec/matchers/equal_tmail.rb
mlist-0.1.22 spec/matchers/equal_tmail.rb
mlist-0.1.21 spec/matchers/equal_tmail.rb
mlist-0.1.20 spec/matchers/equal_tmail.rb
mlist-0.1.19 spec/matchers/equal_tmail.rb
mlist-0.1.18 spec/matchers/equal_tmail.rb
mlist-0.1.17 spec/matchers/equal_tmail.rb
mlist-0.1.16 spec/matchers/equal_tmail.rb
mlist-0.1.14 spec/matchers/equal_tmail.rb
mlist-0.1.13 spec/matchers/equal_tmail.rb
mlist-0.1.12 spec/matchers/equal_tmail.rb
mlist-0.1.11 spec/matchers/equal_tmail.rb
mlist-0.1.10 spec/matchers/equal_tmail.rb
mlist-0.1.9 spec/matchers/equal_tmail.rb