###
# record.should_not have_log_entries
# record.should have_log_entries /blablabla/
# record.should have_log_entries "blablabla"
###
RSpec::Matchers.define :have_log_entries do |expected|

  match do |record|
    if expected.present?
      record.log_entries.select{|log_entry| log_entry.body.to_s[expected]}.any?
    else
      record.log_entries.any?
    end
  end

  failure_message_when_negated do |record|
    log_entry_separator = "\n\n-------------------------------------------------------------------------\n\n"
    entry_messages = record.log_entries.map{|l| "#{l.body}\n#{l.backtrace.to_s.split("\n")[0..10].join("\n")}"}
    message = <<-ERROR_MESSAGE
      No log entries were expected for the record, but #{record.log_entries.size} found"
      #{entry_messages.join(log_entry_separator)}
    ERROR_MESSAGE
    message
  end

  failure_message do |record|
    "Expected to find log entry #{expected} in:\n\n#{record.log_entries.map(&:body).join("\n")}"
  end

  def supports_block_expectations?
    false
  end

end
RSpec::Matchers.alias_matcher :have_log_entry, :have_log_entries
