lib/orangutan/chantek.rb in orangutan-0.0.1 vs lib/orangutan/chantek.rb in orangutan-0.0.2
- old
+ new
@@ -1,68 +1,50 @@
-require 'orangutan/clean_slate'
-require 'orangutan/expectation'
-require 'orangutan/call'
-
-class Orangutan::Chantek
- attr_reader :calls
-
- def initialize
- @calls = []
- @expectations = {}
- end
-
- def stub name, params={}
- c = Class.new(Orangutan::CleanSlate) do
- if params[:clr_interface]
- include params[:clr_interface]
- params[:clr_interface].to_clr_type.get_methods.each do |m_info|
- snake = m_info.name.scan(/[A-Z][a-z0-9]*/).map {|a|a.downcase}.join('_').to_sym
- define_method snake do |*args|
- yield_container, return_value, raiser = __react__ snake, args
- raiser.execute if raiser
- yield yield_container.value if yield_container && block_given?
- return_value
- end
- end
- end
-
- def initialize name, parent
- @name, @parent = name, parent
- end
-
- def method_missing method, *args
- yield_container, return_value, raiser = __react__ method, args
- raiser.execute if raiser
- yield yield_container.value if yield_container && block_given?
- return_value
- end
-
- private
-
- def __react__ method, args
- yield_container, return_value, raiser = nil, nil, nil
- @parent.calls << Orangutan::Call.new(@name, method, args)
- first_match = @parent.first_match(@name, method, args)
- return first_match.yield_container, first_match.return_value, first_match.raiser if first_match
- return yield_container, return_value, raiser
- end
- end
- c.new name, self
- end
-
- def when name
- expectations_for_name = @expectations[name]
- @expectations[name] = expectations_for_name = [] unless expectations_for_name
- expectation = Orangutan::Expectation.new
- expectations_for_name << expectation
- expectation
- end
-
- def first_match name, method, args
- expectations_for_name = @expectations[name]
- if expectations_for_name
- expectations_for_name.each do |expectation|
- return expectation if expectation.matches?(method, *args)
- end
- end
- end
+require 'orangutan/stub_base'
+require 'orangutan/expectation'
+require 'orangutan/call'
+
+module Orangutan
+ class Chantek
+ attr_reader :calls
+
+ def initialize
+ @calls = []
+ @expectations = {}
+ @stubs= {}
+ end
+
+ def stub name, params={}
+ return @stubs[name] if @stubs[name]
+ c = Class.new(StubBase) do
+ if params[:clr_interface]
+ include params[:clr_interface]
+ params[:clr_interface].to_clr_type.get_methods.each do |m_info|
+ snake = m_info.name.scan(/[A-Z][a-z0-9]*/).map {|a|a.downcase}.join('_').to_sym
+ define_method snake do |*args|
+ yield_container, return_container = __react__(snake, args)
+ yield yield_container.value if yield_container && block_given?
+ __return__(method, return_container)
+ end
+ end
+ end
+ end
+ @stubs[name] = c.new(name, self, params[:recursive])
+ end
+
+ def when name
+ expectations_for_name = @expectations[name]
+ @expectations[name] = expectations_for_name = [] unless expectations_for_name
+ expectation = Orangutan::Expectation.new
+ expectations_for_name << expectation
+ expectation
+ end
+
+ def first_match name, method, args
+ expectations_for_name = @expectations[name]
+ if expectations_for_name
+ expectations_for_name.each do |expectation|
+ return expectation if expectation.matches?(method, *args)
+ end
+ end
+ end
+ end
end
\ No newline at end of file