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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
;;; haskell-utils-tests.el --- Tests for Haskell utilities package -*- lexical-binding: t -*-
;; Copyright © 2016 Arthur Fayzrakhmanov. All rights reserved.
;; This file is part of haskell-mode package.
;; You can contact with authors using GitHub issue tracker:
;; https://github.com/haskell/haskell-mode/issues
;; This file 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, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; This package provides regression tests for haskell-utils package.
;;; Code:
(require 'ert)
(require 'haskell-test-utils)
(require 'haskell-utils)
(ert-deftest simple-import-parse ()
(should (equal "A.B.C"
(with-temp-buffer
(insert-lines "import A.B.C")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest qualified-import-parse ()
(should (equal "A.B.C"
(with-temp-buffer
(insert-lines "import qualified A.B.C")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest qualified-as-import-parse ()
(should (equal "AAA.Bc.Cx"
(with-temp-buffer
(insert-lines "import qualified AAA.Bc.Cx as Something")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest international-characters-import-parse ()
(should (equal "Żółć"
(with-temp-buffer
(insert-lines "import Żółć")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest commented-out-import-parse ()
(should (equal nil
(with-temp-buffer
(insert-lines "-- import Nothing")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest non-import-import-parse ()
(should (equal nil
(with-temp-buffer
(insert-lines "something import Nothing")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest many-spaces-import-parse ()
(should (equal "M"
(with-temp-buffer
(insert-lines "\t import\t qualified \t\tM\tas G")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest using-underscores-import-parse ()
(should (equal "Module_1.S_3_3_"
(with-temp-buffer
(insert-lines "import Module_1.S_3_3_")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest slightly-malformed-import-parse ()
(should (equal "q.Module...qwerqwe..."
(with-temp-buffer
(insert-lines "import q.Module...qwerqwe...")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest package-import-parse ()
(should (equal "B"
(with-temp-buffer
(insert-lines "import \"package-1.2.3\" B")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest safe-haskell-import-parse ()
(should (equal "B"
(with-temp-buffer
(insert-lines "import safe B")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest full-import-parse ()
(should (equal "Data.Char.Unicode_v_7"
(with-temp-buffer
(insert-lines "import safe qualified \"unicode-7.0\" Data.Char.Unicode_v_7 as U (func)")
(goto-char (point-min))
(forward-line 0)
(haskell-utils-parse-import-statement-at-point)))))
(ert-deftest type-at-command-composition ()
"Test `haskell-utils-compose-type-at-command'.
Test only position conversion to line and column numbers, do not
test last string compontent, it is used in `:type-at` command to
provide user friendly output only and could be any string, even
empty one. Very likely the way how its composed for multilne
strings will change in future."
(with-temp-buffer
(insert-lines "module A where"
""
"int :: Int"
"int = 369"
""
"act ="
" do print int"
" return int")
(goto-char (point-min))
(let (test-a-points
test-b-points
test-a-result
test-b-result)
;; go to third line, e.g. `int` definition
(forward-line 3)
(setq test-a-points (point))
;; go to at the end of `int` definition, i.e. point stands at whitespace
(forward-char 3)
(setq test-a-points `(,test-a-points . ,(point)))
(goto-char (line-beginning-position))
;; go to do-block line
(forward-line 3)
;; go to `do` keyword beginning
(forward-char 2)
(setq test-b-points (point))
;; go to the end of do-block
(goto-char (point-max))
(setq test-b-points `(,test-b-points . ,(point)))
(setq test-a-result
(haskell-utils-compose-type-at-command test-a-points))
(setq test-b-result
(haskell-utils-compose-type-at-command test-b-points))
(should (string-prefix-p ":type-at nil 4 1 4 4" test-a-result))
(should (string-prefix-p ":type-at nil 7 3 8 16" test-b-result)))))
(ert-deftest parse-repl-response ()
"Test `haskell-utils-repl-response-error-status' function."
(let* ((t1-str "unknown command ':type-at'\nuse :? for help.")
(t2-str "\n<interactive>:3:5: Not in scope: ‘x’")
(t3-str "Couldn't guess that module name. Does it exist?")
(t4-str "Hello World!")
(t5-str " ")
(t6-str "")
(t7-str "\n\n\n\n")
(r1 (haskell-utils-repl-response-error-status t1-str))
(r2 (haskell-utils-repl-response-error-status t2-str))
(r3 (haskell-utils-repl-response-error-status t3-str))
(r4 (haskell-utils-repl-response-error-status t4-str))
(r5 (haskell-utils-repl-response-error-status t5-str))
(r6 (haskell-utils-repl-response-error-status t6-str))
(r7 (haskell-utils-repl-response-error-status t7-str)))
(should (equal r1 'unknown-command))
(should (equal r2 'interactive-error))
(should (equal r3 'option-missing))
(should (equal r4 'no-error))
(should (equal r5 'no-error))
(should (equal r6 'no-error))
(should (equal r7 'no-error))))
(ert-deftest reduce-strign ()
"Test `haskell-utils-reduce-strign' command.
Whitespace sequences at beginning of lines should be replaced
with single whitespace, all newline characters should be
removed."
(should (string-equal "" (haskell-utils-reduce-string "\n")))
(should (string-equal "" (haskell-utils-reduce-string "\r\n")))
(should (string-equal " " (haskell-utils-reduce-string " \n")))
(should (string-equal
"TestTest"
(haskell-utils-reduce-string "Test\nTest")))
(should (string-equal
"Test Test"
(haskell-utils-reduce-string "Test\n Test")))
(should (string-equal
" Test Test"
(haskell-utils-reduce-string " Test\r\n Test")))
(should (string-equal
" TestTest"
(haskell-utils-reduce-string " Test\r\nTest")))
(should (string-equal
" TestTest Test test"
(haskell-utils-reduce-string " Test\r\nTest\n Test test"))))
(ert-deftest post-command-hooks ()
"Test commands related `haskell-utils-async-post-command-flag'.
Tests flag updates and `post-command-hook' cleanup."
(with-temp-switch-to-buffer
;; set some random value to flag to test that it will be reset
(setq haskell-utils-async-post-command-flag "non nil")
(haskell-utils-async-watch-changes)
;; now flag should be empty
(should (null haskell-utils-async-post-command-flag))
;; execute some commands
(save-excursion (insert "Hello World!"))
(execute-kbd-macro (kbd "SPC"))
;; now flag should not be empty
(should (not (null haskell-utils-async-post-command-flag)))
;; check that hook was installed
(should (cl-member #'haskell-utils-async-update-post-command-flag
post-command-hook
:test #'equal))
;; check that flag was cleaned up
(haskell-utils-async-stop-watching-changes (current-buffer))
(should (null haskell-utils-async-post-command-flag))
;; check that hook was removed
(should (not (cl-member #'haskell-utils-async-update-post-command-flag
post-command-hook
:test #'equal)))))
;;; haskell-utils-tests.el ends here
|