Sha256: 55cf0f088efef104095476d9bd10acffb09a9a44c82a29fd17f9f8cf100b009a

Contents?: true

Size: 1.48 KB

Versions: 9

Compression:

Stored size: 1.48 KB

Contents

require File.expand_path("../../../lib/differ", __FILE__)
module HaveAttributesMatcher
  class HaveAttributes
    def initialize(expected)
      @expected = expected
    end
    
    def differ
      @differ ||= Differ.new(:diff_all => false)
    end

    def matches?(target)
      @target = target
      if diff = differ.diff(@expected, target)
        @error = differ.diff_to_message_lines(diff).join("\n")
        false
      else
        true
      end
    end

    def failure_message
      @error
    end
  end
  
  class ReturnValues < HaveAttributes
    def matches?(target)
      differ.use_return = true
      super(@expected.keys.inject({}) { |hash, key| hash.merge!(key => target.send(key)) })
    end
  end
  
  class HaveAllAttributes < HaveAttributes
    def matches?(record)
      differ.diff_all = true
      super(record)
    end
  end
  
  class HaveOneWithAttributes < HaveAttributes
    def matches?(target)
      target.any? do |record|
        super(record)
      end
    end
    
    def failure_message
      "expected to have one record with attributes #{@expected.inspect}"
    end
  end
  
  def return_values(expected)
    ReturnValues.new(expected)
  end

  def have_attributes(expected)
    HaveAttributes.new(expected)
  end
  
  def exactly_have_attributes(expected)
    have_all_attributes(expected)
  end
  
  def have_all_attributes(expected)
    HaveAllAttributes.new(expected)
  end
  
  def have_one_with_attributes(expected)
    HaveOneWithAttributes.new(expected)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dynport_tools-0.2.4 lib/have_attributes.rb
dynport_tools-0.2.3 lib/have_attributes.rb
dynport_tools-0.2.2 lib/have_attributes.rb
dynport_tools-0.2.1 lib/have_attributes.rb
dynport_tools-0.2.0 lib/have_attributes.rb
dynport_tools-0.1.3 lib/have_attributes.rb
dynport_tools-0.1.2 lib/have_attributes.rb
dynport_tools-0.1.1 lib/have_attributes.rb
dynport_tools-0.1.0 lib/have_attributes.rb