Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
bbd2681a
Commit
bbd2681a
authored
Dec 20, 2020
by
Alex Kalderimis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add nicer API for be_sorted
Tests are added for this matcher, using the fast_spec_helper
parent
38dcda29
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
97 additions
and
7 deletions
+97
-7
spec/support/matchers/be_sorted.rb
spec/support/matchers/be_sorted.rb
+64
-7
spec/support_specs/matchers/be_sorted_spec.rb
spec/support_specs/matchers/be_sorted_spec.rb
+33
-0
No files found.
spec/support/matchers/be_sorted.rb
View file @
bbd2681a
...
...
@@ -4,18 +4,75 @@
#
# By default, this checks that the collection is sorted ascending
# but you can check order by specific field and order by passing
# them, eg:
# them, e
ither as arguments, or using the fluent interface, e
g:
#
# ```
# # Usage examples:
# expect(collection).to be_sorted
# expect(collection).to be_sorted(:field)
# expect(collection).to be_sorted(:field, :desc)
# expect(collection).to be_sorted.asc
# expect(collection).to be_sorted.desc.by(&:field)
# expect(collection).to be_sorted.by(&:field).desc
# expect(collection).to be_sorted.by { |x| [x.foo, x.bar] }
# ```
RSpec
::
Matchers
.
define
:be_sorted
do
|
by
,
order
=
:asc
|
RSpec
::
Matchers
.
define
:be_sorted
do
|
on
=
:itself
,
order
=
:asc
|
def
by
(
&
block
)
@comparator
=
block
self
end
def
asc
@direction
=
:asc
self
end
def
desc
@direction
=
:desc
self
end
def
format_with
(
proc
)
@format_with
=
proc
self
end
define_method
:comparator
do
@comparator
||
on
end
define_method
:descending?
do
(
@direction
||
order
.
to_sym
)
==
:desc
end
def
order
(
items
)
descending?
?
items
.
reverse
:
items
end
def
sort
(
items
)
items
.
sort_by
(
&
comparator
)
end
match
do
|
actual
|
next
true
unless
actual
.
present?
# emtpy collection is sorted
next
true
unless
actual
.
present?
# empty collection is sorted
actual
=
actual
.
to_a
if
actual
.
respond_to?
(
:to_a
)
&&
!
actual
.
respond_to?
(
:sort_by
)
@got
=
actual
@expected
=
order
(
sort
(
actual
))
@expected
==
actual
end
def
failure_message
"Expected
#{
show
(
@expected
)
}
, got
#{
show
(
@got
)
}
"
end
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
}
def
show
(
things
)
if
@format_with
things
.
map
(
&
@format_with
)
else
things
end
end
end
spec/support_specs/matchers/be_sorted_spec.rb
0 → 100644
View file @
bbd2681a
# frozen_string_literal: true
require
'fast_spec_helper'
load
File
.
expand_path
(
'../../../spec/support/matchers/be_sorted.rb'
,
__dir__
)
RSpec
.
describe
'be_sorted'
do
it
'matches empty collections, regardless of arguments'
do
expect
([])
.
to
be_sorted
.
and
be_sorted
.
asc
.
and
be_sorted
.
desc
.
and
be_sorted
(
:foo
)
.
and
be_sorted
(
:bar
)
expect
([].
to_set
).
to
be_sorted
expect
({}).
to
be_sorted
end
it
'matches in both directions'
do
expect
([
1
,
2
,
3
]).
to
be_sorted
.
asc
expect
([
3
,
2
,
1
]).
to
be_sorted
.
desc
end
it
'can match on a projection'
do
xs
=
[[
'a'
,
10
],
[
'b'
,
7
],
[
'c'
,
4
]]
expect
(
xs
).
to
be_sorted
.
asc
.
by
(
&
:first
)
expect
(
xs
).
to
be_sorted
(
:first
,
:asc
)
expect
(
xs
).
to
be_sorted
.
desc
.
by
(
&
:second
)
expect
(
xs
).
to
be_sorted
(
:second
,
:desc
)
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment