1 ;;; sort-tests.el --- Tests for sort.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 (defun sort-tests-random-word (n)
26 (mapconcat (lambda (_) (string (let ((c (random 52)))
27 (+ (if (> c
25) 71 65)
29 (make-list n nil
) ""))
31 (defun sort-tests--insert-words-sort-and-compare (words separator function reverse less-predicate
)
40 (funcall function reverse
(point-min) (point-max))
43 (let ((x (sort (copy-sequence words
) less-predicate
)))
44 (if reverse
(reverse x
) x
))
46 (should (string= (substring (buffer-string) 0 -
1) sorted-words
)))))
48 ;;; This function uses randomly generated tests and should satisfy
49 ;;; most needs for this lib.
50 (cl-defun sort-tests-test-sorter-function (separator function
&key generator less-pred noreverse
)
51 "Check that FUNCTION correctly sorts words separated by SEPARATOR.
52 This checks whether it is equivalent to sorting a list of such
53 words via LESS-PREDICATE, and then inserting them separated by
55 LESS-PREDICATE defaults to `string-lessp'.
56 GENERATOR is a function called with one argument that returns a
57 word, it defaults to `sort-tests-random-word'.
58 NOREVERSE means that the first arg of FUNCTION is not used for
61 ;; Sort n words of length n.
62 (let ((words (mapcar (or generator
#'sort-tests-random-word
) (make-list n n
)))
64 (less-pred (or less-pred
#'string
<)))
65 (sort-tests--insert-words-sort-and-compare words separator function nil less-pred
)
67 (sort-tests--insert-words-sort-and-compare
68 words separator function
'reverse less-pred
))
69 (let ((less-pred-case (lambda (a b
) (funcall less-pred
(downcase a
) (downcase b
))))
71 (sort-tests--insert-words-sort-and-compare words separator function nil less-pred-case
)
73 (sort-tests--insert-words-sort-and-compare
74 words separator function
'reverse less-pred-case
))))))
76 (ert-deftest sort-tests--lines
()
77 (sort-tests-test-sorter-function "\n" #'sort-lines
))
79 (ert-deftest sort-tests--paragraphs
()
80 (let ((paragraph-separate "[\s\t\f]*$"))
81 (sort-tests-test-sorter-function "\n\n" #'sort-paragraphs
)))
83 (ert-deftest sort-tests--numeric-fields
()
84 (cl-labels ((field-to-number (f) (string-to-number (car (split-string f
)))))
85 (sort-tests-test-sorter-function "\n" (lambda (_ l r
) (sort-numeric-fields 1 l
(1- r
)))
87 :generator
(lambda (_) (format "%s %s" (random) (sort-tests-random-word 20)))
88 :less-pred
(lambda (a b
) (< (field-to-number a
)
89 (field-to-number b
))))))
91 (ert-deftest sort-tests--fields-1
()
92 (cl-labels ((field-n (f n
) (elt (split-string f
) (1- n
))))
93 (sort-tests-test-sorter-function "\n" (lambda (_ l r
) (sort-fields 1 l
(1- r
)))
95 :generator
(lambda (n) (concat (sort-tests-random-word n
) " " (sort-tests-random-word n
)))
96 :less-pred
(lambda (a b
) (string< (field-n a
1) (field-n b
1))))))
98 (ert-deftest sort-tests--fields-2
()
99 (cl-labels ((field-n (f n
) (elt (split-string f
) (1- n
))))
100 (sort-tests-test-sorter-function "\n" (lambda (_ l r
) (sort-fields 2 l
(1- r
)))
102 :generator
(lambda (n) (concat (sort-tests-random-word n
) " " (sort-tests-random-word n
)))
103 :less-pred
(lambda (a b
) (string< (field-n a
2) (field-n b
2))))))
105 (provide 'sort-tests
)
106 ;;; sort-tests.el ends here