Skip to content

Commit

Permalink
Unit test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rwols committed May 15, 2020
1 parent fad74a5 commit 9a305ef
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 46 deletions.
8 changes: 4 additions & 4 deletions plugin/core/signature_help.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import html
from .logging import debug
from .typing import Tuple, Optional, List, Protocol
from .typing import Tuple, Optional, List, Protocol, Union, Dict


class ScopeRenderer(Protocol):
Expand All @@ -14,7 +14,7 @@ def punctuation(self, content: str) -> str:
def parameter(self, content: str, emphasize: bool = False) -> str:
...

def markdown(self, content: str) -> str:
def markup(self, content: Union[str, Dict[str, str]]) -> str:
...


Expand Down Expand Up @@ -130,7 +130,7 @@ def build_popup_content(self, renderer: ScopeRenderer) -> str:
formatted.append("</pre></div>")

if signature.documentation:
formatted.append("<p>{}</p>".format(renderer.markdown(signature.documentation)))
formatted.append("<p>{}</p>".format(renderer.markup(signature.documentation)))

if signature.parameters and self._active_parameter_index in range(0, len(signature.parameters)):
parameter = signature.parameters[self._active_parameter_index]
Expand All @@ -139,7 +139,7 @@ def build_popup_content(self, renderer: ScopeRenderer) -> str:
if parameter_documentation:
formatted.append("<p><b>{}</b>: {}</p>".format(
parameter_label,
renderer.markdown(parameter_documentation)))
renderer.markup(parameter_documentation)))

return "\n".join(formatted)

Expand Down
6 changes: 3 additions & 3 deletions plugin/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def text_document_range_formatting(view: sublime.View, region: sublime.Region) -
FORMAT_MARKUP_CONTENT = 0x4


def minihtml(view: sublime.View, content: Union[str, dict, list], allowed_formats: int) -> str:
def minihtml(view: sublime.View, content: Union[str, Dict[str, str], list], allowed_formats: int) -> str:
"""
Formats provided input content into markup accepted by minihtml.
Expand All @@ -201,12 +201,12 @@ def minihtml(view: sublime.View, content: Union[str, dict, list], allowed_format
:returns: Formatted string
"""
if allowed_formats == 0:
raise Exception("Must specify at least one format")
raise ValueError("Must specify at least one format")
parse_string = bool(allowed_formats & FORMAT_STRING)
parse_marked_string = bool(allowed_formats & FORMAT_MARKED_STRING)
parse_markup_content = bool(allowed_formats & FORMAT_MARKUP_CONTENT)
if parse_string and parse_marked_string:
raise Exception("Not allowed to specify FORMAT_STRING and FORMAT_MARKED_STRING at the same time")
raise ValueError("Not allowed to specify FORMAT_STRING and FORMAT_MARKED_STRING at the same time")
is_plain_text = True
result = ''
if (parse_string or parse_marked_string) and isinstance(content, str):
Expand Down
9 changes: 6 additions & 3 deletions plugin/signature_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .core.registry import session_for_view, client_from_session, LSPViewEventListener
from .core.settings import client_configs, settings
from .core.signature_help import create_signature_help, SignatureHelp
from .core.typing import List, Dict, Optional
from .core.typing import List, Dict, Optional, Union
from .core.views import text_document_position_params
from .core.views import FORMAT_STRING, FORMAT_MARKUP_CONTENT, minihtml

Expand All @@ -30,7 +30,7 @@ def punctuation(self, content: str) -> str:
def parameter(self, content: str, emphasize: bool = False) -> str:
return self._wrap_with_scope_style(content, "variable.parameter", emphasize)

def markdown(self, content: str) -> str:
def markup(self, content: Union[str, Dict[str, str]]) -> str:
return minihtml(self._view, content, allowed_formats=FORMAT_STRING | FORMAT_MARKUP_CONTENT)

def _wrap_with_scope_style(self, content: str, scope: str, emphasize: bool = False, escape: bool = True) -> str:
Expand Down Expand Up @@ -73,7 +73,10 @@ def initialize(self) -> None:
self._initialized = True

def on_modified_async(self) -> None:
pos = self.view.sel()[0].begin()
try:
pos = self.view.sel()[0].begin()
except IndexError:
return
# TODO: this will fire too often, narrow down using scopes or regex
if not self._initialized:
self.initialize()
Expand Down
38 changes: 26 additions & 12 deletions tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def ensure_document_listener_created(self) -> bool:
return True
return False

def test_sends_did_open_to_multiple_sessions(self) -> Generator:
window = sublime.active_window()
self.assertTrue(window)
self.config1 = make_stdio_test_config()
def setUp(self) -> Generator:
self.window = sublime.active_window()
self.assertTrue(self.window)
self.config1 = deepcopy(make_stdio_test_config())
self.config1.init_options["serverResponse"] = {
'capabilities': {
'textDocumentSync': {
Expand All @@ -46,16 +46,18 @@ def test_sends_did_open_to_multiple_sessions(self) -> Generator:
}
}
self.config2 = deepcopy(self.config1)
self.config2.name = "foobar"
self.config2.name = "TEST-2"
self.wm = windows.lookup(self.window)
add_config(self.config1)
add_config(self.config2)
self.wm = windows.lookup(window)
self.wm._configs.all.append(self.config1)
self.wm._configs.all.append(self.config2)
filename = expand(join("$packages", "LSP", "tests", "testfile.txt"), window)
open_view = window.find_open_file(filename)

def test_sends_did_open_to_multiple_sessions(self) -> Generator:
filename = expand(join("$packages", "LSP", "tests", "testfile.txt"), self.window)
open_view = self.window.find_open_file(filename)
close_test_view(open_view)
self.view = window.open_file(filename)
self.view = self.window.open_file(filename)
yield {"condition": lambda: not self.view.is_loading(), "timeout": TIMEOUT_TIME}
self.assertTrue(self.wm._configs.match_view(self.view))
# self.init_view_settings()
Expand All @@ -80,13 +82,17 @@ def test_sends_did_open_to_multiple_sessions(self) -> Generator:
self.view.run_command("insert", {"characters": "a"})
yield from self.await_message("textDocument/didChange")
status_string = self.view.get_status("lsp_clients")
self.assertEqual(set(s.strip() for s in status_string.split(',')), set(("TEST", "foobar")))
self.assertEqual(set(s.strip() for s in status_string.split(',')), set(("TEST", "TEST-2")))
close_test_view(self.view)
yield from self.await_message("textDocument/didClose")
remove_config(self.config2)
remove_config(self.config1)

def doCleanups(self) -> Generator:
self.wm.end_config_sessions(self.config1.name)
self.wm.end_config_sessions(self.config2.name)
if self.session1:
yield lambda: self.session1.client is None
if self.session2:
yield lambda: self.session2.client is None
close_test_view(self.view)
try:
remove_config(self.config2)
Expand All @@ -96,6 +102,14 @@ def doCleanups(self) -> Generator:
remove_config(self.config1)
except ValueError:
pass
try:
self.wm._configs.all.remove(self.config2)
except ValueError:
pass
try:
self.wm._configs.all.remove(self.config1)
except ValueError:
pass
yield from super().doCleanups()

def await_message(self, method: str) -> Generator:
Expand Down
38 changes: 14 additions & 24 deletions tests/test_signature_help.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from LSP.plugin.core.signature_help import (
create_signature_help, SignatureHelp, get_documentation,
parse_signature_information, ScopeRenderer, render_signature_label
)
from LSP.plugin.core.signature_help import create_signature_help
from LSP.plugin.core.signature_help import parse_signature_information
from LSP.plugin.core.signature_help import render_signature_label
from LSP.plugin.core.signature_help import ScopeRenderer
from LSP.plugin.core.signature_help import SignatureHelp
from LSP.plugin.core.typing import Union, Dict
import unittest

signature = {
Expand Down Expand Up @@ -58,8 +60,8 @@
<variable.parameter emphasize>value</variable.parameter>: int
<punctuation>)</punctuation> -&gt; None</entity.name.function>
</pre></div>
<p>The default function for foobaring</p>
<p><b>value</b>: A number to foobar on</p>"""
<p>{'value': 'The default function for foobaring'}</p>
<p><b>value</b>: {'value': 'A number to foobar on'}</p>"""

MISSING_LABEL_SIGNATURE = """<div class="highlight"><pre>
Expand All @@ -75,8 +77,8 @@
<variable.parameter emphasize>value</variable.parameter>: int
<punctuation>)</punctuation> -&gt; None</entity.name.function>
</pre></div>
<p>The default function for foobaring</p>
<p><b>value</b>: A number to foobar on</p>"""
<p>{'value': 'The default function for foobaring'}</p>
<p><b>value</b>: {'value': 'A number to foobar on'}</p>"""


OVERLOADS_SECOND = """**2** of **2** overloads (use the ↑ ↓ keys to navigate):
Expand All @@ -89,8 +91,8 @@
\n<variable.parameter>multiplier</variable.parameter>: int
<punctuation>)</punctuation> -&gt; None</entity.name.function>
</pre></div>
<p>Foobaring with a multiplier</p>
<p><b>value</b>: A number to foobar on</p>"""
<p>{'value': 'Foobaring with a multiplier'}</p>
<p><b>value</b>: {'value': 'A number to foobar on'}</p>"""

OVERLOADS_SECOND_SECOND_PARAMETER = """**2** of **2** overloads (use the ↑ ↓ keys to navigate):
Expand All @@ -102,7 +104,7 @@
\n<variable.parameter emphasize>multiplier</variable.parameter>: int
<punctuation>)</punctuation> -&gt; None</entity.name.function>
</pre></div>
<p>Foobaring with a multiplier</p>
<p>{'value': 'Foobaring with a multiplier'}</p>
<p><b>multiplier</b>: Change foobar to work on larger increments</p>"""


Expand All @@ -129,25 +131,13 @@ def parameter(self, content: str, emphasize: bool = False) -> str:
def _wrap_with_scope_style(self, content: str, scope: str, emphasize: bool = False) -> str:
return '\n<{}{}>{}</{}>'.format(scope, " emphasize" if emphasize else "", content, scope)

def markdown(self, content: str) -> str:
def markup(self, content: Union[str, Dict[str, str]]) -> str:
return content


renderer = MockRenderer()


class GetDocumentationTests(unittest.TestCase):

def test_absent(self):
self.assertIsNone(get_documentation({}))

def test_is_str(self):
self.assertEqual(get_documentation({'documentation': 'str'}), 'str')

def test_is_dict(self):
self.assertEqual(get_documentation({'documentation': {'value': 'value'}}), 'value')


class CreateSignatureHelpTests(unittest.TestCase):

def test_none(self):
Expand Down

0 comments on commit 9a305ef

Please sign in to comment.