Commit 8450a388 authored by Peter Leitzen's avatar Peter Leitzen Committed by Grzegorz Bizon

Prefer `flat_map` over `map` + `flatten` in specs

Although `flat_map` is equivalent to `map` + `flatten(1)`
(note the level 1) we can apply this same refactoring to all cases.
parent 02cc617b
...@@ -72,8 +72,8 @@ describe Gitlab::Metrics::Dashboard::Processor do ...@@ -72,8 +72,8 @@ describe Gitlab::Metrics::Dashboard::Processor do
private private
def all_metrics def all_metrics
dashboard[:panel_groups].map do |group| dashboard[:panel_groups].flat_map do |group|
group[:panels].map { |panel| panel[:metrics] } group[:panels].flat_map { |panel| panel[:metrics] }
end.flatten end
end end
end end
...@@ -9,7 +9,7 @@ describe Gitlab::Prometheus::MetricGroup do ...@@ -9,7 +9,7 @@ describe Gitlab::Prometheus::MetricGroup do
subject do subject do
described_class.for_project(project) described_class.for_project(project)
.map(&:metrics).flatten .flat_map(&:metrics)
.map(&:id) .map(&:id)
end end
......
...@@ -62,7 +62,7 @@ describe Groups::ParticipantsService do ...@@ -62,7 +62,7 @@ describe Groups::ParticipantsService do
service.instance_variable_set(:@noteable, epic) service.instance_variable_set(:@noteable, epic)
result = service.group_members result = service.group_members
expected_users = (group.self_and_hierarchy.map(&:users).flatten + subproject.users) expected_users = (group.self_and_hierarchy.flat_map(&:users) + subproject.users)
.map(&method(:user_to_autocompletable)) .map(&method(:user_to_autocompletable))
expect(expected_users.count).to eq(5) expect(expected_users.count).to eq(5)
......
...@@ -158,7 +158,7 @@ describe Projects::JobsController, :clean_gitlab_redis_shared_state do ...@@ -158,7 +158,7 @@ describe Projects::JobsController, :clean_gitlab_redis_shared_state do
get_show_json get_show_json
json_response.dig('pipeline', 'details', 'stages').tap do |stages| json_response.dig('pipeline', 'details', 'stages').tap do |stages|
expect(stages.map(&:keys).flatten) expect(stages.flat_map(&:keys))
.to eq %w[name title status path dropdown_path] .to eq %w[name title status path dropdown_path]
end end
end end
......
...@@ -7,8 +7,8 @@ describe Gitlab::DatabaseImporters::CommonMetrics::Importer do ...@@ -7,8 +7,8 @@ describe Gitlab::DatabaseImporters::CommonMetrics::Importer do
context "does import common_metrics.yml" do context "does import common_metrics.yml" do
let(:groups) { subject.content['panel_groups'] } let(:groups) { subject.content['panel_groups'] }
let(:panels) { groups.map { |group| group['panels'] }.flatten } let(:panels) { groups.flat_map { |group| group['panels'] } }
let(:metrics) { panels.map { |group| group['metrics'] }.flatten } let(:metrics) { panels.flat_map { |group| group['metrics'] } }
let(:metric_ids) { metrics.map { |metric| metric['id'] } } let(:metric_ids) { metrics.map { |metric| metric['id'] } }
before do before do
......
...@@ -179,9 +179,9 @@ describe Gitlab::ImportExport::ProjectTreeSaver do ...@@ -179,9 +179,9 @@ describe Gitlab::ImportExport::ProjectTreeSaver do
end end
it 'has priorities associated to labels' do it 'has priorities associated to labels' do
priorities = saved_project_json['issues'].first['label_links'].map { |link| link['label']['priorities'] } priorities = saved_project_json['issues'].first['label_links'].flat_map { |link| link['label']['priorities'] }
expect(priorities.flatten).not_to be_empty expect(priorities).not_to be_empty
end end
it 'has issue resource label events' do it 'has issue resource label events' do
......
...@@ -101,9 +101,9 @@ describe Gitlab::Metrics::Dashboard::Processor do ...@@ -101,9 +101,9 @@ describe Gitlab::Metrics::Dashboard::Processor do
private private
def all_metrics def all_metrics
dashboard[:panel_groups].map do |group| dashboard[:panel_groups].flat_map do |group|
group[:panels].map { |panel| panel[:metrics] } group[:panels].flat_map { |panel| panel[:metrics] }
end.flatten end
end end
def get_metric_details(metric) def get_metric_details(metric)
......
...@@ -17,7 +17,7 @@ describe Gitlab::Prometheus::MetricGroup do ...@@ -17,7 +17,7 @@ describe Gitlab::Prometheus::MetricGroup do
end end
it 'returns exactly three metric queries' do it 'returns exactly three metric queries' do
expect(subject.map(&:metrics).flatten.map(&:id)).to contain_exactly( expect(subject.flat_map(&:metrics).map(&:id)).to contain_exactly(
common_metric_group_a.id, common_metric_group_b_q1.id, common_metric_group_a.id, common_metric_group_b_q1.id,
common_metric_group_b_q2.id) common_metric_group_b_q2.id)
end end
...@@ -37,7 +37,7 @@ describe Gitlab::Prometheus::MetricGroup do ...@@ -37,7 +37,7 @@ describe Gitlab::Prometheus::MetricGroup do
subject do subject do
described_class.for_project(other_project) described_class.for_project(other_project)
.map(&:metrics).flatten .flat_map(&:metrics)
.map(&:id) .map(&:id)
end end
......
...@@ -81,10 +81,9 @@ describe WikiPage do ...@@ -81,10 +81,9 @@ describe WikiPage do
grouped_entries = described_class.group_by_directory(wiki.list_pages) grouped_entries = described_class.group_by_directory(wiki.list_pages)
actual_order = actual_order =
grouped_entries.map do |page_or_dir| grouped_entries.flat_map do |page_or_dir|
get_slugs(page_or_dir) get_slugs(page_or_dir)
end end
.flatten
expect(actual_order).to eq(expected_order) expect(actual_order).to eq(expected_order)
end end
end end
......
...@@ -265,7 +265,7 @@ describe Issuable::BulkUpdateService do ...@@ -265,7 +265,7 @@ describe Issuable::BulkUpdateService do
end end
it 'removes the label IDs from all issues passed' do it 'removes the label IDs from all issues passed' do
expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(merge_requests.id) expect(issues.map(&:reload).flat_map(&:label_ids)).not_to include(merge_requests.id)
end end
it 'does not update issues not passed in' do it 'does not update issues not passed in' do
...@@ -297,11 +297,11 @@ describe Issuable::BulkUpdateService do ...@@ -297,11 +297,11 @@ describe Issuable::BulkUpdateService do
let(:remove_labels) { [regression] } let(:remove_labels) { [regression] }
it 'removes the label IDs from all issues passed' do it 'removes the label IDs from all issues passed' do
expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(regression.id) expect(issues.map(&:reload).flat_map(&:label_ids)).not_to include(regression.id)
end end
it 'ignores the label IDs parameter' do it 'ignores the label IDs parameter' do
expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(merge_requests.id) expect(issues.map(&:reload).flat_map(&:label_ids)).not_to include(merge_requests.id)
end end
it 'does not update issues not passed in' do it 'does not update issues not passed in' do
...@@ -320,11 +320,11 @@ describe Issuable::BulkUpdateService do ...@@ -320,11 +320,11 @@ describe Issuable::BulkUpdateService do
end end
it 'removes the label IDs from all issues passed' do it 'removes the label IDs from all issues passed' do
expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(merge_requests.id) expect(issues.map(&:reload).flat_map(&:label_ids)).not_to include(merge_requests.id)
end end
it 'ignores the label IDs parameter' do it 'ignores the label IDs parameter' do
expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(regression.id) expect(issues.map(&:reload).flat_map(&:label_ids)).not_to include(regression.id)
end end
it 'does not update issues not passed in' do it 'does not update issues not passed in' do
......
...@@ -179,7 +179,7 @@ describe Issues::UpdateService, :mailer do ...@@ -179,7 +179,7 @@ describe Issues::UpdateService, :mailer do
it 'sends email to user2 about assign of new issue and email to user3 about issue unassignment' do it 'sends email to user2 about assign of new issue and email to user3 about issue unassignment' do
deliveries = ActionMailer::Base.deliveries deliveries = ActionMailer::Base.deliveries
email = deliveries.last email = deliveries.last
recipients = deliveries.last(2).map(&:to).flatten recipients = deliveries.last(2).flat_map(&:to)
expect(recipients).to include(user2.email, user3.email) expect(recipients).to include(user2.email, user3.email)
expect(email.subject).to include(issue.title) expect(email.subject).to include(issue.title)
end end
......
...@@ -99,7 +99,7 @@ describe MergeRequests::UpdateService, :mailer do ...@@ -99,7 +99,7 @@ describe MergeRequests::UpdateService, :mailer do
it 'sends email to user2 about assign of new merge request and email to user3 about merge request unassignment' do it 'sends email to user2 about assign of new merge request and email to user3 about merge request unassignment' do
deliveries = ActionMailer::Base.deliveries deliveries = ActionMailer::Base.deliveries
email = deliveries.last email = deliveries.last
recipients = deliveries.last(2).map(&:to).flatten recipients = deliveries.last(2).flat_map(&:to)
expect(recipients).to include(user2.email, user3.email) expect(recipients).to include(user2.email, user3.email)
expect(email.subject).to include(merge_request.title) expect(email.subject).to include(merge_request.title)
end end
......
...@@ -33,7 +33,7 @@ module Spec ...@@ -33,7 +33,7 @@ module Spec
# Packs string components into an openssh-encoded pubkey. # Packs string components into an openssh-encoded pubkey.
def pack_pubkey_components(strings) def pack_pubkey_components(strings)
(strings.map { |s| [s.length].pack('N') }).zip(strings).flatten.join (strings.flat_map { |s| [s.length].pack('N') }).zip(strings).join
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment