Skip to content

Commit

Permalink
[core] Merge pull request rspec/rspec-core#2886 from Liberatys/extend…
Browse files Browse the repository at this point in the history
…-reserved-memoized-helper-names

Extend reserved name check

---
This commit was imported from rspec/rspec-core@6f8ee75.
  • Loading branch information
JonRowe committed Apr 28, 2021
1 parent 016e93a commit a47cf48
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions rspec-core/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Enhancements:

* Improve pluralisation of words ending with `s` (like process). (Joshua Pinter, #2779)
* Add ordering by file modification time (most recent first). (Matheus Richard, #2778)
* Extend reserved memoized helper name checking for #let and #subject. (Nick Flückiger, #2886)

Bug fixes:

Expand Down
10 changes: 7 additions & 3 deletions rspec-core/lib/rspec/core/memoized_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,13 @@ def let(name, &block)
# We have to pass the block directly to `define_method` to
# allow it to use method constructs like `super` and `return`.
raise "#let or #subject called without a block" if block.nil?
raise(
"#let or #subject called with a reserved name #initialize"
) if :initialize == name

# A list of reserved words that can't be used as a name for a memoized helper
# Matches for both symbols and passed strings
if [:initialize, :to_s].include?(name.to_sym)
raise ArgumentError, "#let or #subject called with reserved name `#{name}`"
end

our_module = MemoizedHelpers.module_for(self)

# If we have a module clash in our helper module
Expand Down
24 changes: 21 additions & 3 deletions rspec-core/spec/rspec/core/memoized_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,28 @@ def count
end.to raise_error(/#let or #subject called without a block/)
end

it 'raises an error when attempting to define a reserved method name' do
it 'raises an error when attempting to define a reserved name #initialize' do
expect do
RSpec.describe { let(:initialize) { true }}
end.to raise_error(/#let or #subject called with a reserved name #initialize/)
RSpec.describe { let(:initialize) { true } }
end.to raise_error(/#let or #subject called with reserved name `initialize`/)
end

it 'raises an error when attempting to define a reserved name #initialize as a string' do
expect do
RSpec.describe { let('initialize') { true } }
end.to raise_error(/#let or #subject called with reserved name `initialize`/)
end

it 'raises an error when attempting to define a reserved name #to_s' do
expect do
RSpec.describe { let(:to_s) { true } }
end.to raise_error(/#let or #subject called with reserved name `to_s`/)
end

it 'raises an error when attempting to define a reserved name #to_s as a string' do
expect do
RSpec.describe { let('to_s') { true } }
end.to raise_error(/#let or #subject called with reserved name `to_s`/)
end

let(:a_value) { "a string" }
Expand Down

0 comments on commit a47cf48

Please sign in to comment.