Sha256: b40325e961bde8cb904890815c2dac073e5eb45ab97b08cd93255367162a2cba

Contents?: true

Size: 1.39 KB

Versions: 54

Compression:

Stored size: 1.39 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_for_should do
    "Expected #{severity}(#{entry.inspect}); got entries:\n\n#{entries}"
  end

  failure_message_for_should_not 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

54 entries across 54 versions & 2 rubygems

Version Path
copy_tuner_client-0.3.4 spec/support/fake_logger.rb
copy_tuner_client-0.3.3 spec/support/fake_logger.rb
copy_tuner_client-0.3.2 spec/support/fake_logger.rb
copy_tuner_client-0.3.1 spec/support/fake_logger.rb
copy_tuner_client-0.3.0 spec/support/fake_logger.rb
copy_tuner_client-0.2.4 spec/support/fake_logger.rb
copy_tuner_client-0.2.3 spec/support/fake_logger.rb
copy_tuner_client-0.2.2 spec/support/fake_logger.rb
copy_tuner_client-0.2.1 spec/support/fake_logger.rb
copy_tuner_client-0.2.0 spec/support/fake_logger.rb
copy_tuner_client-0.1.1.beta19 spec/support/fake_logger.rb
copy_tuner_client-0.1.1.beta18 spec/support/fake_logger.rb
copy_tuner_client-0.1.1.beta17 spec/support/fake_logger.rb
copy_tuner_client-0.1.1.beta16 spec/support/fake_logger.rb
copy_tuner_client-0.1.1.beta15 spec/support/fake_logger.rb
copy_tuner_client-0.1.1.beta14 spec/support/fake_logger.rb
copy_tuner_client-0.1.1.beta13 spec/support/fake_logger.rb
copy_tuner_client-0.1.1.beta12 spec/support/fake_logger.rb
copy_tuner_client-0.1.1.beta11 spec/support/fake_logger.rb
copy_tuner_client-0.1.1.beta10 spec/support/fake_logger.rb