Sha256: b8243d24415accc038e6becd6d96b78450094b5209f045f974a49c16107db3b2

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require_relative 'base'

module Logux
  module Test
    module Matchers
      class ResponseChunks < Base
        attr_reader :includes, :excludes, :meta

        def initialize(meta:, includes:, excludes: [])
          @meta = meta
          @includes = includes
          @excludes = excludes
        end

        def matches?(actual)
          @actual = JSON.parse(actual.body)

          match_includes? && match_excludes?
        end

        def failure_message
          data = "expected that #{pretty(@actual)} to has " \
            "#{includes.join(', ')} chunks"
          !excludes.empty? && data += " and doesn't" \
            " has #{excludes.join(', ')} chunks"
          data
        end

        private

        def match_includes?
          @actual.any? do |command|
            command.first.in?(includes) &&
              (meta.nil? || (meta.present? && command[1] == meta))
          end
        end

        def match_excludes?
          @actual.empty? || @actual.none? do |command|
            command.first.in?(excludes)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logux_rails-0.1.0 lib/logux/test/matchers/response_chunks.rb