Rename "message" to "frame" and write tests using ert

master
Hugo Thunnissen 6 years ago
parent 1de0721ec6
commit 4b78f9c3ed

@ -8,11 +8,11 @@
(message "Not implemented yet")) (message "Not implemented yet"))
;; TODO: make this send more than just the command ;; 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" "Send a message to a STOMP process"
(process-send-string (process-send-string
process 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) (defun stomp-filter-function (callback)
"Filter function to be used with processes" "Filter function to be used with processes"
@ -21,45 +21,45 @@
(with-current-buffer (process-buffer proc) (with-current-buffer (process-buffer proc)
(goto-char (point-max)) (goto-char (point-max))
(insert string) (insert string)
(let ((message (stomp-shift-message (current-buffer)))) (let ((frame (stomp-shift-frame (current-buffer))))
(if message (funcall callback message)))))) (if frame (funcall callback frame))))))
(defun stomp-shift-message (buffer) (defun stomp-shift-frame (buffer)
"Read a message from a buffer. After reading a message, it will "Read a frame from a buffer. After reading a frame, it will
be deleted from the buffer." be deleted from the buffer."
(let ((message (stomp-read-message buffer))) (let ((frame (stomp-read-frame buffer)))
(cond (cond
(message (stomp-delete-message buffer) message) (frame (stomp-delete-frame buffer) frame)
(t nil)))) (t nil))))
(defun stomp-delete-message (buffer) (defun stomp-delete-frame (buffer)
"Delete the top-most messsage in a buffer" "Delete the top-most messsage in a buffer"
(with-current-buffer 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) (defun stomp-read-frame (buffer)
"Attempt to read a single message from a buffer. "Attempt to read a single frame from a buffer.
The buffer may contain multiple messages, but only the first The buffer may contain multiple frames, but only the first
message (from top to bottom) will be read. If there are no frame (from top to bottom) will be read. If there are no
messages in the buffer, nil will be returned." frames in the buffer, nil will be returned."
(with-current-buffer buffer (with-current-buffer buffer
(goto-char (point-min)) (goto-char (point-min))
(while (search-forward "\r" nil t) (while (search-forward "\r" nil t)
(replace-match "")) (replace-match ""))
(let ((message ()) (let ((frame ())
(headers-end-point (stomp-find-headers-end-point buffer)) (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)) (goto-char (point-min))
(cond (cond
((and message-end-point headers-end-point) ((and frame-end-point headers-end-point)
(map-put message 'command (current-word)) (map-put frame 'command (current-word))
(map-put message 'headers (stomp-read-headers buffer headers-end-point)) (map-put frame 'headers (stomp-read-headers buffer headers-end-point))
;; TODO: take content-length header into account ;; 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))))) (t nil)))))
(defun stomp-read-headers (buffer end-point &optional headers) (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 (with-current-buffer buffer
(unless headers (goto-char (point-min))) (unless headers (goto-char (point-min)))
(forward-line) (forward-line)
@ -72,8 +72,8 @@
(car header) (car header)
(replace-regexp-in-string "\n$" "" (car (last header)))))))))) (replace-regexp-in-string "\n$" "" (car (last header))))))))))
(defun stomp-find-message-end-point (buffer) (defun stomp-find-frame-end-point (buffer)
"Find the null byte at the end of a STOMP message" "Find the null byte at the end of a STOMP frame"
(with-current-buffer buffer (with-current-buffer buffer
(goto-char (point-min)) (goto-char (point-min))
(search-forward "\u0000") (search-forward "\u0000")

@ -1,27 +1,59 @@
#!/bin/sh #!/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) (setq cwd default-directory)
(defun stomp-message-buffer (test-file) (defun stomp-frame-buffer (test-file)
"Create a buffer with a stomp message in it for testing purposes" "Create a buffer with a stomp frame in it for testing purposes"
(find-file (format "%s/testdata/%s" cwd test-file))) (find-file (format "%s/testdata/%s" cwd test-file)))
(defun simple-message-buffer () (defun simple-frame-buffer ()
"Simple buffer to be used for testing" "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" "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 (load (format "%sstomp.el" default-directory) nil t)
(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)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;; 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")))))

Binary file not shown.
Loading…
Cancel
Save