Sha256: b73e8d97743c3d4c8b61fff879774c78527d11b76c519a22d85369025c027cff

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

require 'sanford-protocol'

require 'sanford/runner'

module Sanford

  class TestRunner
    include Sanford::Runner

    attr_reader :handler, :response

    def init!
      if !@request.kind_of?(Sanford::Protocol::Request)
        @request = test_request(@request)
      end
      @response = build_response catch(:halt){ @handler.init; nil }
    end

    # we override the `run` method because the TestRunner wants to control
    # storing any generated response. If `init` generated a response, we don't
    # want to `run` at all.

    def run
      @response ||= build_response catch_halt{ @handler.run }
    end

    protected

    def test_request(params)
      Sanford::Protocol::Request.new('version', 'name', params)
    end

    def build_response(response_args)
      Sanford::Protocol::Response.new(response_args.status, response_args.data) if response_args
    end

    module Helpers
      module_function

      def test_runner(handler_class, params = nil, logger = nil)
        TestRunner.new(handler_class, params || {}, logger)
      end

    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sanford-0.7.0 lib/sanford/test_runner.rb
sanford-0.6.5 lib/sanford/test_runner.rb
sanford-0.6.4 lib/sanford/test_runner.rb