fakes 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/core/arg_behaviour.rb +7 -3
- data/lib/core/arg_matching/arg_match_factory.rb +12 -0
- data/lib/core/arg_matching/arg_match_protocol.rb +6 -0
- data/lib/core/arg_matching/block_arg_matcher.rb +14 -0
- data/lib/core/arg_matching/combined_arg_matcher.rb +24 -0
- data/lib/core/arg_matching/matches.rb +26 -0
- data/lib/core/arg_matching/regular_arg_matcher.rb +13 -0
- data/lib/core/arg_set.rb +1 -1
- data/lib/core/ignore_set.rb +2 -2
- data/lib/core/version.rb +1 -1
- data/lib/fakes.rb +6 -0
- data/spec/specs/arg_behaviour_spec.rb +15 -13
- data/spec/specs/arg_match_factory_spec.rb +35 -0
- data/spec/specs/arg_matching_scenarios_spec.rb +57 -0
- data/spec/specs/arg_set_spec.rb +4 -1
- data/spec/specs/block_arg_matcher_spec.rb +14 -0
- data/spec/specs/combined_arg_matcher_spec.rb +44 -0
- data/spec/specs/fake_spec.rb +17 -0
- data/spec/specs/ignore_set_spec.rb +11 -2
- data/spec/specs/matches_spec.rb +36 -0
- data/spec/specs/regular_arg_matcher_spec.rb +15 -0
- metadata +20 -2
data/lib/core/arg_behaviour.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
module Fakes
|
2
2
|
module ArgBehaviour
|
3
|
-
attr_accessor :return_value,:times_called
|
3
|
+
attr_accessor :return_value,:times_called,:arg_matcher
|
4
|
+
|
5
|
+
def initialize_matcher_using(args)
|
6
|
+
@arg_matcher = ArgMatchFactory.create_arg_matcher_using(args)
|
7
|
+
end
|
4
8
|
|
5
9
|
def and_return(item)
|
6
10
|
@return_value = item
|
@@ -16,11 +20,11 @@ module Fakes
|
|
16
20
|
end
|
17
21
|
|
18
22
|
def matches?(args)
|
19
|
-
return @args
|
23
|
+
return @arg_matcher.matches?(args)
|
20
24
|
end
|
21
25
|
|
22
26
|
def was_called_with?(args)
|
23
|
-
|
27
|
+
ArgMatchFactory.create_arg_matcher_using(args).matches?(@called_args)
|
24
28
|
end
|
25
29
|
|
26
30
|
def process
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Fakes
|
2
|
+
class ArgMatchFactory
|
3
|
+
def self.create_arg_matcher_using(args)
|
4
|
+
matcher = CombinedArgMatcher.new
|
5
|
+
args.each do|arg|
|
6
|
+
current_matcher = arg.respond_to?(:is_used_in_the_arg_matching_process?) ? arg : RegularArgMatcher.new(arg)
|
7
|
+
matcher.add current_matcher
|
8
|
+
end
|
9
|
+
matcher
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Fakes
|
2
|
+
class CombinedArgMatcher
|
3
|
+
include ArgMatchProtocol
|
4
|
+
attr_reader :all_matchers
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
@all_matchers = options.fetch(:matchers,[])
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches?(args)
|
11
|
+
matches = true
|
12
|
+
for index in 0..@all_matchers.count - 1
|
13
|
+
matches &= @all_matchers[index].matches?(args[index])
|
14
|
+
return false unless matches
|
15
|
+
end
|
16
|
+
matches
|
17
|
+
end
|
18
|
+
|
19
|
+
def add(matcher)
|
20
|
+
@all_matchers << matcher
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Fakes
|
2
|
+
class Matches
|
3
|
+
class << self
|
4
|
+
def any
|
5
|
+
condition(lambda{|ignored| true})
|
6
|
+
end
|
7
|
+
|
8
|
+
def greater_than(value)
|
9
|
+
condition(lambda{|number| number > value})
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def in_range(range)
|
14
|
+
condition(lambda{|item| range === item})
|
15
|
+
end
|
16
|
+
|
17
|
+
def regex(regex)
|
18
|
+
condition(lambda{|string| regex =~ string})
|
19
|
+
end
|
20
|
+
|
21
|
+
def condition(conditional_block)
|
22
|
+
return BlockArgMatcher.new(conditional_block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/core/arg_set.rb
CHANGED
data/lib/core/ignore_set.rb
CHANGED
@@ -16,9 +16,9 @@ module Fakes
|
|
16
16
|
return true
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
19
|
def was_called_with?(args)
|
21
|
-
|
20
|
+
matcher = ArgMatchFactory.create_arg_matcher_using(args)
|
21
|
+
return @arg_sets.select{|set| matcher.matches?(set)}.count > 0
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
data/lib/core/version.rb
CHANGED
data/lib/fakes.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
require 'developwithpassion_arrays'
|
2
2
|
|
3
|
+
require 'core/arg_matching/arg_match_factory'
|
4
|
+
require 'core/arg_matching/arg_match_protocol'
|
5
|
+
require 'core/arg_matching/block_arg_matcher'
|
6
|
+
require 'core/arg_matching/combined_arg_matcher'
|
7
|
+
require 'core/arg_matching/matches'
|
8
|
+
require 'core/arg_matching/regular_arg_matcher'
|
3
9
|
require 'core/arg_behaviour'
|
4
10
|
require 'core/arg_set'
|
5
11
|
require 'core/fake'
|
@@ -48,34 +48,36 @@ module Fakes
|
|
48
48
|
sut.called_args.should == 2
|
49
49
|
end
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
context "when matching a set of arguments" do
|
53
|
+
let(:matcher){Object.new}
|
53
54
|
before (:each) do
|
54
|
-
sut.
|
55
|
+
sut.arg_matcher = matcher
|
56
|
+
|
57
|
+
matcher.stub(:matches?).with(2).and_return(true)
|
58
|
+
matcher.stub(:matches?).with(3).and_return(false)
|
55
59
|
end
|
56
|
-
it "should match if its
|
60
|
+
it "should match if its argument matcher matches the argument set" do
|
57
61
|
sut.matches?(2).should be_true
|
58
62
|
sut.matches?(3).should be_false
|
59
63
|
end
|
60
64
|
end
|
61
65
|
|
62
|
-
context "when matching a set of arguments that is passed in as a dictionary" do
|
63
|
-
before (:each) do
|
64
|
-
sut.args = {:id => 0,:name => "JP"}
|
65
|
-
end
|
66
|
-
it "should match if its hash is the same" do
|
67
|
-
sut.matches?(:id => 0,:name => "JP").should be_true
|
68
|
-
end
|
69
|
-
end
|
70
66
|
|
71
67
|
context "when determining whether it was called with a set of arguments" do
|
68
|
+
let(:the_matcher){Object.new}
|
69
|
+
|
70
|
+
before (:each) do
|
71
|
+
ArgMatchFactory.stub(:create_arg_matcher_using).with(2).and_return(the_matcher)
|
72
|
+
the_matcher.stub(:matches?).with(2).and_return(true)
|
73
|
+
end
|
74
|
+
|
72
75
|
before (:each) do
|
73
76
|
sut.called_args = 2
|
74
77
|
end
|
75
78
|
|
76
|
-
it "should
|
79
|
+
it "should make the decision by using the matcher created to match the arguments" do
|
77
80
|
sut.was_called_with?(2).should be_true
|
78
|
-
sut.was_called_with?(3).should be_false
|
79
81
|
end
|
80
82
|
end
|
81
83
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Fakes
|
4
|
+
describe ArgMatchFactory do
|
5
|
+
context "when creating an argument matcher" do
|
6
|
+
context "and none of the arguments are matchers themselves" do
|
7
|
+
before (:each) do
|
8
|
+
@result = ArgMatchFactory.create_arg_matcher_using([2,3,4])
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should create a combined matcher that is composed of regular matchers" do
|
12
|
+
@result.all_matchers.count.should == 3
|
13
|
+
@result.all_matchers.each do|matcher|
|
14
|
+
matcher.class.should == RegularArgMatcher
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
context "and the arguments are matchers themselves" do
|
19
|
+
let(:matcher){Object.new}
|
20
|
+
before (:each) do
|
21
|
+
matcher.stub(:respond_to?).with(:is_used_in_the_arg_matching_process?).and_return(true)
|
22
|
+
end
|
23
|
+
|
24
|
+
before (:each) do
|
25
|
+
@result = ArgMatchFactory.create_arg_matcher_using([matcher])
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should create a combined matcher that only using the matchers provided" do
|
29
|
+
@result.all_matchers.count.should == 1
|
30
|
+
@result.all_matchers[0].should == matcher
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Fakes
|
4
|
+
describe "Arg matching scenarios" do
|
5
|
+
it "should be able to intercept using argument matchers" do
|
6
|
+
fake = Fake.new
|
7
|
+
|
8
|
+
fake.hello("World")
|
9
|
+
|
10
|
+
fake.received(:hello).called_with(Matches.any).should_not be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be able to intercept using greater than matchers" do
|
14
|
+
fake = Fake.new
|
15
|
+
|
16
|
+
fake.hello(10)
|
17
|
+
|
18
|
+
fake.received(:hello).called_with(Matches.greater_than(2)).should_not be_nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should be able to intercept using regex matchers" do
|
22
|
+
fake = Fake.new
|
23
|
+
|
24
|
+
fake.hello("This is cool")
|
25
|
+
|
26
|
+
fake.received(:hello).called_with(Matches.regex(/is/)).should_not be_nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be able to intercept using range matchers" do
|
30
|
+
fake = Fake.new
|
31
|
+
|
32
|
+
fake.hello(7)
|
33
|
+
|
34
|
+
fake.received(:hello).called_with(Matches.in_range(4..8)).should_not be_nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should be able to intercept using conditional matchers" do
|
38
|
+
fake = Fake.new
|
39
|
+
|
40
|
+
fake.hello(7)
|
41
|
+
|
42
|
+
fake.received(:hello).called_with(Matches.condition(lambda{|item| item < 10})).should_not be_nil
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should be able to intercept by mixing regular arguments with matchers" do
|
46
|
+
fake = Fake.new
|
47
|
+
|
48
|
+
fake.hello(7,4,"Yes")
|
49
|
+
|
50
|
+
fake.received(:hello).called_with(7,Matches.greater_than(2),Matches.regex(/Y/)).should_not be_nil
|
51
|
+
fake.received(:hello).called_with(7,Matches.any,Matches.regex(/Y/)).should_not be_nil
|
52
|
+
fake.received(:hello).called_with(7,Matches.any,"Yes").should_not be_nil
|
53
|
+
fake.received(:hello).called_with(6,Matches.any,"Yes").should be_nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/spec/specs/arg_set_spec.rb
CHANGED
@@ -3,11 +3,14 @@ require 'spec_helper'
|
|
3
3
|
module Fakes
|
4
4
|
describe ArgSet do
|
5
5
|
context "when created" do
|
6
|
-
let(:sut){ArgSet.new(1)}
|
6
|
+
let(:sut){ArgSet.new([1])}
|
7
7
|
|
8
|
+
|
8
9
|
it "should initialize required members" do
|
9
10
|
sut.times_called.should == 0
|
10
11
|
end
|
12
|
+
|
11
13
|
end
|
14
|
+
|
12
15
|
end
|
13
16
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Fakes
|
4
|
+
describe BlockArgMatcher do
|
5
|
+
context "when determining if it matches a value" do
|
6
|
+
let(:sut){BlockArgMatcher.new(lambda{|item| true})}
|
7
|
+
|
8
|
+
it "should decide by using its provided block" do
|
9
|
+
sut.matches?(2).should be_true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Fakes
|
4
|
+
describe CombinedArgMatcher do
|
5
|
+
context "when determining whether it matches an set of arguments" do
|
6
|
+
let(:args){[2,3]}
|
7
|
+
let(:first_matcher){Object.new}
|
8
|
+
let(:second_matcher){Object.new}
|
9
|
+
let(:matchers){[first_matcher,second_matcher]}
|
10
|
+
let(:options){{:matchers => matchers}}
|
11
|
+
let(:sut){CombinedArgMatcher.new(options)}
|
12
|
+
before (:each) do
|
13
|
+
first_matcher.stub(:matches?).with(2).and_return(3)
|
14
|
+
second_matcher.stub(:matches?).with(3).and_return(3)
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
before (:each) do
|
19
|
+
@result = sut.matches?(args)
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
it "should match if each of its argument matchers matches its respective argument" do
|
24
|
+
@result.should be_true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
context "when adding a matcher" do
|
28
|
+
let(:first_matcher){Object.new}
|
29
|
+
let(:matchers){[]}
|
30
|
+
let(:options){{:matchers => matchers}}
|
31
|
+
let(:sut){CombinedArgMatcher.new(options)}
|
32
|
+
|
33
|
+
before (:each) do
|
34
|
+
sut.add(first_matcher)
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
it "should add the matcher to its set of matchers" do
|
39
|
+
matchers[0].should == first_matcher
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
data/spec/specs/fake_spec.rb
CHANGED
@@ -135,6 +135,22 @@ module Fakes
|
|
135
135
|
end
|
136
136
|
|
137
137
|
context "scenarios" do
|
138
|
+
context "setting up return values using argument matchers" do
|
139
|
+
it "should be able to intercept on methods using the matches factory" do
|
140
|
+
fake = Fake.new
|
141
|
+
|
142
|
+
fake.stub(:hello).with(Matches.regex(/W/)).and_return("Hello World")
|
143
|
+
fake.hello("World").should == "Hello World"
|
144
|
+
end
|
145
|
+
it "should be able to intercept on methods using combinations of explicit values and matchers" do
|
146
|
+
fake = Fake.new
|
147
|
+
|
148
|
+
fake.stub(:hello).with(Matches.regex(/W/),Matches.greater_than(3),10).and_return("Hello World")
|
149
|
+
|
150
|
+
fake.hello("World",4,10).should == "Hello World"
|
151
|
+
fake.hello("World",2,10).should == nil
|
152
|
+
end
|
153
|
+
end
|
138
154
|
context "setting up return values" do
|
139
155
|
it "should be able to intercept on methods that take a singular value" do
|
140
156
|
fake = Fake.new
|
@@ -214,6 +230,7 @@ module Fakes
|
|
214
230
|
fake.received(:hello).called_with(1,[1,2,3,4]).should_not be_nil
|
215
231
|
fake.received(:hello).called_with(1,[1,2,3,5]).should be_nil
|
216
232
|
end
|
233
|
+
|
217
234
|
end
|
218
235
|
end
|
219
236
|
end
|
@@ -28,11 +28,20 @@ module Fakes
|
|
28
28
|
end
|
29
29
|
|
30
30
|
context "when determining if it was called with a set of arguments" do
|
31
|
+
let(:the_matcher){Object.new}
|
32
|
+
|
33
|
+
before (:each) do
|
34
|
+
ArgMatchFactory.stub(:create_arg_matcher_using).with(2).and_return(the_matcher)
|
35
|
+
the_matcher.stub(:matches?).with(3).and_return(true)
|
36
|
+
the_matcher.stub(:matches?).with(1).and_return(true)
|
37
|
+
end
|
38
|
+
|
31
39
|
before (:each) do
|
32
40
|
sut.capture_args(1)
|
33
|
-
sut.capture_args(
|
41
|
+
sut.capture_args(3)
|
34
42
|
end
|
35
|
-
|
43
|
+
|
44
|
+
it "should make the decision by using the matcher created to match the argument sets that were stored" do
|
36
45
|
sut.was_called_with?(2).should be_true
|
37
46
|
end
|
38
47
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Fakes
|
2
|
+
describe Matches do
|
3
|
+
context "when creating matchers" do
|
4
|
+
it "should be able to create a matcher that matches anything" do
|
5
|
+
(1..10).each{|item| Matches.any.matches?(item).should be_true}
|
6
|
+
end
|
7
|
+
it "should be able to create a numeric greater than matcher" do
|
8
|
+
match = Matches.greater_than(5)
|
9
|
+
match.matches?(4).should be_false
|
10
|
+
match.matches?(5).should be_false
|
11
|
+
match.matches?(6).should be_true
|
12
|
+
|
13
|
+
end
|
14
|
+
it "should be able to create a range matcher" do
|
15
|
+
match = Matches.in_range((1..10))
|
16
|
+
match.matches?(4).should be_true
|
17
|
+
match.matches?(10).should be_true
|
18
|
+
match.matches?(11).should be_false
|
19
|
+
end
|
20
|
+
it "should be able to create a regex string matcher" do
|
21
|
+
match = Matches.regex(/a|e|i|o|u/)
|
22
|
+
match.matches?("awwef").should be_true
|
23
|
+
match.matches?("rwwgf").should be_false
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should be able to create a lambda based matcher" do
|
27
|
+
match = Matches.condition(lambda{|item| item > 3})
|
28
|
+
match.matches?(2).should be_false
|
29
|
+
match.matches?(7).should be_true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Fakes
|
4
|
+
describe RegularArgMatcher do
|
5
|
+
context "when determining if it matches a value" do
|
6
|
+
let(:sut){RegularArgMatcher.new(2)}
|
7
|
+
|
8
|
+
it "should match if the incoming value matches the value it was created to match" do
|
9
|
+
sut.matches?(2).should be_true
|
10
|
+
sut.matches?(3).should be_false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fakes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -108,6 +108,12 @@ files:
|
|
108
108
|
- Rakefile
|
109
109
|
- fakes.gemspec
|
110
110
|
- lib/core/arg_behaviour.rb
|
111
|
+
- lib/core/arg_matching/arg_match_factory.rb
|
112
|
+
- lib/core/arg_matching/arg_match_protocol.rb
|
113
|
+
- lib/core/arg_matching/block_arg_matcher.rb
|
114
|
+
- lib/core/arg_matching/combined_arg_matcher.rb
|
115
|
+
- lib/core/arg_matching/matches.rb
|
116
|
+
- lib/core/arg_matching/regular_arg_matcher.rb
|
111
117
|
- lib/core/arg_set.rb
|
112
118
|
- lib/core/fake.rb
|
113
119
|
- lib/core/ignore_set.rb
|
@@ -116,11 +122,17 @@ files:
|
|
116
122
|
- lib/fakes.rb
|
117
123
|
- spec/spec_helper.rb
|
118
124
|
- spec/specs/arg_behaviour_spec.rb
|
125
|
+
- spec/specs/arg_match_factory_spec.rb
|
126
|
+
- spec/specs/arg_matching_scenarios_spec.rb
|
119
127
|
- spec/specs/arg_set_spec.rb
|
128
|
+
- spec/specs/block_arg_matcher_spec.rb
|
129
|
+
- spec/specs/combined_arg_matcher_spec.rb
|
120
130
|
- spec/specs/fake_spec.rb
|
121
131
|
- spec/specs/ignore_set_spec.rb
|
122
132
|
- spec/specs/kernel_spec.rb
|
133
|
+
- spec/specs/matches_spec.rb
|
123
134
|
- spec/specs/method_stub_spec.rb
|
135
|
+
- spec/specs/regular_arg_matcher_spec.rb
|
124
136
|
homepage: http://www.developwithpassion.com
|
125
137
|
licenses: []
|
126
138
|
post_install_message:
|
@@ -148,8 +160,14 @@ summary: Simple faking library
|
|
148
160
|
test_files:
|
149
161
|
- spec/spec_helper.rb
|
150
162
|
- spec/specs/arg_behaviour_spec.rb
|
163
|
+
- spec/specs/arg_match_factory_spec.rb
|
164
|
+
- spec/specs/arg_matching_scenarios_spec.rb
|
151
165
|
- spec/specs/arg_set_spec.rb
|
166
|
+
- spec/specs/block_arg_matcher_spec.rb
|
167
|
+
- spec/specs/combined_arg_matcher_spec.rb
|
152
168
|
- spec/specs/fake_spec.rb
|
153
169
|
- spec/specs/ignore_set_spec.rb
|
154
170
|
- spec/specs/kernel_spec.rb
|
171
|
+
- spec/specs/matches_spec.rb
|
155
172
|
- spec/specs/method_stub_spec.rb
|
173
|
+
- spec/specs/regular_arg_matcher_spec.rb
|