orangutan 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -82,6 +82,20 @@ calls.each {|call| puts "#{call.name}.#{call.method}(#{call.args.join(',')})" }
82
82
 
83
83
  # Recorded call sequences can also be used for assertions in combination with or instead of checking expectations.
84
84
 
85
+ # Stubbing methods on object instances
86
+
87
+ register_object :math, Math # registers the object (this just gives the object a name for referencing in calls)
88
+ stub_method :math, :cos # replaces the implementation of the cos method with a recorder
89
+ so_when(:math).receives(:cos).with(0).return("i just can't take it anymore")
90
+
91
+ # Registering as a mock framework with rspec
92
+
93
+ require 'orangutan/mock_adapter'
94
+ Spec::Runner.configure do |config|
95
+ # turn off mocking
96
+ config.mock_with Orangutan::MockAdapter
97
+ end
98
+
85
99
  Questions:
86
100
 
87
101
  * Why Orangutan?
@@ -94,8 +108,4 @@ Definately not. What a ridiculous question. I'm appalled.
94
108
 
95
109
  * What's Chantek?
96
110
 
97
- Chantek is a famous orangutan that can solve sudokus and the rubik's cube - http://en.wikipedia.org/wiki/Chantek
98
-
99
- * Why do I not need to register this as a mock framework?
100
-
101
- Most frameworks such as rspec's mocks, mocha and stubba replace methods on existing classes. Because this can't be done with this library - you can only create pure stub objects and setup method behaviours, there's nothing that needs to be undone at the end of a test/example.
111
+ Chantek is a famous orangutan that can solve sudokus and the rubik's cube - http://en.wikipedia.org/wiki/Chantek
@@ -1,7 +1,9 @@
1
+ require 'orangutan/object_extensions'
1
2
  require 'orangutan/stub_base'
2
3
  require 'orangutan/expectation'
3
4
  require 'orangutan/call'
4
5
  require 'orangutan/reflector'
6
+ require 'orangutan/stub_include'
5
7
 
6
8
  module Orangutan
7
9
  module Chantek
@@ -16,6 +18,37 @@ module Orangutan
16
18
  @expectations = {}
17
19
  @stubs= {}
18
20
  @events = {}
21
+ @stubbed_methods = []
22
+ end
23
+
24
+ def register_object name, object
25
+ @stubs[name] ||= object
26
+ end
27
+
28
+ def stub_method name, method
29
+ object = @stubs[name]
30
+ raise "could not find an object registered with the name #{name}" unless object
31
+ chantek = self
32
+ object.instance_eval do
33
+ @parent = chantek
34
+ @name = name
35
+ end
36
+ @stubbed_methods << [name, method]
37
+ object.eigen_eval do
38
+ alias_method("__unstubbed_#{method}", method)
39
+ end
40
+ implement_method object.eigen, method
41
+ end
42
+
43
+ def restore_method name, method
44
+ @stubs[name].eigen_eval do
45
+ alias_method(method, "__unstubbed_#{method}")
46
+ end
47
+ @stubbed_methods.delete [name, method]
48
+ end
49
+
50
+ def restore_methods
51
+ @stubbed_methods.each {|tuple| restore_method tuple[0], tuple[1]}
19
52
  end
20
53
 
21
54
  def stub name, params={}
@@ -39,6 +72,8 @@ module Orangutan
39
72
 
40
73
  def implement_method clazz, name
41
74
  clazz.instance_eval do
75
+ include StubInclude
76
+
42
77
  define_method name do |*args|
43
78
  yield_container, return_container = __react__(name, args)
44
79
  yield_container.value.each {|v| yield *v } if yield_container && block_given?
@@ -11,6 +11,7 @@ module Orangutan
11
11
  end
12
12
 
13
13
  def teardown_mocks_for_rspec
14
+ restore_methods
14
15
  end
15
16
  end
16
17
  end
@@ -0,0 +1,11 @@
1
+ class Object
2
+ def eigen
3
+ class << self
4
+ self
5
+ end
6
+ end
7
+
8
+ def eigen_eval(&block)
9
+ eigen.instance_eval(&block)
10
+ end
11
+ end
@@ -1,7 +1,11 @@
1
+ require 'orangutan/stub_include'
2
+
1
3
  module Orangutan
2
- class StubBase
4
+ class StubBase
3
5
  instance_methods.each { |m| undef_method m unless m =~ /^__/ }
4
6
 
7
+ include StubInclude
8
+
5
9
  def initialize name, parent, recursive=false
6
10
  @name, @parent, @recursive = name, parent, recursive
7
11
  end
@@ -13,22 +17,5 @@ module Orangutan
13
17
  end
14
18
  __return__(method, return_container)
15
19
  end
16
- private
17
- def __return__ method, return_container
18
- return *return_container.value if return_container
19
- return @parent.stub(:"#{@name}/#{method}", :recursive => true) if @recursive
20
- nil
21
- end
22
-
23
- def __react__ method, args
24
- yield_container, return_container = nil, nil
25
- @parent.calls << Call.new(@name, method, args)
26
- first_match = @parent.first_match(@name, method, args)
27
- if first_match
28
- first_match.raiser.execute if first_match.raiser
29
- yield_container, return_container = first_match.yield_container, first_match.return_container
30
- end
31
- return yield_container, return_container
32
- end
33
20
  end
34
21
  end
@@ -0,0 +1,20 @@
1
+ module Orangutan
2
+ module StubInclude
3
+ def __return__ method, return_container
4
+ return *return_container.value if return_container
5
+ return @parent.stub(:"#{@name}/#{method}", :recursive => true) if @recursive
6
+ nil
7
+ end
8
+
9
+ def __react__ method, args
10
+ yield_container, return_container = nil, nil
11
+ @parent.calls << Call.new(@name, method, args)
12
+ first_match = @parent.first_match(@name, method, args)
13
+ if first_match
14
+ first_match.raiser.execute if first_match.raiser
15
+ yield_container, return_container = first_match.yield_container, first_match.return_container
16
+ end
17
+ return yield_container, return_container
18
+ end
19
+ end
20
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orangutan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Ryall
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-23 00:00:00 +11:00
12
+ date: 2010-03-06 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -29,9 +29,11 @@ files:
29
29
  - lib/orangutan/container.rb
30
30
  - lib/orangutan/expectation.rb
31
31
  - lib/orangutan/mock_adapter.rb
32
+ - lib/orangutan/object_extensions.rb
32
33
  - lib/orangutan/raiser.rb
33
34
  - lib/orangutan/reflector.rb
34
35
  - lib/orangutan/stub_base.rb
36
+ - lib/orangutan/stub_include.rb
35
37
  - lib/orangutan.rb
36
38
  - README
37
39
  - MIT-LICENSE