Skip to content

Commit

Permalink
Implement RubyVMSpecWalker#on_blocks_in_examples
Browse files Browse the repository at this point in the history
Last hook!
  • Loading branch information
lekemula committed May 16, 2024
1 parent 080f9e6 commit 982914b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 39 deletions.
24 changes: 12 additions & 12 deletions lib/solargraph/rspec/ruby_vm_spec_walker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ def walk!
handler.call(block_ast)
end

# # @param blocks_in_examples [Parser::AST::Node]
# each_block(block_ast.children[2]) do |blocks_in_examples|
# @handlers[:on_blocks_in_examples].each do |handler|
# handler.call(blocks_in_examples)
# end
# end
# @param blocks_in_examples [RubyVM::AbstractSyntaxTree::Node]
each_block(block_ast.children[1]) do |blocks_in_examples|
@handlers[:on_blocks_in_examples].each do |handler|
handler.call(blocks_in_examples)
end
end
end

walker.on :ITER do |block_ast|
Expand All @@ -278,12 +278,12 @@ def walk!
handler.call(block_ast)
end

# # @param blocks_in_examples [Parser::AST::Node]
# each_block(block_ast.children[2]) do |blocks_in_examples|
# @handlers[:on_blocks_in_examples].each do |handler|
# handler.call(blocks_in_examples)
# end
# end
# @param blocks_in_examples [RubyVM::AbstractSyntaxTree::Node]
each_block(block_ast.children[1]) do |blocks_in_examples|
@handlers[:on_blocks_in_examples].each do |handler|
handler.call(blocks_in_examples)
end
end
end

walker.walk
Expand Down
54 changes: 29 additions & 25 deletions spec/solargraph/rspec/ruby_vm_spec_walker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,31 +271,35 @@ def walk_code(code)
end
end

# describe '#on_blocks_in_examples' do
# it 'yields each context block' do
# code = <<~RUBY
# RSpec.describe SomeClass, type: :model do
# it 'does something' do
# expect { subject }.to change { SomeClass.count }.by(1)
# end

# context 'when something' do
# it 'does something' do
# do_something { subject }
# end
# end
# end
# RUBY
describe '#on_blocks_in_examples' do
it 'yields each context block' do
code = <<~RUBY
RSpec.describe SomeClass, type: :model do
it 'does something' do
expect { subject }.to change { SomeClass.count }.by(1)
end
# called = 0
# @param walker [Solargraph::Rspec::RubyVMSpecWalker]
# walk_code(code) do |walker|
# walker.on_blocks_in_examples do |_|
# called += 1
# end
# end
context 'when something' do
around do
do_something { subject }
end
# expect(called).to eq(3)
# end
# end
it 'does something' do
do_something_too { subject }
end
end
end
RUBY

called = 0
# @param walker [Solargraph::Rspec::SpecWalker]
walk_code(code) do |walker|
walker.on_blocks_in_examples do |_|
called += 1
end
end

expect(called).to eq(4)
end
end
end
8 changes: 6 additions & 2 deletions spec/solargraph/rspec/spec_walker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,13 @@ def walk_code(code)
end
context 'when something' do
it 'does something' do
around do
do_something { subject }
end
it 'does something' do
do_something_too { subject }
end
end
end
RUBY
Expand All @@ -188,7 +192,7 @@ def walk_code(code)
end
end

expect(called).to eq(3)
expect(called).to eq(4)
end
end
end

0 comments on commit 982914b

Please sign in to comment.