Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Extend reserved name check #2886

Merged
merged 4 commits into from Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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 lib/rspec/core/memoized_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,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 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