File: spy_describers.rb

package info (click to toggle)
ruby-flexmock 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 840 kB
  • sloc: ruby: 7,598; makefile: 6
file content (85 lines) | stat: -rw-r--r-- 2,241 bytes parent folder | download | duplicates (2)
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
class FlexMock

    module SpyDescribers
      def spy_description(spy, sym, args, kw, options)
        result = "have received "
        result << FlexMock.format_call(sym, args, kw)
        result << times_description(options[:times])
        result << block_description(options[:with_block])
        result
      end

      def describe_spy_expectation(spy, sym, args, kw, options={})
        describe_spy(spy, sym, args, kw, options)
      end

      def describe_spy_negative_expectation(spy, sym, args, kw, options={})
        describe_spy(spy, sym, args, kw, options, " NOT")
      end

      def describe_spy(spy, sym, args, kw, options, not_clause="")
        result = "expected "
        result << FlexMock.format_call(sym, args, kw)
        result << " to#{not_clause} be received by " << spy.inspect
        result << times_description(options[:times])
        result << block_description(options[:with_block])
        result << ".\n"
        result << describe_calls(spy)
        result
      end

      def describe_calls(spy)
        result = ''
        if spy.flexmock_calls.empty?
          result << "No messages have been received\n"
        else
          result << "The following messages have been received:\n"
          spy.flexmock_calls.each do |call_record|
            append_call_record(result, call_record)
          end
        end
        result
      end

      def append_call_record(result, call_record)
        result <<
          "    " <<
          FlexMock.format_call(call_record.method_name, call_record.args, call_record.kw)
        if call_record.expectation
          result <<
            " matched by " <<
            call_record.expectation.description
        end
        result << "\n"
      end

      def times_description(times)
        case times
        when 0
          " never"
        when 1
          " once"
        when 2
          " twice"
        when nil
          ""
        else
          " #{times} times"
        end
      end

      def block_description(needs_block)
        case needs_block
        when true
          " with a block"
        when false
          " without a block"
        else
          ""
        end
      end

      extend SpyDescribers
    end

end