Sha256: 99cbc81e770bccdc5244b1ebc1a94a702cc63d1b3eacd954ab4753ac5f10f2c5

Contents?: true

Size: 1.58 KB

Versions: 16

Compression:

Stored size: 1.58 KB

Contents

module DynportTools::HaveAttributesMatcher
  class HaveAttributes
    def initialize(expected)
      @expected = expected
    end
    
    def differ
      @differ ||= DynportTools::Differ.new(:diff_all => false)
    end

    def matches?(target)
      @target = if target.respond_to?(:attributes)
        differ.symbolize_keys = true
        target.attributes
      else
        target
      end
      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

16 entries across 16 versions & 1 rubygems

Version Path
dynport_tools-0.2.22 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.21 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.20 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.19 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.18 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.17 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.16 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.15 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.14 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.13 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.12 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.11 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.10 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.9 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.8 lib/dynport_tools/have_attributes.rb
dynport_tools-0.2.6 lib/dynport_tools/have_attributes.rb