Sha256: 8059b6a6bf4a84b9bbd5c9e16cf1dd5751e8db5824d7e127c54b17284e42d59b

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# encoding: utf-8
require 'rspec/expectations'
# require 'rspec/expectations/differ'


module Ndd
  module RSpec
    module Matchers

      def be_reverse_sorted_by(attribute)
        BeReverseSortedBy.new(attribute)
      end

      # ----------------------------------------------------------------------------------------------------------------
      # Ensures that an enumerable (responding to <code>Enumerable#collect</code>) is sorted in reverse order by the
      # given attribute of the objects it contains.
      #
      # Examples:
      #   Duck = Struct.new(:color)
      #   [Duck.new('White'), Duck.new('Grey')].should be_sorted_by(:color) }
      #
      class BeReverseSortedBy

        def initialize(attribute = nil)
          @attribute = attribute
        end

        def matches?(actual)
          @actual = actual
          attributes = actual.collect { |e| e.send(@attribute) }
          @actual_attributes = attributes
          @sorted_attributes = attributes.sort.reverse
          @sorted_attributes == @actual_attributes
        end

        def description
          "be sorted by '#@attribute' in reverse order"
        end

        def failure_message
          return <<-MESSAGE

expected '#{@actual.inspect}' to be sorted by '#@attribute' in reverse order
expected attributes: #{@sorted_attributes.inspect}
     got attributes: #{@actual_attributes.inspect}

          MESSAGE
        end

        def ==(other)
          matches?(other)
        end

      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ndd-rspec-1.0.0 lib/ndd/rspec/matchers/be_reverse_sorted_by.rb