|
| 1 | +;;; rg.el-test.el --- rg.el: Tests |
| 2 | + |
| 3 | +;; Copyright (C) 2017 David Landell <[email protected]> |
| 4 | +;; |
| 5 | +;; Author: David Landell <[email protected]> |
| 6 | +;; Homepage: https://github.com/davja/rg.el |
| 7 | + |
| 8 | +;; This file is not part of GNU Emacs. |
| 9 | + |
| 10 | +;; This program is free software; you can redistribute it and/or |
| 11 | +;; modify it under the terms of the GNU General Public License |
| 12 | +;; as published by the Free Software Foundation; either version 3 |
| 13 | +;; of the License, or (at your option) any later version. |
| 14 | + |
| 15 | +;; This program is distributed in the hope that it will be useful, |
| 16 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +;; GNU General Public License for more details. |
| 19 | + |
| 20 | +;; You should have received a copy of the GNU General Public License |
| 21 | +;; along with this program; if not, write to the Free Software |
| 22 | +;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 23 | +;; 02110-1301, USA. |
| 24 | + |
| 25 | +;;; Commentary: |
| 26 | + |
| 27 | +;;; Code: |
| 28 | + |
| 29 | + |
| 30 | +;; Unit tests |
| 31 | + |
| 32 | +(ert-deftest rg-unit/test-case-expand-template () |
| 33 | +"Test that rg-expand-template handles case settings correctly." |
| 34 | + (let ((template "<F> <C> <R>")) |
| 35 | + (let ((case-fold-search t)) |
| 36 | + (should (s-matches? (regexp-anywhere "-i") (rg-expand-template template "foo"))) |
| 37 | + (should-not (s-matches? (regexp-anywhere "-i") (rg-expand-template template "fOo")))) |
| 38 | + (let ((case-fold-search nil)) |
| 39 | + (should-not (s-matches? (regexp-anywhere "-i") (rg-expand-template template "foo"))) |
| 40 | + (should-not (s-matches? (regexp-anywhere "-i") (rg-expand-template template "Foo")))))) |
| 41 | + |
| 42 | +(ert-deftest rg-unit/test-build-template () |
| 43 | +"Test that rg-build-template is correct." |
| 44 | + (let* ((rg-command "rg") |
| 45 | + (rg-custom-type-aliases nil) |
| 46 | + (notype-template (rg-build-template)) |
| 47 | + (type-template (rg-build-template t)) |
| 48 | + (custom-template (rg-build-template t "glob"))) |
| 49 | + (should (s-matches? (regexp-anywhere-but-last "<C>") notype-template)) |
| 50 | + (should (s-matches? (regexp-last "<R>") notype-template)) |
| 51 | + (should-not (s-matches? (regexp-anywhere-but-last "--type <F>") notype-template)) |
| 52 | + (should-not (s-matches? (regexp-anywhere-but-last "--type-add 'custom:") notype-template)) |
| 53 | + |
| 54 | + (should (s-matches? (regexp-anywhere-but-last "--type <F>") type-template)) |
| 55 | + (should-not (s-matches? (regexp-anywhere-but-last "--type-add 'custom:") type-template)) |
| 56 | + |
| 57 | + (should (s-matches? (regexp-anywhere-but-last "--type-add 'custom: *glob'") custom-template)) |
| 58 | + (should (s-matches? (regexp-anywhere-but-last "--type <F>") custom-template)))) |
| 59 | + |
| 60 | + |
| 61 | +;; Integration tests |
| 62 | + |
| 63 | +(ert-deftest rg-integration/test-read-files-default-alias () :tags '(need-rg) |
| 64 | +"Test that rg-read-files detects the current file and selects matching alias." |
| 65 | + (let (prompt) |
| 66 | + (noflet ((completing-read (pr &rest args) (setq prompt pr))) |
| 67 | + (find-file "test/data/foo.el") |
| 68 | + (rg-read-files "foo") |
| 69 | + (should (s-matches? "\[elisp\]" prompt)) |
| 70 | + (find-file "test/data/foo.baz") |
| 71 | + (rg-read-files "foo") |
| 72 | + (should-not (s-matches? "\[.*\]" prompt)) |
| 73 | + (let ((rg-custom-type-aliases '(("test" . "*.baz")))) |
| 74 | + (find-file "test/data/foo.baz") |
| 75 | + (rg-read-files "foo") |
| 76 | + (should (s-matches? "\[test\]" prompt)))))) |
| 77 | + |
| 78 | +(ert-deftest rg-integration/test-search-alias-builtin () :tags '(need-rg) |
| 79 | +"Test that rg builtin aliases works." |
| 80 | + (let ((case-fold-search t)) |
| 81 | + (rg "hello" "elisp" (concat default-directory "test/data")) |
| 82 | + (with-current-buffer "*rg*" |
| 83 | + (should (rg-wait-for-search-result)) |
| 84 | + (let ((bufstr (buffer-substring-no-properties (point-min) (point-max)))) |
| 85 | + (should (= 3 (s-count-matches "foo.el.*hello" bufstr))) |
| 86 | + (should (= 3 (s-count-matches "bar.el.*hello" bufstr))) |
| 87 | + (should (= 0 (s-count-matches "foo.baz.*hello" bufstr))) |
| 88 | + (should (= 0 (s-count-matches "bar.baz.*hello" bufstr))))))) |
| 89 | + |
| 90 | +(ert-deftest rg-integration/test-search-alias-custom () :tags '(need-rg) |
| 91 | +"Test that aliases defined in rg-custom-type-aliases works if explicitly selected." |
| 92 | + (let ((case-fold-search t) |
| 93 | + (rg-custom-type-aliases '(("test" . "*.baz")))) |
| 94 | + (rg "hello" "test" (concat default-directory "test/data")) |
| 95 | + (with-current-buffer "*rg*" |
| 96 | + (should (rg-wait-for-search-result)) |
| 97 | + (let ((bufstr (buffer-substring-no-properties (point-min) (point-max)))) |
| 98 | + (should (= 0 (s-count-matches "foo.el.*hello" bufstr))) |
| 99 | + (should (= 0 (s-count-matches "bar.el.*hello" bufstr))) |
| 100 | + (should (= 3 (s-count-matches "foo.baz.*hello" bufstr))) |
| 101 | + (should (= 3 (s-count-matches "bar.baz.*hello" bufstr))))))) |
| 102 | + |
| 103 | +(ert-deftest rg-integration/test-search-alias-all-custom () :tags '(need-rg) |
| 104 | +"Test that aliases defined in rg-custom-type-aliases works if |
| 105 | + implicitly selected via '--type all'." |
| 106 | +(let ((case-fold-search t) |
| 107 | + (rg-custom-type-aliases '(("test" . "*.baz")))) |
| 108 | + (rg "hello" "all" (concat default-directory "test/data")) |
| 109 | + (with-current-buffer "*rg*" |
| 110 | + (should (rg-wait-for-search-result)) |
| 111 | + (let ((bufstr (buffer-substring-no-properties (point-min) (point-max)))) |
| 112 | + (should (= 3 (s-count-matches "foo.el.*hello" bufstr))) |
| 113 | + (should (= 3 (s-count-matches "bar.el.*hello" bufstr))) |
| 114 | + (should (= 3 (s-count-matches "foo.baz.*hello" bufstr))) |
| 115 | + (should (= 3 (s-count-matches "bar.baz.*hello" bufstr))))))) |
| 116 | + |
| 117 | +(ert-deftest rg-integration/test-search-no-alias() :tags '(need-rg) |
| 118 | +"Test that custom file pattern that is not an alias works." |
| 119 | + (let ((case-fold-search t)) |
| 120 | + (rg "hello" "*.baz" (concat default-directory "test/data")) |
| 121 | + (with-current-buffer "*rg*" |
| 122 | + (should (rg-wait-for-search-result)) |
| 123 | + (let ((bufstr (buffer-substring-no-properties (point-min) (point-max)))) |
| 124 | + (should (= 0 (s-count-matches "foo.el.*hello" bufstr))) |
| 125 | + (should (= 0 (s-count-matches "bar.el.*hello" bufstr))) |
| 126 | + (should (= 3 (s-count-matches "foo.baz.*hello" bufstr))) |
| 127 | + (should (= 3 (s-count-matches "bar.baz.*hello" bufstr))))))) |
| 128 | + |
| 129 | +(ert-deftest rg-integration/test-search-uppercase-regexp () :tags '(need-rg) |
| 130 | +"Test that uppercase search triggers case sensitive search." |
| 131 | + (let ((case-fold-search t)) |
| 132 | + (rg "Hello" "all" (concat default-directory "test/data")) |
| 133 | + (with-current-buffer "*rg*" |
| 134 | + (should (rg-wait-for-search-result)) |
| 135 | + (let ((bufstr (buffer-substring-no-properties (point-min) (point-max)))) |
| 136 | + (should (= 1 (s-count-matches "foo.el.*hello" bufstr))) |
| 137 | + (should (= 1 (s-count-matches "bar.el.*hello" bufstr))))))) |
| 138 | + |
| 139 | +(ert-deftest rg-integration/test-search-case-sensitive-regexp () |
| 140 | +:tags '(need-rg) |
| 141 | +"Test explicit case sensitive search." |
| 142 | + (let ((case-fold-search nil)) |
| 143 | + (rg "hello" "all" (concat default-directory "test/data"))) |
| 144 | + (with-current-buffer "*rg*" |
| 145 | + (should (rg-wait-for-search-result)) |
| 146 | + (let ((case-fold-search t) |
| 147 | + (bufstr (buffer-substring-no-properties (point-min) (point-max)))) |
| 148 | + (should (= 1 (s-count-matches "foo.el.*hello" bufstr))) |
| 149 | + (should (= 1 (s-count-matches "bar.el.*hello" bufstr)))))) |
| 150 | + |
| 151 | +(provide 'rg.el-test) |
| 152 | + |
| 153 | +;;; rg.el-test.el ends here |
0 commit comments