Skip to content

Commit ed7e2ac

Browse files
committed
improve tests
1 parent dcfd8c6 commit ed7e2ac

File tree

2 files changed

+80
-55
lines changed

2 files changed

+80
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,24 @@
11
(ns com.github.clojure-lsp.intellij.foo-test
22
(:require
33
[clojure.test :refer [deftest is]]
4-
[com.github.clojure-lsp.intellij.db :as db]
5-
[com.github.clojure-lsp.intellij.server :as server]
64
[com.github.clojure-lsp.intellij.test-utils :as test-utils]
75
[com.github.ericdallo.clj4intellij.app-manager :as app-manager]
8-
[com.github.ericdallo.clj4intellij.test :as clj4intellij.test])
9-
(:import
10-
[com.github.clojure_lsp.intellij.extension SettingsState]
11-
[com.intellij.openapi.components ServiceManager]
12-
[com.intellij.openapi.editor LogicalPosition]))
6+
[com.github.ericdallo.clj4intellij.test :as clj4intellij.test]))
137

148
(set! *warn-on-reflection* true)
159

16-
17-
18-
19-
20-
21-
22-
23-
2410
(deftest foo-test
2511
(let [project-name "clojure.core"
26-
fixture (clj4intellij.test/setup project-name)
27-
deps-file (.createFile fixture "deps.edn" "{}")
28-
_ (.setTestDataPath fixture "testdata")
29-
clj-file (.copyFileToProject fixture "foo.clj")
30-
project (.getProject fixture)]
12+
{:keys [fixture project deps-file]} (test-utils/setup-test-project project-name)
13+
clj-file (.copyFileToProject fixture "foo.clj")]
3114
(is (= project-name (.getName project)))
3215
(is deps-file)
3316

34-
(app-manager/write-command-action
35-
project
36-
(fn [] (.openFileInEditor fixture clj-file)))
37-
38-
;; Para configurações persistentes via ServiceManager
39-
(let [my-settings (ServiceManager/getService SettingsState)] ;; Substitua pela classe real
40-
(.loadState my-settings my-settings));; Atualiza estado
41-
42-
(clj4intellij.test/dispatch-all)
43-
(test-utils/dispatch-all-until {:project project})
44-
(println "status LSP >> " (db/get-in project [:status]))
45-
(let [editor (.getEditor fixture)
46-
document (.getDocument editor)
47-
offset (.getLineStartOffset document 2)
48-
caret (.getCaretModel editor)
49-
pos (.getLogicalPosition caret)
50-
new-position (LogicalPosition. 2 8)]
51-
(println (.getText document))
52-
@(app-manager/invoke-later!
53-
{:invoke-fn (fn []
54-
#_(.moveToOffset caret (+ offset 9))
55-
(.moveToLogicalPosition caret new-position))}))
56-
(test-utils/run-editor-action "ClojureLSP.ForwardSlurp" project)
57-
(clj4intellij.test/dispatch-all)
58-
(println (-> fixture .getEditor .getDocument .getText))
59-
(.checkResultByFile fixture "foo_expected.clj")
60-
(server/shutdown! project)))
61-
62-
17+
(let [editor (test-utils/open-file-in-editor fixture clj-file)]
18+
(test-utils/setup-lsp-server project)
19+
(test-utils/move-caret-to-position editor 2 8)
20+
(test-utils/run-editor-action "ClojureLSP.ForwardSlurp" project)
21+
(clj4intellij.test/dispatch-all)
22+
(println (test-utils/get-editor-text fixture))
23+
(test-utils/check-result-by-file fixture "foo_expected.clj")
24+
(test-utils/teardown-test-project project))))

src/test/clojure/com/github/clojure_lsp/intellij/test_utils.clj

+69-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
(ns com.github.clojure-lsp.intellij.test-utils
22
(:require
33
[com.github.clojure-lsp.intellij.client :as lsp-client]
4+
[com.github.clojure-lsp.intellij.server :as server]
45
[com.github.ericdallo.clj4intellij.app-manager :as app-manager]
5-
[com.github.ericdallo.clj4intellij.test :as clj4intellij.test])
6-
(:import
7-
[com.intellij.openapi.wm WindowManager]
6+
[com.github.ericdallo.clj4intellij.test :as clj4intellij.test]
7+
[com.github.clojure-lsp.intellij.db :as db])
8+
(:import
9+
[com.github.clojure_lsp.intellij.extension SettingsState]
810
[com.intellij.ide DataManager]
9-
[com.intellij.openapi.actionSystem ActionManager]))
11+
[com.intellij.openapi.actionSystem ActionManager]
12+
[com.intellij.openapi.components ServiceManager]
13+
[com.intellij.openapi.editor LogicalPosition]
14+
[com.intellij.openapi.wm WindowManager]))
1015

1116
(set! *warn-on-reflection* true)
1217

1318
(defn get-status-bar-widget [project widget-id]
1419
(let [status-bar (.. (WindowManager/getInstance) (getStatusBar project))]
1520
(.getWidget status-bar widget-id)))
1621

17-
(defn run-editor-action [action-id project]
22+
(defn run-editor-action
23+
"Runs an editor action with the given ID for the specified project."
24+
[action-id project]
1825
(let [action (.getAction (ActionManager/getInstance) action-id)
1926
context (.getDataContext (DataManager/getInstance))]
2027
(println "Running action:" action-id)
@@ -26,6 +33,7 @@
2633
(com.intellij.openapi.actionSystem.AnActionEvent/createFromDataContext action-id nil context))))))
2734

2835
(defn dispatch-all-until
36+
"Dispatches all events until the LSP server is started or the timeout is reached."
2937
[{:keys [project millis timeout]
3038
:or {millis 1000
3139
timeout 10000}}]
@@ -48,4 +56,59 @@
4856
(do
4957
(clj4intellij.test/dispatch-all)
5058
(Thread/sleep millis)
51-
(recur)))))))
59+
(recur)))))))
60+
61+
(defn teardown-test-project
62+
"Shuts down all resources for the given project."
63+
[project]
64+
(server/shutdown! project))
65+
66+
(defn setup-test-project
67+
"Sets up a test project with the given name and optional deps.edn content.
68+
Returns a map with :fixture, :project, and :deps-file."
69+
([project-name]
70+
(setup-test-project project-name "{}"))
71+
([project-name deps-content]
72+
(let [fixture (clj4intellij.test/setup project-name)
73+
deps-file (.createFile fixture "deps.edn" deps-content)
74+
_ (.setTestDataPath fixture "testdata")
75+
project (.getProject fixture)]
76+
{:fixture fixture
77+
:project project
78+
:deps-file deps-file})))
79+
80+
(defn open-file-in-editor
81+
"Opens a file in the editor and returns the editor instance."
82+
[fixture file]
83+
(let [project (.getProject fixture)]
84+
(app-manager/write-command-action
85+
project
86+
(fn [] (.openFileInEditor fixture file)))
87+
(.getEditor fixture)))
88+
89+
(defn move-caret-to-position
90+
"Moves the caret to the specified logical position in the editor."
91+
[editor line column]
92+
(let [caret (.getCaretModel editor)
93+
new-position (LogicalPosition. line column)]
94+
@(app-manager/invoke-later!
95+
{:invoke-fn (fn [] (.moveToLogicalPosition caret new-position))})))
96+
97+
(defn get-editor-text
98+
"Returns the text content of the editor's document."
99+
[fixture]
100+
(-> fixture .getEditor .getDocument .getText))
101+
102+
(defn check-result-by-file
103+
"Checks if the current editor content matches the expected file."
104+
[fixture expected-file]
105+
(.checkResultByFile fixture expected-file))
106+
107+
(defn setup-lsp-server
108+
"Sets up and waits for the LSP server to be ready."
109+
[project]
110+
(let [my-settings (ServiceManager/getService SettingsState)]
111+
(.loadState my-settings my-settings)
112+
(clj4intellij.test/dispatch-all)
113+
(dispatch-all-until {:project project})
114+
(println "status LSP >> " (db/get-in project [:status]))))

0 commit comments

Comments
 (0)