summaryrefslogtreecommitdiff
path: root/spec/mspec/lib/mspec/expectations/should.rb
blob: c1790e0ac82877058f736ef1d718050f5671c2fc (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
class Object
  NO_MATCHER_GIVEN = Object.new

  def should(matcher = NO_MATCHER_GIVEN, &block)
    MSpec.expectation
    state = MSpec.current.state
    raise "should outside example" unless state
    MSpec.actions :expectation, state

    if NO_MATCHER_GIVEN.equal?(matcher)
      SpecPositiveOperatorMatcher.new(self)
    else
      # The block was given to #should syntactically, but it was intended for a matcher like #raise_error
      matcher.block = block if block

      unless matcher.matches? self
        expected, actual = matcher.failure_message
        SpecExpectation.fail_with(expected, actual)
      end
    end
  end

  def should_not(matcher = NO_MATCHER_GIVEN, &block)
    MSpec.expectation
    state = MSpec.current.state
    raise "should_not outside example" unless state
    MSpec.actions :expectation, state

    if NO_MATCHER_GIVEN.equal?(matcher)
      SpecNegativeOperatorMatcher.new(self)
    else
      # The block was given to #should_not syntactically, but it was intended for the matcher
      matcher.block = block if block

      if matcher.matches? self
        expected, actual = matcher.negative_failure_message
        SpecExpectation.fail_with(expected, actual)
      end
    end
  end
end