Skip to content

Commit

Permalink
Added example_methods configuration option
Browse files Browse the repository at this point in the history
Merge pull request #6 from mskog/configuration_example_methods.

Fixes #5
  • Loading branch information
lekemula authored Jul 10, 2024
2 parents c86ff34 + ccd2d2d commit 49baac4
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Added `example_methods` as a configuration option to add your own example methods

Add here...

## v0.2.2 - 2024-06-23
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ rspec:
- let_it_be
```
If you have your own custom `example`-like methods like `it`, you can add them to your `.solargraph.yml` file like this:

```yaml
# .solargraph.yml
# ...
rspec:
example_methods:
- my_it
```

This is useful if you use gems like [rspec-given](https://github.com/rspec-given/rspec-given) which introduces its own `let` and `example` methods.


### Gem completions

Solargraph utilizes the YARD documentation to provide code completion. If you want to have completion for gems in your project, you can generate YARD documentation for them ([Read more](https://solargraph.org/guides/yard)).
Expand Down
10 changes: 10 additions & 0 deletions lib/solargraph/rspec/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def let_methods
(Rspec::LET_METHODS + additional_let_methods).map(&:to_sym)
end

# @return [Array<Symbol>]
def example_methods
(Rspec::EXAMPLE_METHODS + additional_example_methods).map(&:to_sym)
end

private

# @return [Hash]
Expand All @@ -32,6 +37,11 @@ def additional_let_methods
(rspec_raw_data['let_methods'] || []).map(&:to_sym)
end

# @return [Array<Symbol>]
def additional_example_methods
(rspec_raw_data['example_methods'] || []).map(&:to_sym)
end

# @return [Hash]
def raw_data
@solargraph_config.raw_data
Expand Down
2 changes: 1 addition & 1 deletion lib/solargraph/rspec/spec_walker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def walk!
end

walker.on :ITER do |block_ast|
next unless NodeTypes.a_example_block?(block_ast)
next unless NodeTypes.a_example_block?(block_ast, config)

@handlers[:on_example_block].each do |handler|
handler.call(PinFactory.build_location_range(block_ast))
Expand Down
5 changes: 3 additions & 2 deletions lib/solargraph/rspec/spec_walker/node_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ def self.a_subject_block?(block_ast)
end

# @param ast [RubyVM::AbstractSyntaxTree::Node]
# @param config [Config]
# @return [Boolean]
def self.a_example_block?(block_ast)
Solargraph::Rspec::EXAMPLE_METHODS.include?(method_with_block_name(block_ast))
def self.a_example_block?(block_ast, config)
config.example_methods.map(&:to_s).include?(method_with_block_name(block_ast))
end

# @param ast [RubyVM::AbstractSyntaxTree::Node]
Expand Down
44 changes: 44 additions & 0 deletions spec/solargraph/rspec/convention_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,50 @@ class MyClass; end
expect(completion_at(filename, [3, 31])).to include('something')
end
end

describe 'example_methods' do
before(:each) do
Solargraph::Rspec::Convention.instance_variable_set(:@config, nil)
@global_path = File.realpath(Dir.mktmpdir)
@orig_env = ENV.fetch('SOLARGRAPH_GLOBAL_CONFIG', nil)
ENV['SOLARGRAPH_GLOBAL_CONFIG'] = File.join(@global_path, '.solargraph.yml')

File.open(File.join(@global_path, '.solargraph.yml'), 'w') do |file|
configuration.each_line do |line|
file.puts line
end
end
end

after(:each) do
ENV['SOLARGRAPH_GLOBAL_CONFIG'] = @orig_env
FileUtils.remove_entry(@global_path)
end

let(:configuration) do
<<~YAML
rspec:
example_methods:
- my_example
YAML
end

it 'generates method for additional example-like methods' do
load_string filename, <<~RUBY
RSpec.describe SomeNamespace::Transaction, type: :model do
let(:transaction) { described_class.new }
my_example 'should do something' do
transaction
end
end
RUBY

assert_public_instance_method(api_map, 'RSpec::ExampleGroups::TestSomeNamespaceTransaction#transaction',
['undefined'])
expect(completion_at(filename, [4, 7])).to include('transaction')
end
end
end

describe 'error handling' do
Expand Down
9 changes: 5 additions & 4 deletions spec/solargraph/rspec/spec_walker/node_types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,21 @@ def parse(code)
end

describe '.a_example_block?' do
let(:config) { Solargraph::Rspec::Config.new }

it 'returns true for example block with name' do
node = parse('it("does something") { }')
expect(described_class.a_example_block?(node.children[2])).to be(true)
expect(described_class.a_example_block?(node.children[2], config)).to be(true)
end

it 'returns true for example block without name' do
node = parse('it { }')

expect(described_class.a_example_block?(node.children[2])).to be(true)
expect(described_class.a_example_block?(node.children[2], config)).to be(true)
end

it 'returns false for non-example block' do
node = parse('non_example { }')
expect(described_class.a_example_block?(node.children[2])).to be(false)
expect(described_class.a_example_block?(node.children[2], config)).to be(false)
end
end

Expand Down

0 comments on commit 49baac4

Please sign in to comment.