Sha256: f87e064dddc44c6f57e9e319804dfe6b585bc1cef44c82a234c11e09ccf838b4

Contents?: true

Size: 1.37 KB

Versions: 46

Compression:

Stored size: 1.37 KB

Contents

class FakeLogger
  def initialize
    @entries = {
      :info  => [],
      :debug => [],
      :warn  => [],
      :error => [],
      :fatal => [],
    }
  end

  def info(message = nil, &block)
    log(:info, message, &block)
  end

  def debug(message = nil, &block)
    log(:debug, message, &block)
  end

  def warn(message = nil, &block)
    log(:warn, message, &block)
  end

  def error(message = nil, &block)
    log(:error, message, &block)
  end

  def fatal(message = nil, &block)
    log(:fatal, message, &block)
  end

  def log(severity, message = nil, &block)
    message ||= block.call
    @entries[severity] << message
  end

  def has_entry?(level, expected_entry)
    @entries[level].any? { |actual_entry| actual_entry.include?(expected_entry) }
  end

  attr_reader :entries
end

RSpec::Matchers.define :have_entry do |severity, entry|
  match do |logger|
    @logger = logger
    logger.has_entry?(severity, entry)
  end

  failure_message do
    "Expected #{severity}(#{entry.inspect}); got entries:\n\n#{entries}"
  end

  failure_message_when_negated do
    "Unexpected #{severity}(#{entry.inspect}); got entries:\n\n#{entries}"
  end

  def entries
    lines = @logger.entries.inject([]) do |result, (severity, entries)|
      if entries.empty?
        result
      else
        result << "#{severity}:\n#{entries.join("\n")}"
      end
    end
    lines.join("\n\n")
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
copy_tuner_client-0.19.0 spec/support/fake_logger.rb
copy_tuner_client-0.18.0 spec/support/fake_logger.rb
copy_tuner_client-0.17.1 spec/support/fake_logger.rb
copy_tuner_client-0.16.3 spec/support/fake_logger.rb
copy_tuner_client-0.16.2 spec/support/fake_logger.rb
copy_tuner_client-0.16.1 spec/support/fake_logger.rb
copy_tuner_client-0.16.0 spec/support/fake_logger.rb
copy_tuner_client-0.15.1 spec/support/fake_logger.rb
copy_tuner_client-0.15.0 spec/support/fake_logger.rb
copy_tuner_client-0.14.2 spec/support/fake_logger.rb
copy_tuner_client-0.14.1 spec/support/fake_logger.rb
copy_tuner_client-0.14.0 spec/support/fake_logger.rb
copy_tuner_client-0.13.6 spec/support/fake_logger.rb
copy_tuner_client-0.13.5 spec/support/fake_logger.rb
copy_tuner_client-0.13.3 spec/support/fake_logger.rb
copy_tuner_client-0.13.2 spec/support/fake_logger.rb
copy_tuner_client-0.13.1 spec/support/fake_logger.rb
copy_tuner_client-0.13.0 spec/support/fake_logger.rb
copy_tuner_client-0.12.0 spec/support/fake_logger.rb
copy_tuner_client-0.11.0 spec/support/fake_logger.rb