Welcome to mirror list, hosted at ThFree Co, Russian Federation.

be_sorted.rb « matchers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1455060fe715c3f66eb5c0e51f18674fd80ba2ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# frozen_string_literal: true

# Assert that this collection is sorted by argument and order
#
# By default, this checks that the collection is sorted ascending
# but you can check order by specific field and order by passing
# them, eg:
#
# ```
# expect(collection).to be_sorted(:field, :desc)
# ```
RSpec::Matchers.define :be_sorted do |by, order = :asc|
  match do |actual|
    next true unless actual.present? # emtpy collection is sorted

    actual
      .then { |collection| by ? collection.sort_by(&by) : collection.sort }
      .then { |sorted_collection| order.to_sym == :desc ? sorted_collection.reverse : sorted_collection }
      .then { |sorted_collection| sorted_collection == actual }
  end
end