assembly_line 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Sandro Turriate
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.
@@ -0,0 +1,97 @@
1
+ Assembly Line
2
+ =============
3
+
4
+ Assembly Line allows you to group together a series of rspec `let` statements which can later be evaluated to set up a specific state for your specs. It's an excellent compliment to factory_girl because you can use your factories to build up each component, then use AssemblyLine to bring them all together.
5
+
6
+ As a system grows in complexity, more objects are required to set up the necessary state to write tests. Sometimes you'll need to write a test that requires access to the beginning, middle, and end of a very large hierarchy, and AssemblyLine can expose each part of the hierarchy that you care about.
7
+
8
+ Installation
9
+ ------------
10
+ gem install assembly_line
11
+
12
+ Edit your *spec/spec_helper.rb*
13
+
14
+ require 'assembly_line' # unnecessary if you've used config.gem 'assembly_line'
15
+ require 'spec/support/assemblies' # unnecessary if your spec_helper already requires spec/support/*
16
+
17
+ Spec::Runner.configure do |config|
18
+ config.extend AssemblyLine
19
+ end
20
+
21
+ Define an AssemblyLine
22
+
23
+ Assembly.define(:user_with_payment_address) do
24
+ let(:user) { Factory(:user)}
25
+ let(:payment_address) { Factory(:payment_address, :user => user) }
26
+ end
27
+
28
+ Example
29
+ -------
30
+
31
+ Place your AssemblyLine definitions in *spec/support/assemblies.rb*
32
+
33
+ AssemblyLine.define(:drinks) do
34
+ let(:drinks) { [:gin, :vodka] }
35
+ end
36
+
37
+ AssemblyLine.define(:party) do
38
+ depends_on :drinks
39
+
40
+ let(:host) { :rochelle }
41
+ let(:attendees) { [:nick, :ellis, :coach] }
42
+ let(:party) { @party = Party.new(host, attendees) }
43
+ let(:party_crasher) { attendees << :crasher; :crasher }
44
+ end
45
+
46
+ Use your AssemblyLine in a test
47
+
48
+ describe "README example" do
49
+ context "attendees" do
50
+ Assemble(:party)
51
+
52
+ it "does not count the host as an attendee" do
53
+ party.attendees.should_not include(host)
54
+ end
55
+ end
56
+
57
+ context "party crasher" do
58
+ Assemble(:party).invoke(:party_crasher)
59
+
60
+ it "exemplifies method invocation after assembly" do
61
+ party.attendees.should include(:crasher)
62
+ end
63
+ end
64
+
65
+ context "drinks" do
66
+ Assemble(:party)
67
+
68
+ it "does not include the list of standard drinks" do
69
+ party.drinks.should_not include(drinks)
70
+ end
71
+ end
72
+ end
73
+
74
+ Thanks!
75
+ -------
76
+
77
+ - l4rk (initial spike declaring modules and including them in the rspec context)
78
+ - veezus (code contributions, introduced modular design / dependencies)
79
+ - bigtiger (named the project)
80
+ - leshill (support and testing)
81
+
82
+
83
+ Note on Patches/Pull Requests
84
+ -----------------------------
85
+
86
+ - Fork the project.
87
+ - Make your feature addition or bug fix.
88
+ - Add tests for it. This is important so I don't break it in a
89
+ future version unintentionally.
90
+ - Commit, do not mess with rakefile, version, or history.
91
+ (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)
92
+ - Send me a pull request. Bonus points for topic branches.
93
+
94
+ Copyright
95
+ ---------
96
+
97
+ Copyright (c) 2010 Sandro Turriate. See MIT_LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "assembly_line"
8
+ gem.summary = %Q{Modularize setup blocks for rspec}
9
+ gem.description = %Q{Assembly Line allows you to group together a series of rspec `let` statements which can later be evaluated to set up a specific state for your specs.}
10
+ gem.email = "sandro.turriate@gmail.com"
11
+ gem.homepage = "http://github.com/sandro/assembly_line"
12
+ gem.authors = ["Sandro Turriate"]
13
+ gem.add_development_dependency "rspec", "1.2.9"
14
+ gem.add_development_dependency "yard", "0.5.3"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ begin
39
+ require 'yard'
40
+ YARD::Rake::YardocTask.new
41
+ rescue LoadError
42
+ task :yardoc do
43
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
44
+ end
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,19 @@
1
+ require 'forwardable'
2
+ require 'assembly_line/registry'
3
+ require 'assembly_line/constructor'
4
+
5
+ module AssemblyLine
6
+ VERSION = "0.1.0".freeze
7
+
8
+ def self.define(name, &block)
9
+ Registry.add(name, block)
10
+ end
11
+
12
+ def self.assemble(name, rspec_context, options={})
13
+ Registry.locate(name).assemble(rspec_context, options)
14
+ end
15
+
16
+ def Assemble(name, options={})
17
+ AssemblyLine.assemble(name, self, options)
18
+ end
19
+ end
@@ -0,0 +1,48 @@
1
+ module AssemblyLine
2
+ class Constructor
3
+ extend Forwardable
4
+
5
+ def_delegators :rspec_context, :let, :before
6
+
7
+ attr_reader :name, :code_block, :rspec_context, :options
8
+
9
+ def initialize(name, code_block)
10
+ @name = name
11
+ @code_block = code_block
12
+ end
13
+
14
+ def assemble(context, options)
15
+ @options = options
16
+ @rspec_context = context
17
+ instance_eval(&code_block)
18
+ self
19
+ end
20
+
21
+ def invoke(*methods)
22
+ if methods.any?
23
+ invoke_in_setup *methods
24
+ else
25
+ invoke_in_setup name
26
+ end
27
+ end
28
+
29
+ def depends_on(*constructors)
30
+ if options[:depends_on]
31
+ constructors = Array(options[:depends_on])
32
+ end
33
+ constructors.each do |name|
34
+ AssemblyLine.assemble(name, rspec_context)
35
+ end
36
+ end
37
+
38
+ protected
39
+
40
+ def invoke_in_setup(*methods)
41
+ before(:all) do
42
+ methods.each do |method_name|
43
+ send(method_name)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,27 @@
1
+ module AssemblyLine
2
+ module Registry
3
+ extend self
4
+
5
+ def add(name, block)
6
+ constructors[name] = Constructor.new(name, block)
7
+ end
8
+
9
+ def list
10
+ constructors.keys.dup
11
+ end
12
+
13
+ def locate(name)
14
+ constructors[name] || raise(ArgumentError, "AssemblyLine could not find definition for: '#{name.inspect}'")
15
+ end
16
+
17
+ def clear
18
+ constructors.clear
19
+ end
20
+
21
+ protected
22
+
23
+ def constructors
24
+ @constructors ||= {}
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe AssemblyLine::Constructor do
4
+ let(:code_block) do
5
+ lambda do
6
+ depends_on :drinks
7
+
8
+ let(:host) { :rochelle }
9
+ let(:attendees) { [] }
10
+ end
11
+ end
12
+ let(:constructor) { AssemblyLine::Constructor.new(:gathering, code_block) }
13
+ let(:rspec_context) { mock('rspec_context', :let => nil, :depends_on => nil) }
14
+ let(:options) { {:depends_on => :location} }
15
+
16
+ describe "#assemble" do
17
+ before do
18
+ constructor.stub(:depends_on => nil)
19
+ end
20
+ it "persists context" do
21
+ constructor.assemble(rspec_context, options)
22
+ constructor.rspec_context.should == rspec_context
23
+ end
24
+
25
+ it "persists context" do
26
+ constructor.assemble(rspec_context, options)
27
+ constructor.options.should == options
28
+ end
29
+
30
+ it "evaluates the code block" do
31
+ rspec_context.should_receive(:let).twice
32
+ constructor.assemble(rspec_context, options)
33
+ end
34
+
35
+ it "returns itself" do
36
+ constructor.assemble(rspec_context, options).should == constructor
37
+ end
38
+ end
39
+
40
+ describe "#invoke" do
41
+ context "no arguments provided" do
42
+ it "calls the 'gathering' method" do
43
+ constructor.should_receive(:invoke_in_setup).with(:gathering)
44
+ constructor.invoke
45
+ end
46
+ end
47
+
48
+ context "arguments provided" do
49
+ it "calls the 'host' method" do
50
+ constructor.should_receive(:invoke_in_setup).with(:host)
51
+ constructor.invoke :host
52
+ end
53
+
54
+ it "calls the 'host' method followed by the 'attendees' method" do
55
+ constructor.should_receive(:invoke_in_setup).with(:host, :attendees)
56
+ constructor.invoke :host, :attendees
57
+ end
58
+ end
59
+ end
60
+
61
+ describe "#depends_on" do
62
+ context "declared in the assembly" do
63
+ it "attempts to assemble the 'drinks' dependency" do
64
+ AssemblyLine.should_receive(:assemble).with(:drinks, rspec_context)
65
+ constructor.assemble(rspec_context, {})
66
+ end
67
+ end
68
+
69
+ context "passed in from options" do
70
+ it "attempts to assemble the 'location' assembly" do
71
+ AssemblyLine.should_receive(:assemble).with(:location, rspec_context)
72
+ constructor.assemble(rspec_context, options)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe AssemblyLine::Registry do
4
+ let(:registry) { AssemblyLine::Registry.dup }
5
+ let(:code_block) { lambda {} }
6
+
7
+ after do
8
+ registry.clear
9
+ end
10
+
11
+ describe "#add" do
12
+ it "adds a new constructor to the list of registered assembly lines" do
13
+ registry.add(:party, code_block)
14
+ registry.list.should include(:party)
15
+ end
16
+ end
17
+
18
+ describe "#list" do
19
+ it "lists the registered assembly lines by name" do
20
+ registry.add(:one, code_block)
21
+ registry.add(:two, code_block)
22
+ registry.list.should =~ [:one, :two]
23
+ end
24
+ end
25
+
26
+ describe "#clear" do
27
+ it "clears the registry" do
28
+ registry.add(:one, code_block)
29
+ registry.add(:two, code_block)
30
+ expect do
31
+ registry.clear
32
+ end.to change { registry.list.size }.from(2).to(0)
33
+ end
34
+ end
35
+
36
+ describe "#locate" do
37
+ context "when AssemblyLine has been registered" do
38
+ before do
39
+ registry.add(:party, code_block)
40
+ end
41
+
42
+ it "returns the registered Constructor" do
43
+ registry.locate(:party).should be_instance_of(AssemblyLine::Constructor)
44
+ end
45
+ end
46
+
47
+ context "when AssemblyLine has not been registered" do
48
+ it "raises an ArgumentError" do
49
+ expect do
50
+ registry.locate(:does_not_exist)
51
+ end.to raise_error(ArgumentError, "AssemblyLine could not find definition for: ':does_not_exist'")
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe AssemblyLine do
4
+ context "API" do
5
+ subject { AssemblyLine }
6
+ it { should respond_to(:define) }
7
+ it { should respond_to(:assemble) }
8
+
9
+ end
10
+
11
+ describe ".define" do
12
+ it "adds the AssemblyLine to the Registry" do
13
+ definition = lambda {}
14
+ AssemblyLine::Registry.should_receive(:add).with(:user_with_address, definition)
15
+ AssemblyLine.define(:user_with_address, &definition)
16
+ end
17
+ end
18
+
19
+ describe ".assemble" do
20
+ let(:rspec_context) { mock('rspec context') }
21
+
22
+ context "cannot locate the AssemblyLine" do
23
+ it "raises an error" do
24
+ expect do
25
+ AssemblyLine.assemble(:does_not_exist, rspec_context)
26
+ end.to raise_error(ArgumentError)
27
+ end
28
+ end
29
+
30
+ context "AssemblyLine exists" do
31
+ it "assembles the AssemblyLine" do
32
+ constructor = mock('constructor')
33
+ constructor.should_receive(:assemble).with(rspec_context, options)
34
+ AssemblyLine::Registry.stub(:locate => constructor)
35
+ AssemblyLine.assemble(:party, rspec_context)
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "#Assemble" do
41
+ it "delegates to AssemblyLine.assemble" do
42
+ klass = Class.new do
43
+ extend AssemblyLine
44
+ end
45
+ AssemblyLine.should_receive(:assemble).with(:party, klass, {})
46
+ klass.Assemble(:party)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,122 @@
1
+ require 'spec_helper'
2
+
3
+ describe "AssemblyLine in practice" do
4
+ extend AssemblyLine
5
+
6
+ class Party < Struct.new(:host, :attendees)
7
+ attr_writer :drinks
8
+ def drinks
9
+ @drinks ||= []
10
+ end
11
+ end
12
+
13
+ AssemblyLine.define(:before_block) do
14
+ before(:each) do
15
+ @feedback = "before called"
16
+ end
17
+ end
18
+
19
+ AssemblyLine.define(:location) do
20
+ let(:location) { "123 main st." }
21
+ end
22
+
23
+ AssemblyLine.define(:drinks) do
24
+ let(:drinks) { [:gin, :vodka] }
25
+ end
26
+
27
+ AssemblyLine.define(:party) do
28
+ depends_on :drinks
29
+
30
+ let(:host) { :rochelle }
31
+ let(:attendees) { [:nick, :ellis, :coach] }
32
+ let(:party) { @party = Party.new(host, attendees) }
33
+ let(:party_crasher) { attendees << :crasher; :crasher }
34
+ let(:new_host) { party.host = :bob }
35
+ end
36
+
37
+ describe "invoking a method at assembly time" do
38
+ context "default invocation" do
39
+ Assemble(:party).invoke
40
+ it "has set up a party instance variable without explicitly calling 'party'" do
41
+ @party.should be_instance_of(Party)
42
+ end
43
+ end
44
+
45
+ context "invoking one method" do
46
+ Assemble(:party).invoke(:party_crasher)
47
+ it "added a party crasher to the party" do
48
+ attendees.should include(:crasher)
49
+ end
50
+ end
51
+
52
+ context "invoking many methods" do
53
+ Assemble(:party).invoke(:party_crasher, :new_host)
54
+ it "added a party crasher to the party" do
55
+ attendees.should include(:crasher)
56
+ end
57
+
58
+ it "changed the host to 'bob'" do
59
+ party.host.should == :bob
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "dependencies" do
65
+ context "loading the built-in dependency" do
66
+ Assemble(:party)
67
+ it "loads the 'drinks' dependency when loading the party assembly" do
68
+ party
69
+ drinks.should == [:gin, :vodka]
70
+ end
71
+ end
72
+
73
+ context "overriding the dependency" do
74
+ Assemble(:party, :depends_on => :location)
75
+
76
+ it "loads the 'location' assembly line when loading the party assembly" do
77
+ party
78
+ location.should == "123 main st."
79
+ end
80
+
81
+ it "does not load the built-in 'drinks' dependency" do
82
+ party
83
+ expect do
84
+ drinks
85
+ end.to raise_error(NameError)
86
+ end
87
+ end
88
+ end
89
+
90
+ describe "before blocks" do
91
+ Assemble(:before_block)
92
+ it "invoked the before block" do
93
+ @feedback.should == "before called"
94
+ end
95
+ end
96
+
97
+ describe "README example" do
98
+ context "attendees" do
99
+ Assemble(:party)
100
+
101
+ it "does not count the host as an attendee" do
102
+ party.attendees.should_not include(host)
103
+ end
104
+ end
105
+
106
+ context "party crasher" do
107
+ Assemble(:party).invoke(:party_crasher)
108
+
109
+ it "exemplifies method invocation after assembly" do
110
+ party.attendees.should include(:crasher)
111
+ end
112
+ end
113
+
114
+ context "drinks" do
115
+ Assemble(:party)
116
+
117
+ it "does not include the list of standard drinks" do
118
+ party.drinks.should_not include(drinks)
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,2 @@
1
+ --color
2
+ --backtrace
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'assembly_line'
4
+ require 'spec/autorun'
5
+
6
+ Spec::Runner.configure do |config|
7
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assembly_line
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sandro Turriate
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-03 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: yard
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.3
34
+ version:
35
+ description: Assembly Line allows you to group together a series of rspec `let` statements which can later be evaluated to set up a specific state for your specs.
36
+ email: sandro.turriate@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.md
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - MIT_LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - VERSION
50
+ - lib/assembly_line.rb
51
+ - lib/assembly_line/constructor.rb
52
+ - lib/assembly_line/registry.rb
53
+ - spec/assembly_line/constructor_spec.rb
54
+ - spec/assembly_line/registry_spec.rb
55
+ - spec/assembly_line_spec.rb
56
+ - spec/functional/assemble_spec.rb
57
+ - spec/spec.opts
58
+ - spec/spec_helper.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/sandro/assembly_line
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.5
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Modularize setup blocks for rspec
87
+ test_files:
88
+ - spec/assembly_line/constructor_spec.rb
89
+ - spec/assembly_line/registry_spec.rb
90
+ - spec/assembly_line_spec.rb
91
+ - spec/functional/assemble_spec.rb
92
+ - spec/spec_helper.rb