File: dap-java-steps.el

package info (click to toggle)
dap-mode 0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,556 kB
  • sloc: lisp: 6,299; makefile: 31; java: 27; xml: 23
file content (189 lines) | stat: -rw-r--r-- 7,054 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
;;; dap-java-steps.el --- Step definitions for dap-java  -*- lexical-binding: t; -*-

;; Copyright (C) 2018  Ivan Yonchovski

;; Author: Ivan Yonchovski <ivan.yonchovski@tick42.com>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.


;;; Code:

(require 'f)
(require 's)
(require 'dap-java)

(defun dap-java-steps-async-wait (pred callback)
  "Call CALLBACK when PRED becomes true."
  (let (timer
        (retry-count 0))
    (setq timer (run-with-timer
                 1
                 1
                 (lambda (&rest rest)
                   (if (funcall pred)
                       (progn
                         (cancel-timer timer)
                         (funcall callback))
                     (setq retry-count (1+ retry-count))
                     (message "The function failed, attempt %s" retry-count)))))))

(Given "^I have maven project \"\\([^\"]+\\)\" in \"\\([^\"]+\\)\"$"
  (lambda (project-name dir-name)
    (setq default-directory dap-java-test-root)

    ;; create directory structure
    (mkdir (expand-file-name
            (f-join   dir-name project-name "src" "main" "java" "temp")) t)

    ;; add pom.xml
    (with-temp-file (expand-file-name "pom.xml" (f-join dir-name project-name))
      (insert (format "
<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
  xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>%s</artifactId>
  <packaging>jar</packaging>
  <version>1</version>
  <name>test-project</name>
  <url>http://maven.apache.org</url>

  <properties>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
</project>" project-name)))))

(And "^I open a java file \"\\([^\"]+\\)\"$"
  (lambda (file-name)
    (setq default-directory dap-java-test-root)
    (message "making directory %s" (f-dirname file-name))
    (mkdir (f-dirname file-name) t)
    (find-file file-name)
    (save-buffer)))

(And "^I open a project file \"\\([^\"]+\\)\"$"
  (lambda (file-name)
    (find-file (f-join dap-java-maven-project-root file-name))))

(And "^I add project \"\\([^\"]+\\)\" folder \"\\([^\"]+\\)\" to the list of workspace folders$"
  (lambda (project dir-name)
    (lsp-workspace-folders-add (f-join dap-java-test-root dir-name project))))

(And "^I start lsp-java$"
  (lambda ()
    (lsp)))

(Then "^The server status must become \"\\([^\"]+\\)\"$"
  (lambda (status callback)
    (dap-java-steps-async-wait
     (lambda ()
       (if (s-matches? status (s-trim (lsp-mode-line)))
           t
         (progn
           (message "Server status is %s" (lsp-mode-line))
           nil)))
     callback)))

(When "^I invoke \"\\([^\"]+\\)\" I should see error message \"\\([^\"]+\\)\"$"
  (lambda (command message)
    (condition-case err
        (progn
          (funcall (intern command))
          (cl-assert nil t (format "Command %s should have failed." command)))
      (error (cl-assert (string= message (error-message-string err)) t (error-message-string err))))))

(Then "^I should see buffer \"\\([^\"]+\\)\" with content \"\\([^\"]+\\)\"$"
  (lambda (buffer-name buffer-content callback)
    (dap-java-steps-async-wait
     (lambda ()
       (when-let (buffer (get-buffer buffer-name))
         (with-current-buffer buffer
           (string= (buffer-string) buffer-content))))
     callback)))

(Then "^I should see buffer \"\\([^\"]+\\)\" which contains \"\\([^\"]+\\)\"$"
  (lambda (buffer-name buffer-content callback)
    (dap-java-steps-async-wait
     (lambda ()
       (when-let (buffer (get-buffer buffer-name))
         (with-current-buffer buffer
           (s-contains? buffer-content (buffer-string)))))
     callback)))


(And "^I attach handler \"\\([^\"]+\\)\" to hook \"\\([^\"]+\\)\"$"
  (lambda (handler-name hook-name)
    (add-hook
     (intern hook-name)
     (lambda (&rest args)
       (puthash handler-name 'called dap-handlers-called)))))

(Then "^The hook handler \"\\([^\"]+\\)\" would be called$"
  (lambda (handler-name callback)
    (dap-java-steps-async-wait
     (lambda ()
       (let ((result (eql 'called (gethash handler-name dap-handlers-called))))
         ;; clear the hash
         (remhash handler-name dap-handlers-called)
         result))
     callback)))

(When "^I go in active window"
  (lambda () (select-window (car (window-list )))))

(Then "^I should see message matching regexp \"\\(.+\\)\"$"
  "Asserts that MESSAGE has been printed."
  (lambda (message)
    (let ((msg "Expected '%s' to be included in the list of printed messages, but was not."))
      (setq message (s-replace "\\\"" "\"" message))
      (cl-assert (--find (string-match message it) (-map 's-trim ecukes-message-log)) nil msg message))))

(Then "^I should see the following overlay \"\\([^\"]+\\)\"$"
  (lambda (face)
    (let ((overlay-faces (--map (plist-get (overlay-properties it) 'face)
                                (overlays-in (save-mark-and-excursion
                                               (beginning-of-line)
                                               (point))
                                             (save-mark-and-excursion
                                               (end-of-line)
                                               (point))))))
      (cl-assert (cl-find (intern face) overlay-faces)
                 t
                 (format "Actual %s" overlay-faces)))))

(Then "^I should not see the following overlay \"\\([^\"]+\\)\"$"
  (lambda (face)
    (let ((overlay-faces (--map (plist-get (overlay-properties it) 'face)
                                (overlays-in (save-mark-and-excursion
                                               (beginning-of-line)
                                               (point))
                                             (save-mark-and-excursion
                                               (end-of-line)
                                               (point))))))
      (cl-assert (not (cl-find (intern face) overlay-faces))
                 t
                 (format "Actual %s" overlay-faces)))))

(And "^I call:$"
  (lambda (fn-to-call)
    (call-interactively (intern fn-to-call))))

(And "^I kill buffer \"\\([^\"]+\\)\"$"
  (lambda (buffer-name)
    (kill-buffer buffer-name)))

(provide 'dap-java-steps)
;;; dap-java-steps.el ends here