summaryrefslogtreecommitdiff
path: root/spec/mspec/lib/mspec/matchers/base.rb
blob: 94d3b71e55a5766b57a7192fb4bf382c811d2f3f (plain)
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
module MSpecMatchers
end

class MSpecEnv
  include MSpecMatchers
end

# Expectations are sometimes used in a module body
class Module
  include MSpecMatchers
end

class SpecPositiveOperatorMatcher < BasicObject
  def initialize(actual)
    @actual = actual
  end

  def ==(expected)
    method_missing(:==, expected)
  end

  def !=(expected)
    method_missing(:!=, expected)
  end

  def equal?(expected)
    method_missing(:equal?, expected)
  end

  def method_missing(name, *args, &block)
    result = @actual.__send__(name, *args, &block)
    unless result
      ::SpecExpectation.fail_predicate(@actual, name, args, block, result, "to be truthy")
    end
  end
end

class SpecNegativeOperatorMatcher < BasicObject
  def initialize(actual)
    @actual = actual
  end

  def ==(expected)
    method_missing(:==, expected)
  end

  def !=(expected)
    method_missing(:!=, expected)
  end

  def equal?(expected)
    method_missing(:equal?, expected)
  end

  def method_missing(name, *args, &block)
    result = @actual.__send__(name, *args, &block)
    if result
      ::SpecExpectation.fail_predicate(@actual, name, args, block, result, "to be falsy")
    end
  end
end