diff --git a/tests/haskell-utils-tests.el b/tests/haskell-utils-tests.el index faad5ea70..acffda417 100644 --- a/tests/haskell-utils-tests.el +++ b/tests/haskell-utils-tests.el @@ -1,102 +1,179 @@ +;;; haskell-utils-tests.el --- Tests for Haskell utilities package + +;; Copyright © 2016 Athur 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-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