Sha256: 569d840b096209fd12ec890abfd2c3d6d2c687cd041c30095c9b1fb35740b246

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require_relative 'base'

module Logux
  module Test
    module Matchers
      class SendToLogux < Base
        def matches?(actual)
          @difference = state_changes_inside { actual.call }
          return !@difference.empty? if expected.empty?

          expected.all? do |ex|
            @difference.find do |state|
              state['commands'].any? do |c|
                match_commands?(c, ex)
              end
            end
          end
        end

        def failure_message
          "expected that #{pretty(@difference)} to include "\
            "commands #{pretty(expected)}"
        end

        private

        def state_changes_inside
          before_state = Logux::Test::Store.instance.data.dup
          yield
          after_state = Logux::Test::Store.instance.data

          (after_state - before_state).map { |d| JSON.parse(d) }
        end

        def match_commands?(stored_command, expected_command)
          expected_command.each_with_index.all? do |part, index|
            part.stringify_keys! if part.is_a?(Hash)
            matcher = if part.is_a?(RSpec::Matchers::BuiltIn::BaseMatcher)
                        part
                      else
                        RSpec::Matchers::BuiltIn::Eq.new(part)
                      end
            matcher.matches?(stored_command[index])
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

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