fakes 0.3.2 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/core/arg_matching/matches.rb +8 -0
- data/lib/core/class_swap.rb +27 -0
- data/lib/core/class_swaps.rb +29 -0
- data/lib/core/version.rb +1 -1
- data/lib/fakes.rb +12 -3
- data/spec/spec_helper.rb +16 -0
- data/spec/specs/class_swap_spec.rb +73 -0
- data/spec/specs/class_swaps_spec.rb +99 -0
- data/spec/specs/extensions_spec.rb +38 -0
- data/spec/specs/matches_spec.rb +10 -0
- metadata +10 -4
- data/spec/specs/kernel_spec.rb +0 -6
@@ -0,0 +1,27 @@
|
|
1
|
+
module Fakes
|
2
|
+
class ClassSwap
|
3
|
+
attr_accessor :original,:replacement
|
4
|
+
attr_reader :klass
|
5
|
+
|
6
|
+
def initialize(klass,replacement,options ={})
|
7
|
+
@klass = klass.to_s.to_sym
|
8
|
+
@replacement = replacement
|
9
|
+
@remove_strategy = options.fetch(:remove_strategy,lambda{|klass_symbol| Object.send(:remove_const,klass_symbol)})
|
10
|
+
@set_strategy = options.fetch(:set_strategy,lambda{|klass_symbol,new_value| Object.const_set(klass_symbol,new_value)})
|
11
|
+
end
|
12
|
+
|
13
|
+
def initiate
|
14
|
+
swap_to(replacement){|original| @original = original}
|
15
|
+
end
|
16
|
+
|
17
|
+
def reset
|
18
|
+
swap_to(@original)
|
19
|
+
end
|
20
|
+
|
21
|
+
def swap_to(new_value,&block)
|
22
|
+
current = @remove_strategy.call(@klass)
|
23
|
+
yield current if block_given?
|
24
|
+
@set_strategy.call(@klass,new_value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Fakes
|
4
|
+
class ClassSwaps
|
5
|
+
include Singleton
|
6
|
+
attr_reader :swaps
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@swaps = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_fake_for(klass,the_fake)
|
13
|
+
symbol = klass.to_s.to_sym
|
14
|
+
ensure_swap_does_not_already_exist_for(symbol)
|
15
|
+
swap = ClassSwap.new(klass,the_fake)
|
16
|
+
@swaps[symbol] = swap
|
17
|
+
swap.initiate
|
18
|
+
end
|
19
|
+
|
20
|
+
def ensure_swap_does_not_already_exist_for(symbol)
|
21
|
+
raise "A swap already exists for the class #{symbol}" if @swaps.has_key?(symbol)
|
22
|
+
end
|
23
|
+
|
24
|
+
def reset
|
25
|
+
@swaps.values.each{|item| item.reset}
|
26
|
+
@swaps.clear
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/core/version.rb
CHANGED
data/lib/fakes.rb
CHANGED
@@ -7,17 +7,26 @@ require 'core/arg_matching/matches'
|
|
7
7
|
require 'core/arg_matching/regular_arg_matcher'
|
8
8
|
require 'core/arg_behaviour'
|
9
9
|
require 'core/arg_set'
|
10
|
+
require 'core/class_swap'
|
11
|
+
require 'core/class_swaps'
|
10
12
|
require 'core/fake'
|
11
13
|
require 'core/ignore_set'
|
12
14
|
require 'core/method_stub'
|
15
|
+
require 'singleton'
|
13
16
|
|
14
|
-
|
17
|
+
class Object
|
15
18
|
def fake
|
16
19
|
return Fakes::Fake.new
|
17
20
|
end
|
18
|
-
end
|
19
|
-
class Object
|
20
21
|
def matches
|
21
22
|
return Fakes::Matches
|
22
23
|
end
|
24
|
+
def fake_class(klass)
|
25
|
+
item = fake
|
26
|
+
Fakes::ClassSwaps.instance.add_fake_for(klass,item)
|
27
|
+
item
|
28
|
+
end
|
29
|
+
def reset_fake_classes
|
30
|
+
Fakes::ClassSwaps.instance.reset
|
31
|
+
end
|
23
32
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,19 @@
|
|
1
1
|
Dir.chdir(File.join(File.dirname(__FILE__),"..,lib".split(','))) do
|
2
2
|
require 'fakes.rb'
|
3
3
|
end
|
4
|
+
|
5
|
+
def catch_exception(&block)
|
6
|
+
begin
|
7
|
+
yield
|
8
|
+
rescue Exception => e
|
9
|
+
e
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module RSpec
|
14
|
+
Matchers.define :contain do|string_to_find|
|
15
|
+
match do|string_element|
|
16
|
+
string_element.include?(string_to_find)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Fakes
|
4
|
+
class MyClass
|
5
|
+
end
|
6
|
+
describe ClassSwap do
|
7
|
+
context "when created" do
|
8
|
+
let(:sut){ClassSwap.new(MyClass,Object.new)}
|
9
|
+
|
10
|
+
it "should store the symbol of the class it is going to replace" do
|
11
|
+
sut.klass.should == MyClass.to_s.to_sym
|
12
|
+
end
|
13
|
+
end
|
14
|
+
context "when initiated" do
|
15
|
+
let(:replacement){Object.new}
|
16
|
+
let(:the_sym){MyClass.to_s.to_sym}
|
17
|
+
let(:remove_strategy){Proc.new do|klass_to_remove|
|
18
|
+
@klass_to_remove = klass_to_remove
|
19
|
+
MyClass
|
20
|
+
end}
|
21
|
+
let(:set_strategy){Proc.new do|klass_to_change,new_value|
|
22
|
+
@klass_to_change = klass_to_change
|
23
|
+
@replacement_value = new_value
|
24
|
+
MyClass
|
25
|
+
end}
|
26
|
+
let(:sut){ClassSwap.new(MyClass,replacement,:remove_strategy => remove_strategy,:set_strategy => set_strategy)}
|
27
|
+
|
28
|
+
before (:each) do
|
29
|
+
sut.initiate
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should remove the current value of the class constant and store it for reset at a later point" do
|
33
|
+
@klass_to_remove.should == the_sym
|
34
|
+
end
|
35
|
+
it "should replace the current value of the symbol with the replacement value" do
|
36
|
+
@klass_to_change.should == the_sym
|
37
|
+
@replacement_value.should == replacement
|
38
|
+
end
|
39
|
+
end
|
40
|
+
context "when reset" do
|
41
|
+
let(:original){MyClass}
|
42
|
+
let(:the_sym){MyClass.to_s.to_sym}
|
43
|
+
let(:replacement){Object.new}
|
44
|
+
let(:remove_strategy){Proc.new do|klass_to_remove|
|
45
|
+
@klass_to_remove = klass_to_remove
|
46
|
+
MyClass
|
47
|
+
end}
|
48
|
+
let(:set_strategy){Proc.new do|klass_to_change,new_value|
|
49
|
+
@klass_to_change = klass_to_change
|
50
|
+
@replacement_value = new_value
|
51
|
+
MyClass
|
52
|
+
end}
|
53
|
+
let(:sut){ClassSwap.new(MyClass,replacement,:remove_strategy => remove_strategy,:set_strategy => set_strategy)}
|
54
|
+
|
55
|
+
before (:each) do
|
56
|
+
sut.original = MyClass
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
before (:each) do
|
63
|
+
sut.reset
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should switch the value of the class back to its original value" do
|
67
|
+
@klass_to_remove.should == the_sym
|
68
|
+
@klass_to_change.should == the_sym
|
69
|
+
@replacement_value.should == MyClass
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Fakes
|
4
|
+
class SomeClass
|
5
|
+
def self.calculate
|
6
|
+
42
|
7
|
+
end
|
8
|
+
end
|
9
|
+
describe ClassSwaps do
|
10
|
+
class MySwap
|
11
|
+
attr_accessor :inititated,:was_reset
|
12
|
+
|
13
|
+
def initiate
|
14
|
+
@inititated = true
|
15
|
+
end
|
16
|
+
def reset
|
17
|
+
@was_reset = true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
context 'when a class swap is registered ' do
|
21
|
+
context 'and it does not already exist' do
|
22
|
+
let(:the_sym){MyClass.to_s.to_sym}
|
23
|
+
let(:replacement){Object.new}
|
24
|
+
let(:the_swap){MySwap.new}
|
25
|
+
let(:sut){ClassSwaps.instance}
|
26
|
+
before (:each) do
|
27
|
+
ClassSwap.stub(:new).with(MyClass,replacement).and_return(the_swap)
|
28
|
+
end
|
29
|
+
after (:each) do
|
30
|
+
sut.swaps.clear
|
31
|
+
end
|
32
|
+
|
33
|
+
before (:each) do
|
34
|
+
sut.add_fake_for(MyClass,replacement)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should add a new class swap to the set of class swaps' do
|
38
|
+
ClassSwaps.instance.swaps[the_sym].should == the_swap
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should initiate the swap" do
|
42
|
+
the_swap.inititated.should be_true
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
end
|
47
|
+
context 'and it already exist' do
|
48
|
+
let(:the_sym){MyClass.to_s.to_sym}
|
49
|
+
let(:sut){ClassSwaps.instance}
|
50
|
+
before (:each) do
|
51
|
+
sut.swaps[the_sym] = 2
|
52
|
+
end
|
53
|
+
after (:each) do
|
54
|
+
sut.swaps.clear
|
55
|
+
end
|
56
|
+
|
57
|
+
before (:each) do
|
58
|
+
@exception = catch_exception {sut.add_fake_for(MyClass,Object.new)}
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should throw an error indicating that the swap is already present' do
|
62
|
+
@exception.message.should contain(MyClass.to_s)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
context 'when reset' do
|
67
|
+
let(:first_swap){MySwap.new}
|
68
|
+
let(:second_swap){MySwap.new}
|
69
|
+
let(:sut){ClassSwaps.instance}
|
70
|
+
|
71
|
+
before (:each) do
|
72
|
+
sut.swaps[:first] = first_swap
|
73
|
+
sut.swaps[:second] = second_swap
|
74
|
+
end
|
75
|
+
|
76
|
+
before (:each) do
|
77
|
+
sut.reset
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should reset each of the swaps' do
|
81
|
+
first_swap.was_reset.should be_true
|
82
|
+
second_swap.was_reset.should be_true
|
83
|
+
end
|
84
|
+
it "should clear the swaps" do
|
85
|
+
sut.swaps.count.should == 0
|
86
|
+
end
|
87
|
+
end
|
88
|
+
context "Integration Test" do
|
89
|
+
let(:replacement){ Object.new }
|
90
|
+
|
91
|
+
it 'should be able to swap class values' do
|
92
|
+
ClassSwaps.instance.add_fake_for(Dir,replacement)
|
93
|
+
Dir.should == replacement
|
94
|
+
ClassSwaps.instance.reset
|
95
|
+
Dir.should_not == replacement
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
describe Object do
|
2
|
+
it "should be able to create a new fake" do
|
3
|
+
fake.class.should == Fakes::Fake
|
4
|
+
end
|
5
|
+
|
6
|
+
context "when specifying a fake for a class" do
|
7
|
+
let(:swaps){fake}
|
8
|
+
before (:each) do
|
9
|
+
Fakes::ClassSwaps.stub(:instance).and_return(swaps)
|
10
|
+
end
|
11
|
+
before (:each) do
|
12
|
+
@result = fake_class Dir
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should register a new fake with the class swaps" do
|
16
|
+
swaps.received(:add_fake_for).called_with(Dir,matches.not_nil).should_not be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return the newly created fake" do
|
20
|
+
@result.should_not be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
context "resetting fake classes" do
|
26
|
+
let(:swaps){fake}
|
27
|
+
before (:each) do
|
28
|
+
Fakes::ClassSwaps.stub(:instance).and_return(swaps)
|
29
|
+
end
|
30
|
+
before (:each) do
|
31
|
+
reset_fake_classes
|
32
|
+
end
|
33
|
+
it "should be able to reset all of the fake classes" do
|
34
|
+
swaps.received(:reset)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
data/spec/specs/matches_spec.rb
CHANGED
@@ -17,6 +17,16 @@ module Fakes
|
|
17
17
|
match.matches?(10).should be_true
|
18
18
|
match.matches?(11).should be_false
|
19
19
|
end
|
20
|
+
it "should be able to create a nil matcher" do
|
21
|
+
match = Matches.nil
|
22
|
+
match.matches?(nil).should be_true
|
23
|
+
match.matches?(10).should be_false
|
24
|
+
end
|
25
|
+
it "should be able to create a not nil matcher" do
|
26
|
+
match = Matches.not_nil
|
27
|
+
match.matches?(10).should be_true
|
28
|
+
match.matches?(nil).should be_false
|
29
|
+
end
|
20
30
|
it "should be able to create a regex string matcher" do
|
21
31
|
match = Matches.regex(/a|e|i|o|u/)
|
22
32
|
match.matches?("awwef").should be_true
|
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.3.
|
4
|
+
version: 0.3.7
|
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-06-
|
12
|
+
date: 2012-06-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -114,6 +114,8 @@ files:
|
|
114
114
|
- lib/core/arg_matching/matches.rb
|
115
115
|
- lib/core/arg_matching/regular_arg_matcher.rb
|
116
116
|
- lib/core/arg_set.rb
|
117
|
+
- lib/core/class_swap.rb
|
118
|
+
- lib/core/class_swaps.rb
|
117
119
|
- lib/core/fake.rb
|
118
120
|
- lib/core/ignore_set.rb
|
119
121
|
- lib/core/method_stub.rb
|
@@ -125,10 +127,12 @@ files:
|
|
125
127
|
- spec/specs/arg_matching_scenarios_spec.rb
|
126
128
|
- spec/specs/arg_set_spec.rb
|
127
129
|
- spec/specs/block_arg_matcher_spec.rb
|
130
|
+
- spec/specs/class_swap_spec.rb
|
131
|
+
- spec/specs/class_swaps_spec.rb
|
128
132
|
- spec/specs/combined_arg_matcher_spec.rb
|
133
|
+
- spec/specs/extensions_spec.rb
|
129
134
|
- spec/specs/fake_spec.rb
|
130
135
|
- spec/specs/ignore_set_spec.rb
|
131
|
-
- spec/specs/kernel_spec.rb
|
132
136
|
- spec/specs/matches_spec.rb
|
133
137
|
- spec/specs/method_stub_spec.rb
|
134
138
|
- spec/specs/regular_arg_matcher_spec.rb
|
@@ -163,10 +167,12 @@ test_files:
|
|
163
167
|
- spec/specs/arg_matching_scenarios_spec.rb
|
164
168
|
- spec/specs/arg_set_spec.rb
|
165
169
|
- spec/specs/block_arg_matcher_spec.rb
|
170
|
+
- spec/specs/class_swap_spec.rb
|
171
|
+
- spec/specs/class_swaps_spec.rb
|
166
172
|
- spec/specs/combined_arg_matcher_spec.rb
|
173
|
+
- spec/specs/extensions_spec.rb
|
167
174
|
- spec/specs/fake_spec.rb
|
168
175
|
- spec/specs/ignore_set_spec.rb
|
169
|
-
- spec/specs/kernel_spec.rb
|
170
176
|
- spec/specs/matches_spec.rb
|
171
177
|
- spec/specs/method_stub_spec.rb
|
172
178
|
- spec/specs/regular_arg_matcher_spec.rb
|