Sha256: 78a66017d3e06006d2b001e7f262d6cf530efb508fcb4a7ce1ce6595ebc146dc

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

# Copyright (c) 2008-16 Peter H. Boling of RailsBling.com
# Released under the MIT license
# Note: the RspecMatchers no longer use these methods.  Instead they are composed matchers:
# See: http://www.relishapp.com/rspec/rspec-expectations/v/3-5/docs/composing-matchers

module SanitizeEmail
  # Helpers for test-unit
  module TestHelpers
    # Error raised when unable to match an expected part of email in order to fail the test
    class UnexpectedMailType < StandardError; end

    def string_matching_attribute(matcher, part, attribute)
      string_matching(matcher, part, attribute)
    end

    def string_matching(matcher, part, attribute)
      # Can we match a regex against it?
      raise UnexpectedMailType, "Cannot match #{matcher} for #{part}" unless attribute.respond_to?(:=~)
      attribute =~ if matcher.is_a?(Regexp)
                     matcher
                   else
                     Regexp.new(Regexp.escape(matcher))
                   end
    end

    # Normalize arrays to strings
    def array_matching(matcher, part, attribute)
      attribute = attribute.join(', ') if attribute.respond_to?(:join)
      string_matching(matcher, part, attribute)
    end

    def email_matching(matcher, part, mail_or_part)
      email_attribute_matching(matcher, part, mail_or_part.send(part))
    end

    def email_attribute_matching(matcher, part, attribute)
      array_matching(matcher, part, attribute)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sanitize_email-2.0.3 lib/sanitize_email/test_helpers.rb
sanitize_email-2.0.2 lib/sanitize_email/test_helpers.rb
sanitize_email-2.0.1 lib/sanitize_email/test_helpers.rb