lib/deas/test_runner.rb in deas-0.38.0 vs lib/deas/test_runner.rb in deas-0.39.0

- old
+ new

@@ -3,103 +3,102 @@ require 'deas/runner' require 'deas/view_handler' module Deas - InvalidServiceHandlerError = Class.new(StandardError) + InvalidViewHandlerError = Class.new(StandardError) class TestRunner < Runner - attr_reader :response_value + attr_reader :content_type_args def initialize(handler_class, args = nil) if !handler_class.include?(Deas::ViewHandler) - raise InvalidServiceHandlerError, "#{handler_class.inspect} is not a"\ - " Deas::ServiceHandler" + raise InvalidViewHandlerError, "#{handler_class.inspect} is not a " \ + "Deas::ViewHandler" end - args = (args || {}).dup + @run_return_value = nil + @content_type_args = nil + @halted = false + + a = (args || {}).dup super(handler_class, { - :request => args.delete(:request), - :response => args.delete(:response), - :session => args.delete(:session), - :params => NormalizedParams.new(args.delete(:params) || {}).value, - :logger => args.delete(:logger), - :router => args.delete(:router), - :template_source => args.delete(:template_source) + :logger => a.delete(:logger), + :router => a.delete(:router), + :template_source => a.delete(:template_source), + :request => a.delete(:request), + :session => a.delete(:session), + :params => NormalizedParams.new(a.delete(:params) || {}).value }) - args.each{|key, value| self.handler.send("#{key}=", value) } + a.each{|key, value| self.handler.send("#{key}=", value) } - @response_value = catch(:halt){ self.handler.init; nil } + catch(:halt){ self.handler.deas_init } end + def halted?; @halted; end + def run - @response_value ||= catch(:halt){ self.handler.run } + catch(:halt){ self.handler.deas_run } if !self.halted? + @run_return_value end - # Helpers + # helpers - def halt(*args) - throw(:halt, HaltArgs.new(args)) + def content_type(extname, params = nil) + @content_type_args = ContentTypeArgs.new(extname, params) + super end - class HaltArgs < Struct.new(:body, :headers, :status) - def initialize(args) - super(*[ - !args.last.kind_of?(::Hash) && !args.last.kind_of?(::Integer) ? args.pop : nil, - args.last.kind_of?(::Hash) ? args.pop : nil, - args.first.kind_of?(::Integer) ? args.first : nil - ]) - end + def halt(*args) + @halted = true + @run_return_value ||= HaltArgs.new(args) + super end - def redirect(path, *halt_args) - throw(:halt, RedirectArgs.new(path, halt_args)) + def redirect(location, *halt_args) + @run_return_value ||= RedirectArgs.new(location, HaltArgs.new(halt_args)) + super end - class RedirectArgs < Struct.new(:path, :halt_args) - def redirect?; true; end + def send_file(file_path, opts = nil) + @run_return_value ||= SendFileArgs.new(file_path, opts) + super end - def content_type(*args) - return @content_type if args.empty? - opts, value = [ - args.last.kind_of?(Hash) ? args.pop : {}, - args.last - ] - @content_type = ContentTypeArgs.new(value, opts) + def source_render(source, template_name, locals = nil) + @run_return_value ||= RenderArgs.new(source, template_name, locals) + super end - ContentTypeArgs = Struct.new(:value, :opts) - def status(*args) - return @status if args.empty? - value = args.last - @status = StatusArgs.new(value) + def source_partial(source, template_name, locals = nil) + # partials don't interact with the response body so they shouldn't affect + # the run return value (like renders do). Render the markup and discard + # it to test the template. Return the render args so you can test the + # expected partials were rendered. + super + RenderArgs.new(source, template_name, locals) end - StatusArgs = Struct.new(:value) - def headers(*args) - return @headers if args.empty? - value = args.last - @headers = HeadersArgs.new(value) - end - HeadersArgs = Struct.new(:value) + ContentTypeArgs = Struct.new(:extname, :params) - def send_file(file_path, options = nil, &block) - SendFileArgs.new(file_path, options, block) + class HaltArgs < Struct.new(:status, :headers, :body) + def initialize(args) + a = args.dup + super(*[ + a.first.instance_of?(::Fixnum) ? a.shift : nil, + a.first.kind_of?(::Hash) ? a.shift : nil, + a.first.respond_to?(:each) ? a.shift : nil + ]) + end end - SendFileArgs = Struct.new(:file_path, :options, :block) - def source_render(source, template_name, locals = nil) - super # render the markup and discard it - RenderArgs.new(source, template_name, locals) + class RedirectArgs < Struct.new(:location, :halt_args) + def redirect?; true; end end - RenderArgs = Struct.new(:source, :template_name, :locals) - def source_partial(source, template_name, locals = nil) - super # render the markup and discard it - RenderArgs.new(source, template_name, locals) - end + SendFileArgs = Struct.new(:file_path, :opts) + RenderArgs = Struct.new(:source, :template_name, :locals) class NormalizedParams < Deas::Runner::NormalizedParams def file_type?(value) value.kind_of?(::Tempfile) || value.kind_of?(::File) ||