-
-
Notifications
You must be signed in to change notification settings - Fork 397
/
configuration.rb
231 lines (207 loc) · 8.05 KB
/
configuration.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
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
226
227
228
229
230
231
RSpec::Support.require_rspec_expectations "syntax"
module RSpec
module Expectations
# Provides configuration options for rspec-expectations.
# If you are using rspec-core, you can access this via a
# block passed to `RSpec::Core::Configuration#expect_with`.
# Otherwise, you can access it via RSpec::Expectations.configuration.
#
# @example
# RSpec.configure do |rspec|
# rspec.expect_with :rspec do |c|
# # c is the config object
# end
# end
#
# # or
#
# RSpec::Expectations.configuration
class Configuration
# @private
FALSE_POSITIVE_BEHAVIOURS =
{
:warn => lambda { |message| RSpec.warning message },
:raise => lambda { |message| raise ArgumentError, message },
:nothing => lambda { |_| true },
}
def initialize
@on_potential_false_positives = :warn
@strict_predicate_matchers = false
end
# Configures the supported syntax.
# @param [Array<Symbol>, Symbol] values the syntaxes to enable
# @example
# RSpec.configure do |rspec|
# rspec.expect_with :rspec do |c|
# c.syntax = :should
# # or
# c.syntax = :expect
# # or
# c.syntax = [:should, :expect]
# end
# end
def syntax=(values)
if Array(values).include?(:expect)
Expectations::Syntax.enable_expect
else
Expectations::Syntax.disable_expect
end
if Array(values).include?(:should)
Expectations::Syntax.enable_should
else
Expectations::Syntax.disable_should
end
end
# Configures the maximum character length that RSpec will print while
# formatting an object. You can set length to nil to prevent RSpec from
# doing truncation.
# @param [Fixnum] length the number of characters to limit the formatted output to.
# @example
# RSpec.configure do |rspec|
# rspec.expect_with :rspec do |c|
# c.max_formatted_output_length = 200
# end
# end
def max_formatted_output_length=(length)
RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = length
end
# The list of configured syntaxes.
# @return [Array<Symbol>] the list of configured syntaxes.
# @example
# unless RSpec::Matchers.configuration.syntax.include?(:expect)
# raise "this RSpec extension gem requires the rspec-expectations `:expect` syntax"
# end
def syntax
syntaxes = []
syntaxes << :should if Expectations::Syntax.should_enabled?
syntaxes << :expect if Expectations::Syntax.expect_enabled?
syntaxes
end
if ::RSpec.respond_to?(:configuration)
def color?
::RSpec.configuration.color_enabled?
end
else
# Indicates whether or not diffs should be colored.
# Delegates to rspec-core's color option if rspec-core
# is loaded; otherwise you can set it here.
attr_writer :color
# Indicates whether or not diffs should be colored.
# Delegates to rspec-core's color option if rspec-core
# is loaded; otherwise you can set it here.
def color?
defined?(@color) && @color
end
end
# Adds `should` and `should_not` to the given classes
# or modules. This can be used to ensure `should` works
# properly on things like proxy objects (particular
# `Delegator`-subclassed objects on 1.8).
#
# @param [Array<Module>] modules the list of classes or modules
# to add `should` and `should_not` to.
def add_should_and_should_not_to(*modules)
modules.each do |mod|
Expectations::Syntax.enable_should(mod)
end
end
# Sets or gets the backtrace formatter. The backtrace formatter should
# implement `#format_backtrace(Array<String>)`. This is used
# to format backtraces of errors handled by the `raise_error`
# matcher.
#
# If you are using rspec-core, rspec-core's backtrace formatting
# will be used (including respecting the presence or absence of
# the `--backtrace` option).
#
# @!attribute [rw] backtrace_formatter
attr_writer :backtrace_formatter
def backtrace_formatter
@backtrace_formatter ||= if defined?(::RSpec.configuration.backtrace_formatter)
::RSpec.configuration.backtrace_formatter
else
NullBacktraceFormatter
end
end
# Sets if custom matcher descriptions and failure messages
# should include clauses from methods defined using `chain`.
# @param value [Boolean]
attr_writer :include_chain_clauses_in_custom_matcher_descriptions
# Indicates whether or not custom matcher descriptions and failure messages
# should include clauses from methods defined using `chain`. It is
# false by default for backwards compatibility.
def include_chain_clauses_in_custom_matcher_descriptions?
@include_chain_clauses_in_custom_matcher_descriptions ||= false
end
# @private
def reset_syntaxes_to_default
self.syntax = [:should, :expect]
RSpec::Expectations::Syntax.warn_about_should!
end
# @api private
# Null implementation of a backtrace formatter used by default
# when rspec-core is not loaded. Does no filtering.
NullBacktraceFormatter = Module.new do
def self.format_backtrace(backtrace)
backtrace
end
end
# Configures whether RSpec will warn about matcher use which will
# potentially cause false positives in tests.
#
# @param [Boolean] boolean
def warn_about_potential_false_positives=(boolean)
if boolean
self.on_potential_false_positives = :warn
elsif warn_about_potential_false_positives?
self.on_potential_false_positives = :nothing
else
# no-op, handler is something else
end
end
#
# Configures what RSpec will do about matcher use which will
# potentially cause false positives in tests.
#
# @param [Symbol] behavior can be set to :warn, :raise or :nothing
def on_potential_false_positives=(behavior)
unless FALSE_POSITIVE_BEHAVIOURS.key?(behavior)
raise ArgumentError, "Supported values are: #{FALSE_POSITIVE_BEHAVIOURS.keys}"
end
@on_potential_false_positives = behavior
end
# Configures RSpec to check predicate matchers to `be(true)` / `be(false)` (strict),
# or `be_truthy` / `be_falsey` (not strict).
# Historically, the default was `false`, but `true` is recommended.
def strict_predicate_matchers=(flag)
raise ArgumentError, "Pass `true` or `false`" unless flag == true || flag == false
@strict_predicate_matchers = flag
end
attr_reader :strict_predicate_matchers
def strict_predicate_matchers?
@strict_predicate_matchers
end
# Indicates what RSpec will do about matcher use which will
# potentially cause false positives in tests, generally you want to
# avoid such scenarios so this defaults to `true`.
attr_reader :on_potential_false_positives
# Indicates whether RSpec will warn about matcher use which will
# potentially cause false positives in tests, generally you want to
# avoid such scenarios so this defaults to `true`.
def warn_about_potential_false_positives?
on_potential_false_positives == :warn
end
# @private
def false_positives_handler
FALSE_POSITIVE_BEHAVIOURS.fetch(@on_potential_false_positives)
end
end
# The configuration object.
# @return [RSpec::Expectations::Configuration] the configuration object
def self.configuration
@configuration ||= Configuration.new
end
# set default syntax
configuration.reset_syntaxes_to_default
end
end