Skip to content

Commit d586354

Browse files
committed
Merge pull request #1090 from geraldus/utils-coverage
Utils coverage
2 parents d8c7c24 + 5741ce7 commit d586354

File tree

4 files changed

+62
-22
lines changed

4 files changed

+62
-22
lines changed

haskell-commands.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ happened since function invocation)."
639639
(min-pos (caar pos-reg))
640640
(max-pos (cdar pos-reg))
641641
(sig (haskell-utils-reduce-string response))
642-
(res-type (haskell-utils-parse-repl-response sig)))
642+
(res-type (haskell-utils-repl-response-error-status sig)))
643643

644644
(cl-case res-type
645645
;; neither popup presentation buffer

haskell-process.el

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,11 @@ Returns NIL when no completions found."
297297
(reqstr (concat ":complete repl"
298298
mlimit
299299
(haskell-string-literal-encode inputstr)))
300-
(rawstr (haskell-process-queue-sync-request process reqstr)))
301-
;; TODO use haskell-utils-parse-repl-response
302-
(if (string-prefix-p "unknown command " rawstr)
303-
(error "GHCi lacks `:complete' support (try installing 7.8 or ghci-ng)")
300+
(rawstr (haskell-process-queue-sync-request process reqstr))
301+
(response-status (haskell-utils-repl-response-error-status rawstr)))
302+
(if (eq 'unknown-command response-status)
303+
(error
304+
"GHCi lacks `:complete' support (try installing GHC 7.8+ or ghci-ng)")
304305
(let* ((s1 (split-string rawstr "\r?\n" t))
305306
(cs (mapcar #'haskell-string-literal-decode (cdr s1)))
306307
(h0 (car s1))) ;; "<limit count> <all count> <unused string>"

haskell-utils.el

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
;;; haskell-utils.el --- General utility functions used by haskell-mode modules -*- lexical-binding: t -*-
22

3-
;; Copyright (C) 2013 Herbert Valerio Riedel
3+
;; Copyright © 2013 Herbert Valerio Riedel
4+
;; 2016 Arthur Fayzrakhmanov
45

56
;; Author: Herbert Valerio Riedel <[email protected]>
67

@@ -47,7 +48,8 @@
4748
(defun haskell-utils-read-directory-name (prompt default)
4849
"Read directory name and normalize to true absolute path.
4950
Refer to `read-directory-name' for the meaning of PROMPT and
50-
DEFAULT. If `haskell-process-load-or-reload-prompt' is nil, accept `default'."
51+
DEFAULT. If `haskell-process-load-or-reload-prompt' is nil,
52+
accept `default'."
5153
(let ((filename (file-truename (read-directory-name prompt default default))))
5254
(concat (replace-regexp-in-string "/$" "" filename) "/")))
5355

@@ -105,25 +107,37 @@ only a single space. Then removes all newlines."
105107
(let ((s_ (replace-regexp-in-string "^\s+" " " s)))
106108
(replace-regexp-in-string "\n" "" s_)))
107109

108-
(defun haskell-utils-parse-repl-response (r)
109-
"Parse response R from REPL and return special kind of result.
110-
The result is the response string itself with the special property
111-
response-type added.
112-
113-
This property could be one of the following:
110+
(defun haskell-utils-repl-response-error-status (response)
111+
"Parse response REPL's RESPONSE for errors.
112+
Returns one of the following symbols:
114113
115114
+ unknown-command
116115
+ option-missing
117116
+ interactive-error
118-
+ success"
119-
(let ((first-line (car (split-string r "\n"))))
117+
+ no-error
118+
119+
*Warning*: this funciton covers only three kind of responses:
120+
121+
* \"unknown command …\"
122+
REPL missing requested command
123+
* \"<interactive>:3:5: …\"
124+
interactive REPL error
125+
* \"Couldn't guess that module name. Does it exist?\"
126+
(:type-at and maybe some other commands error)
127+
* *all other reposnses* are treated as success reposneses and
128+
'no-error is returned."
129+
(let ((first-line (car (split-string response "\n" t))))
120130
(cond
121-
((string-match-p "^unknown command" first-line) 'unknown-command)
122-
((string-match-p "^Couldn't guess that module name. Does it exist?"
123-
first-line)
131+
((null first-line) 'no-error)
132+
((string-match-p "^unknown command" first-line)
133+
'unknown-command)
134+
((string-match-p
135+
"^Couldn't guess that module name. Does it exist?"
136+
first-line)
124137
'option-missing)
125-
((string-match-p "^<interactive>:" first-line) 'interactive-error)
126-
(t 'success))))
138+
((string-match-p "^<interactive>:" first-line)
139+
'interactive-error)
140+
(t 'no-error))))
127141

128142
(defun haskell-utils-compose-type-at-command (pos)
129143
"Prepare :type-at command to be send to haskell process.

tests/haskell-utils-tests.el

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;;; haskell-utils-tests.el --- Tests for Haskell utilities package
22

3-
;; Copyright © 2016 Athur Fayzrakhmanov. All rights reserved.
3+
;; Copyright © 2016 Arthur Fayzrakhmanov. All rights reserved.
44

55
;; This file is part of haskell-mode package.
66
;; You can contact with authors using GitHub issue tracker:
@@ -132,7 +132,7 @@
132132
(haskell-utils-parse-import-statement-at-point)))))
133133

134134
(ert-deftest type-at-command-composition ()
135-
"Test haskell-utils-compose-type-at-command.
135+
"Test `haskell-utils-compose-type-at-command'.
136136
Test only position conversion to line and column numbers, do not
137137
test last string compontent, it is used in `:type-at` command to
138138
provide user friendly output only and could be any string, even
@@ -176,4 +176,29 @@ strings will change in future."
176176
(haskell-utils-compose-type-at-command test-b-points))
177177
(should (string-prefix-p ":type-at nil 4 1 4 4" test-a-result))
178178
(should (string-prefix-p ":type-at nil 7 3 8 16" test-b-result)))))
179+
180+
(ert-deftest parse-repl-response ()
181+
"Test `haskell-utils-repl-response-error-status' function."
182+
(let* ((t1-str "unknown command ':type-at'\nuse :? for help.")
183+
(t2-str "\n<interactive>:3:5: Not in scope: ‘x’")
184+
(t3-str "Couldn't guess that module name. Does it exist?")
185+
(t4-str "Hello World!")
186+
(t5-str " ")
187+
(t6-str "")
188+
(t7-str "\n\n\n\n")
189+
(r1 (haskell-utils-repl-response-error-status t1-str))
190+
(r2 (haskell-utils-repl-response-error-status t2-str))
191+
(r3 (haskell-utils-repl-response-error-status t3-str))
192+
(r4 (haskell-utils-repl-response-error-status t4-str))
193+
(r5 (haskell-utils-repl-response-error-status t5-str))
194+
(r6 (haskell-utils-repl-response-error-status t6-str))
195+
(r7 (haskell-utils-repl-response-error-status t7-str)))
196+
(should (equal r1 'unknown-command))
197+
(should (equal r2 'interactive-error))
198+
(should (equal r3 'option-missing))
199+
(should (equal r4 'no-error))
200+
(should (equal r5 'no-error))
201+
(should (equal r6 'no-error))
202+
(should (equal r7 'no-error))))
203+
179204
;;; haskell-utils-tests.el ends here

0 commit comments

Comments
 (0)