developwithpassion_fakes 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ blah.rb
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@developwithpassion.fakes --create
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib",__FILE__)
3
+ require "developwithpassion_fakes/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "developwithpassion_fakes"
7
+ s.version = DevelopWithPassion::Fakes::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Develop With Passion®"]
10
+ s.email = ["open_source@developwithpassion.com"]
11
+ s.homepage = "http://www.developwithpassion.com"
12
+ s.summary = %q{Simple faking library}
13
+ s.description = %q{Faking library that allows inspection of received calls after they have been made. Also supports tracking calls with multiple argument sets.}
14
+ s.rubyforge_project = "developwithpassion_fakes"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,24 @@
1
+ module DevelopWithPassion
2
+ module Fakes
3
+ module ArgBehaviour
4
+ attr_accessor :return_value,:times_called
5
+
6
+ def and_return(item)
7
+ @return_value = item
8
+ end
9
+
10
+ def capture_args(*args)
11
+ @times_called += 1
12
+ @called_args = *args
13
+ end
14
+
15
+ def matches?(*args)
16
+ return @args == args
17
+ end
18
+
19
+ def was_called_with?(*args)
20
+ return @called_args == args
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ module DevelopWithPassion
2
+ module Fakes
3
+ class ArgSet
4
+ include ArgBehaviour
5
+
6
+ def initialize(*args)
7
+ @args = *args
8
+ @times_called = 0
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ module DevelopWithPassion
2
+ module Fakes
3
+ class Fake
4
+ def initialize(invocation_set = {})
5
+ @method_invocations = invocation_set
6
+ end
7
+
8
+ def method_missing(name,*args,&block)
9
+ return @method_invocations.has_key?(name.to_sym) ? @method_invocations[name.to_sym].invoke(*args) : handle_unexpected_method_invocation(name,*args,block)
10
+ end
11
+
12
+ def handle_unexpected_method_invocation(name,*args,block)
13
+ method = stub(name.to_sym)
14
+ method.ignore_arg
15
+ return method.invoke(*args)
16
+ end
17
+
18
+ def stub(symbol)
19
+ return @method_invocations[symbol] || @method_invocations[symbol] = MethodStub.new
20
+ end
21
+
22
+ def received(symbol)
23
+ return @method_invocations[symbol]
24
+ end
25
+
26
+ def never_received?(symbol)
27
+ return !@method_invocations.has_key?(symbol)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ module DevelopWithPassion
2
+ module Fakes
3
+ class IgnoreSet
4
+ include ArgBehaviour
5
+
6
+ def initialize
7
+ @times_called = 0
8
+ end
9
+
10
+ def matches?(*args)
11
+ return true
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ module DevelopWithPassion
2
+ module Fakes
3
+ class MethodStub
4
+ def initialize(arg_sets = [])
5
+ @arg_sets = arg_sets
6
+ end
7
+
8
+ def with(*args)
9
+ return add_new_set(ArgSet.new(*args))
10
+ end
11
+
12
+ def add_new_set(set)
13
+ @arg_sets << set
14
+ return set
15
+ end
16
+
17
+ def ignore_arg
18
+ return add_new_set(IgnoreSet.new)
19
+ end
20
+
21
+ def and_return(item)
22
+ ignore_arg.and_return(item)
23
+ end
24
+
25
+
26
+ def invoke(*args)
27
+ set = @arg_sets.find{|item| item.matches?(*args)} || ignore_arg
28
+ set.capture_args(*args)
29
+ return set.return_value
30
+ end
31
+
32
+ def called_with(*args)
33
+ return @arg_sets.find{|item| item.was_called_with?(*args)}
34
+ end
35
+
36
+ def times?(value)
37
+ total = @arg_sets.inject(0){|sum,item|sum += item.times_called}
38
+ return total == value
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module DevelopWithPassion
2
+ module Fakes
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'developwithpassion_fakes/arg_behaviour'
2
+ require 'developwithpassion_fakes/arg_set'
3
+ require 'developwithpassion_fakes/fake'
4
+ require 'developwithpassion_fakes/ignore_set'
5
+ require 'developwithpassion_fakes/method_stub'
@@ -0,0 +1,9 @@
1
+ require 'rspec'
2
+
3
+ Dir.chdir(File.join(File.dirname(__FILE__),"..,lib".split(','))) do
4
+ Dir.glob("**/*.rb").each do |file|
5
+ full_path = File.expand_path(file)
6
+ $:.unshift File.expand_path(File.dirname(full_path))
7
+ require full_path
8
+ end
9
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ module DevelopWithPassion
4
+ module Fakes
5
+ describe ArgBehaviour do
6
+ class AnArg
7
+ attr_accessor :called_args,:args
8
+ def initialize
9
+ @times_called = 0
10
+ end
11
+ end
12
+
13
+ let(:sut){AnArg.new}
14
+ before (:each) do
15
+ sut.send(:extend,ArgBehaviour)
16
+ end
17
+ context "when a return value is specified" do
18
+ before (:each) do
19
+ sut.and_return(2)
20
+ end
21
+ it "should store the return value to be returned during invocation" do
22
+ sut.return_value.should == 2
23
+ end
24
+ end
25
+
26
+ context "when handling an invocation" do
27
+ before (:each) do
28
+ sut.capture_args(2)
29
+ end
30
+ it "should increment the number of times it was called" do
31
+ sut.times_called.should == 1
32
+ end
33
+ it "should store the arguments it was called with" do
34
+ sut.called_args.should == [2]
35
+ end
36
+ end
37
+
38
+ context "when matching a set of arguments" do
39
+ before (:each) do
40
+ sut.args = [2]
41
+ end
42
+ it "should match if its own set of arguments are the same" do
43
+ sut.matches?(2).should be_true
44
+ sut.matches?(3).should be_false
45
+ end
46
+ end
47
+
48
+ context "when determining whether it was called with a set of arguments" do
49
+ before (:each) do
50
+ sut.called_args = [2]
51
+ end
52
+
53
+ it "should match if the arguments are the same as the arguments it was invoked with" do
54
+ sut.was_called_with?(2).should be_true
55
+ sut.was_called_with?(3).should be_false
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ module DevelopWithPassion
4
+ module Fakes
5
+ describe ArgSet do
6
+ context "when created" do
7
+ let(:sut){ArgSet.new(1)}
8
+
9
+ it "should initialize required members" do
10
+ sut.times_called.should == 0
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,140 @@
1
+ require 'spec_helper'
2
+
3
+ module DevelopWithPassion
4
+ module Fakes
5
+ describe Fake do
6
+ context "when stubbing a method" do
7
+ let(:invocations){Hash.new}
8
+ let(:sut){Fake.new(invocations)}
9
+ let(:symbol){:hello}
10
+ let(:new_method){Object.new}
11
+
12
+ context "and the method is not currently setup to be called" do
13
+ before (:each) do
14
+ MethodStub.stub(:new).and_return(new_method)
15
+ end
16
+ before (:each) do
17
+ @result = sut.stub(symbol)
18
+ end
19
+ it "should add a new method stub to the list of all invocations" do
20
+ invocations[symbol].should == new_method
21
+ end
22
+ it "should return the method invocation to continue specifying call behaviour" do
23
+ @result.should == new_method
24
+ end
25
+ end
26
+
27
+ context "and the method is already in the list of invocations" do
28
+ before (:each) do
29
+ invocations[symbol] = new_method
30
+ end
31
+ before (:each) do
32
+ @result = sut.stub(symbol)
33
+ end
34
+
35
+ it "should not readd the method to the list of invocations" do
36
+ invocations.count.should == 1
37
+ end
38
+
39
+ it "should return the method invocation to continue specifying call behaviour" do
40
+ @result.should == new_method
41
+ end
42
+ end
43
+ end
44
+ context "when accessing the behaviour for a received call" do
45
+ let(:invocations){Hash.new}
46
+ let(:sut){Fake.new(invocations)}
47
+ let(:symbol){:hello}
48
+ let(:method_invocation){Object.new}
49
+
50
+ before (:each) do
51
+ invocations[symbol] = method_invocation
52
+ end
53
+ before (:each) do
54
+ @result = sut.received(symbol)
55
+ end
56
+ it "should return the method invocation for the called method" do
57
+ @result.should == method_invocation
58
+ end
59
+ end
60
+ context "when verifying whether a call was never received" do
61
+ let(:invocations){Hash.new}
62
+ let(:sut){Fake.new(invocations)}
63
+ let(:existing){:hello}
64
+ let(:method_invocation){Object.new}
65
+
66
+ before (:each) do
67
+ invocations[existing] = method_invocation
68
+ end
69
+
70
+
71
+ it "should base its decision on the list of received invocations" do
72
+ [:other,existing].each do|item|
73
+ sut.never_received?(item).should_not be_equal(invocations.has_key?(item))
74
+ end
75
+ end
76
+ end
77
+ context "when method missing is triggered" do
78
+ class FakeInvocation
79
+ attr_accessor :invoke_was_called,:args,:return_value,:ignores_args
80
+
81
+ def initialize(return_value)
82
+ @return_value = return_value
83
+ end
84
+
85
+ def invoke(args)
86
+ @args = args
87
+ return @return_value
88
+ end
89
+
90
+ def ignore_arg
91
+ @ignores_args = true
92
+ end
93
+ end
94
+ let(:invocations){Hash.new}
95
+ let(:sut){Fake.new(invocations)}
96
+ let(:symbol){:hello}
97
+ let(:invocation){FakeInvocation.new(Object.new)}
98
+ let(:args){"world"}
99
+ context "and the method is for an invocation that was prepared" do
100
+ before (:each) do
101
+ invocations[symbol] = invocation
102
+ end
103
+ before (:each) do
104
+ @result = sut.hello(args)
105
+ end
106
+ it "should trigger the invocation with the arguments" do
107
+ invocation.args.should == args
108
+ end
109
+ it "should return the result of triggering the invocation" do
110
+ @result.should == invocation.return_value
111
+ end
112
+ end
113
+ context "and the method is for an invocation that was not prepared" do
114
+ before (:each) do
115
+ MethodStub.stub(:new).and_return(invocation)
116
+ end
117
+ before (:each) do
118
+ @result = sut.hello(args)
119
+ end
120
+ it "should add a new invocation which ignores arguments to the list of all invocations" do
121
+ invocations.has_key?(:hello).should be_true
122
+ end
123
+
124
+ it "should configure the new invocation to ignore all arguments" do
125
+ invocation.ignores_args.should be_true
126
+ end
127
+
128
+ it "should invoke the invocation with the arguments" do
129
+ invocation.args.should == args
130
+ end
131
+
132
+ it "should return the result of triggering the new invocation" do
133
+ @result.should == invocation.return_value
134
+ end
135
+ end
136
+
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ module DevelopWithPassion
4
+ module Fakes
5
+ describe IgnoreSet do
6
+ let(:sut){IgnoreSet.new}
7
+
8
+ context "when created" do
9
+ it "should initialize required members" do
10
+ sut.times_called.should == 0
11
+ end
12
+ end
13
+
14
+ context "when matching an argument set" do
15
+ it "should match any argument set" do
16
+ sut.matches?(1,2,3,4).should be_true
17
+ sut.matches?(3,"hello",4,5).should be_true
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,152 @@
1
+ require 'spec_helper'
2
+
3
+ module DevelopWithPassion
4
+ module Fakes
5
+ describe MethodStub do
6
+ let(:args){[1,2,3]}
7
+ let(:argument_set){Object.new}
8
+
9
+ context "when specifying a set of arguments it should be called with" do
10
+ let(:arg_sets){[]}
11
+ let(:sut){MethodStub.new(arg_sets)}
12
+
13
+ before (:each) do
14
+ ArgSet.stub(:new).with(args).and_return(argument_set)
15
+ end
16
+ before (:each) do
17
+ @result = sut.with(args)
18
+ end
19
+ it "should add a new argument set to its list of arguments" do
20
+ arg_sets[0].should == argument_set
21
+ end
22
+ it "should return the argument set to continue specifying behaviour" do
23
+ @result.should == argument_set
24
+ end
25
+ end
26
+
27
+ context "when ignoring arguments" do
28
+ let(:ignored_set){Object.new}
29
+ let(:arg_sets){[]}
30
+ let(:sut){MethodStub.new(arg_sets)}
31
+
32
+ before (:each) do
33
+ IgnoreSet.stub(:new).and_return(ignored_set)
34
+ end
35
+ before (:each) do
36
+ @result = sut.ignore_arg
37
+ end
38
+ it "should add the ignored set to the list of argument sets" do
39
+ arg_sets[0].should == ignored_set
40
+ end
41
+ it "should return the ignored set to specify other behaviour" do
42
+ @result.should == ignored_set
43
+ end
44
+ end
45
+
46
+ context "when invoked with a set of arguments" do
47
+ let(:arg_sets){[]}
48
+ let(:sut){MethodStub.new(arg_sets)}
49
+ class DummyArgSet
50
+ attr_accessor :args
51
+
52
+ def capture_args(*args)
53
+ @args = *args
54
+ end
55
+ end
56
+ context "and it has the specified argument set" do
57
+ let(:arguments){[1]}
58
+ let(:arg_set){DummyArgSet.new(2)}
59
+ before (:each) do
60
+ arg_sets.push(arg_set)
61
+ arg_set.stub(:matches?).and_return(true)
62
+ arg_set.stub(:return_value).and_return(2)
63
+ end
64
+ before (:each) do
65
+ @result = sut.invoke(arguments)
66
+ end
67
+ it "should tell the argument set to capture the arguments it was called with" do
68
+ arg_set.args.should == [arguments]
69
+ end
70
+ it "should return using from the arg set" do
71
+ @result.should == 2
72
+ end
73
+
74
+ end
75
+ context "and it does not have the specified argument set" do
76
+ let(:arguments){[1]}
77
+ let(:arg_set){DummyArgSet.new(2)}
78
+ before (:each) do
79
+ sut.stub(:ignore_arg).and_return(arg_set)
80
+ arg_set.stub(:return_value).and_return(2)
81
+ end
82
+ before (:each) do
83
+ @result = sut.invoke(arguments)
84
+ end
85
+ it "should tell the argument set to capture the arguments it was called with" do
86
+ arg_set.args.should == [arguments]
87
+ end
88
+ it "should return using from the missing arg set" do
89
+ @result.should == 2
90
+ end
91
+ end
92
+
93
+ end
94
+
95
+ context "when determining whether it was called with a set of arguments" do
96
+ let(:arg_sets){[]}
97
+ let(:sut){MethodStub.new(arg_sets)}
98
+ class DummyArgSet
99
+ attr_accessor :args
100
+
101
+ def capture_args(*args)
102
+ @args = *args
103
+ end
104
+ end
105
+ let(:arguments){[1]}
106
+
107
+ context "and one of its argument sets was called with the set of arguments" do
108
+ let(:arg_set){DummyArgSet.new(2)}
109
+ before (:each) do
110
+ arg_sets.push(arg_set)
111
+ arg_set.stub(:was_called_with?).with(arguments).and_return(true)
112
+ end
113
+ before (:each) do
114
+ @result = sut.called_with(arguments)
115
+ end
116
+
117
+ it "should return the argument set that was called with the arguments" do
118
+ @result.should == arg_set
119
+ end
120
+ end
121
+ context "and none of its argument sets were called with the arguments" do
122
+ before (:each) do
123
+ @result = sut.called_with(arguments)
124
+ end
125
+ it "should return nil" do
126
+ @result.should be_nil
127
+ end
128
+ end
129
+
130
+ end
131
+
132
+ context "when verifying whether it was called a certain number of times" do
133
+ let(:arg_sets){[]}
134
+ let(:sut){MethodStub.new(arg_sets)}
135
+ let(:arg_set){DummyArgSet.new(2)}
136
+ let(:arg_set_2){DummyArgSet.new(2)}
137
+
138
+ before (:each) do
139
+ arg_sets.push(arg_set)
140
+ arg_sets.push(arg_set_2)
141
+ arg_set.stub(:times_called).and_return(2)
142
+ arg_set_2.stub(:times_called).and_return(3)
143
+ end
144
+
145
+ it "it should return whether the sum of its argset invocations is the same as the number of request made" do
146
+ sut.times?(5).should be_true
147
+ sut.times?(3).should be_false
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: developwithpassion_fakes
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - "Develop With Passion\xC2\xAE"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-02-10 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ description: Faking library that allows inspection of received calls after they have been made. Also supports tracking calls with multiple argument sets.
27
+ email:
28
+ - open_source@developwithpassion.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - .gitignore
37
+ - .rvmrc
38
+ - Gemfile
39
+ - Rakefile
40
+ - developwithpassion_fakes.gemspec
41
+ - lib/developwithpassion_fakes.rb
42
+ - lib/developwithpassion_fakes/arg_behaviour.rb
43
+ - lib/developwithpassion_fakes/arg_set.rb
44
+ - lib/developwithpassion_fakes/fake.rb
45
+ - lib/developwithpassion_fakes/ignore_set.rb
46
+ - lib/developwithpassion_fakes/method_stub.rb
47
+ - lib/developwithpassion_fakes/version.rb
48
+ - spec/spec_helper.rb
49
+ - spec/specs/arg_behaviour_spec.rb
50
+ - spec/specs/arg_set_spec.rb
51
+ - spec/specs/fake_spec.rb
52
+ - spec/specs/ignore_set_spec.rb
53
+ - spec/specs/method_stub_spec.rb
54
+ homepage: http://www.developwithpassion.com
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project: developwithpassion_fakes
77
+ rubygems_version: 1.8.11
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Simple faking library
81
+ test_files:
82
+ - spec/spec_helper.rb
83
+ - spec/specs/arg_behaviour_spec.rb
84
+ - spec/specs/arg_set_spec.rb
85
+ - spec/specs/fake_spec.rb
86
+ - spec/specs/ignore_set_spec.rb
87
+ - spec/specs/method_stub_spec.rb