Skip to content

Commit

Permalink
processing step positions request getgauge/gauge-vscode#109 #19
Browse files Browse the repository at this point in the history
  • Loading branch information
BugDiver committed Jan 25, 2018
1 parent 485cdca commit f9e06ce
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 20 deletions.
9 changes: 6 additions & 3 deletions getgauge/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from getgauge.connection import read_message, send_message
from getgauge.executor import set_response_values, execute_method, run_hook
from getgauge.impl_loader import load_impls
from getgauge.messages.messages_pb2 import Message
from getgauge.messages.spec_pb2 import Parameter
from getgauge.messages.messages_pb2 import Message, StepPositionsResponse
from getgauge.messages.spec_pb2 import Parameter, Span
from getgauge.python import Table, create_execution_context_from, DataStoreFactory
from getgauge.refactor import refactor_step
from getgauge.registry import registry, _MessagesStore
Expand Down Expand Up @@ -123,7 +123,10 @@ def _cache_file(request, response, socket):


def _step_positions(request, response, socket):
pass
positions = registry.get_step_positions(request.stepPositionsRequest.filePath)
create_pos = lambda p: StepPositionsResponse.StepPosition(**{'stepValue': p['stepValue'], 'span': Span(**p['span'])})
response.messageType = Message.StepPositionsResponse
response.stepPositionsResponse.stepPositions.extend([create_pos(x) for x in positions])


def _kill_runner(request, response, socket):
Expand Down
8 changes: 7 additions & 1 deletion getgauge/registry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import sys
import re
import sys
import tempfile
from subprocess import call

Expand Down Expand Up @@ -116,6 +116,12 @@ def is_continue_on_failure(self, func, exception):
return True
return False

def get_step_positions(self, file_name):
positions = []
for step, infos in self.__steps_map.items():
positions = positions + [{'stepValue': step, 'span': i.span} for i in infos if i.file_name == file_name]
return positions

def clear(self):
self.__steps_map, self.__continue_on_failures = {}, {}
for hook in Registry.hooks:
Expand Down
10 changes: 5 additions & 5 deletions getgauge/static_loader.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import re

from os import path
from redbaron import RedBaron

from baron.utils import BaronError
from redbaron import RedBaron

from getgauge.util import *
from getgauge.registry import registry
from getgauge.util import *


def load_file(content, file_name):
def load_steps(content, file_name):
try:
red = RedBaron(content)
for func in red.find_all('def'):
Expand Down Expand Up @@ -38,7 +38,7 @@ def load_files(step_impl_dir):
file_path = os.path.join(step_impl_dir, f)
if f.endswith('.py'):
impl_file = open(file_path, 'r+')
load_file(impl_file.read(), file_path)
load_steps(impl_file.read(), file_path)
impl_file.close()
elif path.isdir(file_path):
load_files(file_path)
4 changes: 2 additions & 2 deletions python.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
]
},
"gaugeVersionSupport": {
"minimum": "0.8.0",
"minimum": "0.9.8",
"maximum": ""
},
"lspLangId" : "python"
"lspLangId": "python"
}
2 changes: 1 addition & 1 deletion start.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from getgauge import connection, processor
from getgauge.impl_loader import copy_skel_files
from getgauge.util import get_step_impl_dir
from getgauge.static_loader import load_files
from getgauge.util import get_step_impl_dir


def main():
Expand Down
19 changes: 19 additions & 0 deletions tests/test_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,25 @@ def test_Processor_refactor_request_when_multiple_impl_found(self):
self.assertEqual('Reason: Multiple Implementation found for `Step <a> with <b>`',
response.refactorResponse.error)

def test_Processor_step_position_request(self):
registry.add_step('Step <a> with <b>', 'func', 'foo.py',
{'start': 0, 'startChar': 0, 'end': 3, 'endChar': 10})
registry.add_step('Step 1', 'func', 'foo.py', {'start': 4, 'startChar': 0, 'end': 7, 'endChar': 10})

response = Message()
request = Message()
request.stepPositionsRequest.filePath = 'foo.py'

processors[Message.StepPositionsRequest](request, response, None)

self.assertEqual(Message.StepPositionsResponse, response.messageType)
self.assertEqual('', response.refactorResponse.error)

steps = [(p.stepValue, p.span.start) for p in response.stepPositionsResponse.stepPositions]

self.assertIn(('Step {} with {}', 0), steps)
self.assertIn(('Step 1', 4), steps)


def impl(a, b):
pass
Expand Down
18 changes: 17 additions & 1 deletion tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_Registry_get_multiple_impls(self):

self.assertEqual(
set([info.step_text
for info in registry.get_infos_for(parsed_step_text)]),
for info in registry.get_infos_for(parsed_step_text)]),
{infos[0]['text'], infos[1]['text']})

def test_Registry_before_suite(self):
Expand Down Expand Up @@ -313,6 +313,22 @@ def test_Registry_after_step_with_tags(self):
registry.after_step(['A']))
self.assertEqual([info1['func']], registry.after_step(['A', 'c']))

def test_Registry__step_positions_of_a_given_file(self):
infos = [{'text': 'Say <hello> to <getgauge>', 'func': 'func', 'file_name': 'foo.py', 'span': {'start': 1}},
{'text': 'Step 1', 'func': 'func1', 'file_name': 'bar.py', 'span': {'start': 3}}]

for info in infos:
registry.add_step(info['text'], info['func'], info['file_name'], info['span'])

positions = registry.get_step_positions('foo.py')

self.assertIn({'stepValue': 'Say {} to {}', 'span': {'start': 1}}, positions)
self.assertNotIn({'stepValue': 'Step 1', 'span': {'start': 3}}, positions)

positions = registry.get_step_positions('bar.py')

self.assertIn({'stepValue': 'Step 1', 'span': {'start': 3}}, positions)

def tearDown(self):
global registry
registry = Registry()
Expand Down
13 changes: 6 additions & 7 deletions tests/test_static_loader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

from getgauge.registry import registry
from getgauge.static_loader import load_file
from getgauge.static_loader import load_steps


class StaticLoaderTests(unittest.TestCase):
Expand All @@ -20,7 +20,7 @@ def print_word(word):
print(word)
"""
load_file(content, "foo.py")
load_steps(content, "foo.py")

self.assertTrue(registry.is_implemented("print hello"))
self.assertTrue(registry.is_implemented("print {}."))
Expand All @@ -39,13 +39,12 @@ def print_word(word):
print(word)
"""
load_file(content, "foo.py")
load_steps(content, "foo.py")

self.assertTrue(registry.is_implemented("print hello"))
self.assertTrue(registry.is_implemented("print {}."))
self.assertFalse(registry.is_implemented("some other decorator"))


def test_loader_populates_registry_with_duplicate_steps(self):
content = """
@step("print hello")
Expand All @@ -58,7 +57,7 @@ def print_word():
print("hello")
"""
load_file(content, "foo.py")
load_steps(content, "foo.py")
self.assertTrue(registry.has_multiple_impls("print hello"))

def test_loader_does_not_populate_registry_for_content_having_parse_error(self):
Expand All @@ -68,7 +67,7 @@ def print():
print(.__str_())
"""
load_file(content, "foo.py")
load_steps(content, "foo.py")

self.assertFalse(registry.is_implemented("print hello"))

Expand All @@ -79,7 +78,7 @@ def print():
print("hello")
"""
load_file(content, "foo.py")
load_steps(content, "foo.py")

self.assertTrue(registry.is_implemented("say hello"))
self.assertTrue(registry.get_info_for("say hello").has_alias)
Expand Down

0 comments on commit f9e06ce

Please sign in to comment.