diff --git a/stomp.el b/stomp.el index c866163..07773d1 100644 --- a/stomp.el +++ b/stomp.el @@ -8,11 +8,11 @@ (message "Not implemented yet")) ;; TODO: make this send more than just the command -(defun stomp-send-message (process message) +(defun stomp-send-frame (process frame) "Send a message to a STOMP process" (process-send-string process - (format "%s\n\n\u0000\n" (alist-get 'command message)))) + (format "%s\n\n\u0000\n" (alist-get 'command frame)))) (defun stomp-filter-function (callback) "Filter function to be used with processes" @@ -21,45 +21,45 @@ (with-current-buffer (process-buffer proc) (goto-char (point-max)) (insert string) - (let ((message (stomp-shift-message (current-buffer)))) - (if message (funcall callback message)))))) + (let ((frame (stomp-shift-frame (current-buffer)))) + (if frame (funcall callback frame)))))) -(defun stomp-shift-message (buffer) - "Read a message from a buffer. After reading a message, it will +(defun stomp-shift-frame (buffer) + "Read a frame from a buffer. After reading a frame, it will be deleted from the buffer." - (let ((message (stomp-read-message buffer))) + (let ((frame (stomp-read-frame buffer))) (cond - (message (stomp-delete-message buffer) message) + (frame (stomp-delete-frame buffer) frame) (t nil)))) -(defun stomp-delete-message (buffer) +(defun stomp-delete-frame (buffer) "Delete the top-most messsage in a buffer" (with-current-buffer buffer - (delete-region (point-min) (+ 2 (stomp-find-message-end-point buffer))))) + (delete-region (point-min) (+ 2 (stomp-find-frame-end-point buffer))))) -(defun stomp-read-message (buffer) - "Attempt to read a single message from a buffer. - The buffer may contain multiple messages, but only the first - message (from top to bottom) will be read. If there are no - messages in the buffer, nil will be returned." +(defun stomp-read-frame (buffer) + "Attempt to read a single frame from a buffer. + The buffer may contain multiple frames, but only the first + frame (from top to bottom) will be read. If there are no + frames in the buffer, nil will be returned." (with-current-buffer buffer (goto-char (point-min)) (while (search-forward "\r" nil t) (replace-match "")) - (let ((message ()) + (let ((frame ()) (headers-end-point (stomp-find-headers-end-point buffer)) - (message-end-point (stomp-find-message-end-point buffer))) + (frame-end-point (stomp-find-frame-end-point buffer))) (goto-char (point-min)) (cond - ((and message-end-point headers-end-point) - (map-put message 'command (current-word)) - (map-put message 'headers (stomp-read-headers buffer headers-end-point)) + ((and frame-end-point headers-end-point) + (map-put frame 'command (current-word)) + (map-put frame 'headers (stomp-read-headers buffer headers-end-point)) ;; TODO: take content-length header into account - (map-put message 'content (buffer-substring (+ 1 headers-end-point) message-end-point))) + (map-put frame 'content (buffer-substring (+ 1 headers-end-point) frame-end-point))) (t nil))))) (defun stomp-read-headers (buffer end-point &optional headers) - "Attempt to read the headers of a stomp message in *buffer*" + "Attempt to read the headers of a stomp frame in *buffer*" (with-current-buffer buffer (unless headers (goto-char (point-min))) (forward-line) @@ -72,8 +72,8 @@ (car header) (replace-regexp-in-string "\n$" "" (car (last header)))))))))) -(defun stomp-find-message-end-point (buffer) - "Find the null byte at the end of a STOMP message" +(defun stomp-find-frame-end-point (buffer) + "Find the null byte at the end of a STOMP frame" (with-current-buffer buffer (goto-char (point-min)) (search-forward "\u0000") diff --git a/test.el b/test.el index 2927955..ab9bd78 100755 --- a/test.el +++ b/test.el @@ -1,27 +1,59 @@ #!/bin/sh -":"; exec emacs -Q --script "$0" -- "$@" +":"; exec emacs -Q -batch -l ert -l "$0" -f ert-run-tests-batch-and-exit + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;; SETUP ;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (setq cwd default-directory) -(defun stomp-message-buffer (test-file) - "Create a buffer with a stomp message in it for testing purposes" +(defun stomp-frame-buffer (test-file) + "Create a buffer with a stomp frame in it for testing purposes" (find-file (format "%s/testdata/%s" cwd test-file))) -(defun simple-message-buffer () +(defun simple-frame-buffer () "Simple buffer to be used for testing" - (stomp-message-buffer "stomp_message.txt")) + (stomp-frame-buffer "stomp_frame.txt")) -(defun carriage-return-message-buffer () +(defun carriage-return-frame-buffer () "Simple buffer with carriage returns" - (stomp-message-buffer "stomp_message_cr.txt")) + (stomp-frame-buffer "stomp_frame_cr.txt")) -(load (format "%sstomp.el" default-directory) nil t) +(defun multiple-frame-buffer () + "Buffer with multiple frames" + (stomp-frame-buffer "multiple_frames.txt")) -;; Simple test for basic buffer -(setq recieved (stomp-read-message (simple-message-buffer))) -(pp (alist-get 'command recieved)) - -;; Test for buffer containing carriage returns -(pp (stomp-read-message (carriage-return-message-buffer))) +(load (format "%sstomp.el" default-directory) nil t) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;; START UNIT TESTS ;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(ert-deftest can-read-simple-frame () + "Can read a simple message with headers and content" + (setq recieved (stomp-read-frame (simple-frame-buffer))) + + (should (equal (alist-get 'command recieved) "BLA")) + (should (equal (alist-get 'headers recieved) + '(("ok" . "bye") + ("some-header" . "value")))) + (should (equal (alist-get 'content recieved) "content\n"))) + +(ert-deftest can-read-carriage-return-frame () + "Can read a simple frame with headers and content, containing carriage returns" + (setq recieved (stomp-read-frame (carriage-return-frame-buffer))) + + (should (equal (alist-get 'command recieved) "BLA")) + (should (equal (alist-get 'headers recieved) + '(("ok" . "bye") + ("some-header" . "value")))) + (should (equal (alist-get 'content recieved) "content\n"))) + +(ert-deftest can-delete-one-frame () + "Can delete the topmost frame in a buffer" + (let ((buffer (multiple-frame-buffer))) + (stomp-delete-frame buffer) + (with-current-buffer buffer + (should (equal (buffer-string) "BLA\nsome-header:value\nok:bye\n\ncontent\n\u0000\n"))))) diff --git a/testdata/multiple_frames.txt b/testdata/multiple_frames.txt new file mode 100644 index 0000000..8e96564 Binary files /dev/null and b/testdata/multiple_frames.txt differ diff --git a/testdata/stomp_message.txt b/testdata/stomp_frame.txt similarity index 80% rename from testdata/stomp_message.txt rename to testdata/stomp_frame.txt index b7dc5f8..ba671fe 100644 Binary files a/testdata/stomp_message.txt and b/testdata/stomp_frame.txt differ diff --git a/testdata/stomp_message_cr.txt b/testdata/stomp_frame_cr.txt similarity index 80% rename from testdata/stomp_message_cr.txt rename to testdata/stomp_frame_cr.txt index e1a94c5..0999eb5 100644 Binary files a/testdata/stomp_message_cr.txt and b/testdata/stomp_frame_cr.txt differ