;;; erc-tests.el --- Tests for erc. -*- lexical-binding:t -*- ;; Copyright (C) 2020-2021 Free Software Foundation, Inc. ;; Author: Lars Ingebrigtsen ;; This file is part of GNU Emacs. ;; GNU Emacs 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. ;; GNU Emacs 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. If not, see . ;;; Code: (require 'ert) (require 'erc) (require 'erc-ring) (ert-deftest erc--read-time-period () (cl-letf (((symbol-function 'read-string) (lambda (&rest _) ""))) (should (equal (erc--read-time-period "foo: ") nil))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) " "))) (should (equal (erc--read-time-period "foo: ") nil))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) " 432 "))) (should (equal (erc--read-time-period "foo: ") 432))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "432"))) (should (equal (erc--read-time-period "foo: ") 432))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "1h"))) (should (equal (erc--read-time-period "foo: ") 3600))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "1h10s"))) (should (equal (erc--read-time-period "foo: ") 3610))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "1d"))) (should (equal (erc--read-time-period "foo: ") 86400)))) (ert-deftest erc-ring-previous-command-base-case () (ert-info ("Create ring when nonexistent and do nothing") (let (erc-input-ring erc-input-ring-index) (erc-previous-command) (should (ring-p erc-input-ring)) (should (zerop (ring-length erc-input-ring))) (should-not erc-input-ring-index))) (should-not erc-input-ring)) (ert-deftest erc-ring-previous-command () (with-current-buffer (get-buffer-create "*#fake*") (erc-mode) (insert "\n\n") (setq erc-input-marker (make-marker) ; these are all local erc-insert-marker (make-marker) erc-send-completed-hook nil) (set-marker erc-insert-marker (point-max)) (erc-display-prompt) (should (= (point) erc-input-marker)) (add-hook 'erc-pre-send-functions #'erc-add-to-input-ring nil t) ;; (cl-letf (((symbol-function 'erc-process-input-line) (lambda (&rest _) (insert-before-markers (erc-display-message-highlight 'notice "echo: one\n")))) ((symbol-function 'erc-command-no-process-p) (lambda (&rest _) t))) (ert-info ("Create ring, populate, recall") (insert "/one") (erc-send-current-line) (should (ring-p erc-input-ring)) (should (zerop (ring-member erc-input-ring "/one"))) ; equal (should (save-excursion (forward-line -1) (goto-char (point-at-bol)) (looking-at-p "[*]+ echo: one"))) (should-not erc-input-ring-index) (erc-bol) (should (looking-at "$")) (erc-previous-command) (erc-bol) (should (looking-at "/one")) (should (zerop erc-input-ring-index))) (ert-info ("Back to one") (should (= (ring-length erc-input-ring) (1+ erc-input-ring-index))) (erc-previous-command) (should-not erc-input-ring-index) (erc-bol) (should (looking-at "$")) (should (equal (ring-ref erc-input-ring 0) "/one"))) (ert-info ("Swap input after prompt with previous (#bug46339)") (insert "abc") (erc-previous-command) (should (= 1 erc-input-ring-index)) (erc-bol) (should (looking-at "/one")) (should (equal (ring-ref erc-input-ring 0) "abc")) (should (equal (ring-ref erc-input-ring 1) "/one")) (erc-next-command) (erc-bol) (should (looking-at "abc"))))) (when noninteractive (kill-buffer "*#fake*")))