flexmock 1.3.2 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile.lock +9 -9
  3. data/Rakefile +3 -1
  4. data/lib/flexmock.rb +1 -1
  5. data/lib/flexmock/argument_matchers.rb +1 -1
  6. data/lib/flexmock/argument_types.rb +1 -2
  7. data/lib/flexmock/base.rb +2 -1
  8. data/lib/flexmock/call_record.rb +30 -0
  9. data/lib/flexmock/call_validator.rb +68 -0
  10. data/lib/flexmock/composite_expectation.rb +56 -0
  11. data/lib/flexmock/core.rb +30 -64
  12. data/lib/flexmock/core_class_methods.rb +1 -1
  13. data/lib/flexmock/default_framework_adapter.rb +1 -1
  14. data/lib/flexmock/deprecated_methods.rb +8 -3
  15. data/lib/flexmock/errors.rb +1 -1
  16. data/lib/flexmock/expectation.rb +7 -84
  17. data/lib/flexmock/expectation_builder.rb +133 -0
  18. data/lib/flexmock/expectation_director.rb +6 -4
  19. data/lib/flexmock/expectation_recorder.rb +42 -0
  20. data/lib/flexmock/extensions/active_record_model.rb +109 -0
  21. data/lib/flexmock/mock_builder.rb +139 -0
  22. data/lib/flexmock/mock_container.rb +13 -214
  23. data/lib/flexmock/noop.rb +1 -1
  24. data/lib/flexmock/ordering.rb +1 -2
  25. data/lib/flexmock/partial_mock.rb +95 -57
  26. data/lib/flexmock/rails.rb +1 -1
  27. data/lib/flexmock/recorder.rb +4 -3
  28. data/lib/flexmock/rspec.rb +1 -1
  29. data/lib/flexmock/rspec_spy_matcher.rb +4 -0
  30. data/lib/flexmock/spy_describers.rb +22 -5
  31. data/lib/flexmock/test_unit.rb +2 -40
  32. data/lib/flexmock/test_unit_integration.rb +4 -4
  33. data/lib/flexmock/test_unit_testcase_extensions.rb +49 -0
  34. data/lib/flexmock/undefined.rb +1 -1
  35. data/lib/flexmock/validators.rb +18 -12
  36. data/lib/flexmock/version.rb +1 -1
  37. data/test/based_partials_test.rb +1 -1
  38. data/test/container_methods_test.rb +1 -1
  39. data/test/default_framework_adapter_test.rb +1 -1
  40. data/test/demeter_mocking_test.rb +52 -2
  41. data/test/deprecated_methods_test.rb +1 -1
  42. data/test/examples_from_readme_test.rb +1 -1
  43. data/test/expectation_description_test.rb +1 -1
  44. data/test/extended_should_receive_test.rb +1 -1
  45. data/test/mock_builder_test.rb +68 -0
  46. data/test/naming_test.rb +1 -1
  47. data/test/new_instances_test.rb +1 -1
  48. data/test/partial_mock_test.rb +23 -6
  49. data/test/record_mode_test.rb +1 -1
  50. data/test/rspec_integration/integration_spec.rb +1 -1
  51. data/test/samples_test.rb +1 -2
  52. data/test/should_ignore_missing_test.rb +1 -1
  53. data/test/should_receive_test.rb +1 -1
  54. data/test/test_setup.rb +17 -0
  55. data/test/test_unit_integration/auto_test_unit_test.rb +1 -1
  56. data/test/tu_integration_test.rb +1 -1
  57. data/test/undefined_test.rb +1 -1
  58. metadata +52 -52
  59. data/TAGS +0 -1056
  60. data/lib/flexmock/composite.rb +0 -10
  61. data/lib/flexmock/rails/view_mocking.rb +0 -144
  62. data/test/rails_view_stub_test.rb +0 -145
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
  #
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -13,7 +13,6 @@ require 'flexmock/argument_types'
13
13
 
14
14
  class FlexMock
15
15
 
16
- ####################################################################
17
16
  # Translate arbitrary method calls into expectations on the given
18
17
  # mock object.
19
18
  #
@@ -58,7 +57,9 @@ class FlexMock
58
57
  # given arguments.
59
58
  def method_missing(sym, *args, &block)
60
59
  location = caller.first
61
- expectation = @mock.flexmock_define_expectation(location, sym).and_return(&block)
60
+ expectation = @mock.
61
+ flexmock_define_expectation(location, sym).
62
+ and_return(&block)
62
63
  if strict?
63
64
  args = args.collect { |arg| eq(arg) }
64
65
  expectation.with(*args).ordered.once
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -29,6 +29,10 @@ class FlexMock
29
29
  describe_spy_negative_expectation(@spy, @method_name, @args, @options)
30
30
  end
31
31
 
32
+ def description
33
+ spy_description(@spy, @method_name, @args, @options)
34
+ end
35
+
32
36
  def with(*args)
33
37
  @args = args
34
38
  self
@@ -1,6 +1,14 @@
1
1
  class FlexMock
2
2
 
3
3
  module SpyDescribers
4
+ def spy_description(spy, sym, args, options)
5
+ result = "have received "
6
+ result << call_description(sym, args)
7
+ result << times_description(options[:times])
8
+ result << block_description(options[:with_block])
9
+ result
10
+ end
11
+
4
12
  def describe_spy_expectation(spy, sym, args, options={})
5
13
  describe_spy(spy, sym, args, options)
6
14
  end
@@ -14,8 +22,7 @@ class FlexMock
14
22
  def describe_spy(spy, sym, args, options, not_clause="")
15
23
  result = "expected "
16
24
  result << call_description(sym, args)
17
- result << " to#{not_clause} be received by "
18
- result << spy.inspect
25
+ result << " to#{not_clause} be received by " << spy.inspect
19
26
  result << times_description(options[:times])
20
27
  result << block_description(options[:with_block])
21
28
  result << ".\n"
@@ -30,14 +37,24 @@ class FlexMock
30
37
  else
31
38
  result << "The following messages have been received:\n"
32
39
  spy.flexmock_calls.each do |call_record|
33
- result << " " << call_description(call_record.method_name, call_record.args)
34
- result << " matched by " << call_record.expectation.description if call_record.expectation
35
- result << "\n"
40
+ append_call_record(result, call_record)
36
41
  end
37
42
  end
38
43
  result
39
44
  end
40
45
 
46
+ def append_call_record(result, call_record)
47
+ result <<
48
+ " " <<
49
+ call_description(call_record.method_name, call_record.args)
50
+ if call_record.expectation
51
+ result <<
52
+ " matched by " <<
53
+ call_record.expectation.description
54
+ end
55
+ result << "\n"
56
+ end
57
+
41
58
  def times_description(times)
42
59
  case times
43
60
  when 0
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -10,42 +10,4 @@
10
10
  #+++
11
11
 
12
12
  require 'flexmock/test_unit_integration'
13
-
14
- class FlexMock
15
- module GenericTestCase
16
- def self.define_extensions_on(klass)
17
- klass.class_eval do
18
- include FlexMock::ArgumentTypes
19
- include FlexMock::MockContainer
20
-
21
- # Alias the original teardown behavior for later use.
22
- alias :flexmock_original_teardown :teardown
23
-
24
- # Teardown the test case, verifying any mocks that might have been
25
- # defined in this test case.
26
- def teardown
27
- flexmock_teardown
28
- flexmock_original_teardown
29
- end
30
- end
31
- end
32
- end
33
- end
34
-
35
- if defined?(MiniTest)
36
- module MiniTest
37
- class Unit
38
- class TestCase
39
- FlexMock::GenericTestCase.define_extensions_on(self)
40
- end
41
- end
42
- end
43
- else
44
- module Test
45
- module Unit
46
- class TestCase
47
- FlexMock::GenericTestCase.define_extensions_on(self)
48
- end
49
- end
50
- end
51
- end
13
+ require 'flexmock/test_unit_testcase_extensions'
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -15,7 +15,6 @@ require 'flexmock/test_unit_assert_spy_called'
15
15
 
16
16
  class FlexMock
17
17
 
18
- ####################################################################
19
18
  # Test::Unit::TestCase Integration.
20
19
  #
21
20
  # Include this module in any TestCase class in a Test::Unit test
@@ -41,7 +40,6 @@ class FlexMock
41
40
 
42
41
  end
43
42
 
44
- ####################################################################
45
43
  # Adapter for adapting FlexMock to the Test::Unit framework.
46
44
  #
47
45
  class TestUnitFrameworkAdapter
@@ -58,7 +56,9 @@ class FlexMock
58
56
  end
59
57
 
60
58
  def assertion_failed_error
61
- defined?(Test::Unit::AssertionFailedError) ? Test::Unit::AssertionFailedError : MiniTest::Assertion
59
+ defined?(Test::Unit::AssertionFailedError) ?
60
+ Test::Unit::AssertionFailedError :
61
+ MiniTest::Assertion
62
62
  end
63
63
  end
64
64
 
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #---
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
+ # All rights reserved.
6
+
7
+ # Permission is granted for use, copying, modification, distribution,
8
+ # and distribution of modified versions of this work as long as the
9
+ # above copyright notice is included.
10
+ #+++
11
+
12
+ class FlexMock
13
+ module GenericTestCase
14
+ def self.define_extensions_on(klass)
15
+ klass.class_eval do
16
+ include FlexMock::ArgumentTypes
17
+ include FlexMock::MockContainer
18
+
19
+ # Alias the original teardown behavior for later use.
20
+ alias :flexmock_original_teardown :teardown
21
+
22
+ # Teardown the test case, verifying any mocks that might have been
23
+ # defined in this test case.
24
+ def teardown
25
+ flexmock_teardown
26
+ flexmock_original_teardown
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ if defined?(MiniTest)
34
+ module MiniTest
35
+ class Unit
36
+ class TestCase
37
+ FlexMock::GenericTestCase.define_extensions_on(self)
38
+ end
39
+ end
40
+ end
41
+ else
42
+ module Test
43
+ module Unit
44
+ class TestCase
45
+ FlexMock::GenericTestCase.define_extensions_on(self)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -32,11 +32,6 @@ class FlexMock
32
32
  n < @limit
33
33
  end
34
34
 
35
- # Pluralize "call"
36
- def calls(n)
37
- n == 1 ? "call" : "calls"
38
- end
39
-
40
35
  # Human readable description of the validator
41
36
  def describe
42
37
  case @limit
@@ -58,14 +53,25 @@ class FlexMock
58
53
  def validate_count(n, &block)
59
54
  @exp.flexmock_location_filter do
60
55
  FlexMock.framework_adapter.make_assertion(
61
- lambda {
62
- "Method '#{@exp}' called incorrect number of times\n" +
63
- "#{describe_limit} matching #{calls(@limit)} expected\n" +
64
- "#{n} matching #{calls(n)} found\n" +
65
- describe_calls(@exp.mock)
66
- }, &block)
56
+ lambda { construct_validation_count_error_message(n) },
57
+ &block)
67
58
  end
68
59
  end
60
+
61
+ private
62
+
63
+ # Build the error message for an invalid count
64
+ def construct_validation_count_error_message(n)
65
+ "Method '#{@exp}' called incorrect number of times\n" +
66
+ "#{describe_limit} matching #{calls(@limit)} expected\n" +
67
+ "#{n} matching #{calls(n)} found\n" +
68
+ describe_calls(@exp.mock)
69
+ end
70
+
71
+ # Pluralize "call"
72
+ def calls(n)
73
+ n == 1 ? "call" : "calls"
74
+ end
69
75
  end
70
76
 
71
77
  ####################################################################
@@ -3,7 +3,7 @@ class FlexMock
3
3
  NUMBERS = [
4
4
  MAJOR = 1,
5
5
  MINOR = 3,
6
- BUILD = 2,
6
+ BUILD = 3,
7
7
  ]
8
8
  end
9
9
 
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -5,7 +5,7 @@ require 'test/test_setup'
5
5
  class TestDemeterMocking < Test::Unit::TestCase
6
6
  include FlexMock::TestCase
7
7
 
8
- def test_demeter_mocking
8
+ def test_demeter_mocking_basics
9
9
  m = flexmock("A")
10
10
  m.should_receive("children.first").and_return(:first)
11
11
  assert_kind_of FlexMock, m
@@ -36,12 +36,24 @@ class TestDemeterMocking < Test::Unit::TestCase
36
36
  assert_equal :last, m.child.x.y.z.last
37
37
  end
38
38
 
39
- def test_multi_level_deep_demeter_violation
39
+ def test_multi_level_deep_demeter_violation_with_mock
40
40
  a = flexmock("a")
41
41
  a.should_receive("b.c.d.e.f.g.h.i.j.k").and_return(:xyzzy)
42
42
  assert_equal :xyzzy, a.b.c.d.e.f.g.h.i.j.k
43
43
  end
44
44
 
45
+ def test_partial_with_demeter
46
+ a = flexmock(Object.new, "a partial")
47
+ a.should_receive("b.c").and_return(:xyzzy)
48
+ assert_equal :xyzzy, a.b.c
49
+ end
50
+
51
+ def test_multi_level_deep_demeter_violation_with_partial
52
+ a = flexmock(Object.new, "a")
53
+ a.should_receive("b.c.d.e.f.g.h.i.j.k").and_return(:xyzzy)
54
+ assert_equal :xyzzy, a.b.c.d.e.f.g.h.i.j.k
55
+ end
56
+
45
57
  def test_final_method_can_have_multiple_expecations
46
58
  a = flexmock("a")
47
59
  a.should_receive("b.c.d.last").with(1).and_return(:one).once
@@ -62,6 +74,44 @@ class TestDemeterMocking < Test::Unit::TestCase
62
74
  assert_match(/child/i, ex.message)
63
75
  end
64
76
 
77
+ def test_compatible_mock_declarations_are_ok_full_mock_version
78
+ m = flexmock("A")
79
+ b = flexmock("B")
80
+ c = flexmock("C")
81
+ m.should_receive(:b => b)
82
+ b.should_receive(:c => c)
83
+ c.should_receive(:foo => :bar)
84
+ m.should_receive("b.c.baz").and_return(:barg)
85
+ m.should_receive("b.zhar").and_return(:zzz)
86
+
87
+ assert_equal :bar, m.b.c.foo
88
+ assert_equal :barg, m.b.c.baz
89
+ assert_equal :zzz, m.b.zhar
90
+ end
91
+
92
+ def test_compatible_mock_declarations_are_ok_partial_mock_version
93
+ m = flexmock("A")
94
+ b = flexmock(Object.new, "B")
95
+ c = flexmock("C")
96
+ m.should_receive(:b => b)
97
+ b.should_receive(:c => c)
98
+ c.should_receive(:foo => :bar)
99
+ m.should_receive("b.c.baz").and_return(:barg)
100
+ m.should_receive("b.zhar").and_return(:zzz)
101
+
102
+ assert_equal :bar, m.b.c.foo
103
+ assert_equal :barg, m.b.c.baz
104
+ assert_equal :zzz, m.b.zhar
105
+ end
106
+
107
+ def test_paths
108
+ m = flexmock("A")
109
+ b = flexmock("B")
110
+ m.should_receive("a.b" => b)
111
+ m.should_receive("a.b.c.x" => 1)
112
+ m.should_receive("a.b.c.y" => 2)
113
+ end
114
+
65
115
  def test_conflicting_mock_declarations_in_reverse_order_does_not_raise_error
66
116
  # Not all conflicting definitions can be detected.
67
117
  m = flexmock("A")
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,