forked from rspec/rspec-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapters.rb
179 lines (154 loc) · 4.51 KB
/
adapters.rb
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
require 'delegate'
require 'active_support'
require 'active_support/concern'
module RSpec
module Rails
if ::Rails::VERSION::STRING >= '4.1.0'
gem 'minitest'
require 'minitest/assertions'
Assertions = Minitest::Assertions
else
begin
require 'test/unit/assertions'
rescue LoadError
# work around for Rubinius not having a std std-lib
require 'rubysl-test-unit' if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
require 'test/unit/assertions'
end
Assertions = Test::Unit::Assertions
end
# @api private
class AssertionDelegator < Module
# @api private
def initialize(*assertion_modules)
assertion_class = Class.new(SimpleDelegator) do
include ::RSpec::Rails::Assertions
include ::RSpec::Rails::MinitestCounters
assertion_modules.each { |mod| include mod }
end
super() do
# @api private
define_method :build_assertion_instance do
assertion_class.new(self)
end
# @api private
def assertion_instance
@assertion_instance ||= build_assertion_instance
end
assertion_modules.each do |mod|
mod.public_instance_methods.each do |method|
next if method == :method_missing || method == "method_missing"
define_method(method.to_sym) do |*args, &block|
assertion_instance.send(method.to_sym, *args, &block)
end
end
end
end
end
end
# Adapts example groups for `Minitest::Test::LifecycleHooks`
#
# @api private
module MinitestLifecycleAdapter
extend ActiveSupport::Concern
included do |group|
group.before { after_setup }
group.after { before_teardown }
group.around do |example|
before_setup
example.run
after_teardown
end
end
def before_setup
end
def after_setup
end
def before_teardown
end
def after_teardown
end
end
# @api private
module MinitestCounters
# @api private
def assertions
@assertions ||= 0
end
# @api private
def assertions=(assertions)
@assertions = assertions
end
end
# @api private
module SetupAndTeardownAdapter
extend ActiveSupport::Concern
module ClassMethods
# @api private
#
# Wraps `setup` calls from within Rails' testing framework in `before`
# hooks.
def setup(*methods)
methods.each do |method|
if method.to_s =~ /^setup_(with_controller|fixtures|controller_request_and_response)$/
prepend_before { __send__ method }
else
before { __send__ method }
end
end
end
# @api private
#
# Wraps `teardown` calls from within Rails' testing framework in
# `after` hooks.
def teardown(*methods)
methods.each { |method| after { __send__ method } }
end
end
# @api private
def method_name
@example
end
end
# @private
module MinitestAssertionAdapter
extend ActiveSupport::Concern
module ClassMethods
# @api private
#
# Returns the names of assertion methods that we want to expose to
# examples without exposing non-assertion methods in Test::Unit or
# Minitest.
def assertion_method_names
::RSpec::Rails::Assertions.public_instance_methods.select{|m| m.to_s =~ /^(assert|flunk|refute)/} +
[:build_message]
end
# @api private
def define_assertion_delegators
assertion_method_names.each do |m|
define_method(m.to_sym) do |*args, &block|
assertion_delegator.send(m.to_sym, *args, &block)
end
end
end
end
# @api private
class AssertionDelegator
include ::RSpec::Rails::Assertions
include ::RSpec::Rails::MinitestCounters
end
# @api private
def assertion_delegator
@assertion_delegator ||= AssertionDelegator.new
end
included do
define_assertion_delegators
end
end
# Backwards compatibility. It's unlikely that anyone is using this
# constant, but we had forgotten to mark it as `@private` earlier
#
# @api private
TestUnitAssertionAdapter = MinitestAssertionAdapter
end
end