fennel-ls/test/code-action.fnl
Michele Campeotto b1986395cf Provide human readable code action descriptions
These strings will be displayed in a menu for the user to select.

The error codes, in addition to not being human readable sentences,
describe the issue being reported, not the solution that will be
applied.

The code action response only includes the human readable string, so the
tests need to check for that. I considered adding a custom field but
decided against going out of spec.
2025-03-11 08:58:18 -07:00

56 lines
2.0 KiB
Fennel

(local faith (require :faith))
(local {: view} (require :fennel))
(local {: create-client} (require :test.utils))
(local {: apply-edits} (require :fennel-ls.utils))
(create-client "(print :hi)")
(fn check [file-contents action-I-want-to-take desired-file-contents]
(let [{: client : uri :locations [range] : encoding : text} (create-client file-contents)
[{:result responses}] (client:code-action uri range.range)
action (accumulate [result nil
_ action (ipairs responses) &until result]
(if (= action.title action-I-want-to-take)
action))]
(if (not action)
(error
(.. "I couldn't find your action \"" action-I-want-to-take "\" in:\n"
(view (icollect [_ action (ipairs responses)]
action.title)))))
(let [edits (?. action :edit :changes uri)
edited-text (apply-edits text edits encoding)]
(faith.= desired-file-contents edited-text))))
(fn check-negative [file-contents action-not-suggested]
(let [{: client : uri :locations [range]} (create-client file-contents)
[{:result responses}] (client:code-action uri range.range)]
(each [_ action (ipairs responses)]
(assert (not= action.title action-not-suggested)
(.. "I found your action \"" action-not-suggested "\" in:\n"
(view (icollect [_ action (ipairs responses)]
action.title)))))))
(fn test-fix-op-no-arguments []
(check "(let [x (+====)]
(print x))"
"Replace with the corresponding literal"
"(let [x 0]
(print x))"))
(fn test-fix-unused-definition []
(check "(local x==== 10)"
"Prefix with _ to silence warning"
"(local _x 10)"))
; (fn test-fix-method-function []
; (check "(local x {})
; (fn x:y [a b c]
; (print client a b c))"
; "TO-BE-NAMED"
; "(local x {})
; (fn x.y [client a b c]
; (print client a b c))"))
{: test-fix-op-no-arguments
: test-fix-unused-definition}