Sha256: 10e1700c89376f9ed8e6b11ba7e0ec7829b85c17521a1e3e6f991acaf0a5a0df

Contents?: true

Size: 1.33 KB

Versions: 14

Compression:

Stored size: 1.33 KB

Contents

module Spec
  module Matchers
    
    class HaveAddress
      def initialize(field, expected)
        @field, @expected = field, expected
      end
      
      def matches?(email)
        @actual = addresses(email)
        missing = expected_addresses.reject {|e| @actual.include?(e)}
        extra = @actual.reject {|e| expected_addresses.include?(e)}
        extra.empty? && missing.empty?
      end
      
      def failure_message
        "expected #{@field} address to contain #{expected_addresses.inspect} but was #{@actual.inspect}"
      end
      
      def negative_failure_message
        "expected #{@field} address not to contain #{expected_addresses.inspect} but it did"
      end
      
      private
        def addresses(email)
          email[@field.to_s].addrs.collect(&:address) rescue []
        end
        
        def expected_addresses
          case @expected
          when Array
            @expected.collect { |a| extract_address(a) }
          when String
            [extract_address(@expected)]
          end
        end
        
        def extract_address(string)
          address = string.sub(/.*?<(.*?)>/, '\1')
          address if address =~ /\A([^@\s]+)@(localhost|(?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
        end
    end
    
    def have_address(field, expected)
      HaveAddress.new(field, expected)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

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