1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#!/usr/bin/env ruby
#---
# Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
# All rights reserved.
#
# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#+++
require 'flexmock/noop'
class FlexMock
####################################################################
# Match any object
class AnyMatcher
def ===(target)
true
end
def inspect
"ANY"
end
end
####################################################################
# Match only things that are equal.
class EqualMatcher
def initialize(obj)
@obj = obj
end
def ===(target)
@obj == target
end
def inspect
"==(#{@obj.inspect})"
end
end
ANY = AnyMatcher.new
####################################################################
# Match only things where the block evaluates to true.
class ProcMatcher
def initialize(&block)
@block = block
end
def ===(target)
@block.call(target)
end
def inspect
"on{...}"
end
end
####################################################################
# Match hashes that match all the fields of +hash+.
class HashMatcher
def initialize(hash)
@hash = hash
end
def ===(target)
@hash.all? { |k, v| target[k] == v }
end
def empty?
@hash.empty?
end
def inspect
"hsh(#{@hash.inspect})"
end
end
####################################################################
# Match objects that implement all the methods in +methods+.
class DuckMatcher
def initialize(methods)
@methods = methods
end
def ===(target)
@methods.all? { |m| target.respond_to?(m) }
end
def inspect
"ducktype(#{@methods.map{|m| m.inspect}.join(',')})"
end
end
end
|