Sha256: 17ea97414ec789a3851a6f803cd74159691fc699ae51c62c75f8e217e348e346

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require_relative 'helpers/format_helper'
require_relative 'helpers/resolve_account_helper'
require_relative 'helpers/resolve_amount_helper'

RSpec::Matchers.define :credit_account do |account, owner: nil|
  include Generalis::RSpec::FormatHelper
  include Generalis::RSpec::ResolveAccountHelper
  include Generalis::RSpec::ResolveAmountHelper

  match do |transaction|
    transaction.prepare

    @account = resolve_account(account, owner: owner)
    entries = transaction.entries.select { |entry| entry.credit? && entry.account == @account }

    entries.any? && matches_amount?(entries)
  end

  chain(:with_amount) do |amount, currency = nil|
    @amount = resolve_amount(amount, currency)
  end

  failure_message do |transaction|
    message  = "expected transaction to credit account #{format_account(@account)}"
    message += " with amount #{format_money(@amount)}" if @amount
    message += "\n"
    message += "\nCredits:\n"
    message += transaction.entries.select(&:credit?).map { |entry| format_entry(entry) }.join("\n")
    message += "\nDebits:\n"
    message += transaction.entries.select(&:debit?).map { |entry| format_entry(entry) }.join("\n")
    message
  end

  # @param entries [Array<Generalis::Entry>]
  # @return [Boolean]
  def matches_amount?(entries)
    return true if @amount.nil?

    entries
      .select { |entry| entry.currency.casecmp(@amount.currency).zero? }
      .sum(&:amount) == @amount
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
generalis-0.1.0 lib/rspec/credit_account_matcher.rb