Skip to content

Commit

Permalink
add tests and fix code actions
Browse files Browse the repository at this point in the history
  • Loading branch information
6cdh authored and jeapostrophe committed Feb 9, 2023
1 parent 5f283eb commit fc37990
Show file tree
Hide file tree
Showing 27 changed files with 725 additions and 17 deletions.
6 changes: 3 additions & 3 deletions doc-trace.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@
#:edit (WorkspaceEdit
#:changes
(hasheq (string->symbol (path->uri src-obj))
(TextEdit #:range (Range #:start (abs-pos->Pos doc-text start)
#:end (abs-pos->Pos doc-text finish))
#:newText "_")))))
(list (TextEdit #:range (Range #:start (abs-pos->Pos doc-text start)
#:end (abs-pos->Pos doc-text start))
#:newText "_"))))))

(set-add! warn-diags diag)))
(define/override (syncheck:add-mouse-over-status src-obj start finish text)
Expand Down
36 changes: 26 additions & 10 deletions tests/client.rkt
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#lang racket/base
#lang racket

;; This module provides a simple incomplete lsp client for test.

(provide with-racket-lsp
Lsp?
client-send
client-wait-response
make-request
make-notification)

(require racket/os
json
"../msg-io.rkt")

(struct Lsp
Expand All @@ -23,12 +25,13 @@
(for ([str (in-port read-line in)])
(displayln (format "LSP ERROR: ~a" str) (current-error-port))))

;; (-> string? (-> Lsp? any/c) void?)
(define (with-racket-lsp path thunk)
(define/contract (with-racket-lsp path proc)
(-> string? (-> Lsp? any/c) void?)

(define racket-path (find-executable-path "racket"))
(define-values (sp stdout stdin stderr)
(subprocess #f #f #f racket-path path))
(define err-thd (thread (forward-errors stderr)))
(define _err-thd (thread (forward-errors stderr)))
(define lsp (Lsp stdout stdin stderr))

(define init-req
Expand All @@ -38,7 +41,7 @@
(client-send lsp init-req)
(client-wait-response lsp)

(thunk (Lsp stdout stdin stderr))
(proc (Lsp stdout stdin stderr))

(define shutdown-req
(make-request lsp "shutdown" #f))
Expand All @@ -47,25 +50,38 @@

(define exit-notf (make-notification "exit" #f))
(client-send lsp exit-notf)
(client-wait-response lsp)
(client-should-no-response lsp)

(subprocess-wait sp))

(define (client-send lsp req)
(define/contract (client-send lsp req)
(-> Lsp? jsexpr? void?)

(display-message/flush req (Lsp-stdin lsp)))

(define (client-wait-response lsp)
(define/contract (client-wait-response lsp)
(-> Lsp? jsexpr?)

(read-message (Lsp-stdout lsp)))

(define (make-request lsp method params)
(define/contract (client-should-no-response lsp)
(-> Lsp? eof-object?)

(read-message (Lsp-stdout lsp)))

(define/contract (make-request lsp method params)
(-> Lsp? string? jsexpr? jsexpr?)

(define req
(hasheq 'jsonrpc "2.0"
'id (Lsp-genid lsp)
'method method))
(cond [(not params) req]
[else (hash-set req 'params params)]))

(define (make-notification method params)
(define/contract (make-notification method params)
(-> string? jsexpr? jsexpr?)

(define req
(hasheq 'jsonrpc "2.0"
'method method))
Expand Down
2 changes: 1 addition & 1 deletion tests/lifecycle/test-main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
(define racket-path (find-executable-path "racket"))
(define-values (sp stdout stdin stderr)
(subprocess #f #f #f racket-path "-t" "../../main.rkt"))
(define err-thd (thread (forward-errors stderr)))
(define _err-thd (thread (forward-errors stderr)))

;; Initialize request
(display-message/flush init-req stdin)
Expand Down
24 changes: 24 additions & 0 deletions tests/textDocument/code-action/code-action.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#lang racket

(require "../with-document.rkt"
chk
json)

(define uri "file:///test.rkt")

(define code
#<<END
#lang racket/base

(define x 0)
END
)

(module+ test
(with-document "../../../main.rkt" uri code
(λ (lsp)
(let ([req (read-json (open-input-file "req1.json"))]
[resp (read-json (open-input-file "resp1.json"))])
(client-send lsp req)

(chk #:= (client-wait-response lsp) resp)))))
23 changes: 23 additions & 0 deletions tests/textDocument/code-action/req1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"id": 0,
"jsonrpc": "2.0",
"method": "textDocument/codeAction",
"params": {
"context": {
"diagnostics": []
},
"range": {
"end": {
"character": 9,
"line": 2
},
"start": {
"character": 8,
"line": 2
}
},
"textDocument": {
"uri": "file:///test.rkt"
}
}
}
47 changes: 47 additions & 0 deletions tests/textDocument/code-action/resp1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"id": 0,
"jsonrpc": "2.0",
"result": [
{
"diagnostics": [
{
"message": "unused variable",
"range": {
"end": {
"character": 9,
"line": 2
},
"start": {
"character": 8,
"line": 2
}
},
"severity": 3,
"source": "file:///test.rkt"
}
],
"edit": {
"changes": {
"file:///test.rkt": [
{
"newText": "_",
"range": {
"end": {
"character": 8,
"line": 2
},
"start": {
"character": 8,
"line": 2
}
}
}
]
}
},
"isPreferred": false,
"kind": "quickfix",
"title": "Add prefix `_` to ignore"
}
]
}
26 changes: 26 additions & 0 deletions tests/textDocument/completion/change-req.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"jsonrpc": "2.0",
"method": "textDocument/didChange",
"params": {
"contentChanges": [
{
"range": {
"end": {
"character": 0,
"line": 2
},
"start": {
"character": 0,
"line": 2
}
},
"rangeLength": 0,
"text": "()"
}
],
"textDocument": {
"uri": "file:///test.rkt",
"version": 1
}
}
}
18 changes: 18 additions & 0 deletions tests/textDocument/completion/comp-req.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"id": 0,
"jsonrpc": "2.0",
"method": "textDocument/completion",
"params": {
"context": {
"triggerCharacter": "(",
"triggerKind": 2
},
"position": {
"character": 1,
"line": 2
},
"textDocument": {
"uri": "file:///test.rkt"
}
}
}
39 changes: 39 additions & 0 deletions tests/textDocument/completion/completion.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#lang racket

(require "../with-document.rkt"
"../../../json-util.rkt"
chk
json)

(define uri "file:///test.rkt")

(define code
#<<END
#lang racket/base
END
)

(module+ test
(with-document "../../../main.rkt" uri code
(λ (lsp)

;; completion requires a document change.
;; only move cursor to that position is not enough.
(define didchange-req (read-json (open-input-file "change-req.json")))
(client-send lsp didchange-req)
(client-wait-response lsp)

(define comp-req (read-json (open-input-file "comp-req.json")))
(client-send lsp comp-req)
;; we only verify the returned completion item list
;; meet some conditions
(let ([resp (client-wait-response lsp)])
(chk (jsexpr-has-key? resp '(result)))
(define result (jsexpr-ref resp '(result)))
(chk (list? result))
(chk (for/and ([item result])
(jsexpr-has-key? item '(label))))
(chk (for/and ([item result])
(string? (jsexpr-ref item '(label)))))
(chk (for/and ([item result])
(not (string=? "" (jsexpr-ref item '(label))))))))))
14 changes: 14 additions & 0 deletions tests/textDocument/find-symbol/definition-req1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": 0,
"jsonrpc": "2.0",
"method": "textDocument/definition",
"params": {
"position": {
"character": 2,
"line": 5
},
"textDocument": {
"uri": "file:///test.rkt"
}
}
}
17 changes: 17 additions & 0 deletions tests/textDocument/find-symbol/definition-resp1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": 0,
"jsonrpc": "2.0",
"result": {
"range": {
"end": {
"character": 8,
"line": 4
},
"start": {
"character": 7,
"line": 4
}
},
"uri": "file:///test.rkt"
}
}
14 changes: 14 additions & 0 deletions tests/textDocument/find-symbol/highlight-req1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": 0,
"jsonrpc": "2.0",
"method": "textDocument/documentHighlight",
"params": {
"position": {
"character": 2,
"line": 5
},
"textDocument": {
"uri": "file:///test.rkt"
}
}
}
30 changes: 30 additions & 0 deletions tests/textDocument/find-symbol/highlight-resp1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"id": 0,
"jsonrpc": "2.0",
"result": [
{
"range": {
"end": {
"character": 3,
"line": 5
},
"start": {
"character": 2,
"line": 5
}
}
},
{
"range": {
"end": {
"character": 8,
"line": 4
},
"start": {
"character": 7,
"line": 4
}
}
}
]
}
17 changes: 17 additions & 0 deletions tests/textDocument/find-symbol/ref-req1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": 0,
"jsonrpc": "2.0",
"method": "textDocument/references",
"params": {
"context": {
"includeDeclaration": true
},
"position": {
"character": 7,
"line": 4
},
"textDocument": {
"uri": "file:///test.rkt"
}
}
}
Loading

0 comments on commit fc37990

Please sign in to comment.