r_spec-clone 1.7.2 → 2.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.
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RSpec
4
- module Clone
5
- module Error
6
- # Exception for undefined subjects.
7
- class UndefinedSubject < ::RuntimeError
8
- end
9
- end
10
- end
11
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "shared"
4
- require_relative File.join("..", "expectation_target", "block")
5
-
6
- module RSpec
7
- module Clone
8
- module ExpectationHelper
9
- # {RSpec::Clone::Dsl.its}'s expectation helper module.
10
- module Its
11
- include Shared
12
-
13
- # Wraps the target of an expectation with the actual value.
14
- #
15
- # @return [Block] The wrapped target of an expectation.
16
- #
17
- # @example
18
- # is_expected # => #<RSpec::Clone::ExpectationTarget::Block:0x00007f @callable=#<Proc:0x00007f>>
19
- #
20
- # @api public
21
- def is_expected
22
- ExpectationTarget::Block.new(method(:actual))
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,250 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "matchi"
4
-
5
- module RSpec
6
- module Clone
7
- module ExpectationHelper
8
- # Abstract expectation helper base module.
9
- #
10
- # This module defines a number of methods to create expectations, which
11
- # are automatically included into examples.
12
- #
13
- # It also includes a collection of expectation matchers.
14
- #
15
- # @see https://github.com/fixrb/matchi
16
- module Shared
17
- # Equivalence matcher
18
- #
19
- # @example
20
- # matcher = eq("foo")
21
- # matcher.match? { "foo" } # => true
22
- # matcher.match? { "bar" } # => false
23
- #
24
- # @param expected [#eql?] An expected equivalent object.
25
- #
26
- # @return [#match?] An equivalence matcher.
27
- #
28
- # @api public
29
- def eq(expected)
30
- ::Matchi::Eq.new(expected)
31
- end
32
-
33
- alias eql eq
34
-
35
- # Identity matcher
36
- #
37
- # @example
38
- # object = "foo"
39
- # matcher = be(object)
40
- # matcher.match? { object } # => true
41
- # matcher.match? { "foo" } # => false
42
- #
43
- # @param expected [#equal?] The expected identical object.
44
- #
45
- # @return [#match?] An identity matcher.
46
- #
47
- # @api public
48
- def be(expected)
49
- ::Matchi::Be.new(expected)
50
- end
51
-
52
- alias equal be
53
-
54
- # Comparisons matcher
55
- #
56
- # @example
57
- # matcher = be_within(1).of(41)
58
- # matcher.match? { 42 } # => true
59
- # matcher.match? { 43 } # => false
60
- #
61
- # @param delta [Numeric] A numeric value.
62
- #
63
- # @return [#match?] A comparison matcher.
64
- #
65
- # @api public
66
- def be_within(delta)
67
- ::Matchi::BeWithin.new(delta)
68
- end
69
-
70
- # Regular expressions matcher
71
- #
72
- # @example
73
- # matcher = match(/^foo$/)
74
- # matcher.match? { "foo" } # => true
75
- # matcher.match? { "bar" } # => false
76
- #
77
- # @param expected [#match] A regular expression.
78
- #
79
- # @return [#match?] A regular expression matcher.
80
- #
81
- # @api public
82
- def match(expected)
83
- ::Matchi::Match.new(expected)
84
- end
85
-
86
- # Expecting errors matcher
87
- #
88
- # @example
89
- # matcher = raise_exception(NameError)
90
- # matcher.match? { RSpec::Clone::Boom! } # => true
91
- # matcher.match? { true } # => false
92
- #
93
- # @param expected [Exception, #to_s] The expected exception name.
94
- #
95
- # @return [#match?] An error matcher.
96
- #
97
- # @api public
98
- def raise_exception(expected)
99
- ::Matchi::RaiseException.new(expected)
100
- end
101
-
102
- # True matcher
103
- #
104
- # @example
105
- # matcher = be_true
106
- # matcher.match? { true } # => true
107
- # matcher.match? { false } # => false
108
- # matcher.match? { nil } # => false
109
- # matcher.match? { 4 } # => false
110
- #
111
- # @return [#match?] A `true` matcher.
112
- #
113
- # @api public
114
- def be_true
115
- be(true)
116
- end
117
-
118
- # False matcher
119
- #
120
- # @example
121
- # matcher = be_false
122
- # matcher.match? { false } # => true
123
- # matcher.match? { true } # => false
124
- # matcher.match? { nil } # => false
125
- # matcher.match? { 4 } # => false
126
- #
127
- # @return [#match?] A `false` matcher.
128
- #
129
- # @api public
130
- def be_false
131
- be(false)
132
- end
133
-
134
- # Nil matcher
135
- #
136
- # @example
137
- # matcher = be_nil
138
- # matcher.match? { nil } # => true
139
- # matcher.match? { false } # => false
140
- # matcher.match? { true } # => false
141
- # matcher.match? { 4 } # => false
142
- #
143
- # @return [#match?] A `nil` matcher.
144
- #
145
- # @api public
146
- def be_nil
147
- be(nil)
148
- end
149
-
150
- # Type/class matcher
151
- #
152
- # @example
153
- # matcher = be_an_instance_of(String)
154
- # matcher.match? { "foo" } # => true
155
- # matcher.match? { 4 } # => false
156
- #
157
- # @param expected [Class, #to_s] The expected class name.
158
- #
159
- # @return [#match?] A type/class matcher.
160
- #
161
- # @api public
162
- def be_an_instance_of(expected)
163
- ::Matchi::BeAnInstanceOf.new(expected)
164
- end
165
-
166
- # Change matcher
167
- #
168
- # @example
169
- # object = []
170
- # matcher = change(object, :length).by(1)
171
- # matcher.match? { object << 1 } # => true
172
- #
173
- # object = []
174
- # matcher = change(object, :length).by_at_least(1)
175
- # matcher.match? { object << 1 } # => true
176
- #
177
- # object = []
178
- # matcher = change(object, :length).by_at_most(1)
179
- # matcher.match? { object << 1 } # => true
180
- #
181
- # object = "foo"
182
- # matcher = change(object, :to_s).from("foo").to("FOO")
183
- # matcher.match? { object.upcase! } # => true
184
- #
185
- # object = "foo"
186
- # matcher = change(object, :to_s).to("FOO")
187
- # matcher.match? { object.upcase! } # => true
188
- #
189
- # @param object [#object_id] An object.
190
- # @param method [Symbol] The name of a method.
191
- # @param args [Array] A list of arguments.
192
- # @param kwargs [Hash] A list of keyword arguments.
193
- #
194
- # @return [#match?] A change matcher.
195
- #
196
- # @api public
197
- def change(object, method, ...)
198
- ::Matchi::Change.new(object, method, ...)
199
- end
200
-
201
- # Satisfy matcher
202
- #
203
- # @example
204
- # matcher = satisfy { |value| value == 42 }
205
- # matcher.match? { 42 } # => true
206
- #
207
- # @param expected [Proc] A block of code.
208
- #
209
- # @return [#match?] A satisfy matcher.
210
- #
211
- # @api public
212
- def satisfy(&)
213
- ::Matchi::Satisfy.new(&)
214
- end
215
-
216
- private
217
-
218
- # Predicate matcher, or default method missing behavior.
219
- #
220
- # @example Empty predicate matcher
221
- # matcher = be_empty
222
- # matcher.match? { [] } # => true
223
- # matcher.match? { [4] } # => false
224
- def method_missing(name, ...)
225
- return super unless predicate_matcher_name?(name)
226
-
227
- ::Matchi::Predicate.new(name, ...)
228
- end
229
-
230
- # :nocov:
231
-
232
- # Hook method to return whether the obj can respond to id method or not.
233
- def respond_to_missing?(name, include_private = false)
234
- predicate_matcher_name?(name) || super
235
- end
236
-
237
- # :nocov:
238
-
239
- # Predicate matcher name detector.
240
- #
241
- # @param name [Array, Symbol] The name of a potential predicate matcher.
242
- #
243
- # @return [Boolean] Indicates if it is a predicate matcher name or not.
244
- def predicate_matcher_name?(name)
245
- name.start_with?("be_", "have_") && !name.end_with?("!", "?")
246
- end
247
- end
248
- end
249
- end
250
- end
@@ -1,115 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "expresenter"
4
- require "test_tube"
5
-
6
- require_relative File.join("..", "logger")
7
-
8
- module RSpec
9
- module Clone
10
- module ExpectationTarget
11
- # Abstract expectation target base class.
12
- #
13
- # @note `RSpec::Clone::ExpectationTarget::Base` is not intended to be
14
- # instantiated directly by users. Use `expect` instead.
15
- class Base
16
- # Instantiate a new expectation target.
17
- #
18
- # @param input [#object_id, Proc] The code to evaluate.
19
- def initialize(input)
20
- @input = input
21
- end
22
-
23
- # Runs the given expectation, passing if `matcher` returns true.
24
- #
25
- # @example _Absolute requirement_ definition
26
- # expect { "foo".upcase }.to eq("foo")
27
- #
28
- # @param matcher [#match?] The matcher.
29
- #
30
- # @raise (see #result)
31
- # @return (see #result)
32
- #
33
- # @api public
34
- def to(matcher)
35
- absolute_requirement(matcher:, negate: false)
36
- end
37
-
38
- # Runs the given expectation, passing if `matcher` returns false.
39
- #
40
- # @example _Absolute prohibition_ definition
41
- # expect { "foo".size }.not_to be(4)
42
- #
43
- # @param (see #to)
44
- #
45
- # @raise (see #result)
46
- # @return (see #result)
47
- #
48
- # @api public
49
- def not_to(matcher)
50
- absolute_requirement(matcher:, negate: true)
51
- end
52
-
53
- protected
54
-
55
- # @param test [::TestTube::Base] The state of the experiment.
56
- # @param matcher [#match?] The matcher.
57
- # @param negate [Boolean] The assertion is positive or negative.
58
- #
59
- # @return [nil] Write a message to STDOUT.
60
- #
61
- # @raise [SystemExit] Terminate execution immediately by calling
62
- # `Kernel.exit(false)` with a failure message written to STDERR.
63
- def absolute_requirement(test, matcher:, negate:)
64
- result(
65
- passed?(test),
66
- actual: test.actual,
67
- error: test.error,
68
- got: test.got,
69
- matcher:,
70
- negate:
71
- )
72
- end
73
-
74
- # Code experiment result.
75
- #
76
- # @param test [::TestTube::Base] The state of the experiment.
77
- #
78
- # @see https://github.com/fixrb/test_tube
79
- #
80
- # @return [Boolean] The result of the test (passed or failed).
81
- def passed?(test)
82
- test.got.equal?(true)
83
- end
84
-
85
- # :nocov:
86
-
87
- # @param passed [Boolean] The high expectation passed or failed.
88
- # @param actual [#object_id] The actual value.
89
- # @param error [Exception, nil] Any raised exception.
90
- # @param got [Boolean, nil] Any returned value.
91
- # @param matcher [#match?] The matcher.
92
- # @param negate [Boolean] The assertion is positive or negative.
93
- #
94
- # @return [nil] Write a message to STDOUT.
95
- #
96
- # @raise [SystemExit] Terminate execution immediately by calling
97
- # `Kernel.exit(false)` with a failure message written to STDERR.
98
- def result(passed, actual:, error:, got:, matcher:, negate:)
99
- Logger.passed_spec ::Expresenter.call(passed).with(
100
- actual:,
101
- definition: matcher.to_s,
102
- error:,
103
- got:,
104
- negate:,
105
- level: :MUST
106
- )
107
- rescue ::Expresenter::Fail => e
108
- Logger.failed_spec(e)
109
- end
110
-
111
- # :nocov:
112
- end
113
- end
114
- end
115
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module RSpec
6
- module Clone
7
- module ExpectationTarget
8
- # Wraps the target of an expectation with a block.
9
- #
10
- # @example
11
- # expect { something } # => ExpectationTarget::Block wrapping something
12
- #
13
- # # used with `to`
14
- # expect { actual }.to be(42)
15
- #
16
- # # with `not_to`
17
- # expect { actual }.not_to be(4)
18
- #
19
- # @note `RSpec::Clone::ExpectationTarget::Block` is not intended to be
20
- # instantiated directly by users. Use `expect` instead.
21
- class Block < Base
22
- protected
23
-
24
- # @param matcher [#match?] The matcher.
25
- # @param negate [Boolean] The assertion is positive or negative.
26
- #
27
- # @return (see Base#absolute_requirement)
28
- #
29
- # @raise (see Base#absolute_requirement)
30
- def absolute_requirement(matcher:, negate:)
31
- super(
32
- ::TestTube.invoke(matcher:, negate:, &@input),
33
- matcher:,
34
- negate:
35
- )
36
- end
37
- end
38
- end
39
- end
40
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module RSpec
6
- module Clone
7
- module ExpectationTarget
8
- # Wraps the target of an expectation with a value.
9
- #
10
- # @example
11
- # expect(something) # => ExpectationTarget::Value wrapping something
12
- #
13
- # # used with `to`
14
- # expect(actual).to be(42)
15
- #
16
- # # with `not_to`
17
- # expect(actual).not_to be(4)
18
- #
19
- # @note `RSpec::Clone::ExpectationTarget::Value` is not intended to be
20
- # instantiated directly by users. Use `expect` instead.
21
- class Value < Base
22
- protected
23
-
24
- # @param matcher [#match?] The matcher.
25
- # @param negate [Boolean] The assertion is positive or negative.
26
- #
27
- # @return (see Base#absolute_requirement)
28
- #
29
- # @raise (see Base#absolute_requirement)
30
- def absolute_requirement(matcher:, negate:)
31
- super(
32
- ::TestTube.pass(@input, matcher:, negate:),
33
- matcher:,
34
- negate:
35
- )
36
- end
37
- end
38
- end
39
- end
40
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RSpec
4
- module Clone
5
- # Send log messages to the console.
6
- module Logger
7
- # @param report [::Expresenter::Pass] Passed expectation result presenter.
8
- #
9
- # @see https://github.com/fixrb/expresenter
10
- #
11
- # @return [nil] Add a colored message to `$stdout`.
12
- def self.passed_spec(report)
13
- puts report.colored_string
14
- end
15
-
16
- # :nocov:
17
-
18
- # @param report [::Expresenter::Fail] Failed expectation result presenter.
19
- #
20
- # @see https://github.com/fixrb/expresenter
21
- #
22
- # @raise [SystemExit] Terminate execution immediately with message.
23
- def self.failed_spec(report)
24
- abort report.colored_string
25
- end
26
-
27
- # :nocov:
28
-
29
- # The Ruby source filename and line number containing this method or nil
30
- # if this method was not defined in Ruby (i.e. native).
31
- #
32
- # @param filename [String, nil] The Ruby source filename.
33
- # @param line [Integer, nil] The Ruby source line number.
34
- #
35
- # @return [String] The Ruby source filename and line number associated with
36
- # the evaluated spec.
37
- def self.source(filename, line)
38
- puts [filename, line].compact.join(":")
39
- end
40
- end
41
- end
42
- end