Sha256: 3ff82369809ba67c11db595a2764d21c22ba93585e9cfc601a67c6e79ad5e3bb

Contents?: true

Size: 718 Bytes

Versions: 4

Compression:

Stored size: 718 Bytes

Contents

RSpec::Matchers.define :be_sorted_by do |sorting_field, options = {}|
  match do |items|
    if sorting_field.nil?
      true
    else
      values = items.map{|item| item[sorting_field.to_s]}
      values.reverse! if options[:verse] == :desc
      values == values.sort
    end
  end

  description do
    # NOTE: Since `accepts_sort random: nil` is acceptable, this description
    # should say "you should not expect any sorting by any specific field"
    if sorting_field.nil?
      %Q(not be sorted by any specific attribute)
    else
      %Q(be sorted by #{sorting_field.to_json} #{options[:verse]})
    end
  end

  failure_message_for_should do |items|
    %Q(should #{description}, but is #{items})
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rspec-api-0.2.0 lib/rspec-api/matchers/sort.rb
rspec-api-0.1.2 lib/rspec-api/matchers/sort.rb
rspec-api-0.1.1 lib/rspec-api/matchers/sort.rb
rspec-api-0.1.0 lib/rspec-api/matchers/sort.rb