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
97
98
99
100
101
102
103
104
| | ;;; sieve-manage-tests.el --- tests for sieve-manage.el -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Free Software Foundation, Inc.
;; Author: Kai Tetzlaff <emacs@tetzco.de>
;; 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 <https://www.gnu.org/licenses/>.
;;; 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
|