File: deprecated_methods_test.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 (225 lines) | stat: -rw-r--r-- 5,298 bytes parent folder | download
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/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 'test_helper'
require 'flexmock/deprecated_methods'

class TestFlexMock < Minitest::Test
  include FlexMock::Minitest
  include FlexMock::RedirectError

  def s(&block)
    redirect_error(&block)
  end

  def setup
    @mock = flexmock('mock')
  end

  def test_handle
    args = nil
    s { @mock.mock_handle(:hi) { |a, b| args = [a,b] } }
    @mock.hi(1,2)
    assert_equal [1,2], args
  end

  def test_handle_no_block
    s { @mock.mock_handle(:blip) }
    @mock.blip
    assert true, "just checking for failures"
  end

  def test_called_with_block
    called = false
    s { @mock.mock_handle(:blip) { |&block| block.call } }
    @mock.blip { called = true }
    assert called, "Block to blip should be called"
  end

  def test_return_value
    s { @mock.mock_handle(:blip) { 10 } }
    assert_equal 10, @mock.blip
  end

  def test_handle_missing_method
    expected_error = (RUBY_VERSION >= "1.8.0") ? NoMethodError : NameError
    ex = assert_raises(expected_error) {
      @mock.not_defined
    }
    assert_match(/not_defined/, ex.message)
  end

  def test_ignore_missing_method
    @mock.mock_ignore_missing
    @mock.blip
    assert true, "just checking for failures"
  end

  def test_good_counts
    s { @mock.mock_handle(:blip, 3) }
    @mock.blip
    @mock.blip
    @mock.blip
    @mock.flexmock_verify
  end

  def test_bad_counts
    s { @mock.mock_handle(:blip, 3) }
    @mock.blip
    @mock.blip
    begin
      @mock.flexmock_verify
    rescue assertion_failed_error => err
    end
    refute_nil err
  end

  def test_undetermined_counts
    FlexMock.use('fs') { |m|
      s { m.mock_handle(:blip) }
      m.blip
      m.blip
      m.blip
    }
  end

  def test_zero_counts
    assert_raises(check_failed_error) do
      FlexMock.use { |m|
        s { m.mock_handle(:blip, 0) }
        m.blip
      }
    end
  end

  def test_file_io_with_use
    FlexMock.use do |m|
      filedata = ["line 1", "line 2"]
      s { m.mock_handle(:gets, 3) { filedata.shift } }
      assert_equal 2, count_lines(m)
    end
  end

  def count_lines(stream)
    result = 0
    while stream.gets
      result += 1
    end
    result
  end

  def test_use
    assert_raises(assertion_failed_error) {
      FlexMock.use do |m|
	s { m.mock_handle(:blip, 2) }
	m.blip
      end
    }
  end

  def test_failures_during_use
    ex = assert_raises(NameError) {
      FlexMock.use do |m|
	s { m.mock_handle(:blip, 2) }
	xyz
      end
    }
    assert_match(/undefined local variable or method/, ex.message)
  end

  def test_sequential_values
    values = [1,4,9,16]
    s { @mock.mock_handle(:get) { values.shift } }
    assert_equal 1, @mock.get
    assert_equal 4, @mock.get
    assert_equal 9, @mock.get
    assert_equal 16, @mock.get
  end

  def test_respond_to_returns_false_for_non_handled_methods
    assert(!@mock.respond_to?(:blah), "should not respond to blah")
  end

  def test_respond_to_returns_true_for_explicit_methods
    s { @mock.mock_handle(:xyz) }
    assert(@mock.respond_to?(:xyz), "should respond to test")
  end

  def test_respond_to_returns_true_for_missing_methods_when_ignoring_missing
    @mock.mock_ignore_missing
    assert(@mock.respond_to?(:yada), "should respond to yada now")
  end

  def test_respond_to_returns_true_for_missing_methods_when_ignoring_missing_using_should
    @mock.should_ignore_missing
    assert(@mock.respond_to?(:yada), "should respond to yada now")
  end

  def test_method_proc_raises_error_on_unknown
    assert_raises(NameError) {
      @mock.method(:xyzzy)
    }
  end

  def test_method_returns_callable_proc
    got_it = false
    s { @mock.mock_handle(:xyzzy) { got_it = true } }
    method_proc = @mock.method(:xyzzy)
    refute_nil method_proc
    method_proc.call([])
    assert(got_it, "method proc should run")
  end

  def test_method_returns_do_nothing_proc_for_missing_methods
    @mock.mock_ignore_missing
    method_proc = @mock.method(:plugh)
    refute_nil method_proc
    assert_equal FlexMock.undefined, method_proc.call
  end
end

class TestDeprecatedOrderingMethods < Minitest::Test
  include FlexMock::Minitest
  include FlexMock::RedirectError

  def test_deprecated_ordering_methods
    flexmock(:x).should_receive(:msg).globally.ordered(:testgroup)
    assert_equal({ :testgroup => 1 }, flexmock_groups)
    message = redirect_error do
      assert_equal({ :testgroup => 1 }, mock_groups)
    end
    assert_match(/deprecated/i, message)
    assert_match(/\bmock_groups/, message)
    assert_match(/\bflexmock_groups/, message)
  end
end

class TestAnyInstance < Minitest::Test
  include FlexMock::Minitest
  include FlexMock::RedirectError

  class Dog
    def bark
      :woof
    end
  end

  def test_any_instance_still_works_for_backwards_compatibility
    message = redirect_error do
      flexstub(Dog).any_instance do |obj|
        obj.should_receive(:bark).and_return(:whimper)
        assert_match(/deprecated/, message)
      end
    end
    m = Dog.new
    assert_equal :whimper,  m.bark
  end
end