Sha256: d33889ce6bc15a624507e5f88f9bdd16f13fba583d4dd7162308c9d5f27f889b

Contents?: true

Size: 1.99 KB

Versions: 10

Compression:

Stored size: 1.99 KB

Contents

require 'ostruct'
require 'deas/runner'

module Deas

  class TestRunner < Runner

    attr_reader :handler, :return_value

    def initialize(handler_class, args = nil)
      args = (args || {}).dup
      @app_settings = OpenStruct.new(args.delete(:app_settings))
      @logger       = args.delete(:logger) || Deas::NullLogger.new
      @params       = args.delete(:params) || {}
      @request      = args.delete(:request)
      @response     = args.delete(:response)
      @session      = args.delete(:session)

      super(handler_class)
      args.each{|key, value| @handler.send("#{key}=", value) }

      @return_value = catch(:halt){ @handler.init; nil }
    end

    def run
      @return_value ||= catch(:halt){ @handler.run }
    end

    # Helpers

    def halt(*args)
      throw(:halt, HaltArgs.new(args))
    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
    end

    def redirect(path, *halt_args)
      RedirectArgs.new(path, halt_args)
    end

    class RedirectArgs < Struct.new(:path, :halt_args)
      def redirect?; true; end
    end

    def content_type(value, opts={})
      ContentTypeArgs.new(value, opts)
    end

    ContentTypeArgs = Struct.new(:value, :opts)

    def status(value)
      StatusArgs.new(value)
    end

    StatusArgs = Struct.new(:value)

    def headers(value)
      HeadersArgs.new(value)
    end

    HeadersArgs = Struct.new(:value)

    def render(template_name, options = nil, &block)
      RenderArgs.new(template_name, options, block)
    end

    RenderArgs = Struct.new(:template_name, :options, :block)

    def send_file(file_path, options = nil, &block)
      SendFileArgs.new(file_path, options, block)
    end

    SendFileArgs = Struct.new(:file_path, :options, :block)

  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
deas-0.23.1 lib/deas/test_runner.rb
deas-0.23.0 lib/deas/test_runner.rb
deas-0.22.1 lib/deas/test_runner.rb
deas-0.22.0 lib/deas/test_runner.rb
deas-0.21.0 lib/deas/test_runner.rb
deas-0.20.0 lib/deas/test_runner.rb
deas-0.19.0 lib/deas/test_runner.rb
deas-0.18.0 lib/deas/test_runner.rb
deas-0.17.1 lib/deas/test_runner.rb
deas-0.17.0 lib/deas/test_runner.rb