def test_document_line_to_cursor(text, pos, expected, patch): """ Tests whether the word on the cursor is properly detected """ patch.init(Document) patch.object(Document, 'line', return_value=text) doc = Document() r = doc.line_to_cursor(Position(*pos)) assert r == expected
def build_cache(text, line): doc = Document(uri=".text.", text=text) pos = Position(line, 0) context = CompletionContext(ws=None, doc=doc, pos=pos) cache = ContextCache(hub=hub) cache.update(context) return cache
def test_run_error_correct_no_column(magic): endpoint = magic() d = Diagnostics(endpoint=endpoint) ws = magic() doc = Document(uri=".my.uri.", text='function foo\n a = 1\nb="{foo()}"') d.run(ws, doc) endpoint.notify.assert_called_with( "textDocument/publishDiagnostics", { "uri": doc.uri, "diagnostics": [{ "range": { "start": { "line": 2, "character": 0 }, "end": { "line": 2, "character": 10 }, }, "message": "E0126: Type casting not supported from `none` to `string`.", "severity": DiagnosticSeverity.Error, }], }, )
def test_run_error_correct_end_column(magic): endpoint = magic() d = Diagnostics(endpoint=endpoint) ws = magic() doc = Document(uri=".my.uri.", text="a = 1\nb=car") d.run(ws, doc) endpoint.notify.assert_called_with( "textDocument/publishDiagnostics", { "uri": doc.uri, "diagnostics": [{ "range": { "start": { "line": 1, "character": 2 }, "end": { "line": 1, "character": 5 }, }, "message": "E0101: Variable `car` has not been defined.", "severity": DiagnosticSeverity.Error, }], }, )
def test_run_error_correct_column(magic): endpoint = magic() d = Diagnostics(endpoint=endpoint) ws = magic() doc = Document(uri=".my.uri.", text="a = 1\nb=$") d.run(ws, doc) endpoint.notify.assert_called_with( "textDocument/publishDiagnostics", { "uri": doc.uri, "diagnostics": [{ "range": { "start": { "line": 1, "character": 2 }, "end": { "line": 1, "character": 3 }, }, "message": "E0041: `$` is not allowed here", "severity": DiagnosticSeverity.Error, }], }, )
def test_run_error_issue_192(magic): endpoint = magic() d = Diagnostics(endpoint=endpoint) ws = magic() doc = Document( uri=".my.uri.", text= 'when zoom events RecordingCompleted as recording\n transcript=""', ) d.run(ws, doc) endpoint.notify.assert_called_with( "textDocument/publishDiagnostics", { "uri": doc.uri, "diagnostics": [{ "range": { "start": { "line": 0, "character": 0 }, "end": { "line": 0, "character": 47 }, }, "message": "E0139: Service `zoom` does not exist on the hub.", "severity": DiagnosticSeverity.Error, }], }, )
def test_run_error(magic): endpoint = magic() d = Diagnostics(endpoint=endpoint) ws = magic() doc = Document(uri='.my.uri.', text='a = foo') d.run(ws, doc) endpoint.notify.assert_called_with( 'textDocument/publishDiagnostics', { 'uri': doc.uri, 'diagnostics': [{ 'range': { 'start': { 'line': 1, 'character': 5 }, 'end': { 'line': 1, 'character': 8 }, }, 'message': 'E0101: Variable `foo` has not been defined.', 'severity': DiagnosticSeverity.Error }] })
def test_indent(indentor, ws, story, expected): doc = Document(uri=".my.uri.", text=story) lines = story.split("\n") # select the last pos in the provided story pos = Position(line=len(lines) - 1, character=len(lines[-1])) assert ( indentor.indent(ws, doc, pos, indent_unit=" ")["indent"] == expected )
def test_error(magic): doc = Document(uri=".text.", text="a = $\n b = \n") pos = Position(1, 4) context = CompletionContext(ws=magic(), doc=doc, pos=pos) glob = GlobalScopeCache(story_hub=hub) glob.update(context) fns = [*glob.function_table.functions.keys()] assert fns == []
def test_error(magic): doc = Document(uri=".text.", text="a = $\n b = \n") pos = Position(1, 4) context = CompletionContext(ws=magic(), doc=doc, pos=pos) global_ = magic() current = CurrentScopeCache(global_=global_, hub=hub) current.update(context) symbols = [s.name() for s in current.current_scope.symbols()] assert symbols == []
def test_run_no_error(magic): endpoint = magic() d = Diagnostics(endpoint=endpoint) ws = magic() doc = Document(uri=".my.uri.", text="a = 0") d.run(ws, doc) endpoint.notify.assert_called_with("textDocument/publishDiagnostics", { "uri": doc.uri, "diagnostics": [], })
def test_caching(magic): doc = Document(uri=".text.", text="function foo\n a = 1\n\nb = 0") pos = Position(3, 0) context = CompletionContext(ws=magic(), doc=doc, pos=pos) global_ = magic() global_.global_scope = Scope.root() glob = GlobalScopeCache(story_hub=hub) glob.update(context) fns = [*glob.function_table.functions.keys()] assert fns == ["foo"]
def test_complete(magic): doc = Document(uri=".text.", text="a = $") pos = Position(0, 0) context = CompletionContext(ws=magic(), doc=doc, pos=pos) global_ = magic() global_.global_scope = Scope.root() current = CurrentScopeCache(global_=global_, hub=hub) current.update(context) assert [x.symbol.name() for x in current.complete("a")] == ["app"] assert [x.symbol.name() for x in current.complete("b")] == []
def test_run_no_error(magic): endpoint = magic() d = Diagnostics(endpoint=endpoint) ws = magic() doc = Document(uri='.my.uri.', text='a = 0') d.run(ws, doc) endpoint.notify.assert_called_with('textDocument/publishDiagnostics', { 'uri': doc.uri, 'diagnostics': [], })
def test_complete(magic): doc = Document(uri=".text.", text="function foo\n a = 1\n\nb = 0") pos = Position(3, 0) context = CompletionContext(ws=magic(), doc=doc, pos=pos) global_ = magic() global_.global_scope = Scope.root() glob = GlobalScopeCache(story_hub=hub) glob.update(context) assert [x.function.name() for x in glob.complete("f")] == ["foo"] assert [x.function.name() for x in glob.complete("b")] == []
def test_format_multiple(formatter, ws): doc = Document(uri=".my.uri.", text="a=1\nb=2") assert formatter.format(ws, doc) == [ { "range": { "start": {"line": 0, "character": 0}, "end": {"line": 1, "character": 3}, }, "newText": "a = 1\nb = 2", } ]
def test_complete(magic, patch): patch.init(Document) patch.many(Document, ['line_to_cursor', 'word_on_cursor']) c = Completion(plugins=[]) doc = Document() ws = magic() pos = magic() result = c.complete(ws, doc, pos) assert result == { 'isIncomplete': False, 'items': [], }
def test_run_empty(magic, patch): endpoint = magic() patch.object(Diagnostics, 'to_error') patch.object(Api, 'loads') Api.loads.errors.return_value = [] d = Diagnostics(endpoint=endpoint) doc = Document(uri='.my.uri.', text='a = 0') d.run(ws=magic(), doc=doc) endpoint.notify.assert_called_with('textDocument/publishDiagnostics', { 'uri': doc.uri, 'diagnostics': [], })
def test_run_empty(magic, patch): endpoint = magic() patch.object(Diagnostics, "to_error") patch.object(Api, "loads") Api.loads.errors.return_value = [] d = Diagnostics(endpoint=endpoint) doc = Document(uri=".my.uri.", text="a = 0") d.run(ws=magic(), doc=doc) endpoint.notify.assert_called_with("textDocument/publishDiagnostics", { "uri": doc.uri, "diagnostics": [], })
def test_run_story_error_internal(magic, patch): endpoint = magic() se = StoryError(None, None) patch.init(Story) patch.object(Diagnostics, "to_error") patch.object(Api, "loads") Api.loads().errors.return_value = [se] d = Diagnostics(endpoint=endpoint) doc = Document(uri=".my.uri.", text="a = 0") d.run(ws=magic(), doc=doc) d.to_error.assert_not_called() endpoint.notify.assert_called_with("textDocument/publishDiagnostics", { "uri": doc.uri, "diagnostics": [], })
def test_complete(magic, patch): patch.init(Document) patch.many(Document, ["line_to_cursor", "word_to_cursor"]) patch.many(CompletionContext, ["_blocks"]) cache = magic() c = Completion(plugins=[], context_cache=cache) doc = Document() ws = magic() pos = magic() result = c.complete(ws, doc, pos) assert isinstance(cache.update.call_args[0][0], CompletionContext) assert result == { "isIncomplete": False, "items": [], }
def test_indent_edits2(indentor, ws): doc = Document(uri=".my.uri.", text="\ntry") pos = Position(line=1, character=3) assert indentor.indent(ws, doc, pos, indent_unit=" ") == { "indent": indent_unit, "textEdits": [ { "newText": "\n" + indent_unit, "range": { "end": {"character": 3, "line": 1}, "start": {"character": 3, "line": 1}, }, } ], }
def test_caching(magic): doc = Document(uri=".text.", text="a = $") pos = Position(0, 0) context = CompletionContext(ws=magic(), doc=doc, pos=pos) global_ = magic() global_.global_scope = Scope.root() current = CurrentScopeCache(global_=global_, hub=hub) current.update(context) symbols = [s.name() for s in current.current_scope.symbols()] assert symbols == ["app"] # test caching current.update(context) symbols = [s.name() for s in current.current_scope.symbols()] assert symbols == ["app"]
def test_indent_edits(indentor, ws): doc = Document(uri=".my.uri.", text="a = 1") pos = Position(line=0, character=5) assert indentor.indent(ws, doc, pos, indent_unit=" ") == { "indent": "", "textEdits": [ { "newText": "\n", "range": { "end": {"character": 5, "line": 0}, "start": {"character": 5, "line": 0}, }, } ], }
def test_run_story_error(magic, patch): endpoint = magic() se = StoryError(None, None) patch.init(Story) patch.object(Diagnostics, 'to_error') patch.object(Api, 'loads') Api.loads().errors.return_value = [se] d = Diagnostics(endpoint=endpoint) doc = Document(uri='.my.uri.', text='a = 0') d.run(ws=magic(), doc=doc) d.to_error.assert_called_with(se) endpoint.notify.assert_called_with('textDocument/publishDiagnostics', { 'uri': doc.uri, 'diagnostics': [Diagnostics.to_error()], })
def test_complete_plugin(magic, patch): patch.init(Document) patch.many(Document, ['line_to_cursor', 'word_on_cursor']) my_plugin = magic() i1 = magic() i2 = magic() my_plugin.complete.return_value = [i1, i2] c = Completion(plugins=[my_plugin]) doc = Document() ws = magic() pos = magic() result = c.complete(ws, doc, pos) my_plugin.complete.call_args == (CompletionContext(ws, doc, pos)) assert result == { 'isIncomplete': False, 'items': [i1.to_completion(), i2.to_completion()], }
def test_complete_plugin(magic, patch): patch.init(Document) patch.many(Document, ["line_to_cursor", "word_to_cursor"]) patch.many(CompletionContext, ["_blocks"]) my_plugin = magic() i1 = {"label": "i1"} i2 = {"label": "i2"} cache = magic() my_plugin.complete.return_value = [i1, i2] c = Completion(plugins=[my_plugin], context_cache=cache) doc = Document() ws = magic() pos = magic() result = c.complete(ws, doc, pos) assert isinstance(my_plugin.complete.call_args[0][0], CompletionContext) assert result == { "isIncomplete": False, "items": [i1, i2], }
def test_complete_exec(magic, patch): patch.init(Document) patch.many(Document, ["line_to_cursor", "word_to_cursor"]) patch.many(CompletionContext, ["_blocks"]) patch.object(sentry, "handle_exception") cache = magic() plugin = magic() ex = Exception("e") plugin.complete.side_effect = ex c = Completion(plugins=[plugin], context_cache=cache) doc = Document() ws = magic() pos = magic() result = c.complete(ws, doc, pos) assert isinstance(cache.update.call_args[0][0], CompletionContext) assert result == { "isIncomplete": False, "items": [], } assert sentry.handle_exception.call_args == call(ex)
def test_document_line_split(text, expected, patch): doc = Document('fake.uri', text) assert doc._lines == expected # check public API assert [doc.line(i) for i in range(len(expected))] == expected
def test_indent_options(indentor, ws): doc = Document(uri=".my.uri.", text=" try") pos = Position(line=0, character=8) assert ( indentor.indent(ws, doc, pos, indent_unit=" ")["indent"] == " " )