2017-09-30 23:06:14 -04:00
|
|
|
shared_examples "a sequence" do |options|
|
2020-06-05 15:15:18 -04:00
|
|
|
first_value = options[:first_value]
|
2017-09-30 23:06:14 -04:00
|
|
|
second_value = options[:second_value]
|
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
it "has a next value equal to its first value" do
|
|
|
|
expect(subject.next).to eq first_value
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a next value equal to the 2nd value after being incremented" do
|
|
|
|
subject.next
|
2017-09-30 23:06:14 -04:00
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
expect(subject.next).to eq second_value
|
2017-09-30 23:06:14 -04:00
|
|
|
end
|
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
it "has a next value equal to the 1st value after rewinding" do
|
|
|
|
subject.next
|
|
|
|
subject.rewind
|
2017-09-30 23:06:14 -04:00
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
expect(subject.next).to eq first_value
|
2017-09-30 23:06:14 -04:00
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Sequence do
|
2010-08-12 22:42:18 -04:00
|
|
|
describe "a basic sequence" do
|
2011-08-13 01:03:12 -04:00
|
|
|
let(:name) { :test }
|
2020-06-05 15:15:18 -04:00
|
|
|
subject { FactoryBot::Sequence.new(name) { |n| "=#{n}" } }
|
2011-05-19 10:56:45 -04:00
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
its(:name) { should eq name }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:names) { should eq [name] }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2017-09-30 23:06:14 -04:00
|
|
|
it_behaves_like "a sequence", first_value: "=1", second_value: "=2"
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2011-01-25 17:55:40 -05:00
|
|
|
|
2010-08-12 22:42:18 -04:00
|
|
|
describe "a custom sequence" do
|
2017-09-30 23:06:14 -04:00
|
|
|
subject { FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}" } }
|
2010-08-12 22:42:18 -04:00
|
|
|
|
2017-09-30 23:06:14 -04:00
|
|
|
it_behaves_like "a sequence", first_value: "=A", second_value: "=B"
|
2010-08-12 22:42:18 -04:00
|
|
|
end
|
2011-01-27 16:25:31 -05:00
|
|
|
|
2012-04-01 08:20:19 -04:00
|
|
|
describe "a sequence with aliases using default value" do
|
2017-09-30 23:06:14 -04:00
|
|
|
subject do
|
|
|
|
FactoryBot::Sequence.new(:test, aliases: [:alias, :other]) do |n|
|
|
|
|
"=#{n}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
it "has the expected names as its names" do
|
|
|
|
names = [:foo, :bar, :baz]
|
2020-06-05 15:15:18 -04:00
|
|
|
sequence = FactoryBot::Sequence.new(names.first, aliases: names.last(2)) {
|
2019-10-25 13:17:35 -04:00
|
|
|
"=#{n}"
|
2020-06-05 15:15:18 -04:00
|
|
|
}
|
2019-10-25 13:17:35 -04:00
|
|
|
|
|
|
|
expect(sequence.names).to eq names
|
|
|
|
end
|
2012-04-01 08:20:19 -04:00
|
|
|
|
2017-09-30 23:06:14 -04:00
|
|
|
it_behaves_like "a sequence", first_value: "=1", second_value: "=2"
|
2012-04-01 08:20:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "a sequence with custom value and aliases" do
|
2017-09-30 23:06:14 -04:00
|
|
|
subject do
|
|
|
|
FactoryBot::Sequence.new(:test, 3, aliases: [:alias, :other]) do |n|
|
|
|
|
"=#{n}"
|
|
|
|
end
|
2012-03-29 10:21:55 -04:00
|
|
|
end
|
2017-09-30 23:06:14 -04:00
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
it "has the expected names as its names" do
|
|
|
|
names = [:foo, :bar, :baz]
|
2020-06-05 15:15:18 -04:00
|
|
|
sequence = FactoryBot::Sequence.new(names.first, 3, aliases: names.last(2)) {
|
2019-10-25 13:17:35 -04:00
|
|
|
"=#{n}"
|
2020-06-05 15:15:18 -04:00
|
|
|
}
|
2019-10-25 13:17:35 -04:00
|
|
|
expect(sequence.names).to eq names
|
|
|
|
end
|
2017-09-30 23:06:14 -04:00
|
|
|
|
|
|
|
it_behaves_like "a sequence", first_value: "=3", second_value: "=4"
|
2012-03-29 10:21:55 -04:00
|
|
|
end
|
|
|
|
|
2011-01-27 16:25:31 -05:00
|
|
|
describe "a basic sequence without a block" do
|
2017-09-30 23:06:14 -04:00
|
|
|
subject { FactoryBot::Sequence.new(:name) }
|
2011-01-25 17:55:40 -05:00
|
|
|
|
2017-09-30 23:06:14 -04:00
|
|
|
it_behaves_like "a sequence", first_value: 1, second_value: 2
|
2011-01-27 16:25:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "a custom sequence without a block" do
|
2017-09-30 23:06:14 -04:00
|
|
|
subject { FactoryBot::Sequence.new(:name, "A") }
|
2011-01-27 16:25:31 -05:00
|
|
|
|
2017-09-30 23:06:14 -04:00
|
|
|
it_behaves_like "a sequence", first_value: "A", second_value: "B"
|
2011-01-27 16:25:31 -05:00
|
|
|
end
|
2012-05-13 01:54:41 -04:00
|
|
|
|
|
|
|
describe "iterating over items in an enumerator" do
|
2017-09-30 23:06:14 -04:00
|
|
|
subject do
|
|
|
|
FactoryBot::Sequence.new(:name, %w[foo bar].to_enum) { |n| "=#{n}" }
|
|
|
|
end
|
2012-05-13 01:54:41 -04:00
|
|
|
|
|
|
|
it "navigates to the next items until no items remain" do
|
2019-10-25 13:17:35 -04:00
|
|
|
sequence = FactoryBot::Sequence.new(:name, %w[foo bar].to_enum) { |n| "=#{n}" }
|
|
|
|
expect(sequence.next).to eq "=foo"
|
|
|
|
expect(sequence.next).to eq "=bar"
|
|
|
|
|
|
|
|
expect { sequence.next }.to raise_error(StopIteration)
|
2012-05-13 01:54:41 -04:00
|
|
|
end
|
2017-09-30 23:06:14 -04:00
|
|
|
|
|
|
|
it_behaves_like "a sequence", first_value: "=foo", second_value: "=bar"
|
2012-05-13 01:54:41 -04:00
|
|
|
end
|
2013-01-11 12:05:17 -05:00
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
it "a custom sequence and scope increments within the correct scope" do
|
|
|
|
sequence = FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}#{foo}" }
|
|
|
|
scope = double("scope", foo: "attribute")
|
2013-01-11 12:05:17 -05:00
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
expect(sequence.next(scope)).to eq "=Aattribute"
|
|
|
|
end
|
2013-01-11 12:05:17 -05:00
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
it "a custom sequence and scope increments within the correct scope when incrementing" do
|
|
|
|
sequence = FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}#{foo}" }
|
|
|
|
scope = double("scope", foo: "attribute")
|
|
|
|
sequence.next(scope)
|
2013-01-11 12:05:17 -05:00
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
expect(sequence.next(scope)).to eq "=Battribute"
|
|
|
|
end
|
2017-09-30 23:06:14 -04:00
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
it "a custom scope increments within the correct scope after rewinding" do
|
|
|
|
sequence = FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}#{foo}" }
|
|
|
|
scope = double("scope", foo: "attribute")
|
|
|
|
sequence.next(scope)
|
|
|
|
sequence.rewind
|
2017-09-30 23:06:14 -04:00
|
|
|
|
2019-10-25 13:17:35 -04:00
|
|
|
expect(sequence.next(scope)).to eq "=Aattribute"
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|