rspec-expectations 2.13.0 → 3.0.0

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 (142) hide show
  1. checksums.yaml +15 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/Changelog.md +413 -23
  4. data/License.txt +1 -0
  5. data/README.md +109 -80
  6. data/lib/rspec/{matchers → expectations}/configuration.rb +44 -14
  7. data/lib/rspec/expectations/expectation_target.rb +71 -38
  8. data/lib/rspec/expectations/fail_with.rb +12 -44
  9. data/lib/rspec/expectations/handler.rb +135 -35
  10. data/lib/rspec/expectations/minitest_integration.rb +16 -0
  11. data/lib/rspec/expectations/syntax.rb +60 -94
  12. data/lib/rspec/expectations/version.rb +1 -1
  13. data/lib/rspec/expectations.rb +46 -24
  14. data/lib/rspec/matchers/aliased_matcher.rb +41 -0
  15. data/lib/rspec/matchers/built_in/all.rb +78 -0
  16. data/lib/rspec/matchers/built_in/base_matcher.rb +63 -15
  17. data/lib/rspec/matchers/built_in/be.rb +170 -79
  18. data/lib/rspec/matchers/built_in/be_between.rb +77 -0
  19. data/lib/rspec/matchers/built_in/be_instance_of.rb +11 -3
  20. data/lib/rspec/matchers/built_in/be_kind_of.rb +5 -0
  21. data/lib/rspec/matchers/built_in/be_within.rb +40 -16
  22. data/lib/rspec/matchers/built_in/change.rb +275 -74
  23. data/lib/rspec/matchers/built_in/compound.rb +122 -0
  24. data/lib/rspec/matchers/built_in/contain_exactly.rb +256 -0
  25. data/lib/rspec/matchers/built_in/cover.rb +3 -0
  26. data/lib/rspec/matchers/built_in/eq.rb +60 -6
  27. data/lib/rspec/matchers/built_in/eql.rb +17 -5
  28. data/lib/rspec/matchers/built_in/equal.rb +55 -21
  29. data/lib/rspec/matchers/built_in/exist.rb +74 -10
  30. data/lib/rspec/matchers/built_in/has.rb +71 -15
  31. data/lib/rspec/matchers/built_in/include.rb +58 -29
  32. data/lib/rspec/matchers/built_in/match.rb +19 -1
  33. data/lib/rspec/matchers/built_in/operators.rb +119 -0
  34. data/lib/rspec/matchers/built_in/output.rb +150 -0
  35. data/lib/rspec/matchers/built_in/raise_error.rb +91 -33
  36. data/lib/rspec/matchers/built_in/respond_to.rb +42 -20
  37. data/lib/rspec/matchers/built_in/satisfy.rb +19 -3
  38. data/lib/rspec/matchers/built_in/start_and_end_with.rb +54 -18
  39. data/lib/rspec/matchers/built_in/throw_symbol.rb +38 -8
  40. data/lib/rspec/matchers/built_in/yield.rb +237 -55
  41. data/lib/rspec/matchers/built_in.rb +45 -33
  42. data/lib/rspec/matchers/composable.rb +179 -0
  43. data/lib/rspec/matchers/dsl.rb +362 -5
  44. data/lib/rspec/matchers/generated_descriptions.rb +13 -6
  45. data/lib/rspec/matchers/matcher_delegator.rb +37 -0
  46. data/lib/rspec/matchers/pretty.rb +33 -20
  47. data/lib/rspec/matchers.rb +450 -227
  48. data.tar.gz.sig +1 -0
  49. metadata +100 -215
  50. metadata.gz.sig +0 -0
  51. data/features/README.md +0 -49
  52. data/features/Upgrade.md +0 -53
  53. data/features/built_in_matchers/README.md +0 -90
  54. data/features/built_in_matchers/be.feature +0 -175
  55. data/features/built_in_matchers/be_within.feature +0 -48
  56. data/features/built_in_matchers/cover.feature +0 -47
  57. data/features/built_in_matchers/end_with.feature +0 -48
  58. data/features/built_in_matchers/equality.feature +0 -139
  59. data/features/built_in_matchers/exist.feature +0 -45
  60. data/features/built_in_matchers/expect_change.feature +0 -59
  61. data/features/built_in_matchers/expect_error.feature +0 -144
  62. data/features/built_in_matchers/have.feature +0 -109
  63. data/features/built_in_matchers/include.feature +0 -174
  64. data/features/built_in_matchers/match.feature +0 -52
  65. data/features/built_in_matchers/operators.feature +0 -227
  66. data/features/built_in_matchers/predicates.feature +0 -137
  67. data/features/built_in_matchers/respond_to.feature +0 -84
  68. data/features/built_in_matchers/satisfy.feature +0 -33
  69. data/features/built_in_matchers/start_with.feature +0 -48
  70. data/features/built_in_matchers/throw_symbol.feature +0 -91
  71. data/features/built_in_matchers/types.feature +0 -116
  72. data/features/built_in_matchers/yield.feature +0 -146
  73. data/features/custom_matchers/access_running_example.feature +0 -53
  74. data/features/custom_matchers/define_diffable_matcher.feature +0 -27
  75. data/features/custom_matchers/define_matcher.feature +0 -368
  76. data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -38
  77. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
  78. data/features/customized_message.feature +0 -22
  79. data/features/diffing.feature +0 -85
  80. data/features/implicit_docstrings.feature +0 -52
  81. data/features/step_definitions/additional_cli_steps.rb +0 -22
  82. data/features/support/env.rb +0 -5
  83. data/features/syntax_configuration.feature +0 -68
  84. data/features/test_frameworks/test_unit.feature +0 -46
  85. data/lib/rspec/expectations/deprecation.rb +0 -38
  86. data/lib/rspec/expectations/differ.rb +0 -115
  87. data/lib/rspec/expectations/errors.rb +0 -9
  88. data/lib/rspec/expectations/extensions/array.rb +0 -9
  89. data/lib/rspec/expectations/extensions/object.rb +0 -39
  90. data/lib/rspec/expectations/extensions.rb +0 -2
  91. data/lib/rspec/matchers/be_close.rb +0 -9
  92. data/lib/rspec/matchers/built_in/have.rb +0 -108
  93. data/lib/rspec/matchers/built_in/match_array.rb +0 -51
  94. data/lib/rspec/matchers/compatibility.rb +0 -14
  95. data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +0 -39
  96. data/lib/rspec/matchers/matcher.rb +0 -300
  97. data/lib/rspec/matchers/method_missing.rb +0 -12
  98. data/lib/rspec/matchers/operator_matcher.rb +0 -109
  99. data/lib/rspec-expectations.rb +0 -1
  100. data/spec/rspec/expectations/differ_spec.rb +0 -170
  101. data/spec/rspec/expectations/expectation_target_spec.rb +0 -75
  102. data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
  103. data/spec/rspec/expectations/fail_with_spec.rb +0 -95
  104. data/spec/rspec/expectations/handler_spec.rb +0 -206
  105. data/spec/rspec/expectations/syntax_spec.rb +0 -97
  106. data/spec/rspec/matchers/base_matcher_spec.rb +0 -62
  107. data/spec/rspec/matchers/be_close_spec.rb +0 -22
  108. data/spec/rspec/matchers/be_instance_of_spec.rb +0 -63
  109. data/spec/rspec/matchers/be_kind_of_spec.rb +0 -41
  110. data/spec/rspec/matchers/be_spec.rb +0 -516
  111. data/spec/rspec/matchers/be_within_spec.rb +0 -129
  112. data/spec/rspec/matchers/change_spec.rb +0 -542
  113. data/spec/rspec/matchers/configuration_spec.rb +0 -238
  114. data/spec/rspec/matchers/cover_spec.rb +0 -69
  115. data/spec/rspec/matchers/description_generation_spec.rb +0 -176
  116. data/spec/rspec/matchers/dsl_spec.rb +0 -57
  117. data/spec/rspec/matchers/eq_spec.rb +0 -60
  118. data/spec/rspec/matchers/eql_spec.rb +0 -41
  119. data/spec/rspec/matchers/equal_spec.rb +0 -78
  120. data/spec/rspec/matchers/exist_spec.rb +0 -124
  121. data/spec/rspec/matchers/has_spec.rb +0 -122
  122. data/spec/rspec/matchers/have_spec.rb +0 -445
  123. data/spec/rspec/matchers/include_matcher_integration_spec.rb +0 -30
  124. data/spec/rspec/matchers/include_spec.rb +0 -531
  125. data/spec/rspec/matchers/match_array_spec.rb +0 -194
  126. data/spec/rspec/matchers/match_spec.rb +0 -61
  127. data/spec/rspec/matchers/matcher_spec.rb +0 -471
  128. data/spec/rspec/matchers/matchers_spec.rb +0 -37
  129. data/spec/rspec/matchers/method_missing_spec.rb +0 -28
  130. data/spec/rspec/matchers/operator_matcher_spec.rb +0 -223
  131. data/spec/rspec/matchers/raise_error_spec.rb +0 -395
  132. data/spec/rspec/matchers/respond_to_spec.rb +0 -292
  133. data/spec/rspec/matchers/satisfy_spec.rb +0 -44
  134. data/spec/rspec/matchers/start_with_end_with_spec.rb +0 -186
  135. data/spec/rspec/matchers/throw_symbol_spec.rb +0 -116
  136. data/spec/rspec/matchers/yield_spec.rb +0 -437
  137. data/spec/spec_helper.rb +0 -54
  138. data/spec/support/classes.rb +0 -56
  139. data/spec/support/in_sub_process.rb +0 -38
  140. data/spec/support/matchers.rb +0 -22
  141. data/spec/support/ruby_version.rb +0 -10
  142. data/spec/support/shared_examples.rb +0 -13
@@ -1,46 +0,0 @@
1
- Feature: Test::Unit integration
2
-
3
- RSpec-expectations is a stand-alone gem that can be used without
4
- the rest of RSpec. It can easily be used with another test
5
- framework such as Test::Unit if you like RSpec's should/should_not
6
- syntax but prefer the test organization of another framework.
7
-
8
- Scenario: Basic Test::Unit usage
9
- Given a file named "rspec_expectations_test.rb" with:
10
- """ruby
11
- require 'test/unit'
12
- require 'rspec/expectations'
13
-
14
- class RSpecExpectationsTest < Test::Unit::TestCase
15
- RSpec::Matchers.define :be_an_integer do
16
- match { |actual| Integer === actual }
17
- end
18
-
19
- def be_an_int
20
- RSpec.deprecate(:be_an_int, :be_an_integer)
21
- be_an_integer
22
- end
23
-
24
- def test_passing_expectation
25
- x = 1 + 3
26
- x.should == 4
27
- end
28
-
29
- def test_failing_expectation
30
- array = [1, 2]
31
- array.should be_empty
32
- end
33
-
34
- def test_expect_matcher
35
- expect { @a = 5 }.to change { @a }.from(nil).to(5)
36
- end
37
-
38
- def test_custom_matcher_and_deprecation_warning
39
- 1.should be_an_int
40
- end
41
- end
42
- """
43
- When I run `ruby rspec_expectations_test.rb`
44
- Then the output should contain "4 tests, 0 assertions, 1 failures, 0 errors" or "4 tests, 0 assertions, 0 failures, 1 errors"
45
- And the output should contain "expected empty? to return true, got false"
46
- And the output should contain "be_an_int is deprecated"
@@ -1,38 +0,0 @@
1
- module RSpec
2
- unless respond_to?(:deprecate)
3
- class << self
4
- # Used internally by RSpec to display standard deprecation warnings.
5
- # This is also defined in rspec-core, but we can't assume it's loaded
6
- # since rspec-expectations should be usable w/o rspec-core.
7
- def deprecate(method, alternate_method=nil, version=nil)
8
- version_string = version ? "rspec-#{version}" : "a future version of RSpec"
9
-
10
- message = <<-NOTICE
11
-
12
- *****************************************************************
13
- DEPRECATION WARNING: you are using deprecated behaviour that will
14
- be removed from #{version_string}.
15
-
16
- #{caller(0)[2]}
17
-
18
- * #{method} is deprecated.
19
- NOTICE
20
- if alternate_method
21
- message << <<-ADDITIONAL
22
- * please use #{alternate_method} instead.
23
- ADDITIONAL
24
- end
25
-
26
- message << "*****************************************************************"
27
- warn_deprecation(message)
28
- end
29
-
30
- # Used internally by RSpec to display custom deprecation warnings. This
31
- # is also defined in rspec-core, but we can't assume it's loaded since
32
- # rspec-expectations should be usable w/o rspec-core.
33
- def warn_deprecation(message)
34
- warn(message)
35
- end
36
- end
37
- end
38
- end
@@ -1,115 +0,0 @@
1
- require 'diff/lcs'
2
- require 'diff/lcs/hunk'
3
- require 'pp'
4
-
5
- module RSpec
6
- module Expectations
7
- class Differ
8
- # This is snagged from diff/lcs/ldiff.rb (which is a commandline tool)
9
- def diff_as_string(data_new, data_old)
10
- data_old = data_old.split(/\n/).map! { |e| e.chomp }
11
- data_new = data_new.split(/\n/).map! { |e| e.chomp }
12
- diffs = Diff::LCS.diff(data_old, data_new)
13
- output = ""
14
- return output if diffs.empty?
15
- oldhunk = hunk = nil
16
- file_length_difference = 0
17
- diffs.each do |piece|
18
- begin
19
- hunk = Diff::LCS::Hunk.new(
20
- data_old, data_new, piece, context_lines, file_length_difference
21
- )
22
- file_length_difference = hunk.file_length_difference
23
- next unless oldhunk
24
- # Hunks may overlap, which is why we need to be careful when our
25
- # diff includes lines of context. Otherwise, we might print
26
- # redundant lines.
27
- if (context_lines > 0) and hunk.overlaps?(oldhunk)
28
- if hunk.respond_to?(:merge)
29
- # diff-lcs 1.2.x
30
- hunk.merge(oldhunk)
31
- else
32
- # diff-lcs 1.1.3
33
- hunk.unshift(oldhunk)
34
- end
35
- else
36
- output << oldhunk.diff(format)
37
- end
38
- ensure
39
- oldhunk = hunk
40
- output << "\n"
41
- end
42
- end
43
- #Handle the last remaining hunk
44
- output << oldhunk.diff(format) << "\n"
45
- color_diff output
46
- end
47
-
48
- def diff_as_object(actual, expected)
49
- actual_as_string = object_to_string(actual)
50
- expected_as_string = object_to_string(expected)
51
- if diff = diff_as_string(actual_as_string, expected_as_string)
52
- color_diff diff
53
- end
54
- end
55
-
56
- protected
57
-
58
- def format
59
- :unified
60
- end
61
-
62
- def context_lines
63
- 3
64
- end
65
-
66
- def color(text, color_code)
67
- "\e[#{color_code}m#{text}\e[0m"
68
- end
69
-
70
- def red(text)
71
- color(text, 31)
72
- end
73
-
74
- def green(text)
75
- color(text, 32)
76
- end
77
-
78
- def blue(text)
79
- color(text, 34)
80
- end
81
-
82
- def color_diff(diff)
83
- return diff unless RSpec::Matchers.configuration.color?
84
-
85
- diff.lines.map { |line|
86
- case line[0].chr
87
- when "+"
88
- green line
89
- when "-"
90
- red line
91
- when "@"
92
- line[1].chr == "@" ? blue(line) : line
93
- else
94
- line
95
- end
96
- }.join
97
- end
98
-
99
- def object_to_string(object)
100
- case object
101
- when Hash
102
- object.keys.sort_by { |k| k.to_s }.map do |k|
103
- %(#{PP.singleline_pp(k, "")} => #{PP.singleline_pp(object[k], "")})
104
- end.join(",\n")
105
- when String
106
- object =~ /\n/ ? object : object.inspect
107
- else
108
- PP.pp(object,"")
109
- end
110
- end
111
- end
112
-
113
- end
114
- end
115
-
@@ -1,9 +0,0 @@
1
- module RSpec
2
- module Expectations
3
- if defined?(Test::Unit::AssertionFailedError)
4
- class ExpectationNotMetError < Test::Unit::AssertionFailedError; end
5
- else
6
- class ExpectationNotMetError < ::StandardError; end
7
- end
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- # @private
2
- class Array
3
- unless public_instance_methods.map {|m| m.to_s}.include?('none?')
4
- # Supports +none?+ on early patch levels of Ruby 1.8.6
5
- def none?(&block)
6
- !any?(&block)
7
- end
8
- end
9
- end
@@ -1,39 +0,0 @@
1
- module RSpec
2
- module Expectations
3
- module DeprecatedConstants
4
- # Displays deprecation warning when it captures Rspec and Spec. Otherwise
5
- # delegates to super.
6
- def const_missing(name)
7
- case name
8
- when :Rspec, :Spec
9
- RSpec.warn_deprecation <<-WARNING
10
- *****************************************************************
11
- DEPRECATION WARNING: you are using a deprecated constant that will
12
- be removed from a future version of RSpec.
13
-
14
- #{caller(0)[2]}
15
-
16
- * #{name} is deprecated.
17
- * RSpec is the new top-level module in RSpec-2
18
- ***************************************************************
19
- WARNING
20
- RSpec
21
- else
22
- begin
23
- super
24
- rescue Exception => e
25
- e.backtrace.reject! {|l| l =~ Regexp.compile(__FILE__) }
26
- raise e
27
- end
28
- end
29
- end
30
- end
31
-
32
- # @deprecated (no replacement)
33
- def differ=(ignore)
34
- RSpec.deprecate("RSpec::Expectations.differ=(differ)", "nothing at all (diffing is now automatic and no longer configurable)")
35
- end
36
- end
37
- end
38
-
39
- extend RSpec::Expectations::DeprecatedConstants
@@ -1,2 +0,0 @@
1
- require 'rspec/expectations/extensions/array'
2
- require 'rspec/expectations/extensions/object'
@@ -1,9 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- # @deprecated use +be_within+ instead.
4
- def be_close(expected, delta)
5
- RSpec.deprecate("be_close(#{expected}, #{delta})", "be_within(#{delta}).of(#{expected})")
6
- be_within(delta).of(expected)
7
- end
8
- end
9
- end
@@ -1,108 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- module BuiltIn
4
- class Have
5
- def initialize(expected, relativity=:exactly)
6
- @expected = case expected
7
- when :no then 0
8
- when String then expected.to_i
9
- else expected
10
- end
11
- @relativity = relativity
12
- @actual = @collection_name = @plural_collection_name = nil
13
- end
14
-
15
- def relativities
16
- @relativities ||= {
17
- :exactly => "",
18
- :at_least => "at least ",
19
- :at_most => "at most "
20
- }
21
- end
22
-
23
- def matches?(collection_or_owner)
24
- collection = determine_collection(collection_or_owner)
25
- query_method = determine_query_method(collection)
26
- raise not_a_collection unless query_method
27
- @actual = collection.__send__(query_method)
28
- case @relativity
29
- when :at_least then @actual >= @expected
30
- when :at_most then @actual <= @expected
31
- else @actual == @expected
32
- end
33
- end
34
- alias == matches?
35
-
36
- def determine_collection(collection_or_owner)
37
- if collection_or_owner.respond_to?(@collection_name)
38
- collection_or_owner.__send__(@collection_name, *@args, &@block)
39
- elsif (@plural_collection_name && collection_or_owner.respond_to?(@plural_collection_name))
40
- collection_or_owner.__send__(@plural_collection_name, *@args, &@block)
41
- elsif determine_query_method(collection_or_owner)
42
- collection_or_owner
43
- else
44
- collection_or_owner.__send__(@collection_name, *@args, &@block)
45
- end
46
- end
47
-
48
- def determine_query_method(collection)
49
- [:size, :length, :count].detect {|m| collection.respond_to?(m)}
50
- end
51
-
52
- def not_a_collection
53
- "expected #{@collection_name} to be a collection but it does not respond to #length, #size or #count"
54
- end
55
-
56
- def failure_message_for_should
57
- "expected #{relative_expectation} #{@collection_name}, got #{@actual}"
58
- end
59
-
60
- def failure_message_for_should_not
61
- if @relativity == :exactly
62
- return "expected target not to have #{@expected} #{@collection_name}, got #{@actual}"
63
- elsif @relativity == :at_most
64
- return <<-EOF
65
- Isn't life confusing enough?
66
- Instead of having to figure out the meaning of this:
67
- #{Expectations::Syntax.negative_expression("actual", "have_at_most(#{@expected}).#{@collection_name}")}
68
- We recommend that you use this instead:
69
- #{Expectations::Syntax.positive_expression("actual", "have_at_least(#{@expected + 1}).#{@collection_name}")}
70
- EOF
71
- elsif @relativity == :at_least
72
- return <<-EOF
73
- Isn't life confusing enough?
74
- Instead of having to figure out the meaning of this:
75
- #{Expectations::Syntax.negative_expression("actual", "have_at_least(#{@expected}).#{@collection_name}")}
76
- We recommend that you use this instead:
77
- #{Expectations::Syntax.positive_expression("actual", "have_at_most(#{@expected - 1}).#{@collection_name}")}
78
- EOF
79
- end
80
- end
81
-
82
- def description
83
- "have #{relative_expectation} #{@collection_name}"
84
- end
85
-
86
- def respond_to?(m)
87
- @expected.respond_to?(m) || super
88
- end
89
-
90
- private
91
-
92
- def method_missing(method, *args, &block)
93
- @collection_name = method
94
- if inflector = (defined?(ActiveSupport::Inflector) && ActiveSupport::Inflector.respond_to?(:pluralize) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil))
95
- @plural_collection_name = inflector.pluralize(method.to_s)
96
- end
97
- @args = args
98
- @block = block
99
- self
100
- end
101
-
102
- def relative_expectation
103
- "#{relativities[@relativity]}#{@expected}"
104
- end
105
- end
106
- end
107
- end
108
- end
@@ -1,51 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- module BuiltIn
4
- class MatchArray < BaseMatcher
5
- def match(expected, actual)
6
- return false unless actual.respond_to? :to_ary
7
- @extra_items = difference_between_arrays(actual, expected)
8
- @missing_items = difference_between_arrays(expected, actual)
9
- @extra_items.empty? & @missing_items.empty?
10
- end
11
-
12
- def failure_message_for_should
13
- if actual.respond_to? :to_ary
14
- message = "expected collection contained: #{safe_sort(expected).inspect}\n"
15
- message += "actual collection contained: #{safe_sort(actual).inspect}\n"
16
- message += "the missing elements were: #{safe_sort(@missing_items).inspect}\n" unless @missing_items.empty?
17
- message += "the extra elements were: #{safe_sort(@extra_items).inspect}\n" unless @extra_items.empty?
18
- else
19
- message = "expected an array, actual collection was #{actual.inspect}"
20
- end
21
-
22
- message
23
- end
24
-
25
- def failure_message_for_should_not
26
- "Matcher does not support should_not"
27
- end
28
-
29
- def description
30
- "contain exactly #{_pretty_print(expected)}"
31
- end
32
-
33
- private
34
-
35
- def safe_sort(array)
36
- array.sort rescue array
37
- end
38
-
39
- def difference_between_arrays(array_1, array_2)
40
- difference = array_1.to_ary.dup
41
- array_2.to_ary.each do |element|
42
- if index = difference.index(element)
43
- difference.delete_at(index)
44
- end
45
- end
46
- difference
47
- end
48
- end
49
- end
50
- end
51
- end
@@ -1,14 +0,0 @@
1
- RSpec::Matchers.constants.each do |c|
2
- if Class === (klass = RSpec::Matchers.const_get(c))
3
- if klass.public_instance_methods.any? {|m| ['failure_message_for_should',:failure_message_for_should].include?(m)}
4
- klass.class_eval do
5
- alias_method :failure_message, :failure_message_for_should
6
- end
7
- end
8
- if klass.public_instance_methods.any? {|m| ['failure_message_for_should_not',:failure_message_for_should_not].include?(m)}
9
- klass.class_eval do
10
- alias_method :negative_failure_message, :failure_message_for_should_not
11
- end
12
- end
13
- end
14
- end
@@ -1,39 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- module Extensions
4
- module InstanceEvalWithArgs
5
- # based on Bounded Spec InstanceExec (Mauricio Fernandez)
6
- # http://eigenclass.org/hiki/bounded+space+instance_exec
7
- # - uses singleton_class instead of global InstanceExecHelper module
8
- # - this keeps it scoped to classes/modules that include this module
9
- # - only necessary for ruby 1.8.6
10
- def instance_eval_with_args(*args, &block)
11
- return instance_exec(*args, &block) if respond_to?(:instance_exec)
12
-
13
- # If there are no args and the block doesn't expect any, there's no
14
- # need to fake instance_exec with our hack below.
15
- # Notes:
16
- # * lambda { }.arity # => -1
17
- # * lambda { || }.arity # => 0
18
- # * lambda { |*a| }.arity # -1
19
- return instance_eval(&block) if block.arity < 1 && args.empty?
20
-
21
- singleton_class = (class << self; self; end)
22
- begin
23
- orig_critical, Thread.critical = Thread.critical, true
24
- n = 0
25
- n += 1 while respond_to?(method_name="__instance_exec#{n}")
26
- singleton_class.module_eval{ define_method(method_name, &block) }
27
- ensure
28
- Thread.critical = orig_critical
29
- end
30
- begin
31
- return __send__(method_name, *args)
32
- ensure
33
- singleton_class.module_eval{ remove_method(method_name) } rescue nil
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end