Skip to content

Utils coverage #1089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 137 additions & 60 deletions tests/haskell-utils-tests.el
Original file line number Diff line number Diff line change
@@ -1,102 +1,179 @@
;;; haskell-utils-tests.el --- Tests for Haskell utilities package

;; Copyright © 2016 Athur Fayzrakhmanov. All rights reserved.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't treat this as self-praise or etc. this is GPL requirement. Also everyone who contribute to haskell-mode files should list themselves as authors.


;; 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-utils)

(defun insert-lines (&rest lines)
"Insert all LINES in current buffer."
(dolist (line lines)
(insert (concat line "\n"))))

(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)))))
(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)))))
(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)))))
(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)))))
(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)))))
(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)))))
(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)))))
(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)))))
(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)))))
(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)))))
(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)))))
(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)))))
(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))
;; note `insert-line' inserts one extra newline, go up one line
(forward-line -1)
(goto-char (line-end-position))
(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)))))
;;; haskell-utils-tests.el ends here