indirect-machinist 2.0.0.beta3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +47 -0
- data/MIT-LICENSE +21 -0
- data/README.markdown +295 -0
- data/Rakefile +30 -0
- data/VERSION +1 -0
- data/lib/generators/machinist/install/USAGE +2 -0
- data/lib/generators/machinist/install/install_generator.rb +46 -0
- data/lib/generators/machinist/install/templates/blueprints.rb +9 -0
- data/lib/generators/machinist/install/templates/machinist.rb.erb +7 -0
- data/lib/generators/machinist/model/model_generator.rb +13 -0
- data/lib/machinist.rb +5 -0
- data/lib/machinist/active_record.rb +14 -0
- data/lib/machinist/active_record/blueprint.rb +16 -0
- data/lib/machinist/active_record/lathe.rb +24 -0
- data/lib/machinist/blueprint.rb +89 -0
- data/lib/machinist/exceptions.rb +32 -0
- data/lib/machinist/lathe.rb +68 -0
- data/lib/machinist/machinable.rb +95 -0
- data/lib/machinist/railtie.rb +5 -0
- data/lib/machinist/version.rb +3 -0
- data/machinist.gemspec +27 -0
- data/spec/active_record_spec.rb +108 -0
- data/spec/blueprint_spec.rb +76 -0
- data/spec/exceptions_spec.rb +20 -0
- data/spec/inheritance_spec.rb +104 -0
- data/spec/machinable_spec.rb +101 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/support/active_record_environment.rb +65 -0
- metadata +189 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
machinist (2.0.0.beta2)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (3.0.9)
|
10
|
+
activesupport (= 3.0.9)
|
11
|
+
builder (~> 2.1.2)
|
12
|
+
i18n (~> 0.5.0)
|
13
|
+
activerecord (3.0.9)
|
14
|
+
activemodel (= 3.0.9)
|
15
|
+
activesupport (= 3.0.9)
|
16
|
+
arel (~> 2.0.10)
|
17
|
+
tzinfo (~> 0.3.23)
|
18
|
+
activesupport (3.0.9)
|
19
|
+
arel (2.0.10)
|
20
|
+
builder (2.1.2)
|
21
|
+
diff-lcs (1.1.2)
|
22
|
+
i18n (0.5.0)
|
23
|
+
mysql (2.8.1)
|
24
|
+
rake (0.9.2)
|
25
|
+
rcov (0.9.9)
|
26
|
+
rdoc (3.6.1)
|
27
|
+
rspec (2.6.0)
|
28
|
+
rspec-core (~> 2.6.0)
|
29
|
+
rspec-expectations (~> 2.6.0)
|
30
|
+
rspec-mocks (~> 2.6.0)
|
31
|
+
rspec-core (2.6.4)
|
32
|
+
rspec-expectations (2.6.0)
|
33
|
+
diff-lcs (~> 1.1.2)
|
34
|
+
rspec-mocks (2.6.0)
|
35
|
+
tzinfo (0.3.28)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
activerecord
|
42
|
+
machinist!
|
43
|
+
mysql
|
44
|
+
rake
|
45
|
+
rcov
|
46
|
+
rdoc
|
47
|
+
rspec
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2008-2010 Peter Yandell
|
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.
|
21
|
+
|
data/README.markdown
ADDED
@@ -0,0 +1,295 @@
|
|
1
|
+
# Machinist 2
|
2
|
+
|
3
|
+
*Fixtures aren't fun. Machinist is.*
|
4
|
+
|
5
|
+
Machinist 2 is **still in beta**!
|
6
|
+
|
7
|
+
If you're using Rails 3, you'll want to give Machinist 2 a go, but be aware
|
8
|
+
that the documentation is still patchy.
|
9
|
+
|
10
|
+
That said, have a look at [the
|
11
|
+
specs](https://github.com/notahat/machinist/tree/master/spec), starting with
|
12
|
+
[the spec for
|
13
|
+
Machinable](https://github.com/notahat/machinist/blob/master/spec/machinable_spec.rb).
|
14
|
+
No, really, have a look. I wrote this code to be read, and the specs do a
|
15
|
+
pretty clean job of documenting what it all does.
|
16
|
+
|
17
|
+
If, on the other hand, you want the tried, tested, and well-documented official
|
18
|
+
release version of Machinist, [then go with Machinist
|
19
|
+
1](http://github.com/notahat/machinist/tree/1.0-maintenance).
|
20
|
+
|
21
|
+
- [Home page](http://github.com/notahat/machinist)
|
22
|
+
- [Google group](http://groups.google.com/group/machinist-users), for support
|
23
|
+
- [Bug tracker](http://github.com/notahat/machinist/issues), for reporting Machinist bugs
|
24
|
+
|
25
|
+
|
26
|
+
## Introduction
|
27
|
+
|
28
|
+
Machinist makes it easy to create objects for use in tests. It generates data
|
29
|
+
for the attributes you don't care about, and constructs any necessary
|
30
|
+
associated objects, leaving you to specify only the fields you care about in
|
31
|
+
your test. For example:
|
32
|
+
|
33
|
+
describe Comment, "without_spam scope" do
|
34
|
+
it "doesn't include spam" do
|
35
|
+
# This will make a Comment, a Post, and a User (the author of the
|
36
|
+
# Post), generate values for all their attributes, and save them:
|
37
|
+
spam = Comment.make!(:spam => true)
|
38
|
+
|
39
|
+
Comment.without_spam.should_not include(spam)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
You tell Machinist how to do this with blueprints:
|
44
|
+
|
45
|
+
require 'machinist/active_record'
|
46
|
+
|
47
|
+
User.blueprint do
|
48
|
+
username { "user#{sn}" } # Each user gets a unique serial number.
|
49
|
+
end
|
50
|
+
|
51
|
+
Post.blueprint do
|
52
|
+
author
|
53
|
+
title { "Post #{sn}" }
|
54
|
+
body { "Lorem ipsum..." }
|
55
|
+
end
|
56
|
+
|
57
|
+
Comment.blueprint do
|
58
|
+
post
|
59
|
+
email { "commenter#{sn}@example.com" }
|
60
|
+
body { "Lorem ipsum..." }
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
## Installation
|
65
|
+
|
66
|
+
### Upgrading from Machinist 1
|
67
|
+
|
68
|
+
See [the wiki](http://wiki.github.com/notahat/machinist/machinist-2).
|
69
|
+
|
70
|
+
### Rails 3
|
71
|
+
|
72
|
+
In your app's `Gemfile`, in the `group :test` section, add:
|
73
|
+
|
74
|
+
gem 'machinist', '>= 2.0.0.beta2'
|
75
|
+
|
76
|
+
Then run:
|
77
|
+
|
78
|
+
bundle
|
79
|
+
rails generate machinist:install
|
80
|
+
|
81
|
+
If you want Machinist to automatically add a blueprint to your blueprints file
|
82
|
+
whenever you generate a model, add the following to your `config/application.rb`
|
83
|
+
inside the Application class:
|
84
|
+
|
85
|
+
config.generators do |g|
|
86
|
+
g.fixture_replacement :machinist
|
87
|
+
end
|
88
|
+
|
89
|
+
### Rails 2
|
90
|
+
|
91
|
+
See [the wiki](http://wiki.github.com/notahat/machinist/rails-2).
|
92
|
+
|
93
|
+
|
94
|
+
## Usage
|
95
|
+
|
96
|
+
### Blueprints
|
97
|
+
|
98
|
+
A blueprint describes how to generate an object. The blueprint takes care of
|
99
|
+
providing attributes that your test doesn't care about, leaving you to focus on
|
100
|
+
just the attributes that are important for the test.
|
101
|
+
|
102
|
+
A simple blueprint might look like this:
|
103
|
+
|
104
|
+
Post.blueprint do
|
105
|
+
title { "A Post" }
|
106
|
+
body { "Lorem ipsum..." }
|
107
|
+
end
|
108
|
+
|
109
|
+
You can then construct a Post from this blueprint with:
|
110
|
+
|
111
|
+
Post.make!
|
112
|
+
|
113
|
+
When you call `make!`, Machinist calls `Post.new`, then runs through the
|
114
|
+
attributes in your blueprint, calling the block for each attribute to generate
|
115
|
+
a value. It then saves and reloads the Post. (It throws an exception if the
|
116
|
+
Post can't be saved.)
|
117
|
+
|
118
|
+
You can override values defined in the blueprint by passing a hash to make:
|
119
|
+
|
120
|
+
Post.make!(:title => "A Specific Title")
|
121
|
+
|
122
|
+
If you want to generate an object without saving it to the database, replace
|
123
|
+
`make!` with `make`.
|
124
|
+
|
125
|
+
|
126
|
+
### Unique Attributes
|
127
|
+
|
128
|
+
For attributes that need to be unique, you can call the `sn` method from
|
129
|
+
within the attribute block to get a unique serial number for the object.
|
130
|
+
|
131
|
+
User.blueprint do
|
132
|
+
username { "user-#{sn}" }
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
### Associations
|
137
|
+
|
138
|
+
If your object needs associated objects, you can generate them like this:
|
139
|
+
|
140
|
+
Comment.blueprint do
|
141
|
+
post { Post.make }
|
142
|
+
end
|
143
|
+
|
144
|
+
Calling `Comment.make!` will construct a Comment and its associated Post, and
|
145
|
+
save both.
|
146
|
+
|
147
|
+
Machinist is smart enough to look at the association and work out what sort of
|
148
|
+
object it needs to create, so you can shorten the above blueprint to:
|
149
|
+
|
150
|
+
Comment.blueprint do
|
151
|
+
post
|
152
|
+
end
|
153
|
+
|
154
|
+
If you want to override the value for post when constructing the comment, you
|
155
|
+
can do this:
|
156
|
+
|
157
|
+
post = Post.make(:title => "A particular title)
|
158
|
+
comment = Comment.make(:post => post)
|
159
|
+
|
160
|
+
|
161
|
+
For `has_many` and `has_and_belongs_to_many` associations, you can create
|
162
|
+
multiple associated objects like this:
|
163
|
+
|
164
|
+
Post.blueprint do
|
165
|
+
comments(3) # Makes 3 comments.
|
166
|
+
end
|
167
|
+
|
168
|
+
|
169
|
+
### Named Blueprints
|
170
|
+
|
171
|
+
Named blueprints let you define variations on an object. For example, suppose
|
172
|
+
some of your Users are administrators:
|
173
|
+
|
174
|
+
User.blueprint do
|
175
|
+
name { "User #{sn}" }
|
176
|
+
email { "user-#{sn}@example.com" }
|
177
|
+
end
|
178
|
+
|
179
|
+
User.blueprint(:admin) do
|
180
|
+
name { "Admin User #{sn}" }
|
181
|
+
admin { true }
|
182
|
+
end
|
183
|
+
|
184
|
+
Calling:
|
185
|
+
|
186
|
+
User.make!(:admin)
|
187
|
+
|
188
|
+
will use the `:admin` blueprint.
|
189
|
+
|
190
|
+
Named blueprints call the default blueprint to set any attributes not
|
191
|
+
specifically provided, so in this example the `email` attribute will still be
|
192
|
+
generated even for an admin user.
|
193
|
+
|
194
|
+
You must define a default blueprint for any class that has a named blueprint,
|
195
|
+
even if the default blueprint is empty.
|
196
|
+
|
197
|
+
|
198
|
+
### Blueprints on Plain Old Ruby Objects
|
199
|
+
|
200
|
+
Machinist also works with plain old Ruby objects. Let's say you have a class like:
|
201
|
+
|
202
|
+
class Post
|
203
|
+
extend Machinist::Machinable
|
204
|
+
|
205
|
+
attr_accessor :title
|
206
|
+
attr_accessor :body
|
207
|
+
end
|
208
|
+
|
209
|
+
You can blueprint the Post class just like anything else:
|
210
|
+
|
211
|
+
Post.blueprint do
|
212
|
+
title { "A title!" }
|
213
|
+
body { "A body!" }
|
214
|
+
end
|
215
|
+
|
216
|
+
And `Post.make` will construct a new Post.
|
217
|
+
|
218
|
+
|
219
|
+
### Other Tricks
|
220
|
+
|
221
|
+
You can refer to already assigned attributes when constructing a new attribute:
|
222
|
+
|
223
|
+
Post.blueprint do
|
224
|
+
author { "Author #{sn}" }
|
225
|
+
body { "Post by #{object.author}" }
|
226
|
+
end
|
227
|
+
|
228
|
+
|
229
|
+
## Compatibility
|
230
|
+
|
231
|
+
I've tested this with:
|
232
|
+
|
233
|
+
Ruby versions: 1.8.7, 1.9.2
|
234
|
+
Rails versions: 2.3, 3.0
|
235
|
+
|
236
|
+
It may well be happy with other versions too, but I'm not promising anything.
|
237
|
+
Compatibility patches are welcome.
|
238
|
+
|
239
|
+
|
240
|
+
## Developing
|
241
|
+
|
242
|
+
The Machinist specs and source code were written to be read, and I'm pretty
|
243
|
+
happy with them. Don't be afraid to have a look under the hood!
|
244
|
+
|
245
|
+
If you want to submit a patch:
|
246
|
+
|
247
|
+
- Fork the project.
|
248
|
+
- Make your feature addition or bug fix.
|
249
|
+
- Add tests for it. This is important so I don't break it in a
|
250
|
+
future version unintentionally.
|
251
|
+
- Commit, do not mess with rakefile, version, or history.
|
252
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
253
|
+
- Send me a pull request. Bonus points for topic branches.
|
254
|
+
|
255
|
+
|
256
|
+
## Status
|
257
|
+
|
258
|
+
In active use in a number of large Rails 2 apps.
|
259
|
+
|
260
|
+
Development has been sporadic, but is picking up again.
|
261
|
+
|
262
|
+
|
263
|
+
## Contributors
|
264
|
+
|
265
|
+
Machinist is maintained by Pete Yandell ([pete@notahat.com](mailto:pete@notahat.com), [@notahat](http://twitter.com/notahat))
|
266
|
+
|
267
|
+
Other contributors include:
|
268
|
+
|
269
|
+
[Marcos Arias](http://github.com/yizzreel),
|
270
|
+
[Jack Dempsey](http://github.com/jackdempsey),
|
271
|
+
[Jeremy Durham](http://github.com/jeremydurham),
|
272
|
+
[Clinton Forbes](http://github.com/clinton),
|
273
|
+
[Perryn Fowler](http://github.com/perryn),
|
274
|
+
[Niels Ganser](http://github.com/Nielsomat),
|
275
|
+
[Jeremy Grant](http://github.com/jeremygrant),
|
276
|
+
[Jon Guymon](http://github.com/gnarg),
|
277
|
+
[James Healy](http://github.com/yob),
|
278
|
+
[Ben Hoskings](http://github.com/benhoskings),
|
279
|
+
[Evan David Light](http://github.com/elight),
|
280
|
+
[Chris Lloyd](http://github.com/chrislloyd),
|
281
|
+
[Adam Meehan](http://github.com/adzap),
|
282
|
+
[Kyle Neath](http://github.com/kneath),
|
283
|
+
[Lawrence Pit](http://github.com/lawrencepit),
|
284
|
+
[Xavier Shay](http://github.com/xaviershay),
|
285
|
+
[T.J. Sheehy](http://github.com/tjsheehy),
|
286
|
+
[Roland Swingler](http://github.com/knaveofdiamonds),
|
287
|
+
[Gareth Townsend](http://github.com/quamen),
|
288
|
+
[Matt Wastrodowski](http://github.com/towski),
|
289
|
+
[Ian White](http://github.com/ianwhite)
|
290
|
+
|
291
|
+
Thanks to Thoughtbot's [Factory
|
292
|
+
Girl](http://github.com/thoughtbot/factory_girl/tree/master). Machinist was
|
293
|
+
written because I loved the idea behind Factory Girl, but I thought the
|
294
|
+
philosophy wasn't quite right, and I hated the syntax.
|
295
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
require 'rake'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new
|
11
|
+
|
12
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
13
|
+
spec.rcov = true
|
14
|
+
spec.rcov_opts = ['--exclude', 'spec', '--exclude', '.rvm']
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Run the specs.'
|
18
|
+
task :default => :rcov
|
19
|
+
|
20
|
+
|
21
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
22
|
+
rdoc.rdoc_dir = 'doc'
|
23
|
+
rdoc.title = 'Machinist'
|
24
|
+
rdoc.options << '--line-numbers'
|
25
|
+
rdoc.rdoc_files.include('lib')
|
26
|
+
end
|
27
|
+
|
28
|
+
task :notes do
|
29
|
+
system "grep -n -r 'FIXME\\|TODO' lib spec"
|
30
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0.beta2
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Machinist
|
2
|
+
module Generators #:nodoc:
|
3
|
+
class InstallGenerator < Rails::Generators::Base #:nodoc:
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
class_option :test_framework, :type => :string, :aliases => "-t", :desc => "Test framework to use Machinist with"
|
7
|
+
class_option :cucumber, :type => :boolean, :desc => "Set up access to Machinist from Cucumber"
|
8
|
+
|
9
|
+
def blueprints_file
|
10
|
+
if rspec?
|
11
|
+
copy_file "blueprints.rb", "spec/support/blueprints.rb"
|
12
|
+
else
|
13
|
+
copy_file "blueprints.rb", "test/blueprints.rb"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_helper
|
18
|
+
if test_unit?
|
19
|
+
inject_into_file("test/test_helper.rb", :after => "require 'rails/test_help'\n") do
|
20
|
+
"require File.expand_path(File.dirname(__FILE__) + '/blueprints')\n"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def cucumber_support
|
26
|
+
if cucumber?
|
27
|
+
template "machinist.rb.erb", "features/support/machinist.rb"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def rspec?
|
34
|
+
options[:test_framework].to_sym == :rspec
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_unit?
|
38
|
+
options[:test_framework].to_sym == :test_unit
|
39
|
+
end
|
40
|
+
|
41
|
+
def cucumber?
|
42
|
+
options[:cucumber]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|