Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: thoughtbot/factory_girl
...
head fork: spartan-developer/factory_girl
compare: enumerator_sequences
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 29 additions and 3 deletions.
  1. +16 −2 lib/factory_girl/sequence.rb
  2. +13 −1 spec/factory_girl/sequence_spec.rb
View
18 lib/factory_girl/sequence.rb
@@ -16,13 +16,27 @@ def initialize(name, *args, &proc)
end
def next
- @proc ? @proc.call(@value) : @value
+ @proc ? @proc.call(current_value) : current_value
ensure
- @value = @value.next
+ increment_value
end
def names
[@name] + @aliases
end
+
+ private
+
+ def current_value
+ @value.is_a?(Enumerator) ? @value.peek : @value
+ end
+
+ def increment_value
+ if @value.is_a?(Enumerator)
+ @value.next
+ else
+ @value = @value.next
+ end
+ end
end
end
View
14 spec/factory_girl/sequence_spec.rb
@@ -15,7 +15,7 @@
end
end
- describe "a custom sequence" do
+ describe "a custom sequence with string elements" do
subject { FactoryGirl::Sequence.new(:name, "A") {|n| "=#{n}" } }
its(:next) { should == "=A" }
@@ -25,6 +25,18 @@
end
end
+ describe "a custom sequence with an Enumerator" do
+ subject { FactoryGirl::Sequence.new(:name, %w{foo bar baz}.cycle) {|n| "=#{n}" } }
+ its(:next) { should == "=foo" }
+
+ it "should enumerate properly" do
+ subject.next.should == "=foo"
+ subject.next.should == "=bar"
+ subject.next.should == "=baz"
+ subject.next.should == "=foo"
+ end
+ end
+
describe "a sequence with aliases using default value" do
subject { FactoryGirl::Sequence.new(:test, aliases: [:alias, :other]) { |n| "=#{n}" } }
its(:next) { should == "=1" }

No commit comments for this range

Something went wrong with that request. Please try again.