Sha256: e0ceeb7c23433041c1a706e9dc73256c292ba7d4649b63a872d6e19597ee94e8

Contents?: true

Size: 1.75 KB

Versions: 14

Compression:

Stored size: 1.75 KB

Contents

# -*- encoding: utf-8 -*-
module RSpec
  module Mocks
    module ArgumentMatchers
      def an_onstomp_frame(command=false, header_arr=false, body=false)
        OnStompFrameMatcher.new(command, header_arr, body).tap do |m|
          m.match_command = command != false
          m.match_headers = header_arr != false
          m.match_body = body != false
        end
      end
      
      class OnStompFrameMatcher
        attr_accessor :match_command, :match_body, :match_headers
        
        def initialize(com, header_arr, body)
          @match_command = @match_body = @match_headers = true
          @expected = OnStomp::Components::Frame.new(com, {}, body)
          header_arr = [header_arr] if header_arr.is_a?(Hash)
          header_arr.each do |h|
            @expected.headers.merge!(h)
          end if header_arr.is_a?(Array)
        end
        
        def ==(actual)
          actual.is_a?(@expected.class) && matches_command(actual) &&
            matches_headers(actual) && matches_body(actual)
        end
        
        def matches_command actual
          !match_command || actual.command == @expected.command
        end
        
        def matches_body actual
          !match_body || actual.body == @expected.body
        end
        
        def matches_headers actual
          !match_headers || @expected.headers.to_hash.keys.all? { |k| @expected[k] == actual[k] }
        end
        
        def description
          frame_desc = match_command ? "#{@expected.command} frame" : "Any frame"
          header_desc = match_headers ? " with headers #{@expected.headers.to_hash.inspect}" : ''
          body_desc = match_body ? " and body '#{@expected.body}'" : ''
          [frame_desc, header_desc, body_desc].join
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
onstomp-1.0.12 spec/support/custom_argument_matchers.rb
onstomp-1.0.11 spec/support/custom_argument_matchers.rb
onstomp-1.0.10 spec/support/custom_argument_matchers.rb
onstomp-1.0.9 spec/support/custom_argument_matchers.rb
onstomp-1.0.8 spec/support/custom_argument_matchers.rb
onstomp-1.0.7 spec/support/custom_argument_matchers.rb
onstomp-1.0.6 spec/support/custom_argument_matchers.rb
onstomp-1.0.5 spec/support/custom_argument_matchers.rb
onstomp-1.0.4 spec/support/custom_argument_matchers.rb
onstomp-1.0.3 spec/support/custom_argument_matchers.rb
onstomp-1.0.2 spec/support/custom_argument_matchers.rb
onstomp-1.0.1 spec/support/custom_argument_matchers.rb
onstomp-1.0.0 spec/support/custom_argument_matchers.rb
onstomp-1.0.0pre1 spec/support/custom_argument_matchers.rb