rspec-situations 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'rspec'
7
+ gem 'bundler'
8
+ gem 'growl'
9
+ gem 'debugger'
10
+ gem 'guard'
11
+ gem 'guard-bundler'
12
+ gem 'guard-rspec'
13
+
data/Guardfile ADDED
@@ -0,0 +1,12 @@
1
+
2
+ guard 'bundler' do
3
+ watch 'Gemfile'
4
+ watch /^.+\.gemspec/
5
+ end
6
+
7
+
8
+ guard :rspec do
9
+ watch %r{^spec/.+_spec\.rb$}
10
+ watch( %r{^lib/.+\.rb$} ){ 'spec' }
11
+ watch( 'spec/spec_helper.rb' ){ 'spec' }
12
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011, 2012, 2013 by Brett Richardson
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.md ADDED
@@ -0,0 +1,31 @@
1
+ Using Rspec Situations
2
+ ======================
3
+
4
+ Using Rspec Situations is super simple.
5
+
6
+
7
+ Define a situation with the 'situation' method.
8
+ Add a symbol key to identify the situation, and add an optional description if you like, and pass in a block creating the situation.
9
+
10
+
11
+ Describe a set of 1 or more situations with the 'describe_situation' method, and treat it just like a normal describe call.
12
+
13
+
14
+ ```ruby
15
+ describe Apple do
16
+ subject( :apple ){ create :apple }
17
+
18
+ situation( :bought ){ subject.bought_by = create :user }
19
+ situation( :red ){ subject.color = :Red }
20
+
21
+ situation( :1yr_old, 'a year old' ){ subject.created_at = 2.years.ago }
22
+
23
+ describe_situation :stolen, :red do
24
+ it{ should be_tasty }
25
+ end
26
+
27
+ describe_situation :stolen, :1yr_old do
28
+ it{ should_not be_tasty }
29
+ end
30
+ end
31
+ ```
@@ -0,0 +1,65 @@
1
+ require 'rspec/situations/situation'
2
+
3
+ module RSpec::Situations
4
+ module ClassExtensions
5
+
6
+ # Public API method for defining a situation
7
+ def situation( key, description = nil, &block ) # RSpec extention method to add a Situation object to the hash cache
8
+ _rsits_hash[key] = Situation.new( key, description, &block )
9
+ end
10
+
11
+
12
+ # Public API method for describing a set of situations
13
+ def describe_situation( *keys, &block ) # Describe and invoke all relevant situations
14
+ if keys.last.kind_of? String # If the last argument is a string
15
+ description = keys.last # Use it as the description
16
+ keys = keys[0..-1]
17
+ else
18
+ description = _rsits_describe_name *keys # Otherwise, generate a description
19
+ end
20
+
21
+ describe description do
22
+ before do
23
+ _rsits( *keys ).each do |situation| # For each situation we request,
24
+ example.instance_eval &situation.block # Execute its block in the context of an example
25
+ end
26
+ end
27
+
28
+ instance_eval &block # Execute the describe block in the current context
29
+ end
30
+ end
31
+
32
+
33
+ # Get situations defined by the given keys
34
+ def _rsits( *keys ) # Array of all situation objects pertaining to current context
35
+ if keys.length > 0
36
+ _rsits_combined_hash.select{ |k,v| keys.include? k }
37
+ else
38
+ _rsits_combined_hash
39
+ end
40
+ end
41
+
42
+
43
+ # Combine each situation's description into a string
44
+ def _rsits_describe_name( *keys )
45
+ 'when ' + _rsits( *keys ).map{ |k,v| v.description }.join( ' and ' )
46
+ end
47
+
48
+
49
+ # ========================================================================
50
+ private
51
+ # ========================================================================
52
+
53
+
54
+ def _rsits_combined_hash
55
+ _rsits_hash # TODO: Combine with parents, not needed until you want parent and local situations to interact directly with each other.
56
+ end
57
+
58
+
59
+ # Getter / setter for the hash that stores all our situations
60
+ def _rsits_hash # Cache by key of all situations in the current describe/context block.
61
+ @_rsits_hash ||= {}
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+
3
+ RSpec.configure do |c|
4
+ c.extend RSpec::Situations::ClassExtensions
5
+ c.include RSpec::Situations::InstanceExtensions
6
+ end
@@ -0,0 +1,31 @@
1
+ module RSpec::Situations::InstanceExtensions
2
+
3
+ # Generate a string for describe_situation call
4
+ def _rsits_describe_name
5
+ self.class._rsits_describe_name
6
+ end
7
+
8
+
9
+ # Get an array of situations (from the combined hash) depending on the keys we provide
10
+ def _rsits( *keys ) # Go up the example parents and get the situations requested, whereever they may be defined
11
+ _rsits_combined_hash.select do |key, _|
12
+ keys.include? key # Then filter out only the keys we are using
13
+ end.map{ |_, situation| situation } # Now return an array of situations instead of a hash
14
+ end
15
+
16
+
17
+ # ===========================================================================
18
+ private
19
+ # ===========================================================================
20
+
21
+
22
+ # A hash of the current context, merged with parent contexts
23
+ def _rsits_combined_hash
24
+ self.class.instance_variable_get(
25
+ :@parent_groups # For each item in @parent_groups
26
+ ).reduce( {} ) do |final, pg|
27
+ final.merge pg._rsits # Merge the _rsits hash into 'final'
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,15 @@
1
+ module RSpec::Situations
2
+
3
+ class Situation
4
+ attr_reader :description, :block
5
+
6
+ def initialize( key, description = nil, &block )
7
+ @key = key
8
+ @description = description || key.to_s
9
+ @block = block
10
+ end
11
+
12
+ def to_s; @description; end
13
+ end
14
+
15
+ end
@@ -0,0 +1,13 @@
1
+ module RSpec
2
+ module Situations
3
+
4
+ VERSION_NUMBERS = [
5
+ VERSION_MAJOR = 0,
6
+ VERSION_MINOR = 0,
7
+ VERSION_BUILD = 2,
8
+ ]
9
+
10
+ VERSION = VERSION_NUMBERS.join(".")
11
+
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module RSpec
2
+ module Situations
3
+ end
4
+ end
5
+
6
+ require 'rspec/situations/version'
7
+ require 'rspec/situations/situation'
8
+ require 'rspec/situations/class_extensions'
9
+ require 'rspec/situations/instance_extensions'
10
+ require 'rspec/situations/configure'
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-situations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brett Richardson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Adds a super simple method to describe RSpec situations in terms of smaller
63
+ situation blocks.
64
+ email:
65
+ - Brett.Richardson.NZ@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - lib/rspec/situations/class_extensions.rb
71
+ - lib/rspec/situations/configure.rb
72
+ - lib/rspec/situations/instance_extensions.rb
73
+ - lib/rspec/situations/situation.rb
74
+ - lib/rspec/situations/version.rb
75
+ - lib/rspec/situations.rb
76
+ - Gemfile
77
+ - Guardfile
78
+ - MIT-LICENSE
79
+ - README.md
80
+ homepage: http://www.dablweb.com
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.25
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Situations for chained Rspec conditions
104
+ test_files: []