;;; sieve-manage-tests.el --- tests for sieve-manage.el -*- lexical-binding: t; -*- ;; Copyright (C) 2022 Free Software Foundation, Inc. ;; Author: Kai Tetzlaff ;; 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 . ;;; Commentary: ;;; Code: (require 'ert) (require 'sieve-manage) (defvar sieve-script-multibyte-unix "if body :matches \"ä\" { stop; }\n" "Simple multibyte sieve script.") (defun mk-literal-s2c (string) "Encode STRING to managesieve 'literal-s2c'." (let ((data (sieve-manage-encode string))) (concat (format "{%d}\r\n" (length data)) data))) (defun mk-rsp-oknobye (type &optional resp-code string) "Encode TYPE, RESP-CODE and STRING to managesieve 'response-oknobye'." (when (memq type '(OK NO BYE)) (concat (mapconcat #'identity (delq nil (list (symbol-name type) (when resp-code (format "(%s)" resp-code)) (when string (format "\"%s\"" (sieve-manage-encode string))))) " ") "\r\n"))) ;; (mk-rsp-oknobye 'OK nil "Getscript completed.") ;; (mk-rsp-oknobye 'NO "TRYLATER" "Server is busy.") (defun mk-rsp-getscript-ok (script) "Encode SCRIPT to managesieve 'response-getscript'." (concat (mk-literal-s2c script) "\r\n" (mk-rsp-oknobye 'OK "Getscript completed."))) ;; (mk-rsp-getscript-ok sieve-script-multibyte-unix) (defun managesieve-getscript (script) "Simulate managesieve getscript response to test `sieve-manage-getscript' function." (let* ((script-name "test.sieve") ;; `sieve-manage-server' and `sieve-manage-port' are used in ;; `sieve-manage-make-process-buffer' (sieve-manage-server) (sieve-manage-port "sieve") (sieve-buffer (sieve-manage-make-process-buffer)) (output-buffer (generate-new-buffer script-name))) ;; use cl-letf to mock some functions in call chain of ;; sieve-manage-getscript (cl-letf (((symbol-function 'sieve-manage-send) ;; simulate sieve server response with a single ;; multibyte character `ä` (lambda (_) (with-current-buffer sieve-buffer (goto-char (point-min)) (insert (mk-rsp-getscript-ok script))))) ((symbol-function 'accept-process-output) (lambda (&optional _ _ _ _) nil)) ((symbol-function 'get-buffer-process) (lambda (_) nil))) ;; extract sieve script from sieve-buffer and put it into ;; output-buffer (sieve-manage-getscript script-name output-buffer sieve-buffer) ;; extract script from output-buffer and return it as a string (let ((script (with-current-buffer output-buffer (set-buffer-modified-p nil) (buffer-string)))) ;; cleanup (kill-buffer sieve-buffer) (kill-buffer output-buffer) (when (get-buffer sieve-manage-log) (kill-buffer sieve-manage-log)) ;; return script script)))) (ert-deftest ert/managesieve-getscript-multibyte () (should (string= sieve-script-multibyte-unix (managesieve-getscript sieve-script-multibyte-unix)))) ;;; sieve-manage-tests.el ends here