File: recorder.rb

package info (click to toggle)
ruby-flexmock 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: ruby: 7,572; makefile: 6
file content (72 lines) | stat: -rw-r--r-- 2,061 bytes parent folder | download | duplicates (4)
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
#!/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/argument_types'

class FlexMock

  # Translate arbitrary method calls into expectations on the given
  # mock object.
  #
  class Recorder
    include FlexMock::ArgumentTypes

    # Create a method recorder for the mock +mock+.
    def initialize(mock)
      @mock = mock
      @strict = false
    end

    # Place the record in strict mode.  While recording expectations
    # in strict mode, the following will be true.
    #
    # * All expectations will be expected in the order they were
    #   recorded.
    # * All expectations will be expected once.
    # * All arguments will be placed in exact match mode,
    #   including regular expressions and class objects.
    #
    # Strict mode is usually used when giving the recorder to a known
    # good algorithm.  Strict mode captures the exact sequence of
    # calls and validate that the code under test performs the exact
    # same sequence of calls.
    #
    # The recorder may exit strict mode via a
    # <tt>should_be_strict(false)</tt> call.  Non-strict expectations
    # may be recorded at that point, or even explicit expectations
    # (using +should_receieve+) can be specified.
    #
    def should_be_strict(is_strict=true)
      @strict = is_strict
    end

    # Is the recorder in strict mode?
    def strict?
      @strict
    end

    # Record an expectation for receiving the method +sym+ with the
    # given arguments.
    def method_missing(sym, *args, &block)
      expectation = @mock.
        flexmock_define_expectation(caller, sym).
        and_return(&block)
      if strict?
        args = args.collect { |arg| eq(arg) }
        expectation.with(*args).ordered.once
      else
        expectation.with(*args)
      end
      expectation
    end
  end

end