1;;; alchemist-utils-tests.el ---
2
3;; Copyright © 2014-2015 Samuel Tonini
4;;
5;; Author: Samuel Tonini <tonini.samuel@gmail.com>
6
7;; This file is not part of GNU Emacs.
8
9;; This program is free software: you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
13
14;; This program is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22;;; Commentary:
23
24;;; Code:
25
26(require 'test-helper)
27
28(ert-deftest test-cmdlist-runner-builder ()
29  (should (equal (alchemist-utils-build-command "mix help")
30                 "mix help"))
31  (should (equal (alchemist-utils-build-command '("mix" "hex.search"))
32                 "mix hex.search"))
33  (should (equal (alchemist-utils-build-command "elixir -v")
34                 "elixir -v"))
35  (should (equal (alchemist-utils-build-command '("elixirc" ""))
36                 "elixirc")))
37
38(ert-deftest test-utils/is-test-file-p ()
39  "Should return t if visited file is a test file"
40  (with-sandbox
41   (f-touch "this_is_a_test.exs")
42   (find-file "this_is_a_test.exs")
43   (should (alchemist-utils-test-file-p))))
44
45(ert-deftest test-if-string-is-empty ()
46  (should (equal (alchemist-utils-empty-string-p nil)
47                 t))
48  (should (equal (alchemist-utils-empty-string-p "")
49                 t))
50  (should (equal (alchemist-utils-empty-string-p " ")
51                 t))
52  (should (equal (alchemist-utils-empty-string-p "story")
53                 nil))
54  (should (equal (alchemist-utils-empty-string-p "    ")
55                 t)))
56
57(ert-deftest test-utils/prepare-aliases-for-elixir ()
58  (should (equal "[{MyList, List},{AlreadySentError, Plug.Conn.AlreadySentError}]"
59                 (alchemist-utils-prepare-aliases-for-elixir
60                  (list (list "List" "MyList") (list "Plug.Conn.AlreadySentError" "AlreadySentError")))))
61  (should (equal "[]" (alchemist-utils-prepare-aliases-for-elixir '()))))
62
63(ert-deftest test-utils/prepare-modules-for-elixir ()
64  (should (equal "[Ek,Behaviour,Plug.Conn]"
65                 (alchemist-utils-prepare-modules-for-elixir
66                  (list "Ek" "Behaviour" "Plug.Conn"))))
67  (should (equal "[]" (alchemist-utils-prepare-modules-for-elixir '("")))))
68
69(ert-deftest test-utils/snakecase-to-camelcase ()
70  (should (equal "MyCustomModule" (alchemist-utils--snakecase-to-camelcase "my_custom_module")))
71  (should (equal "Foo" (alchemist-utils--snakecase-to-camelcase "foo"))))
72
73(ert-deftest test-utils/add-ext-to-path-if-not-present ()
74  (should (equal "foo.ex" (alchemist-utils-add-ext-to-path-if-not-present "foo.ex" ".ex")))
75  (should (equal "foo.ex" (alchemist-utils-add-ext-to-path-if-not-present "foo" ".ex")))
76  (should (equal "foo.ex.exs" (alchemist-utils-add-ext-to-path-if-not-present "foo.ex" ".exs"))))
77
78(ert-deftest test-utils/path-to-module-name ()
79  (should (equal "MyApp.MyModule" (alchemist-utils-path-to-module-name "my_app/my_module.ex")))
80  (should (equal "Foo.Bar" (alchemist-utils-path-to-module-name "/foo/bar")))
81  (should (equal "Foo" (alchemist-utils-path-to-module-name "//foo//"))))
82
83(ert-deftest test-utils/remove-dot-at-the-end-of-string ()
84  (should (equal "Module" (alchemist-utils-remove-dot-at-the-end "Module.")))
85  (should (equal "Module.Foo" (alchemist-utils-remove-dot-at-the-end "Module.Foo."))))
86
87(ert-deftest test-utils/count-char-in-string ()
88  (should (equal 5 (alchemist-utils-count-char-occurence "\\." "This.Is.A.Long.One.")))
89  (should (equal 2 (alchemist-utils-count-char-occurence "\\." "My.Module.Namespace")))
90  (should (equal 1 (alchemist-utils-count-char-occurence "\\." "Foo.Bar")))
91  (should (equal 0 (alchemist-utils-count-char-occurence "\\." "List"))))
92
93(ert-deftest test-utils/add-trailing-slash-to-path ()
94  (should (equal "/path/to/some/thing/" (alchemist-utils-add-trailing-slash "/path/to/some/thing")))
95  (should (equal "/path/to/some/thing/" (alchemist-utils-add-trailing-slash "/path/to/some/thing/"))))
96
97(ert-deftest test-utils/occur-in-buffer-p ()
98  (with-temp-buffer
99    (insert "1 2 3 foo 4 5 6")
100    (should (alchemist-utils-occur-in-buffer-p (current-buffer) "f[oO]o"))
101    (should-not (alchemist-utils-occur-in-buffer-p (current-buffer) "bar"))))
102
103(ert-deftest test-utils/jump-to-next-matching-line ()
104  (with-temp-buffer
105    (insert "
106foo
107bar
108foo
109")
110    (alchemist-utils-jump-to-next-matching-line "foo" 'beginning-of-line)
111    (should (equal (point) 2))
112    (alchemist-utils-jump-to-next-matching-line "foo" 'beginning-of-line)
113    (should (equal (point) 10))
114    (alchemist-utils-jump-to-next-matching-line "foo" 'beginning-of-line)
115    (should (equal (point) 2))))
116
117(ert-deftest test-utils/jump-to-previous-regex ()
118  (with-temp-buffer
119    (insert "
120foo
121bar
122foo
123")
124    (alchemist-utils-jump-to-previous-matching-line "foo" 'beginning-of-line)
125    (should (equal (point) 10))
126    (alchemist-utils-jump-to-previous-matching-line "foo" 'beginning-of-line)
127    (should (equal (point) 2))))
128
129(ert-deftest test-utils/get-current-elixir-version ()
130  (should (string-match-p "^[0-9]+\.[0-9]+\.[0-9]+.*$"
131                          (alchemist-utils-elixir-version))))
132
133(ert-deftest test-utils/elixir-version-check-p ()
134  (should (alchemist-utils-elixir-version-check-p 1 3 0 "1.3.0"))
135  (should-not (alchemist-utils-elixir-version-check-p 1 3 1 "1.3.0"))
136  (should-not (alchemist-utils-elixir-version-check-p 1 3 0 "1.2.9"))
137  (should-not (alchemist-utils-elixir-version-check-p 2 0 0 "1.3.0"))
138  (should (alchemist-utils-elixir-version-check-p 1 3 0 "2.0.0")))
139
140(provide 'alchemist-utils-tests)
141
142;;; alchemist-utils-tests.el ends here
143