2424
2525(require 'lsp-mode )
2626(require 'lsp-sonarlint )
27+ (require 'cl-lib )
2728(load-file (expand-file-name " lsp-sonarlint-test-utils.el"
2829 (file-name-directory (or load-file-name (buffer-file-name )))))
2930
@@ -118,6 +119,42 @@ SonarLint LSP server."
118119 (lsp-sonarlint-test--line-range->ht (plist-get primary :range )))
119120 (" codeMatches" nil ))))))
120121
122+ (defun lsp-sonarlint-test--overlay-strings (prop-name loc-fun )
123+ " Return a list of plists with PROP-NAME and their locs obtained with LOC-FUN."
124+ (cl-remove nil (mapcar (lambda (ovl ) (if-let ((str-before (overlay-get ovl prop-name)))
125+ `(, prop-name ,(substring-no-properties str-before)
126+ :pos ,(funcall loc-fun ovl))
127+ nil ))
128+ (overlays-in (point-min ) (point-max )))))
129+
130+ (defun lsp-sonarlint-test--buf-string-with-overlay-strings ()
131+ " Contents of current buffer interspersed with overlay-attached strings."
132+ (let* ((pieces '())
133+ (last-pos (point-min ))
134+ (before-strings (lsp-sonarlint-test--overlay-strings
135+ 'before-string #'overlay-start ))
136+ (after-strings (lsp-sonarlint-test--overlay-strings
137+ 'after-string #'overlay-end ))
138+ (all-strings (sort (append before-strings after-strings)
139+ (lambda (str1 str2 )
140+ (let ((pos1 (plist-get str1 :pos ))
141+ (pos2 (plist-get str2 :pos )))
142+ (or (< pos1 pos2)
143+ ; ; before-string is inserted before after-string
144+ (and (= pos1 pos2)
145+ (plist-member str1 'before-string )
146+ (not (plist-member str2 'before-string )))))))))
147+ (dolist (str all-strings)
148+ (let ((pos (plist-get str :pos )))
149+ (push (buffer-substring-no-properties last-pos pos) pieces)
150+ (when-let ((after-string (plist-get str 'after-string )))
151+ (push after-string pieces))
152+ (when-let ((before-string (plist-get str 'before-string )))
153+ (push before-string pieces))
154+ (setq last-pos pos)))
155+ (concat (string-join (nreverse pieces))
156+ (buffer-substring-no-properties last-pos (point-max )))))
157+
121158(ert-deftest lsp-sonarlint-test--display-secondary-messages ()
122159 " Test that secondary locations are displayed correctly."
123160 (let ((target-file-buf (find-file-noselect lsp-sonarlint-test--file-path)))
@@ -139,12 +176,27 @@ SonarLint LSP server."
139176 (sec-flow2 `((:message " Identical code" :range , secondary-range2 )))
140177 (command (lsp-sonarlint-test--secloc-command
141178 primary-loc (list sec-flow1 sec-flow2))))
142- (lsp-sonarlint--show-all-locations command)
143- (with-current-buffer lsp-sonarlint--secondary-messages-buffer-name
144- (should (equal (buffer-string )
145- " Redundant branching
146- Identical code
147- Identical code" )))))))
179+ (lsp-sonarlint--show-all-locations command)))
180+ (with-current-buffer lsp-sonarlint--secondary-messages-buffer-name
181+ (should (equal (lsp-sonarlint-test--buf-string-with-overlay-strings)
182+ " Redundant branching
183+ 1Identical code
184+ 2Identical code" )))
185+ (with-current-buffer target-file-buf
186+ (should (equal (lsp-sonarlint-test--buf-string-with-overlay-strings)
187+ "
188+ int divide_seventeen(int param) {
189+ Redundant branching
190+ if (param == 0) {
191+ Identical code
192+ 1int a = 0;
193+ } else {
194+ Identical code
195+ 2int b = 0;
196+ }
197+ return 10 / param;
198+ }
199+ " )))))
148200
149201
150202; ;; lsp-sonarlint-secondar-locations-test.el ends here
0 commit comments