rspec-given 2.4.5 → 3.0.0.beta.2
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.
- checksums.yaml +4 -4
- data/Gemfile +6 -2
- data/Gemfile.lock +8 -10
- data/README.md +50 -34
- data/README.old +720 -0
- data/Rakefile +23 -17
- data/TODO +13 -0
- data/examples/example_helper.rb +8 -1
- data/examples/integration/and_spec.rb +6 -11
- data/examples/integration/focused_line_spec.rb +2 -1
- data/examples/integration/given_spec.rb +19 -9
- data/examples/integration/invariant_spec.rb +6 -6
- data/examples/integration/then_spec.rb +0 -1
- data/examples/loader.rb +4 -0
- data/examples/minitest_helper.rb +38 -0
- data/examples/stack/stack_spec.rb +2 -3
- data/lib/given.rb +2 -0
- data/lib/rspec/given.rb +1 -19
- data/rakelib/bundler_fix.rb +17 -0
- data/rakelib/gemspec.rake +161 -0
- data/rakelib/metrics.rake +30 -0
- data/rakelib/preview.rake +14 -0
- data/spec/lib/{rspec/given → given}/evaluator_spec.rb +1 -1
- data/spec/lib/{rspec/given → given}/ext/numeric_spec.rb +2 -2
- data/spec/lib/{rspec/given → given}/ext/numeric_specifications.rb +0 -0
- data/spec/lib/{rspec/given → given}/extensions_spec.rb +2 -2
- data/spec/lib/given/failure_matcher_spec.rb +77 -0
- data/spec/lib/given/failure_spec.rb +49 -0
- data/spec/lib/given/file_cache_spec.rb +26 -0
- data/spec/lib/{rspec/given → given}/fuzzy_number_spec.rb +2 -2
- data/spec/lib/{rspec/given → given}/have_failed_spec.rb +5 -4
- data/spec/lib/{rspec/given → given}/lexical_purity_spec.rb +0 -0
- data/spec/lib/given/line_extractor_spec.rb +84 -0
- data/spec/lib/{rspec/given → given}/module_methods_spec.rb +2 -2
- data/spec/lib/{rspec/given → given}/natural_assertion_spec.rb +4 -36
- data/spec/lib/{rspec/given → given}/options_spec.rb +33 -33
- data/spec/spec_helper.rb +20 -0
- data/spec/support/faux_then.rb +2 -2
- data/spec/support/natural_assertion_control.rb +1 -1
- metadata +35 -42
- data/examples/integration/failing_spec.rb +0 -5
- data/lib/rspec/given/configure.rb +0 -20
- data/lib/rspec/given/core.rb +0 -9
- data/lib/rspec/given/evaluator.rb +0 -39
- data/lib/rspec/given/ext/numeric.rb +0 -32
- data/lib/rspec/given/extensions.rb +0 -262
- data/lib/rspec/given/failure.rb +0 -53
- data/lib/rspec/given/file_cache.rb +0 -19
- data/lib/rspec/given/fuzzy_number.rb +0 -70
- data/lib/rspec/given/fuzzy_shortcuts.rb +0 -1
- data/lib/rspec/given/have_failed.rb +0 -77
- data/lib/rspec/given/line_extractor.rb +0 -43
- data/lib/rspec/given/module_methods.rb +0 -70
- data/lib/rspec/given/monkey.rb +0 -42
- data/lib/rspec/given/natural_assertion.rb +0 -193
- data/lib/rspec/given/rspec1_given.rb +0 -11
- data/lib/rspec/given/version.rb +0 -10
- data/spec/lib/rspec/given/failure_spec.rb +0 -17
- data/spec/lib/rspec/given/file_cache_spec.rb +0 -28
- data/spec/lib/rspec/given/line_extractor_spec.rb +0 -86
@@ -1,262 +0,0 @@
|
|
1
|
-
require 'rspec/given/failure'
|
2
|
-
require 'rspec/given/module_methods'
|
3
|
-
require 'rspec/given/natural_assertion'
|
4
|
-
|
5
|
-
module RSpec
|
6
|
-
module Given
|
7
|
-
|
8
|
-
# Provide run-time methods to support RSpec/Given infrastructure.
|
9
|
-
# All the methods in this module are considered private and
|
10
|
-
# implementation-specific.
|
11
|
-
module InstanceExtensions # :nodoc:
|
12
|
-
|
13
|
-
# List of containing contexts in order from innermost to
|
14
|
-
# outermost.
|
15
|
-
def _rg_inner_contexts # :nodoc:
|
16
|
-
self.class.ancestors.select { |context|
|
17
|
-
context.respond_to?(:_rgc_givens)
|
18
|
-
}
|
19
|
-
end
|
20
|
-
|
21
|
-
# List of containing contexts in order from outermost to
|
22
|
-
# innermost.
|
23
|
-
def _rg_contexts # :nodoc:
|
24
|
-
_rg_inner_contexts.reverse
|
25
|
-
end
|
26
|
-
|
27
|
-
# Return the context information for keyword from the innermost
|
28
|
-
# defining context.
|
29
|
-
def _rg_info(keyword) # :nodoc:
|
30
|
-
_rg_inner_contexts.each do |context|
|
31
|
-
h = context._rgc_context_info
|
32
|
-
if h.has_key?(keyword)
|
33
|
-
return h[keyword]
|
34
|
-
end
|
35
|
-
end
|
36
|
-
nil
|
37
|
-
end
|
38
|
-
|
39
|
-
# Should a natural assertion failure message be generated?
|
40
|
-
#
|
41
|
-
# A natural assertion failure message is generated if the
|
42
|
-
# assertion has non-empty content that doesn't use rspec
|
43
|
-
# assertions. The configuration options for natural assertions
|
44
|
-
# are checked and applied accordingly.
|
45
|
-
#
|
46
|
-
def _rg_need_na_message?(nassert) # :nodoc:
|
47
|
-
return false unless nassert.has_content?
|
48
|
-
use_na = _rg_na_configured?
|
49
|
-
return true if use_na == :always
|
50
|
-
return false if !RSpec::Given::MONKEY && nassert.using_rspec_assertion?
|
51
|
-
use_na
|
52
|
-
end
|
53
|
-
|
54
|
-
# Return the configuration value for natural assertions.
|
55
|
-
#
|
56
|
-
# If natural assertions are not configured in the contexts, use
|
57
|
-
# the global configuration value.
|
58
|
-
def _rg_na_configured? # :nodoc:
|
59
|
-
info_value = _rg_info(:natural_assertions_enabled)
|
60
|
-
info_value.nil? ? RSpec::Given.natural_assertions_enabled? : info_value
|
61
|
-
end
|
62
|
-
|
63
|
-
# Establish all the Given preconditions the current and
|
64
|
-
# surrounding describe/context blocks, starting with the
|
65
|
-
# outermost context.
|
66
|
-
def _rg_establish_givens # :nodoc:
|
67
|
-
return if defined?(@_rg_ran) && @_rg_ran
|
68
|
-
@_rg_ran = true
|
69
|
-
_rg_contexts.each do |context|
|
70
|
-
context._rgc_givens.each do |block|
|
71
|
-
instance_eval(&block)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
# Check all the invariants in the current and surrounding
|
77
|
-
# describe/context blocks, starting with the outermost context.
|
78
|
-
def _rg_check_invariants # :nodoc:
|
79
|
-
_rg_contexts.each do |context|
|
80
|
-
context._rgc_invariants.each do |block|
|
81
|
-
_rg_evaluate("Invariant", block)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def _rg_check_ands # :nodoc:
|
87
|
-
return if self.class._rgc_context_info[:and_ran]
|
88
|
-
self.class._rgc_and_blocks.each do |block|
|
89
|
-
_rg_evaluate("And", block)
|
90
|
-
end
|
91
|
-
self.class._rgc_context_info[:and_ran] = true
|
92
|
-
end
|
93
|
-
|
94
|
-
# Implement the run-time semantics of the Then clause.
|
95
|
-
def _rg_then(&block) # :nodoc:
|
96
|
-
_rg_establish_givens
|
97
|
-
_rg_check_invariants
|
98
|
-
_rg_evaluate("Then", block)
|
99
|
-
_rg_check_ands
|
100
|
-
end
|
101
|
-
|
102
|
-
# Evaluate a Then, And, or Invariant assertion.
|
103
|
-
def _rg_evaluate(clause_type, block) # :nodoc:
|
104
|
-
RSpec::Given.matcher_called = false
|
105
|
-
passed = instance_eval(&block)
|
106
|
-
if ! passed && _rg_na_configured? && ! RSpec::Given.matcher_called
|
107
|
-
nassert = NaturalAssertion.new(clause_type, block, self, self.class._rgc_lines)
|
108
|
-
RSpec::Given.fail_with nassert.message if _rg_need_na_message?(nassert)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
module ClassExtensions
|
114
|
-
|
115
|
-
# List of all givens directly in the current describe/context
|
116
|
-
# block.
|
117
|
-
def _rgc_givens # :nodoc:
|
118
|
-
@_rgc_givens ||= []
|
119
|
-
end
|
120
|
-
|
121
|
-
# List of all invariants directly in the current
|
122
|
-
# describe/context block.
|
123
|
-
def _rgc_invariants # :nodoc:
|
124
|
-
@_rgc_invariants ||= []
|
125
|
-
end
|
126
|
-
|
127
|
-
def _rgc_and_blocks
|
128
|
-
@_rgc_and_blocks ||= []
|
129
|
-
end
|
130
|
-
|
131
|
-
def _rgc_context_info
|
132
|
-
@_rgc_context_info ||= {}
|
133
|
-
end
|
134
|
-
|
135
|
-
def _rgc_lines
|
136
|
-
@_rgc_lines ||= LineExtractor.new
|
137
|
-
end
|
138
|
-
|
139
|
-
# Trigger the evaluation of a Given! block by referencing its
|
140
|
-
# name.
|
141
|
-
def _rgc_trigger_given(name) # :nodoc:
|
142
|
-
Proc.new { send(name) }
|
143
|
-
end
|
144
|
-
|
145
|
-
# *DEPRECATED:*
|
146
|
-
#
|
147
|
-
# The Scenario command is deprecated. Using Scenario in an
|
148
|
-
# example will result in a warning message. Eventually the
|
149
|
-
# command will be removed.
|
150
|
-
#
|
151
|
-
# Declare a scenario to contain Given/When/Then declarations. A
|
152
|
-
# Scenario is essentially an RSpec context, with the additional
|
153
|
-
# expectations:
|
154
|
-
#
|
155
|
-
# * There is a single When declaration in a Scenario.
|
156
|
-
# * Scenarios do not nest.
|
157
|
-
#
|
158
|
-
# :call-seq:
|
159
|
-
# Scenario "a scenario description" do ... end
|
160
|
-
#
|
161
|
-
def Scenario(description, &block)
|
162
|
-
file, line = eval("[__FILE__, __LINE__]", block.binding)
|
163
|
-
puts "WARNING: Scenario is deprecated, please use either describe or context (#{file}:#{line})"
|
164
|
-
context(description, &block)
|
165
|
-
end
|
166
|
-
|
167
|
-
# Declare a "given" of the current specification. If the given
|
168
|
-
# is named, the block will be lazily evaluated the first time
|
169
|
-
# the given is mentioned by name in the specification. If the
|
170
|
-
# given is unnamed, the block is evaluated for side effects
|
171
|
-
# every time the specification is executed.
|
172
|
-
#
|
173
|
-
# :call-seq:
|
174
|
-
# Given(:name) { ... code ... }
|
175
|
-
# Given { ... code ... }
|
176
|
-
#
|
177
|
-
def Given(*args, &block)
|
178
|
-
if args.first.is_a?(Symbol)
|
179
|
-
let(args.first, &block)
|
180
|
-
else
|
181
|
-
_rgc_givens << block
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
# Declare a named given of the current specification. Similar
|
186
|
-
# to the named version of the "Given" command, except that the
|
187
|
-
# block is always evaluated.
|
188
|
-
#
|
189
|
-
# :call-seq:
|
190
|
-
# Given!(:name) { ... code ... }
|
191
|
-
#
|
192
|
-
def Given!(name, &block)
|
193
|
-
let(name, &block)
|
194
|
-
_rgc_givens << _rgc_trigger_given(name)
|
195
|
-
end
|
196
|
-
|
197
|
-
# Declare the code that is under test.
|
198
|
-
#
|
199
|
-
# :call-seq:
|
200
|
-
# When(:named_result) { ... code_under_test ... }
|
201
|
-
# When { ... code_under_test ... }
|
202
|
-
#
|
203
|
-
def When(*args, &block)
|
204
|
-
if args.first.is_a?(Symbol)
|
205
|
-
let!(args.first) do
|
206
|
-
begin
|
207
|
-
_rg_establish_givens
|
208
|
-
instance_eval(&block)
|
209
|
-
rescue RSpec::Given.pending_error => ex
|
210
|
-
raise
|
211
|
-
rescue Exception => ex
|
212
|
-
Failure.new(ex)
|
213
|
-
end
|
214
|
-
end
|
215
|
-
else
|
216
|
-
before do
|
217
|
-
_rg_establish_givens
|
218
|
-
instance_eval(&block)
|
219
|
-
end
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
# Provide an assertion about the specification.
|
224
|
-
#
|
225
|
-
# Then supplies an assertion that should be true after all the
|
226
|
-
# Given and When blocks have been run. All invariants in scope
|
227
|
-
# will be checked before the Then block is run.
|
228
|
-
#
|
229
|
-
# :call-seq:
|
230
|
-
# Then { ... assertion ... }
|
231
|
-
#
|
232
|
-
def Then(&block)
|
233
|
-
env = block.binding
|
234
|
-
file, line = eval "[__FILE__, __LINE__]", env
|
235
|
-
description = _rgc_lines.line(file, line) unless RSpec::Given.source_caching_disabled
|
236
|
-
if description
|
237
|
-
cmd = "it(description)"
|
238
|
-
else
|
239
|
-
cmd = "specify"
|
240
|
-
end
|
241
|
-
eval %{#{cmd} do _rg_then(&block) end}, binding, file, line
|
242
|
-
_rgc_context_info[:then_defined] = true
|
243
|
-
end
|
244
|
-
|
245
|
-
# Establish an invariant that must be true for all Then blocks
|
246
|
-
# in the current (and nested) scopes.
|
247
|
-
def Invariant(&block)
|
248
|
-
_rgc_invariants << block
|
249
|
-
end
|
250
|
-
|
251
|
-
def And(&block)
|
252
|
-
fail "And defined without a Then" unless _rgc_context_info[:then_defined]
|
253
|
-
_rgc_and_blocks << block
|
254
|
-
end
|
255
|
-
|
256
|
-
def use_natural_assertions(enabled=true)
|
257
|
-
RSpec::Given.ok_to_use_natural_assertions(enabled)
|
258
|
-
_rgc_context_info[:natural_assertions_enabled] = enabled
|
259
|
-
end
|
260
|
-
end
|
261
|
-
end
|
262
|
-
end
|
data/lib/rspec/given/failure.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
module RSpec
|
2
|
-
module Given
|
3
|
-
|
4
|
-
# Failure objects will raise the given exception whenever you try
|
5
|
-
# to send it *any* message.
|
6
|
-
class Failure < BasicObject
|
7
|
-
undef_method :==, :!=, :!
|
8
|
-
|
9
|
-
def initialize(exception)
|
10
|
-
@exception = exception
|
11
|
-
end
|
12
|
-
|
13
|
-
def is_a?(klass)
|
14
|
-
klass == Failure
|
15
|
-
end
|
16
|
-
|
17
|
-
def ==(other)
|
18
|
-
if failed_matcher?(other)
|
19
|
-
other.matches?(self)
|
20
|
-
else
|
21
|
-
die
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def !=(other)
|
26
|
-
if failed_matcher?(other)
|
27
|
-
! other.matches?(self)
|
28
|
-
else
|
29
|
-
die
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def method_missing(sym, *args, &block)
|
34
|
-
die
|
35
|
-
end
|
36
|
-
|
37
|
-
def respond_to?(method_name)
|
38
|
-
method_name == :call
|
39
|
-
end
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
def die
|
44
|
-
::Kernel.raise @exception
|
45
|
-
end
|
46
|
-
|
47
|
-
def failed_matcher?(other)
|
48
|
-
other.is_a?(::RSpec::Given::HaveFailed::HaveFailedMatcher)
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module RSpec
|
2
|
-
module Given
|
3
|
-
class FileCache
|
4
|
-
def initialize
|
5
|
-
@lines = {}
|
6
|
-
end
|
7
|
-
|
8
|
-
def get(file_name)
|
9
|
-
@lines[file_name] ||= read_lines(file_name)
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def read_lines(file_name)
|
15
|
-
open(file_name) { |f| f.readlines }
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
|
2
|
-
module RSpec
|
3
|
-
module Given
|
4
|
-
module Fuzzy
|
5
|
-
class FuzzyNumber
|
6
|
-
|
7
|
-
DEFAULT_EPSILON = 10 * Float::EPSILON
|
8
|
-
|
9
|
-
attr_reader :exact_value, :delta_amount
|
10
|
-
|
11
|
-
def initialize(exact_value)
|
12
|
-
@exact_value = exact_value
|
13
|
-
@delta_amount = exact_value * DEFAULT_EPSILON
|
14
|
-
end
|
15
|
-
|
16
|
-
def exactly_equals?(other)
|
17
|
-
other.is_a?(self.class) &&
|
18
|
-
exact_value == other.exact_value &&
|
19
|
-
delta_amount == other.delta_amount
|
20
|
-
end
|
21
|
-
|
22
|
-
# Low limit of the fuzzy number.
|
23
|
-
def low_limit
|
24
|
-
exact_value - delta_amount
|
25
|
-
end
|
26
|
-
|
27
|
-
# High limit of the fuzzy number.
|
28
|
-
def high_limit
|
29
|
-
exact_value + delta_amount
|
30
|
-
end
|
31
|
-
|
32
|
-
# True if the other number is in range of the fuzzy number.
|
33
|
-
def ==(other)
|
34
|
-
(other - exact_value).abs <= delta_amount
|
35
|
-
end
|
36
|
-
|
37
|
-
def to_s
|
38
|
-
"<Approximately #{exact_value} +/- #{delta_amount}>"
|
39
|
-
end
|
40
|
-
|
41
|
-
# Set the delta for a fuzzy number.
|
42
|
-
def delta(delta)
|
43
|
-
@delta_amount = delta.abs
|
44
|
-
self
|
45
|
-
end
|
46
|
-
|
47
|
-
# Specifying a percentage of the exact number to be used in
|
48
|
-
# setting the delta.
|
49
|
-
def percent(percentage)
|
50
|
-
delta(exact_value * (percentage / 100.0))
|
51
|
-
end
|
52
|
-
|
53
|
-
# Specifying the number of epsilons to be used in setting the
|
54
|
-
# delta.
|
55
|
-
def epsilon(neps)
|
56
|
-
delta(exact_value * (neps * Float::EPSILON))
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
# Create an approximate number that is approximately equal to
|
61
|
-
# the given number, plus or minus the delta value. If no
|
62
|
-
# explicit delta is given, then the default delta that is about
|
63
|
-
# 10X the size of the smallest possible change in the given
|
64
|
-
# number will be used.
|
65
|
-
def about(*args)
|
66
|
-
FuzzyNumber.new(*args)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
@@ -1 +0,0 @@
|
|
1
|
-
require 'rspec/given/ext/numeric'
|
@@ -1,77 +0,0 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Given
|
5
|
-
module HaveFailed
|
6
|
-
|
7
|
-
# Specializes the RaiseError matcher to handle
|
8
|
-
# Failure/non-failure objects.
|
9
|
-
|
10
|
-
# The implementation of RaiseError changed between RSpec 2.11 and 2.12.
|
11
|
-
if RSpec::Matchers::BuiltIn::RaiseError.instance_methods.include?(:does_not_match?)
|
12
|
-
|
13
|
-
class HaveFailedMatcher < RSpec::Matchers::BuiltIn::RaiseError
|
14
|
-
def matches?(given_proc, negative_expectation = false)
|
15
|
-
if given_proc.is_a?(Failure)
|
16
|
-
super
|
17
|
-
else
|
18
|
-
super(lambda { }, negative_expectation)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def does_not_match?(given_proc)
|
23
|
-
if given_proc.is_a?(Failure)
|
24
|
-
super(given_proc)
|
25
|
-
else
|
26
|
-
super(lambda { })
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def to_s
|
31
|
-
"<Failure matching #{@expected_error}: #{@expected_message.inspect}>"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
else
|
36
|
-
|
37
|
-
class HaveFailedMatcher < RSpec::Matchers::BuiltIn::RaiseError
|
38
|
-
def matches?(given_proc)
|
39
|
-
if given_proc.is_a?(Failure)
|
40
|
-
super
|
41
|
-
else
|
42
|
-
super(lambda { })
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def to_s
|
47
|
-
"<FailureMatcher on #{@expected_error}: #{@expected_message.inspect}>"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
# Simular to raise_error(...), but reads a bit better when using
|
54
|
-
# a failure result from a when clause.
|
55
|
-
#
|
56
|
-
# Typical Usage:
|
57
|
-
#
|
58
|
-
# When(:result) { fail "OUCH" }
|
59
|
-
# Then { result.should have_failed(StandardError, /OUCH/) }
|
60
|
-
#
|
61
|
-
# When(:result) { good_code }
|
62
|
-
# Then { result.should_not have_failed }
|
63
|
-
#
|
64
|
-
# :call-seq:
|
65
|
-
# have_failed([exception_class [, message_pattern]])
|
66
|
-
# have_failed([exception_class [, message_pattern]]) { |ex| ... }
|
67
|
-
#
|
68
|
-
def have_failed(error=Exception, message=nil, &block)
|
69
|
-
HaveFailedMatcher.new(error, message, &block)
|
70
|
-
end
|
71
|
-
alias have_raised have_failed
|
72
|
-
|
73
|
-
def failure(error=Exception, message=nil, &block)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|