cucumber_factory 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +5 -1
- data/VERSION +1 -1
- data/cucumber_factory.gemspec +4 -2
- data/lib/cucumber_factory/factory.rb +98 -96
- data/spec/app_root/app/models/benutzer.rb +2 -0
- data/spec/factory_spec.rb +10 -0
- metadata +3 -1
data/README.rdoc
CHANGED
@@ -16,7 +16,7 @@ Create a step definition stub in <tt>features/step_definitions/factory_steps.rb<
|
|
16
16
|
|
17
17
|
== Example
|
18
18
|
|
19
|
-
The following will call {Movie.make}[http://github.com/notahat/machinist/
|
19
|
+
The following will call {Movie.make}[http://github.com/notahat/machinist], {Factory.create(:movie)}[http://github.com/thoughtbot/factory_girl], Movie.create! or Movie.new, depending on what's available:
|
20
20
|
Given there is a movie
|
21
21
|
|
22
22
|
To create a new record with attributes set, you can say:
|
@@ -33,6 +33,10 @@ You can also refer to the last created object of a kind by saying "above":
|
|
33
33
|
Given there is a movie with the title "Before Sunrise"
|
34
34
|
And "Before Sunset" is a movie with the prequel above
|
35
35
|
|
36
|
+
== Factory support
|
37
|
+
|
38
|
+
{Machinist blueprints}[http://github.com/notahat/machinist] and {factory_girl factories}[http://github.com/thoughtbot/factory_girl] will be used when available.
|
39
|
+
|
36
40
|
=== Credits
|
37
41
|
|
38
42
|
Henning Koch
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.2.
|
1
|
+
1.2.1
|
data/cucumber_factory.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{cucumber_factory}
|
8
|
-
s.version = "1.2.
|
8
|
+
s.version = "1.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Henning Koch"]
|
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
"lib/cucumber_factory.rb",
|
26
26
|
"lib/cucumber_factory/factory.rb",
|
27
27
|
"spec/app_root/app/controllers/application_controller.rb",
|
28
|
+
"spec/app_root/app/models/benutzer.rb",
|
28
29
|
"spec/app_root/app/models/job_offer.rb",
|
29
30
|
"spec/app_root/app/models/machinist_model.rb",
|
30
31
|
"spec/app_root/app/models/movie.rb",
|
@@ -55,7 +56,8 @@ Gem::Specification.new do |s|
|
|
55
56
|
s.rubygems_version = %q{1.3.5}
|
56
57
|
s.summary = %q{Create records from Cucumber features without writing step definitions.}
|
57
58
|
s.test_files = [
|
58
|
-
"spec/app_root/app/models/
|
59
|
+
"spec/app_root/app/models/benutzer.rb",
|
60
|
+
"spec/app_root/app/models/movie.rb",
|
59
61
|
"spec/app_root/app/models/job_offer.rb",
|
60
62
|
"spec/app_root/app/models/machinist_model.rb",
|
61
63
|
"spec/app_root/app/models/plain_ruby_class.rb",
|
@@ -1,96 +1,98 @@
|
|
1
|
-
module Cucumber
|
2
|
-
module Factory
|
3
|
-
|
4
|
-
def self.add_steps(world)
|
5
|
-
steps.each do |step|
|
6
|
-
world.instance_eval do
|
7
|
-
Given(step[0], &step[1].bind(world))
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.steps
|
13
|
-
[
|
14
|
-
[
|
15
|
-
/^"([^\"]*)" is a (.+?)( with the .+?)?$/,
|
16
|
-
lambda { |name, raw_model, raw_attributes| Cucumber::Factory.parse_named_creation(self, name, raw_model, raw_attributes) }
|
17
|
-
],
|
18
|
-
[
|
19
|
-
/^there is a (.+?)( with the .+?)?$/,
|
20
|
-
lambda { |raw_model, raw_attributes| Cucumber::Factory.parse_creation(self, raw_model, raw_attributes) }
|
21
|
-
]
|
22
|
-
]
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.parse(world, command)
|
26
|
-
command = command.sub(/^When |Given |Then /, "")
|
27
|
-
steps.each do |step|
|
28
|
-
match = step[0].match(command)
|
29
|
-
if match
|
30
|
-
step[1].bind(world).call(*match.captures)
|
31
|
-
return
|
32
|
-
end
|
33
|
-
end
|
34
|
-
raise "No step definition for: #{command}"
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.parse_named_creation(world, name, raw_model, raw_attributes)
|
38
|
-
record = parse_creation(world, raw_model, raw_attributes)
|
39
|
-
variable = variable_name_from_prose(name)
|
40
|
-
world.instance_variable_set variable, record
|
41
|
-
end
|
42
|
-
|
43
|
-
def self.parse_creation(world, raw_model, raw_attributes)
|
44
|
-
model_class = model_class_from_prose(raw_model)
|
45
|
-
attributes = {}
|
46
|
-
if raw_attributes.present? && raw_attributes.strip.present?
|
47
|
-
raw_attributes.scan(/(the|and|with| )+(.*?) ("([^\"]*)"|above)/).each do |fragment|
|
48
|
-
value = nil
|
49
|
-
attribute = fragment[1].gsub(" ", "_").to_sym
|
50
|
-
value_type = fragment[2] # 'above' or a quoted string
|
51
|
-
value = fragment[3]
|
52
|
-
association = model_class.reflect_on_association(attribute) if model_class.respond_to?(:reflect_on_association)
|
53
|
-
if association.present?
|
54
|
-
if value_type == "above"
|
55
|
-
value = association.klass.last or raise "There is no last #{attribute}"
|
56
|
-
else
|
57
|
-
value = world.instance_variable_get(variable_name_from_prose(value))
|
58
|
-
end
|
59
|
-
end
|
60
|
-
attributes[attribute] = value
|
61
|
-
end
|
62
|
-
end
|
63
|
-
create_record(model_class, attributes)
|
64
|
-
end
|
65
|
-
|
66
|
-
def self.model_class_from_prose(prose)
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
name =
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
1
|
+
module Cucumber
|
2
|
+
module Factory
|
3
|
+
|
4
|
+
def self.add_steps(world)
|
5
|
+
steps.each do |step|
|
6
|
+
world.instance_eval do
|
7
|
+
Given(step[0], &step[1].bind(world))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.steps
|
13
|
+
[
|
14
|
+
[
|
15
|
+
/^"([^\"]*)" is a (.+?)( with the .+?)?$/,
|
16
|
+
lambda { |name, raw_model, raw_attributes| Cucumber::Factory.parse_named_creation(self, name, raw_model, raw_attributes) }
|
17
|
+
],
|
18
|
+
[
|
19
|
+
/^there is a (.+?)( with the .+?)?$/,
|
20
|
+
lambda { |raw_model, raw_attributes| Cucumber::Factory.parse_creation(self, raw_model, raw_attributes) }
|
21
|
+
]
|
22
|
+
]
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.parse(world, command)
|
26
|
+
command = command.sub(/^When |Given |Then /, "")
|
27
|
+
steps.each do |step|
|
28
|
+
match = step[0].match(command)
|
29
|
+
if match
|
30
|
+
step[1].bind(world).call(*match.captures)
|
31
|
+
return
|
32
|
+
end
|
33
|
+
end
|
34
|
+
raise "No step definition for: #{command}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.parse_named_creation(world, name, raw_model, raw_attributes)
|
38
|
+
record = parse_creation(world, raw_model, raw_attributes)
|
39
|
+
variable = variable_name_from_prose(name)
|
40
|
+
world.instance_variable_set variable, record
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.parse_creation(world, raw_model, raw_attributes)
|
44
|
+
model_class = model_class_from_prose(raw_model)
|
45
|
+
attributes = {}
|
46
|
+
if raw_attributes.present? && raw_attributes.strip.present?
|
47
|
+
raw_attributes.scan(/(the|and|with| )+(.*?) ("([^\"]*)"|above)/).each do |fragment|
|
48
|
+
value = nil
|
49
|
+
attribute = fragment[1].downcase.gsub(" ", "_").to_sym
|
50
|
+
value_type = fragment[2] # 'above' or a quoted string
|
51
|
+
value = fragment[3]
|
52
|
+
association = model_class.reflect_on_association(attribute) if model_class.respond_to?(:reflect_on_association)
|
53
|
+
if association.present?
|
54
|
+
if value_type == "above"
|
55
|
+
value = association.klass.last or raise "There is no last #{attribute}"
|
56
|
+
else
|
57
|
+
value = world.instance_variable_get(variable_name_from_prose(value))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
attributes[attribute] = value
|
61
|
+
end
|
62
|
+
end
|
63
|
+
create_record(model_class, attributes)
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.model_class_from_prose(prose)
|
67
|
+
# don't use \w which depends on the system locale
|
68
|
+
prose.gsub(/[^A-Za-z0-9_]+/, "_").camelize.constantize
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.variable_name_from_prose(prose)
|
72
|
+
# don't use \w which depends on the system locale
|
73
|
+
name = prose.downcase.gsub(/[^A-Za-z0-9_]+/, '_')
|
74
|
+
name = name.gsub(/^_+/, '').gsub(/_+$/, '')
|
75
|
+
name = "_#{name}" unless name.length >= 0 && name =~ /^[a-z]/
|
76
|
+
:"@#{name}"
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def self.factory_girl_factory_name(model_class)
|
82
|
+
model_class.to_s.underscore.to_sym
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.create_record(model_class, attributes)
|
86
|
+
factory_name = factory_girl_factory_name(model_class)
|
87
|
+
if defined?(::Factory) && factory = ::Factory.factories[factory_name]
|
88
|
+
::Factory.create(factory_name, attributes)
|
89
|
+
else
|
90
|
+
create_method = [:make, :create!, :new].detect do |method_name|
|
91
|
+
model_class.respond_to? method_name
|
92
|
+
end
|
93
|
+
model_class.send(create_method, attributes)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
data/spec/factory_spec.rb
CHANGED
@@ -72,6 +72,11 @@ describe Cucumber::Factory do
|
|
72
72
|
JobOffer.should_receive(:new).with({})
|
73
73
|
Cucumber::Factory.parse(@world, "Given there is a job offer")
|
74
74
|
end
|
75
|
+
|
76
|
+
it "should instantiate classes with uppercase characters in their name" do
|
77
|
+
Benutzer.should_receive(:new).with({})
|
78
|
+
Cucumber::Factory.parse(@world, "Given there is a Benutzer")
|
79
|
+
end
|
75
80
|
|
76
81
|
it "should create records with attributes" do
|
77
82
|
Movie.should_receive(:create!).with({ :title => "Sunshine", :year => "2007" })
|
@@ -82,6 +87,11 @@ describe Cucumber::Factory do
|
|
82
87
|
Movie.should_receive(:create!).with({ :box_office_result => "99999999" })
|
83
88
|
Cucumber::Factory.parse(@world, 'Given there is a movie with the box office result "99999999"')
|
84
89
|
end
|
90
|
+
|
91
|
+
it "should create records with attributes containing uppercase characters" do
|
92
|
+
Benutzer.should_receive(:create!).with({ :name => "Susanne" })
|
93
|
+
Cucumber::Factory.parse(@world, 'Given there is a Benutzer with the Name "Susanne"')
|
94
|
+
end
|
85
95
|
|
86
96
|
it "should set instance variables in the world" do
|
87
97
|
Cucumber::Factory.parse(@world, 'Given "Sunshine" is a movie with the title "Sunshine" and the year "2007"')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cucumber_factory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henning Koch
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- lib/cucumber_factory.rb
|
32
32
|
- lib/cucumber_factory/factory.rb
|
33
33
|
- spec/app_root/app/controllers/application_controller.rb
|
34
|
+
- spec/app_root/app/models/benutzer.rb
|
34
35
|
- spec/app_root/app/models/job_offer.rb
|
35
36
|
- spec/app_root/app/models/machinist_model.rb
|
36
37
|
- spec/app_root/app/models/movie.rb
|
@@ -83,6 +84,7 @@ signing_key:
|
|
83
84
|
specification_version: 3
|
84
85
|
summary: Create records from Cucumber features without writing step definitions.
|
85
86
|
test_files:
|
87
|
+
- spec/app_root/app/models/benutzer.rb
|
86
88
|
- spec/app_root/app/models/movie.rb
|
87
89
|
- spec/app_root/app/models/job_offer.rb
|
88
90
|
- spec/app_root/app/models/machinist_model.rb
|