Skip to content

Commit

Permalink
processing step positions request getgauge/gauge-vscode#85 #47
Browse files Browse the repository at this point in the history
  • Loading branch information
BugDiver committed Jan 15, 2018
1 parent 3977eae commit 9c060a1
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/message_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MessageProcessor
@processors[Messages::Message::MessageType::StepNameRequest] = method(:process_step_name_request)
@processors[Messages::Message::MessageType::RefactorRequest] = method(:refactor_step)
@processors[Messages::Message::MessageType::CacheFileRequest] = method(:process_cache_file_request)
# @processors[Messages::Message::MessageType::StepPositionsRequest] = method(:process_step_positions_request)
@processors[Messages::Message::MessageType::StepPositionsRequest] = method(:process_step_positions_request)

def self.is_valid_message(message)
return @processors.has_key? message.messageType
Expand Down
12 changes: 12 additions & 0 deletions lib/method_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ def self.multiple_implementation?(step_value)
@@steps_map[step_value][:locations].length > 1
end

def self.step_positions(file)
step_positions = []
@@steps_map.each_pair do |step, info|
info[:locations].each do |location|
if location[:file] == file
step_positions.push({stepValue: step, span: location[:span]})
end
end
end
step_positions
end

private
@@steps_map = Hash.new
@@steps_with_aliases = []
Expand Down
2 changes: 1 addition & 1 deletion lib/processors/step_name_request_processor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015 ThoughtWorks, Inc.
# Copyright 2018 ThoughtWorks, Inc.

# This file is part of Gauge-Ruby.

Expand Down
35 changes: 35 additions & 0 deletions lib/processors/step_positions_request_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2018 ThoughtWorks, Inc.

# This file is part of Gauge-Ruby.

# Gauge-Ruby is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# Gauge-Ruby is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with Gauge-Ruby. If not, see <http://www.gnu.org/licenses/>.

module Gauge
module Processors
def process_step_positions_request(message)
file = message.stepPositionsRequest.filePath
positions = MethodCache.step_positions(file)
p = create_step_position_messages(positions)
r = Messages::StepPositionsResponse.new(:stepPositions => p)
Messages::Message.new(:messageType => Messages::Message::MessageType::StepPositionsResponse, :messageId => message.messageId, :stepPositionsResponse => r)
end

def create_step_position_messages(positions)
positions.map do |p|
span = Gauge::Messages::Span.new(:start => p[:span].begin.line, :end => p[:span].end.line, :startChar => p[:span].begin.column, :endChar => p[:span].end.column)
Messages::StepPositionsResponse::StepPosition.new(:stepValue => p[:stepValue], :span => span)
end
end
end
end
54 changes: 54 additions & 0 deletions spec/step_positions_processor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2018 ThoughtWorks, Inc.

# This file is part of Gauge-Ruby.

# Gauge-Ruby is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# Gauge-Ruby is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with Gauge-Ruby. If not, see <http://www.gnu.org/licenses/>.

describe Gauge::Processors do
let(:message) {double('message')}
before(:each) {
Gauge::MethodCache.clear
}
context '.process_step_positions_request' do
describe 'should return step positions for a given file' do
before {
content = "step 'foo' do\n\tputs 'hello'\nend\n"
ast = Gauge::CodeParser.code_to_ast content
Gauge::StaticLoader.load_steps 'foo.rb', ast
allow(message).to receive_message_chain(:stepPositionsRequest, :filePath => 'foo.rb')
allow(message).to receive(:messageId) {1}
}
it 'should give step positions' do
positions = subject.process_step_positions_request(message).stepPositionsResponse.stepPositions
expect(positions[0].stepValue).to eq 'foo'
expect(positions[0].span.start).to eq 1
expect(positions[0].span.end).to eq 3
end
end

describe 'should return empty step positions for a given file' do
before {
content = "step 'foo' do\n\tputs 'hello'\nend\n"
ast = Gauge::CodeParser.code_to_ast content
Gauge::StaticLoader.load_steps 'foo.rb', ast
allow(message).to receive_message_chain(:stepPositionsRequest, :filePath => 'bar.rb')
allow(message).to receive(:messageId) {1}
}
it 'should give empty for not loaded file' do
positions = subject.process_step_positions_request(message).stepPositionsResponse.stepPositions
expect(positions.empty?).to eq true
end
end
end
end

0 comments on commit 9c060a1

Please sign in to comment.