PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/test/lsp-io-test.el

https://gitlab.com/vitalii.dr/lsp-mode
Emacs Lisp | 111 lines | 79 code | 18 blank | 14 comment | 0 complexity | b1bf21da08ff7405e75f0c2c480f4b02 MD5 | raw file
  1. ;;; lsp-io-test.el --- unit tests for lsp-io.el -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2016-2018 Vibhav Pant <vibhavp@gmail.com>.
  3. ;; This program is free software: you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Code:
  14. (require 'ert)
  15. (require 'lsp-mode)
  16. (require 'cl-lib)
  17. (defvar lsp--test-results nil)
  18. (defvar lsp--parser-function nil)
  19. (defvar lsp--test-workspace
  20. (make-lsp--workspace
  21. :client (make-lsp--client :ignore-messages '("readFile .* requested"))))
  22. (defun lsp--create-process-message ()
  23. (let ((fn (lsp--create-filter-function nil)))
  24. (lambda (input)
  25. (cl-letf (((symbol-function 'lsp--parser-on-message) (lambda (_msg _workspace)))
  26. ((symbol-function 'json-read-from-string)
  27. (lambda (msg)
  28. (push msg lsp--test-results)))
  29. ((symbol-function 'json-parse-string)
  30. (lambda (msg &rest _)
  31. (push msg lsp--test-results)))
  32. ((symbol-function 'json-parse-buffer)
  33. (lambda (&rest _)
  34. (push (buffer-string) lsp--test-results)))
  35. ((symbol-function 'json-read)
  36. (lambda (&rest _)
  37. (push (buffer-string) lsp--test-results))))
  38. (funcall fn nil input)
  39. (prog1 lsp--test-results
  40. (setq lsp--test-results nil))))))
  41. (ert-deftest lsp--parser-read--multiple-messages ()
  42. (let* ((fn (lsp--create-process-message))
  43. (messages-in '("Content-Length: 2\r\n\r\n{}"
  44. "Content-Length: 2\r\n\r\n{}"
  45. "Content-Length:2\r\n\r\n{}"
  46. "Content-Length: 2\r\n\r\n{}"
  47. "Content-Length:2\r\n\r\n{}"
  48. "Content-Length: 2\r\n\r\n{}"
  49. "Content-Length: 2\r\n\r\n{}"))
  50. (messages (funcall fn (string-join messages-in))))
  51. (should (equal messages '("{}" "{}" "{}" "{}" "{}" "{}" "{}")))))
  52. (ert-deftest lsp--parser-read--multibyte ()
  53. (let* ((fn (lsp--create-process-message))
  54. (message-in "Content-Length: 3\r\n\r\n\xe2\x80\x99")
  55. (messages (funcall fn message-in)))
  56. (should (equal messages '("’")))))
  57. (ert-deftest lsp--parser-read--multibyte-nospace ()
  58. (let* ((fn (lsp--create-process-message))
  59. (message-in "Content-Length:3\r\n\r\n\xe2\x80\x99")
  60. (messages (funcall fn message-in)))
  61. (should (equal messages '("’")))))
  62. (ert-deftest lsp--parser-read--multibyte-received ()
  63. (let* ((fn (lsp--create-process-message))
  64. (message-in "Content-Length: 1152\r\n\r\n{\"jsonrpc\":\"2.0\",\"method\":\"sts/highlight\",\"params\":{\"doc\":{\"version\":0,\"uri\":\"file:///home/kyoncho/Public/Desktop/hellow/hello-world/src/main/java/com/example/helloworld/HelloWorldController.java\"},\"codeLenses\":[{\"range\":{\"start\":{\"line\":9,\"character\":0},\"end\":{\"line\":9,\"character\":11}},\"command\":{\"title\":\"← SampleBean\",\"command\":\"sts.showHoverAtPosition\",\"arguments\":[{\"line\":9,\"character\":0}]},\"data\":\"← SampleBean\"},{\"range\":{\"start\":{\"line\":14,\"character\":4},\"end\":{\"line\":14,\"character\":14}},\"command\":{\"title\":\"← SampleBean\",\"command\":\"sts.showHoverAtPosition\",\"arguments\":[{\"line\":14,\"character\":4}]},\"data\":\"← SampleBean\"},{\"range\":{\"start\":{\"line\":17,\"character\":4},\"end\":{\"line\":17,\"character\":31}},\"command\":{\"title\":\"http://127.0.0.1:8080/hello-world\",\"command\":\"sts.open.url\",\"arguments\":[\"http://127.0.0.1:8080/hello-world\"]},\"data\":\"http://127.0.0.1:8080/hello-world\"},{\"range\":{\"start\":{\"line\":25,\"character\":4},\"end\":{\"line\":25,\"character\":32}},\"command\":{\"title\":\"http://127.0.0.1:8080/hello-world2\",\"command\":\"sts.open.url\",\"arguments\":[\"http://127.0.0.1:8080/hello-world2\"]},\"data\":\"http://127.0.0.1:8080/hello-world2\"}]}}")
  65. (messages (funcall fn message-in)))
  66. (should (equal messages '("{\"jsonrpc\":\"2.0\",\"method\":\"sts/highlight\",\"params\":{\"doc\":{\"version\":0,\"uri\":\"file:///home/kyoncho/Public/Desktop/hellow/hello-world/src/main/java/com/example/helloworld/HelloWorldController.java\"},\"codeLenses\":[{\"range\":{\"start\":{\"line\":9,\"character\":0},\"end\":{\"line\":9,\"character\":11}},\"command\":{\"title\":\"← SampleBean\",\"command\":\"sts.showHoverAtPosition\",\"arguments\":[{\"line\":9,\"character\":0}]},\"data\":\"← SampleBean\"},{\"range\":{\"start\":{\"line\":14,\"character\":4},\"end\":{\"line\":14,\"character\":14}},\"command\":{\"title\":\"← SampleBean\",\"command\":\"sts.showHoverAtPosition\",\"arguments\":[{\"line\":14,\"character\":4}]},\"data\":\"← SampleBean\"},{\"range\":{\"start\":{\"line\":17,\"character\":4},\"end\":{\"line\":17,\"character\":31}},\"command\":{\"title\":\"http://127.0.0.1:8080/hello-world\",\"command\":\"sts.open.url\",\"arguments\":[\"http://127.0.0.1:8080/hello-world\"]},\"data\":\"http://127.0.0.1:8080/hello-world\"},{\"range\":{\"start\":{\"line\":25,\"character\":4},\"end\":{\"line\":25,\"character\":32}},\"command\":{\"title\":\"http://127.0.0.1:8080/hello-world2\",\"command\":\"sts.open.url\",\"arguments\":[\"http://127.0.0.1:8080/hello-world2\"]},\"data\":\"http://127.0.0.1:8080/hello-world2\"}]}}")))))
  67. (ert-deftest lsp--parser-read--multiple-chunks ()
  68. (let* ((fn (lsp--create-process-message)))
  69. (should (equal (funcall fn "Content-Length: 14\r\n\r\n{") nil))
  70. (should (equal (funcall fn "\"somedata\":1") nil))
  71. (should (equal (funcall fn "}Content-Length: 14\r\n\r\n{")
  72. '("{\"somedata\":1}")))
  73. (should (equal (funcall fn "\"somedata\":2}")
  74. '("{\"somedata\":2}")))))
  75. (ert-deftest lsp--parser-read--multiple-multibyte-chunks ()
  76. (let* ((fn (lsp--create-process-message)))
  77. (should (equal (funcall fn "Content-Length: 18\r") nil))
  78. (should (equal (funcall fn "\n\r\n{\"somedata\":\"\xe2\x80") nil))
  79. (should (equal (funcall fn "\x99\"}Content-Length: 14\r\n\r\n{")
  80. '("{\"somedata\":\"’\"}")))
  81. (should (equal (funcall fn "\"somedata\":2}")
  82. '("{\"somedata\":2}")))))
  83. (ert-deftest lsp--non-related-content-on-stdout ()
  84. (let* ((fn (lsp--create-process-message)))
  85. (should (equal (funcall fn "ConentOnStdoutContent-Length: 14\r\n\r\n{\"somedata\":1}")
  86. '("{\"somedata\":1}")))))
  87. (ert-deftest lsp--parser-read--ignored-messages ()
  88. (lsp--on-notification
  89. lsp--test-workspace
  90. (lsp--read-json "{\"jsonrpc\":\"2.0\",\"method\":\"window/logMessage\",\"params\":{\"type\":2,\"message\":\"readFile /some/path requested by TypeScript but content not available\"}}"))
  91. (lsp--on-notification lsp--test-workspace (lsp--read-json "{\"jsonrpc\":\"2.0\",\"method\":\"window/logMessage\",\"params\":{\"type\":2,\"message\":\"Important message\"}}"))
  92. (should (equal (with-current-buffer "*lsp-log*" (buffer-substring-no-properties (point-min) (point-max))) "Important message\n")))
  93. ;;; lsp-io-test.el ends here