Created
February 24, 2012 11:48
-
-
Save nicferrier/1900476 to your computer and use it in GitHub Desktop.
elnode's mock process macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defmacro elnode--mock-process (process-bindings &rest body) | |
| "Allow easier elnode testing by mocking the process functions. | |
| For example: | |
| (elnode--mock-process (:elnode-http-params | |
| (:elnode-http-method \"GET\") | |
| (:elnode-http-query \"a=10\")) | |
| (should (equal 10 (elnode-http-param 't \"a\"))) | |
| ) | |
| Causes: | |
| (process-get anything :elnode-http-method) | |
| to always return \"GET\". | |
| 'process-put' is also remapped, currently to swallow any setting. | |
| 'process-buffer' is also remapped, to deliver the value of the | |
| key ':buffer' if present and a dummy buffer otherwise. | |
| This is a work in progress - not sure what we'll return yet." | |
| (declare (indent defun)) | |
| (declare (debug t)) | |
| (let ((pvvar (make-symbol "pv")) | |
| (pvbuf (make-symbol "buf")) | |
| (result (make-symbol "result"))) | |
| `(let | |
| ;; Turn the list of bindings into an alist | |
| (,result | |
| (,pvvar | |
| (list | |
| ,@(loop | |
| for f in | |
| ;; We need to make sure there is always something in this alist | |
| (append | |
| (list :elnode-mock-process t) | |
| process-bindings) | |
| collect | |
| (if (listp f) | |
| (list 'cons `(quote ,(car f)) (cadr f)) | |
| (list 'cons `,f nil))))) | |
| ;; Make a dummy buffer variable for the process - we fill | |
| ;; this in dynamically in 'process-buffer | |
| (,pvbuf)) | |
| ;; Rebind the process function interface | |
| (flet ((process-get | |
| (proc key) | |
| ;;(message "override pget called %s" key) | |
| (let ((pair (assoc key ,pvvar))) | |
| ;;(message "override pget called %s %s" key pair) | |
| (if pair | |
| (cdr pair)))) | |
| (process-put ; Only adds, doesn't edit. | |
| (proc key value) | |
| ;;(message "override pput called %s %s" key value) | |
| (nconc ,pvvar (list (cons key value))) | |
| ;;(message "pput -> %s" ,pvvar) | |
| ) | |
| (get-or-create-pvbuf | |
| (proc &optional specified-buf) | |
| (message "get-or-create proc buffer called") | |
| (if (bufferp ,pvbuf) | |
| (progn | |
| (message "returning buffer %s" ,pvbuf) | |
| ,pvbuf) | |
| (setq ,pvbuf | |
| (if elnode-require-specified-buffer | |
| (if (bufferp specified-buf) | |
| specified-buf | |
| nil) | |
| (or specified-buf | |
| (get-buffer-create | |
| (generate-new-buffer-name | |
| "* elnode mock proc buf *"))))) | |
| ;; If we've got a buffer value then insert it. | |
| (when (assoc :buffer ,pvvar) | |
| (with-current-buffer ,pvbuf | |
| (insert (cdr (assoc :buffer ,pvvar))))) | |
| ,pvbuf)) | |
| (process-send-string | |
| (proc str) | |
| (with-current-buffer (get-or-create-pvbuf proc) | |
| (save-excursion | |
| (goto-char (point-max)) | |
| (insert str)))) | |
| (process-contact | |
| (proc &optional arg) | |
| (list "localhost" 8000)) | |
| (process-buffer | |
| (proc) | |
| (get-or-create-pvbuf proc)) | |
| (set-process-buffer | |
| (proc buffer) | |
| (get-or-create-pvbuf proc buffer))) | |
| (setq ,result ,@body)) | |
| ;; Now clean up | |
| (when (bufferp ,pvbuf) | |
| (with-current-buffer ,pvbuf | |
| (set-buffer-modified-p nil) | |
| (kill-buffer ,pvbuf))) | |
| ;; Now return whatever the body returned | |
| ,result))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment