summaryrefslogtreecommitdiff
path: root/test/lisp/gnus/gnus-search-tests.el
blob: 63469f8d518e2048460403b1bdc8e9e6c1cfac64 (plain)
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
;;; gnus-search-tests.el --- Tests for Gnus' search routines  -*- lexical-binding: t; -*-

;; Copyright (C) 2017, 2021 Free Software Foundation, Inc.

;; Author: Eric Abrahamsen <eric@ericabrahamsen.net>
;; Keywords:

;; This program 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 of the License, or
;; (at your option) any later version.

;; This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Tests for the search parsing, search engines, and their
;; transformations.

;;; Code:

(require 'ert)
(require 'gnus-search)

(ert-deftest gnus-s-parse ()
  "Test basic structural parsing."
  (let ((pairs
         '(("string" . ("string"))
           ("from:john" . ((from . "john")))
           ("here and there" . ("here" and "there"))
           ("here or there" . ((or "here" "there")))
           ("here (there or elsewhere)" . ("here" ((or "there" "elsewhere"))))
           ("here not there" . ("here" (not "there")))
           ("from:boss or not vacation" . ((or (from . "boss") (not "vacation")))))))
    (dolist (p pairs)
      (should (equal (gnus-search-parse-query (car p)) (cdr p))))))

(ert-deftest gnus-s-expand-keyword ()
  "Test expansion of keywords"
  (let ((gnus-search-expandable-keys
         (default-value 'gnus-search-expandable-keys))
        (pairs
         '(("su" . "subject")
           ("sin" . "since"))))
    (dolist (p pairs)
      (should (equal (gnus-search-query-expand-key (car p))
                     (cdr p))))
    (should-error (gnus-search-query-expand-key "s")
                  :type 'gnus-search-parse-error)))

(ert-deftest gnus-s-parse-date ()
  "Test parsing of date expressions."
  (let ((rel-date (encode-time 0 0 0 15 4 2017))
        (pairs
         '(("January" . (nil 1 nil))
           ("2017" . (nil nil 2017))
           ("15" . (15 nil nil))
           ("January 15" . (15 1 nil))
           ("tuesday" . (11 4 2017))
           ("1d" . (14 4 2017))
           ("1w" . (8 4 2017)))))
    (dolist (p pairs)
      (should (equal (gnus-search-query-parse-date (car p) rel-date)
                     (cdr p))))))

(ert-deftest gnus-s-delimited-string ()
  "Test proper functioning of `gnus-search-query-return-string'."
  (with-temp-buffer
    (insert "one\ntwo words\nthree \"words with quotes\"\n\"quotes at start\"\n/alternate \"quotes\"/\n(more bits)")
    (goto-char (point-min))
    (should (string= (gnus-search-query-return-string)
                     "one"))
    (forward-line)
    (should (string= (gnus-search-query-return-string)
                     "two"))
    (forward-line)
    (should (string= (gnus-search-query-return-string)
                     "three"))
    (forward-line)
    (should (string= (gnus-search-query-return-string "\"")
                     "\"quotes at start\""))
    (forward-line)
    (should (string= (gnus-search-query-return-string "/")
                     "/alternate \"quotes\"/"))
    (forward-line)
    (should (string= (gnus-search-query-return-string ")" t)
                     "more bits"))))

(provide 'gnus-search-tests)
;;; search-tests.el ends here