fakes-rspec 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ module Fakes
2
+ module RSpec
3
+ class BlockCriteria
4
+ def initialize(condition_block)
5
+ @condition_block = condition_block
6
+ end
7
+ def is_satisfied_by(item)
8
+ return @condition_block.call(item)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,46 @@
1
+ module RSpec
2
+ def self.create_received_criteria_from(the_call)
3
+ return Fakes::RSpec::ReceivedCriteria.new(the_call)
4
+ end
5
+ def self.create_received_occurences_criteria_from(the_call,occurence)
6
+ return Fakes::RSpec::ReceivedOccurencesCriteria.new(create_received_criteria_from(the_call),the_call,occurence)
7
+ end
8
+
9
+ Matchers.define :have_received do|symbol,*args|
10
+ @occurence_spec = Fakes::RSpec::NulloSpecification.instance
11
+
12
+ chain :once do
13
+ @occurence_spec = Fakes::RSpec::Occurances.exact(1)
14
+ end
15
+ chain :twice do
16
+ @occurence_spec = Fakes::RSpec::Occurances.exact(2)
17
+ end
18
+ chain :at_least_once do
19
+ @occurence_spec = Fakes::RSpec::Occurances.at_least(1)
20
+ end
21
+ chain :at_least_twice do
22
+ @occurence_spec = Fakes::RSpec::Occurances.at_least(2)
23
+ end
24
+ chain :at_most_once do
25
+ @occurence_spec = Fakes::RSpec::Occurances.at_most(1)
26
+ end
27
+ chain :at_most_twice do
28
+ @occurence_spec = Fakes::RSpec::Occurances.at_most(2)
29
+ end
30
+ chain :at_least do|times|
31
+ @occurence_spec = Fakes::RSpec::Occurances.at_least(times)
32
+ end
33
+ chain :at_most do|times|
34
+ @occurence_spec = Fakes::RSpec::Occurances.at_most(times)
35
+ end
36
+ chain :exactly do|times|
37
+ @occurence_spec = Fakes::RSpec::Occurances.exact(times)
38
+ end
39
+ chain :occurs do|the_proc|
40
+ @occurence_spec = Fakes::RSpec::Occurances.from_block(the_proc)
41
+ end
42
+ match do|the_fake|
43
+ RSpec.create_received_occurences_criteria_from(the_fake.received(symbol),@occurence_spec).is_satisfied_by(*args)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,10 @@
1
+ module Fakes
2
+ module RSpec
3
+ class NulloSpecification
4
+ include Singleton
5
+ def is_satisfied_by(item)
6
+ return true
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ module Fakes
2
+ module RSpec
3
+ class Occurances
4
+ class << self
5
+ def from_block(the_block)
6
+ return BlockCriteria.new(the_block)
7
+ end
8
+ def exact(times)
9
+ return from_block(lambda{|occurances| return occurances == times})
10
+ end
11
+ def at_least(times)
12
+ return from_block(lambda { |occurences| return occurences >= times})
13
+ end
14
+ def at_most(times)
15
+ return from_block(lambda { |occurences| return occurences <= times})
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module Fakes
2
+ module RSpec
3
+ class ReceivedCriteria
4
+ def initialize(the_call)
5
+ @the_call = the_call
6
+ end
7
+
8
+ def is_satisfied_by(*args)
9
+ return false if @the_call == nil
10
+ return args.count == 0 ? true : @the_call.called_with(*args) != nil
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ module Fakes
2
+ module RSpec
3
+ class ReceivedOccurencesCriteria
4
+ def initialize(received_criteria,the_call,occurence)
5
+ @received_criteria = received_criteria
6
+ @the_call = the_call
7
+ @occurence = occurence
8
+ end
9
+
10
+ def is_satisfied_by(*args)
11
+ return @received_criteria.is_satisfied_by(*args) &&
12
+ @occurence.is_satisfied_by((args.count == 0 ? @the_call.total_times_called : @the_call.called_with(*args).times_called))
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Fakes
2
+ module RSpec
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'fakes'
2
+ require 'rspec'
3
+
4
+ require 'singleton'
5
+ require 'core/block_criteria'
6
+ require 'core/nullo_specification'
7
+ require 'core/occurances'
8
+ require 'core/received_criteria'
9
+ require 'core/received_occurances_criteria'
10
+ require 'core/matcher'
11
+ require 'core/received_criteria'
12
+
@@ -0,0 +1,5 @@
1
+ require 'fakes'
2
+
3
+ Dir.chdir(File.join(File.dirname(__FILE__),"..,lib".split(','))) do
4
+ require 'fakes-rspec.rb'
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ module Fakes
4
+ module RSpec
5
+ describe BlockCriteria do
6
+ context "when matching" do
7
+ subject{BlockCriteria.new(lambda{|item| return item == 1})}
8
+ it "should match if its block returns true" do
9
+ subject.is_satisfied_by(1).should be_true
10
+ subject.is_satisfied_by(2).should be_false
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ module Fakes
4
+ module RSpec
5
+ describe NulloSpecification do
6
+ subject{NulloSpecification.instance}
7
+
8
+ it "should be satisfied by anything" do
9
+ [1,10,"a",Object.new].each{|item| subject.is_satisfied_by(item).should be_true}
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ module Fakes
4
+ module RSpec
5
+ describe Occurances do
6
+ context "when creating an exact occurance matcher" do
7
+ subject{Occurances.exact(1)}
8
+
9
+ it "should create a matcher that matches an exact number of occurances" do
10
+ subject.is_satisfied_by(1).should be_true
11
+ subject.is_satisfied_by(2).should be_false
12
+ end
13
+ end
14
+
15
+ context "when creating an at least occurance matcher" do
16
+ subject{Occurances.at_least(3)}
17
+
18
+ it "should create a matcher that matches an exact number of occurances" do
19
+ subject.is_satisfied_by(3).should be_true
20
+ subject.is_satisfied_by(4).should be_true
21
+ subject.is_satisfied_by(2).should be_false
22
+ end
23
+ end
24
+ context "when creating an at most occurance matcher" do
25
+ subject{Occurances.at_most(3)}
26
+
27
+ it "should create a matcher that matches an exact number of occurances" do
28
+ subject.is_satisfied_by(3).should be_true
29
+ subject.is_satisfied_by(2).should be_true
30
+ subject.is_satisfied_by(1).should be_true
31
+ subject.is_satisfied_by(4).should be_false
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ module Fakes
4
+ module RSpec
5
+ describe ReceivedCriteria do
6
+ context "when matching a call made without arguments" do
7
+ it "should match if the call was made" do
8
+ item = fake
9
+ item.hello
10
+
11
+ sut = ReceivedCriteria.new(item.received(:hello))
12
+
13
+ sut.is_satisfied_by.should be_true
14
+ end
15
+ end
16
+ context "when matching a call made with arguments" do
17
+ let(:item){fake}
18
+ before (:each) do
19
+ item.hello("world")
20
+ @sut = ReceivedCriteria.new(item.received(:hello))
21
+ end
22
+ context "and we care about the arguments it was called with" do
23
+ it "should match if it received the correct call" do
24
+ @sut.is_satisfied_by("world").should be_true
25
+ end
26
+ it "should not match if the arguments provided are not in the call history" do
27
+ @sut.is_satisfied_by("yo").should be_false
28
+ end
29
+
30
+ end
31
+ context "and we don't care about the arguments it was called with" do
32
+ it "should match if it received a call to the method" do
33
+ @sut.is_satisfied_by.should be_true
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ module Fakes
4
+ module RSpec
5
+ describe ReceivedOccurencesCriteria do
6
+ let(:original){fake}
7
+ let(:occurence){fake}
8
+ let(:the_call){fake}
9
+ let(:arg_set){fake}
10
+ let(:the_arg){1}
11
+ subject{ReceivedOccurencesCriteria.new(original,the_call,occurence)}
12
+ context "when matching a call made" do
13
+ context "and the original criteria is not satisfied" do
14
+ before (:each) do
15
+ original.stub(:is_satisfied_by).with(the_arg).and_return(false)
16
+ end
17
+ it "should not match" do
18
+ subject.is_satisfied_by(the_arg).should be_false
19
+ end
20
+ it "should not use the occurence or the call" do
21
+ occurence.should_not have_received(:is_satified_by)
22
+ the_call.should_not have_received(:called_with)
23
+ end
24
+ end
25
+ context "and the original criteria is satisfied" do
26
+ before (:each) do
27
+ original.stub(:is_satisfied_by).with(the_arg).and_return(true)
28
+ the_call.stub(:called_with).with(the_arg).and_return(arg_set)
29
+ arg_set.stub(:times_called).and_return(1)
30
+ end
31
+ context "and no arguments were provided to the call" do
32
+ before (:each) do
33
+ original.stub(:is_satisfied_by).and_return(true)
34
+ the_call.stub(:total_times_called).and_return(1)
35
+ occurence.stub(:is_satisfied_by).with(1).and_return(true)
36
+ end
37
+ before (:each) do
38
+ @result = subject.is_satisfied_by
39
+ end
40
+ it "should match if the occurence matches the total number of invocations" do
41
+ @result.should be_true
42
+ end
43
+ end
44
+ context "and its occurence is satisfied" do
45
+ before (:each) do
46
+ occurence.stub(:is_satisfied_by).with(1).and_return(true)
47
+ end
48
+ it "should match" do
49
+ subject.is_satisfied_by(the_arg).should be_true
50
+ end
51
+ end
52
+ context "and its occurence is not satisfied" do
53
+ before (:each) do
54
+ occurence.stub(:is_satisfied_by).with(1).and_return(false)
55
+ end
56
+ it "should not match" do
57
+ subject.is_satisfied_by(the_arg).should be_false
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ describe "using the rspec extensions" do
4
+ it "should be able to determine if a call has not been made" do
5
+ item = fake
6
+ item.hello
7
+ item.last("other")
8
+
9
+ item.should_not have_received(:hello,"world")
10
+ item.should have_received(:hello)
11
+ item.should_not have_received(:once_more)
12
+ item.should_not have_received(:last,"hello")
13
+ item.should have_received(:last,"other")
14
+ end
15
+
16
+ it "should be able to determine if a call has been made a certain number of times" do
17
+ item = fake
18
+ item.hello
19
+ item.hello
20
+ item.last("other")
21
+
22
+ item.should_not have_received(:hello,"world")
23
+ item.should have_received(:hello).twice
24
+ item.should have_received(:hello)
25
+ item.should_not have_received(:once_more)
26
+ item.should_not have_received(:last,"hello")
27
+ item.should have_received(:last,"other").once
28
+ item.should_not have_received(:last).twice
29
+ end
30
+
31
+ it "should be able to determine if a call was made once" do
32
+ item = fake
33
+ item.hello
34
+
35
+ item.should have_received(:hello).once
36
+ end
37
+
38
+ it "should be able to determine if a call was made twice" do
39
+ item = fake
40
+ item.hello
41
+ item.hello
42
+
43
+ item.should have_received(:hello).twice
44
+ end
45
+
46
+ it "should be able to determine if a call was made at least once" do
47
+ item = fake
48
+ item.hello
49
+ item.hello
50
+
51
+ item.should have_received(:hello).at_least_once
52
+ end
53
+
54
+ it "should be able to determine if a call was made at least twice" do
55
+ item = fake
56
+ item.hello
57
+ item.hello
58
+ item.hello
59
+
60
+ item.should have_received(:hello).at_least_twice
61
+ item.should have_received(:hello).at_least_twice
62
+ end
63
+
64
+
65
+ it "should be able to determine if a call was made at most once" do
66
+ item = fake
67
+ item.hello
68
+ item.goodbye
69
+ item.goodbye
70
+
71
+ item.should have_received(:hello).at_most_once
72
+ item.should_not have_received(:goodbye).at_most_once
73
+ end
74
+
75
+ it "should be able to determine if a call was made at most twice" do
76
+ item = fake
77
+ item.hello
78
+ item.goodbye
79
+ item.goodbye
80
+ item.goodbye
81
+
82
+ item.should have_received(:hello).at_most_twice
83
+ item.should_not have_received(:goodbye).at_most_twice
84
+ end
85
+
86
+ it "should be able to determine if a call was made at least a specified number of times" do
87
+ item = fake
88
+ item.hello
89
+ item.goodbye
90
+ item.goodbye
91
+ item.goodbye
92
+
93
+ item.should have_received(:hello).at_most(1)
94
+ item.should_not have_received(:goodbye).at_most(1)
95
+ end
96
+ end
97
+
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fakes-rspec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Develop With Passion®
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
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: rspec
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: guard
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
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: fakes
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Faking library that allows inspection of received calls after they have
111
+ been made. Also supports tracking calls with multiple argument sets.
112
+ email:
113
+ - open_source@developwithpassion.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - lib/core/block_criteria.rb
119
+ - lib/core/matcher.rb
120
+ - lib/core/nullo_specification.rb
121
+ - lib/core/occurances.rb
122
+ - lib/core/received_criteria.rb
123
+ - lib/core/received_occurances_criteria.rb
124
+ - lib/core/version.rb
125
+ - lib/fakes-rspec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/specs/block_criteria_spec.rb
128
+ - spec/specs/nullo_specification_spec.rb
129
+ - spec/specs/occurances_spec.rb
130
+ - spec/specs/received_criteria_spec.rb
131
+ - spec/specs/received_occurences_criteria_spec.rb
132
+ - spec/specs/usage_spec.rb
133
+ homepage: http://www.developwithpassion.com
134
+ licenses: []
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project: fakes-rspec
153
+ rubygems_version: 1.8.21
154
+ signing_key:
155
+ specification_version: 3
156
+ summary: Utility functions for develowithpassion_fakes when working with rSpec
157
+ test_files:
158
+ - spec/spec_helper.rb
159
+ - spec/specs/block_criteria_spec.rb
160
+ - spec/specs/nullo_specification_spec.rb
161
+ - spec/specs/occurances_spec.rb
162
+ - spec/specs/received_criteria_spec.rb
163
+ - spec/specs/received_occurences_criteria_spec.rb
164
+ - spec/specs/usage_spec.rb