# Copyright (C) 2024 Manuel Bustillo # frozen_string_literal: true require 'rails_helper' module Tables RSpec.describe Swap do describe '#each' do let(:swaps) do acc = [] described_class.new(initial_solution).each do |solution| # rubocop:disable Style/MapIntoArray -- #map is not implemented acc << solution.tables.map(&:dup) end acc end context 'when there are two tables with two people each' do let(:initial_solution) do Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| distribution.tables << Set[:a, :b].to_table distribution.tables << Set[:c, :d].to_table end end it 'yields all possible swaps between the tables' do expect(swaps).to contain_exactly( [Set[:a, :d], Set[:c, :b]], [Set[:b, :c], Set[:d, :a]], [Set[:a, :c], Set[:d, :b]], [Set[:b, :d], Set[:c, :a]] ) end end context 'when there are two tables with three people each' do let(:initial_solution) do Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution| distribution.tables << Set[:a, :b, :c].to_table distribution.tables << Set[:d, :e, :f].to_table end end it 'yields all possible swaps between the tables' do expect(swaps).to contain_exactly( [Set[:b, :c, :d], Set[:e, :f, :a]], [Set[:b, :c, :e], Set[:f, :d, :a]], [Set[:b, :c, :f], Set[:d, :e, :a]], [Set[:c, :a, :d], Set[:e, :f, :b]], [Set[:c, :a, :e], Set[:f, :d, :b]], [Set[:c, :a, :f], Set[:d, :e, :b]], [Set[:a, :b, :d], Set[:e, :f, :c]], [Set[:a, :b, :e], Set[:f, :d, :c]], [Set[:a, :b, :f], Set[:d, :e, :c]] ) end end context 'when there are three tables with two people each' do let(:initial_solution) do Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| distribution.tables << Set[:a, :b].to_table distribution.tables << Set[:c, :d].to_table distribution.tables << Set[:e, :f].to_table end end it 'yields all possible swaps between the tables' do expect(swaps).to contain_exactly( [Set[:b, :c], Set[:d, :a], Set[:e, :f]], [Set[:b, :d], Set[:c, :a], Set[:e, :f]], [Set[:a, :c], Set[:d, :b], Set[:e, :f]], [Set[:a, :d], Set[:c, :b], Set[:e, :f]], [Set[:b, :e], Set[:c, :d], Set[:f, :a]], [Set[:b, :f], Set[:c, :d], Set[:e, :a]], [Set[:a, :e], Set[:c, :d], Set[:f, :b]], [Set[:a, :f], Set[:c, :d], Set[:e, :b]], [Set[:a, :b], Set[:d, :e], Set[:f, :c]], [Set[:a, :b], Set[:d, :f], Set[:e, :c]], [Set[:a, :b], Set[:c, :e], Set[:f, :d]], [Set[:a, :b], Set[:c, :f], Set[:e, :d]] ) end end end end end