progenitor 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +162 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/progenitor/all.rb +3 -0
- data/lib/progenitor/factory.rb +28 -0
- data/lib/progenitor/no_such_factory_exception.rb +1 -0
- data/lib/progenitor/sequence.rb +26 -0
- data/progenitor.gemspec +82 -0
- data/test/foo.rb +4 -0
- data/test/foo_factory.rb +33 -0
- data/test/helper.rb +23 -0
- data/test/test_no_such_factory_exception.rb +9 -0
- data/test/test_progenitor.rb +78 -0
- data/test/test_sequence.rb +79 -0
- metadata +171 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "test-unit"
|
10
|
+
gem "mocha"
|
11
|
+
gem "shoulda", ">= 0"
|
12
|
+
gem "bundler", "~> 1.0.0"
|
13
|
+
gem "jeweler", "~> 1.5.2"
|
14
|
+
gem "rcov", ">= 0"
|
15
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Paul Hieromnimon
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
= progenitor
|
2
|
+
|
3
|
+
Progenitor is a factory gem. It can be used as a replacement for fixtures, or just as a unified interface for factories
|
4
|
+
in application code. Unlike Factory Girl, it doesn't assume your products inherit from ActiveRecord::Base, therefore
|
5
|
+
it can be used with objects of any type.
|
6
|
+
|
7
|
+
Also, it is without the bloat of Factory Girl (well under 100 lines of code, not including tests) because I don't try to
|
8
|
+
create a fancy DSL for assignments when a simple = operator will do the same job.
|
9
|
+
|
10
|
+
Finally, you can use Progenitor alongside Factory Girl so that you don't have to replace all your factories right away.
|
11
|
+
Just be to load it after Factory Girl. See the examples for details.
|
12
|
+
|
13
|
+
== Examples
|
14
|
+
=== Loading Progenitor
|
15
|
+
You need to load Progenitor and then load the individual factories.
|
16
|
+
|
17
|
+
# in Gemfile
|
18
|
+
gem 'progenitor'
|
19
|
+
|
20
|
+
|
21
|
+
If you're running a Rails app, add the following:
|
22
|
+
|
23
|
+
# in test/test_helper.rb
|
24
|
+
#load progenitor
|
25
|
+
require 'progenitor/all'
|
26
|
+
#then load each of the individual factories that you create, for example:
|
27
|
+
require 'factories/foo_factory'
|
28
|
+
require 'factories/bar_factory'
|
29
|
+
require 'factories/factory_that_makes_miniature_models_of_factories_factory'
|
30
|
+
|
31
|
+
=== Defining Factories
|
32
|
+
Let's say we want to create a factory for making instances of Foo
|
33
|
+
|
34
|
+
# in test/factories/foo_factory.rb
|
35
|
+
class FooFactory < Progenitor::Factory
|
36
|
+
|
37
|
+
#simplest case
|
38
|
+
create :foo do
|
39
|
+
Foo.new
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
Then, in a test or somewhere, call <tt>Factory :foo</tt> to produce an instance of Foo.
|
45
|
+
|
46
|
+
=== Sequences
|
47
|
+
You can use a sequence if you need to create a persistent object or something else that needs a serial number or ID.
|
48
|
+
|
49
|
+
Sequence can be used in concurrent environments. It synchronizes calls on a Mutex, so it is safe for preemptive
|
50
|
+
multitasking, and fail-fast when using cooperative multitasking (although all its methods release locks before
|
51
|
+
accepting blocks, so it would be very hard to try to reacquire the same lock, but if you somehow managed to, you'd get
|
52
|
+
bailed out by ThreadError)
|
53
|
+
|
54
|
+
# in test/factories/foo_factory.rb
|
55
|
+
class FooFactory < Progenitor::Factory
|
56
|
+
|
57
|
+
# Instantiate a Sequence and assign to a class-instance variable in your factory class
|
58
|
+
# IMPORTANT: do not instantiate the sequence inside the block you pass to create, or it will get reset each time you
|
59
|
+
# call your factory
|
60
|
+
@i = Progenitor::Sequence.new
|
61
|
+
create :numbered_foo do
|
62
|
+
foo = Foo.new
|
63
|
+
foo.id = @i.next_int # call next_int to get a sequence number
|
64
|
+
foo
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
foo0 = Factory :numbered_foo
|
70
|
+
foo0.id #=> 0
|
71
|
+
foo1 = Factory :numbered_foo
|
72
|
+
foo1.id #=> 1
|
73
|
+
|
74
|
+
You can also start sequences at whatever value you desire, and there's a convenience method for when you need the same
|
75
|
+
sequence number for multiple fields
|
76
|
+
|
77
|
+
# in test/factories/foo_factory.rb
|
78
|
+
class FooFactory < Progenitor::Factory
|
79
|
+
|
80
|
+
@i = Progenitor::Sequence.new 1 #the default starting point is 0, but you can set whatever you want
|
81
|
+
create :numbered_foo do
|
82
|
+
foo = Foo.new
|
83
|
+
@i.in_sequence do |n|
|
84
|
+
foo.id = n
|
85
|
+
foo.name = "name#{n}"
|
86
|
+
end
|
87
|
+
foo
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
foo1 = Factory :numbered_foo
|
93
|
+
foo1.id #=> 1
|
94
|
+
foo1.name #=> "name1"
|
95
|
+
|
96
|
+
foo2 = Factory :numbered_foo
|
97
|
+
foo2.id #=> 2
|
98
|
+
foo2.name #=> "name12"
|
99
|
+
|
100
|
+
=== Passing Arguments to Factories
|
101
|
+
We can get fancy and pass arguments to our factories. If you're using Ruby 1.9 you can even specify default arguments.
|
102
|
+
|
103
|
+
# in test/factories/foo_factory.rb
|
104
|
+
class FooFactory < Progenitor::Factory
|
105
|
+
|
106
|
+
#passing arguments
|
107
|
+
create :foo do |bar, baz=3|
|
108
|
+
foo = Foo.new
|
109
|
+
foo.bar = bar
|
110
|
+
foo.baz = baz
|
111
|
+
foo
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
foo = Factory :foo, "something"
|
117
|
+
foo.bar #=> "something"
|
118
|
+
foo.baz #=> 3
|
119
|
+
|
120
|
+
You can even pass blocks to your factories:
|
121
|
+
|
122
|
+
# in test/factories/foo_factory.rb
|
123
|
+
class FooFactory < Progenitor::Factory
|
124
|
+
|
125
|
+
#passing a block
|
126
|
+
create :foo do |arg, &block|
|
127
|
+
foo = Foo.new
|
128
|
+
block.call "in a block"
|
129
|
+
foo
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
foo = Factory :foo, :some_arg do |say_what|
|
135
|
+
puts say_what
|
136
|
+
end
|
137
|
+
|
138
|
+
# "in a block" will be displayed
|
139
|
+
|
140
|
+
=== More Examples
|
141
|
+
See test/test_progenitor.rb for more examples
|
142
|
+
|
143
|
+
== Factory Girl integration
|
144
|
+
If you'd ever want to, you can use Progenitor with Factory Girl. Just make sure that Factory Girl is loaded first,
|
145
|
+
then when you call <tt>Factory :something</tt>, Progenitor will first see if it knows how to create <tt>:something</tt>
|
146
|
+
and if not, will delegate to Factory Girl
|
147
|
+
|
148
|
+
== Contributing to progenitor
|
149
|
+
|
150
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
151
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
152
|
+
* Fork the project
|
153
|
+
* Start a feature/bugfix branch
|
154
|
+
* Commit and push until you are happy with your contribution
|
155
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
156
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
157
|
+
|
158
|
+
== Copyright
|
159
|
+
|
160
|
+
Copyright (c) 2011 Paul Hieromnimon. See LICENSE.txt for
|
161
|
+
further details.
|
162
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "progenitor"
|
16
|
+
gem.homepage = "http://github.com/pavlos/progenitor"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = "Extremely lightweight yet full featured factory gem."
|
19
|
+
gem.description = "Progenitor is a lightweight gem for building factories, especially useful for testing, but
|
20
|
+
there is no reason why it can't be used in application code. Unlike Factory Girl, Progenitor
|
21
|
+
makes no assumption that products are instances of ActiveRecord::Base - it can be used to produce\
|
22
|
+
objects of any type."
|
23
|
+
gem.email = "paul.hieromnimon@gmail.com"
|
24
|
+
gem.authors = ["Paul Hieromnimon"]
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
end
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "progenitor #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'progenitor/no_such_factory_exception'
|
2
|
+
|
3
|
+
module Progenitor
|
4
|
+
class Factory
|
5
|
+
|
6
|
+
@@factories = {}
|
7
|
+
|
8
|
+
def self.factories
|
9
|
+
@@factories
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.create(name, &block)
|
13
|
+
@@factories[name] = block
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def Factory(*args, &block)
|
20
|
+
if Progenitor::Factory.factories.has_key? args[0]
|
21
|
+
name = args.shift
|
22
|
+
Progenitor::Factory.factories[name].call(*args, &block)
|
23
|
+
elsif defined?(::Factory) && (::Factory.respond_to? :default_strategy) #use along side Factory Girl
|
24
|
+
::Factory.default_strategy *args, &block
|
25
|
+
else
|
26
|
+
raise NoSuchFactoryException, "You haven't defined a factory called #{args[0]}"
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
class NoSuchFactoryException < ArgumentError; end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Progenitor
|
2
|
+
class Sequence
|
3
|
+
|
4
|
+
def initialize(start=0)
|
5
|
+
@lock = Mutex.new
|
6
|
+
@current = start
|
7
|
+
end
|
8
|
+
|
9
|
+
def next_int
|
10
|
+
@lock.synchronize do
|
11
|
+
value = @current
|
12
|
+
@current += 1
|
13
|
+
value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def in_sequence(&block)
|
18
|
+
value = next_int
|
19
|
+
block.call value
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
attr_reader :lock # here to be accessed via #send for testing purposes
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
data/progenitor.gemspec
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{progenitor}
|
8
|
+
s.version = "0.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Paul Hieromnimon"]
|
12
|
+
s.date = %q{2011-03-07}
|
13
|
+
s.description = %q{Progenitor is a lightweight gem for building factories, especially useful for testing, but
|
14
|
+
there is no reason why it can't be used in application code. Unlike Factory Girl, Progenitor
|
15
|
+
makes no assumption that products are instances of ActiveRecord::Base - it can be used to produce objects of any type.}
|
16
|
+
s.email = %q{paul.hieromnimon@gmail.com}
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.rdoc"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
"Gemfile",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/progenitor/all.rb",
|
29
|
+
"lib/progenitor/factory.rb",
|
30
|
+
"lib/progenitor/no_such_factory_exception.rb",
|
31
|
+
"lib/progenitor/sequence.rb",
|
32
|
+
"progenitor.gemspec",
|
33
|
+
"test/foo.rb",
|
34
|
+
"test/foo_factory.rb",
|
35
|
+
"test/helper.rb",
|
36
|
+
"test/test_no_such_factory_exception.rb",
|
37
|
+
"test/test_progenitor.rb",
|
38
|
+
"test/test_sequence.rb"
|
39
|
+
]
|
40
|
+
s.homepage = %q{http://github.com/pavlos/progenitor}
|
41
|
+
s.licenses = ["MIT"]
|
42
|
+
s.require_paths = ["lib"]
|
43
|
+
s.rubygems_version = %q{1.3.7}
|
44
|
+
s.summary = %q{Extremely lightweight yet full featured factory gem.}
|
45
|
+
s.test_files = [
|
46
|
+
"test/foo.rb",
|
47
|
+
"test/foo_factory.rb",
|
48
|
+
"test/helper.rb",
|
49
|
+
"test/test_no_such_factory_exception.rb",
|
50
|
+
"test/test_progenitor.rb",
|
51
|
+
"test/test_sequence.rb"
|
52
|
+
]
|
53
|
+
|
54
|
+
if s.respond_to? :specification_version then
|
55
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
56
|
+
s.specification_version = 3
|
57
|
+
|
58
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
59
|
+
s.add_development_dependency(%q<test-unit>, [">= 0"])
|
60
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
61
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
62
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
64
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
65
|
+
else
|
66
|
+
s.add_dependency(%q<test-unit>, [">= 0"])
|
67
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
68
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
69
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
70
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
71
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
72
|
+
end
|
73
|
+
else
|
74
|
+
s.add_dependency(%q<test-unit>, [">= 0"])
|
75
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
76
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
77
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
78
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
79
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
data/test/foo.rb
ADDED
data/test/foo_factory.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
class FooFactory < Progenitor::Factory
|
2
|
+
|
3
|
+
create :foo do
|
4
|
+
foo = Foo.new
|
5
|
+
foo
|
6
|
+
end
|
7
|
+
|
8
|
+
@i = Progenitor::Sequence.new
|
9
|
+
create :numbered_foo do
|
10
|
+
foo = Foo.new
|
11
|
+
foo.id = @i.next_int
|
12
|
+
foo
|
13
|
+
end
|
14
|
+
|
15
|
+
@j = Progenitor::Sequence.new
|
16
|
+
create :doubly_numbered_foo do
|
17
|
+
foo = Foo.new
|
18
|
+
@j.in_sequence do |i|
|
19
|
+
foo.id = i
|
20
|
+
foo.id2 = i
|
21
|
+
end
|
22
|
+
foo
|
23
|
+
end
|
24
|
+
|
25
|
+
create :something_that_takes_args do |*args|
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
create :something_that_takes_args_and_a_block do |*args, &block|
|
30
|
+
block.call
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
require 'mocha'
|
13
|
+
|
14
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
15
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
16
|
+
require 'progenitor/factory'
|
17
|
+
require 'progenitor/sequence'
|
18
|
+
require 'progenitor/no_such_factory_exception'
|
19
|
+
require 'foo'
|
20
|
+
require 'foo_factory'
|
21
|
+
|
22
|
+
class Test::Unit::TestCase
|
23
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestProgenitor < Test::Unit::TestCase
|
4
|
+
|
5
|
+
should "not fail" do
|
6
|
+
Factory :foo
|
7
|
+
end
|
8
|
+
|
9
|
+
should "produce an instance of foo" do
|
10
|
+
foo = Factory :foo
|
11
|
+
assert foo.instance_of? Foo
|
12
|
+
end
|
13
|
+
|
14
|
+
should "raise exception if not found" do
|
15
|
+
assert_raise NoSuchFactoryException do
|
16
|
+
Factory :nothing
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
should "pass along params" do
|
21
|
+
Progenitor::Factory.factories[:something_that_takes_args].expects(:call).with(:foo, :bar)
|
22
|
+
Factory :something_that_takes_args, :foo, :bar
|
23
|
+
end
|
24
|
+
|
25
|
+
should "pass along block" do
|
26
|
+
result = Factory :something_that_takes_args_and_a_block, :foo, :bar do
|
27
|
+
:return_value
|
28
|
+
end
|
29
|
+
assert_equal :return_value, result
|
30
|
+
end
|
31
|
+
|
32
|
+
context "factory with a sequence" do
|
33
|
+
setup do
|
34
|
+
# need to reload this to reset the sequence
|
35
|
+
load 'foo_factory.rb'
|
36
|
+
end
|
37
|
+
|
38
|
+
context "numbered foo" do
|
39
|
+
should "produce a numbered object" do
|
40
|
+
foo = Factory :numbered_foo
|
41
|
+
assert_equal 0, foo.id
|
42
|
+
end
|
43
|
+
|
44
|
+
should "maintain its value between invocations" do
|
45
|
+
foo = Factory :numbered_foo
|
46
|
+
assert_equal 0, foo.id
|
47
|
+
foo = Factory :numbered_foo
|
48
|
+
assert_equal 1, foo.id
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "doubly numbered foo" do
|
53
|
+
should "produce a doubly numbered object" do
|
54
|
+
foo = Factory :doubly_numbered_foo
|
55
|
+
assert_equal 0, foo.id
|
56
|
+
assert_equal foo.id, foo.id2
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when used with FactoryGirl" do
|
62
|
+
|
63
|
+
should "delegate to factory girl if it is loaded" do
|
64
|
+
::Factory = mock('factory girl') do
|
65
|
+
expects(:respond_to?).with(:default_strategy).returns(true)
|
66
|
+
expects(:default_strategy).with :non_existant_factory
|
67
|
+
end
|
68
|
+
|
69
|
+
Factory :non_existant_factory
|
70
|
+
end
|
71
|
+
|
72
|
+
teardown do
|
73
|
+
Object.send :remove_const, "Factory"
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestSequence < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "a sequence built with no args" do
|
6
|
+
setup do
|
7
|
+
@sequence = Progenitor::Sequence.new
|
8
|
+
end
|
9
|
+
context "next_int" do
|
10
|
+
should "start at zero by default" do
|
11
|
+
assert_equal 0, @sequence.next_int
|
12
|
+
end
|
13
|
+
|
14
|
+
should "go up by one each time it is called" do
|
15
|
+
assert_equal 0, @sequence.next_int
|
16
|
+
assert_equal 1, @sequence.next_int
|
17
|
+
assert_equal 2, @sequence.next_int
|
18
|
+
assert_equal 3, @sequence.next_int
|
19
|
+
end
|
20
|
+
|
21
|
+
should "synchronize next_int on a mutex" do
|
22
|
+
Mutex.any_instance.expects(:synchronize)
|
23
|
+
@sequence.next_int
|
24
|
+
end
|
25
|
+
|
26
|
+
should "start where in_sequence left off" do
|
27
|
+
@sequence.in_sequence{}
|
28
|
+
assert_equal 1, @sequence.next_int
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "in_sequence" do
|
33
|
+
|
34
|
+
should "yield to its block, starting at 0" do
|
35
|
+
@sequence.in_sequence do |i|
|
36
|
+
assert_equal 0, i
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
should "increment its sequence number on a successive invocation" do
|
41
|
+
@sequence.in_sequence {}
|
42
|
+
@sequence.in_sequence do |i|
|
43
|
+
assert_equal 1, i
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
should "start where next_int left off" do
|
48
|
+
@sequence.next_int
|
49
|
+
@sequence.in_sequence do |i|
|
50
|
+
assert_equal 1, i
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
should "release its lock before calling the block" do
|
55
|
+
@sequence.send(:lock).expects(:lock)
|
56
|
+
@sequence.in_sequence do
|
57
|
+
assert_false @sequence.send(:lock).locked?
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "a sequence with a starting value specified" do
|
64
|
+
setup do
|
65
|
+
@sequence = Progenitor::Sequence.new 42
|
66
|
+
end
|
67
|
+
|
68
|
+
should "start at that value" do
|
69
|
+
assert_equal 42, @sequence.next_int
|
70
|
+
end
|
71
|
+
|
72
|
+
should "go up by one each time it is called" do
|
73
|
+
assert_equal 42, @sequence.next_int
|
74
|
+
assert_equal 43, @sequence.next_int
|
75
|
+
assert_equal 44, @sequence.next_int
|
76
|
+
assert_equal 45, @sequence.next_int
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: progenitor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 0.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Paul Hieromnimon
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-03-07 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: test-unit
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
prerelease: false
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: mocha
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
segments:
|
41
|
+
- 0
|
42
|
+
version: "0"
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: shoulda
|
48
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *id003
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: bundler
|
61
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ~>
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
segments:
|
67
|
+
- 1
|
68
|
+
- 0
|
69
|
+
- 0
|
70
|
+
version: 1.0.0
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: *id004
|
74
|
+
- !ruby/object:Gem::Dependency
|
75
|
+
name: jeweler
|
76
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ~>
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
segments:
|
82
|
+
- 1
|
83
|
+
- 5
|
84
|
+
- 2
|
85
|
+
version: 1.5.2
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: *id005
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rcov
|
91
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
version: "0"
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *id006
|
102
|
+
description: |-
|
103
|
+
Progenitor is a lightweight gem for building factories, especially useful for testing, but
|
104
|
+
there is no reason why it can't be used in application code. Unlike Factory Girl, Progenitor
|
105
|
+
makes no assumption that products are instances of ActiveRecord::Base - it can be used to produce objects of any type.
|
106
|
+
email: paul.hieromnimon@gmail.com
|
107
|
+
executables: []
|
108
|
+
|
109
|
+
extensions: []
|
110
|
+
|
111
|
+
extra_rdoc_files:
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.rdoc
|
114
|
+
files:
|
115
|
+
- .document
|
116
|
+
- Gemfile
|
117
|
+
- LICENSE.txt
|
118
|
+
- README.rdoc
|
119
|
+
- Rakefile
|
120
|
+
- VERSION
|
121
|
+
- lib/progenitor/all.rb
|
122
|
+
- lib/progenitor/factory.rb
|
123
|
+
- lib/progenitor/no_such_factory_exception.rb
|
124
|
+
- lib/progenitor/sequence.rb
|
125
|
+
- progenitor.gemspec
|
126
|
+
- test/foo.rb
|
127
|
+
- test/foo_factory.rb
|
128
|
+
- test/helper.rb
|
129
|
+
- test/test_no_such_factory_exception.rb
|
130
|
+
- test/test_progenitor.rb
|
131
|
+
- test/test_sequence.rb
|
132
|
+
has_rdoc: true
|
133
|
+
homepage: http://github.com/pavlos/progenitor
|
134
|
+
licenses:
|
135
|
+
- MIT
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
hash: -559232159
|
147
|
+
segments:
|
148
|
+
- 0
|
149
|
+
version: "0"
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
+
none: false
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
segments:
|
156
|
+
- 0
|
157
|
+
version: "0"
|
158
|
+
requirements: []
|
159
|
+
|
160
|
+
rubyforge_project:
|
161
|
+
rubygems_version: 1.3.7
|
162
|
+
signing_key:
|
163
|
+
specification_version: 3
|
164
|
+
summary: Extremely lightweight yet full featured factory gem.
|
165
|
+
test_files:
|
166
|
+
- test/foo.rb
|
167
|
+
- test/foo_factory.rb
|
168
|
+
- test/helper.rb
|
169
|
+
- test/test_no_such_factory_exception.rb
|
170
|
+
- test/test_progenitor.rb
|
171
|
+
- test/test_sequence.rb
|