magic-support 0.2.1 → 0.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b309e0c4212931c949a0cfe73bc7aea984ad4eb4d70c8dbadf433e224147d9f9
4
- data.tar.gz: bf84fd93b7fcfa2ac15a4b89e440cd3ee1cae63f779ca67d6b74244aa28b92e4
3
+ metadata.gz: 5ba52d68fd1ed326db4b48fea592086c453fbccb92092460b79f51bb8c368278
4
+ data.tar.gz: 6c07b4da16765a4ab27ed1cee7cdaf4c6202f2debb33d9cb266e8e1272c93e72
5
5
  SHA512:
6
- metadata.gz: cbbf0b2f3347a495e5265391b529a42c42692e8d35ee7e5ff532faecd1e55c8602443df7524318bf5bd9f39efba608cf4ef344f4d7b0a679839062ef4fe01339
7
- data.tar.gz: b62b4ec44998f16f0bc04293938d93883157c276d4f2ba537fc60abf38f62990f59e1aa58652391846dd10184f114c1306606d90dc6727b803fb75401919f727
6
+ metadata.gz: ec575506b2dea96321c08c9aa75d2ccf373c8b63d15cdbb0db9afd5c0f3f7483d9f13052f2c81a8f326d3ffdb1dd8c026cef86393f231bfb3eb70598c50d4324
7
+ data.tar.gz: c51019c9da5d4cc1a62cd775e80a417874ccad09092204aba21774cc8663caba5395ddb8aa5f918970f16409e41a486acb13d89ff042465cda5d48f7aaf28a53
data/CHANGELOG.md CHANGED
@@ -1,3 +1,34 @@
1
+ ## [0.3.1] — 2025-05-18
2
+
3
+ ### RSpec
4
+
5
+ #### Method specs
6
+
7
+ ##### Fixed
8
+
9
+ - Specs for methods with punctuation suffixes got broken in v0.3.0.
10
+
11
+
12
+ ## [0.3.0] — 2025-05-18
13
+
14
+ ### RSpec
15
+
16
+ #### Method specs
17
+
18
+ ##### Changed
19
+
20
+ - Set tested method’s owner to an overridden subject if any.
21
+
22
+ ##### Added
23
+
24
+ - Support for nesting of method specs.
25
+ - Support for named overridden subjects to follow the original RSpec API.
26
+
27
+ ##### Fixed
28
+
29
+ - `receiver` should always be equal to `subject.receiver`.
30
+
31
+
1
32
  ## [0.2.1] — 2025-05-13
2
33
 
3
34
  ### RSpec
data/README.md CHANGED
@@ -219,6 +219,12 @@ RSpec.describe MyModule do
219
219
  end
220
220
  ```
221
221
 
222
+ ##### Nesting
223
+
224
+ One may nest method specs.
225
+ This could be useful when a method being tested affects other methods.
226
+ See the spec for examples.
227
+
222
228
  ## Development
223
229
 
224
230
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Magic
4
4
  module Support
5
- VERSION = '0.2.1'
5
+ VERSION = '0.3.1'
6
6
  end
7
7
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpec
4
+ module Method # :nodoc:
5
+ # Adds the `its_result` to RSpec method example groups.
6
+ # Included by default.
7
+ module Its
8
+ def its_result(*args, &) = its(args, &)
9
+ end
10
+
11
+ ExampleGroup::ClassMethods.include Its
12
+ end
13
+ end
data/lib/rspec/method.rb CHANGED
@@ -11,7 +11,7 @@ module RSpec # :nodoc:
11
11
  }
12
12
  .map { |name, pattern| "(?<#{name}>#{pattern})" }
13
13
  .join
14
- .then { /(?:^|\s)#{_1}(?:$|\s)/ } # may be surrounded by some text
14
+ .then { /(?:^|\s)#{_1}(?:$|\s)?/ } # may be surrounded by some text
15
15
 
16
16
  # @api public
17
17
  # Container module for method specs.
@@ -21,45 +21,57 @@ module RSpec # :nodoc:
21
21
  included do
22
22
  metadata[:type] ||= :method
23
23
 
24
- description.match(REFERENCE) in { scope:, name: } or
25
- raise ArgumentError, "'#{description}' doesn't look like a method reference"
26
-
27
- subject_method = instance_method :subject
24
+ setup_method_context
25
+ end
28
26
 
29
- while subject_method.owner.then { not _1.respond_to? :metadata or _1.metadata[:method] }
30
- subject_method = subject_method.super_method or break
27
+ class_methods do
28
+ def subject(name = nil, &)
29
+ if name
30
+ alias_method :__subject__, let(name, &)
31
+
32
+ self::NamedSubjectPreventSuper.__send__ :define_method, name do
33
+ raise NotImplementedError, '`super` in named subjects is not supported'
34
+ end
35
+ else
36
+ let :__subject__, &
37
+ end
31
38
  end
32
39
 
33
- if subject_method
34
- let(:__subject__) { subject_method.bind_call self }
35
- else
36
- let(:__subject__) { method(:subject).super_method[] }
37
- end
40
+ private
38
41
 
39
- let :receiver do
40
- case [ scope, __subject__ ]
41
- in '#', receiver
42
- receiver
43
- in '.', Module => receiver
44
- receiver
45
- in '.', receiver
46
- receiver.class
47
- end
48
- end
42
+ def setup_method_context
43
+ description.match(REFERENCE) in { scope:, name: } or
44
+ raise ArgumentError, "'#{description}' doesn't look like a method reference"
49
45
 
50
- let(:method_name) { name.to_sym }
51
- end
46
+ backup_subject
52
47
 
53
- class_methods do
54
- def subject(&)
55
- prepend Module.new {
56
- define_method(:subject) { super().unbind.bind instance_eval(&) }
57
- }
48
+ let :receiver do
49
+ case [ scope, __subject__ ]
50
+ in '#', receiver
51
+ receiver
52
+ in '.', Module => receiver
53
+ receiver
54
+ in '.', receiver
55
+ receiver.class
56
+ end
57
+ end
58
+
59
+ let(:method_name) { name.to_sym }
58
60
  end
59
61
 
60
- next unless defined? RSpec::Its
62
+ def backup_subject
63
+ subject_method = instance_method :subject
64
+
65
+ while subject_method.owner.then { not _1.respond_to? :metadata or _1.metadata[:method] }
66
+ subject_method = subject_method.super_method or break
67
+ end
61
68
 
62
- def its_result(*args, &) = its(args, &)
69
+ if subject_method
70
+ let(:__subject__) { subject_method.bind_call self }
71
+ else
72
+ let(:__subject__) { method(:subject).super_method[] }
73
+ end
74
+ end
63
75
  end
64
76
 
65
77
  def subject
@@ -75,6 +87,8 @@ module RSpec # :nodoc:
75
87
  end
76
88
  end
77
89
 
90
+ require_relative 'method/its' if defined? Its
91
+
78
92
  shared_context :method, description: Method::REFERENCE do
79
93
  include Method::ExampleGroup
80
94
 
@@ -94,6 +108,12 @@ module RSpec # :nodoc:
94
108
  end
95
109
  end
96
110
 
111
+ shared_context :nested_method, full_description: /(.*#{Method::REFERENCE}){2,}/ do
112
+ next unless metadata[:description].match? Method::REFERENCE
113
+
114
+ setup_method_context
115
+ end
116
+
97
117
  configure do
98
118
  _1.backtrace_exclusion_patterns << %r'/lib/rspec/method'
99
119
  end
@@ -0,0 +1,7 @@
1
+ module RSpec
2
+ module Method
3
+ module Its
4
+ def its_result: (*untyped) -> void
5
+ end
6
+ end
7
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magic-support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Senko
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-05-13 00:00:00.000000000 Z
10
+ date: 2025-05-18 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activesupport
@@ -46,6 +46,7 @@ files:
46
46
  - lib/magic/support/authors.rb
47
47
  - lib/magic/support/version.rb
48
48
  - lib/rspec/method.rb
49
+ - lib/rspec/method/its.rb
49
50
  - lib/rubygems/author.rb
50
51
  - sig/lib/magic/support/author.rbs
51
52
  - sig/magic/core_ext.rbs
@@ -53,13 +54,14 @@ files:
53
54
  - sig/magic/loader.rbs
54
55
  - sig/magic/support.rbs
55
56
  - sig/rspec/method.rbs
57
+ - sig/rspec/method/its.rbs
56
58
  - sig/rubygems/author.rbs
57
59
  homepage: https://github.com/Alexander-Senko/magic-support
58
60
  licenses:
59
61
  - MIT
60
62
  metadata:
61
63
  source_code_uri: https://github.com/Alexander-Senko/magic-support
62
- changelog_uri: https://github.com/Alexander-Senko/magic-support/blob/v0.2.1/CHANGELOG.md
64
+ changelog_uri: https://github.com/Alexander-Senko/magic-support/blob/v0.3.1/CHANGELOG.md
63
65
  rdoc_options: []
64
66
  require_paths:
65
67
  - lib