Sha256: d6fb2e77fd4016327ee959edc5c43b39632fbfd09ac45351b48e114a637baa04

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

module Muack
  class Failure < Exception
    attr_reader :expected
    def build_expected obj, expected_defis
      @expected = expected_defis.uniq{ |d| [d.msg, d.args] }.map{ |defi|
                    "#{obj.inspect}.#{defi.msg}(" \
                    "#{defi.args.map(&:inspect).join(', ')})"
                  }.join("\n      or: ")
    end
  end

  class Unexpected < Failure
    attr_reader :was
    def initialize obj, expected_defis, msg, args
      @was = "#{obj.inspect}.#{msg}(" \
             "#{args.map(&:inspect).join(', ')})"
      if expected_defis.empty?
        super("\nUnexpected call: #{@was}")
      else
        build_expected(obj, expected_defis)
        super("\nExpected: #{expected}\n but was: #{was}")
      end
    end
  end

  class Expected < Failure
    attr_reader :expected_times, :actual_times
    def initialize obj, defi, expected_times, actual_times
      build_expected(obj, [defi])
      @expected_times = expected_times
      @actual_times   = actual_times

      super("\nExpected: #{expected}\n  called #{expected_times} times\n" \
            " but was #{actual_times} times.")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
muack-1.1.1 lib/muack/failure.rb
muack-1.1.0 lib/muack/failure.rb