You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.8 KiB
EmacsLisp

#!/bin/sh
":"; exec emacs -Q -batch -l ert -l "$0" -f ert-run-tests-batch-and-exit
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;; SETUP ;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq cwd default-directory)
(defmacro with-testing-buffer (file-name &rest body)
"Execute 'body using a buffer with the contents of file 'file-name,
killing the buffer afterwards"
(list 'with-current-buffer (list 'find-file (format "%s/test/testdata/%s" cwd file-name))
(list 'let (list (list 'result (append '(progn) body)))
'(kill-buffer (current-buffer))
'result)))
(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"
(with-testing-buffer
"stomp_frame.txt"
(setq recieved (stomp-read-frame (current-buffer)))
(should (equal "BLA" (alist-get 'command recieved)))
(should (equal '(("ok" . "bye")
("some-header" . "value"))
(alist-get 'headers recieved)))
(should (equal "content\n" (alist-get 'content recieved)))))
(ert-deftest can-read-carriage-return-frame ()
"Can read a simple frame with headers and content, containing carriage returns"
(with-testing-buffer
"stomp_frame_cr.txt"
(setq recieved (stomp-read-frame (current-buffer)))
(should (equal "BLA" (alist-get 'command recieved)))
(should (equal '(("ok" . "bye")
("some-header" . "value"))
(alist-get 'headers recieved)))
(should (equal "content\r\n" (alist-get 'content recieved)))))
(ert-deftest can-read-fixed-length-frame ()
"Can read a frame that has a content-length header correctly"
(with-testing-buffer
"content_length_frame.txt"
(setq recieved (stomp-read-frame (current-buffer)))
(should (equal "MESSAGE" (alist-get 'command recieved)))
(should (equal '(("destination" . "/banana/kiwi")
("content-length" . "11"))
(alist-get 'headers recieved)))
(should (equal "jwe\u0000jsiwli\u0000" (alist-get 'content recieved)))))
(ert-deftest returns-nil-when-incomplete-frame ()
"When there is no complete frame at the top of a buffer, read-frame
should return nil"
(with-testing-buffer
"incomplete_frame.txt"
(setq recieved (stomp-read-frame (current-buffer)))
(should (equal nil recieved))))
(ert-deftest returns-nil-when-incomplete-frame-no-headers ()
"When no 'end of headers' marker can be found, stomp-read-frame should return nil"
(with-testing-buffer
"incomplete_frame_no_headers.txt"
(setq recieved (stomp-read-frame (current-buffer)))
(should (equal nil recieved))))
(ert-deftest can-delete-one-frame ()
"Can delete the topmost frame in a buffer"
(with-testing-buffer
"multiple_frames.txt"
(let ((buffer (current-buffer)))
(stomp-delete-frame buffer nil)
(with-current-buffer buffer
(should (equal "BLA\nsome-header:value\nok:bye\n\ncontent\n\u0000\n" (buffer-string)))))))
(ert-deftest can-compose-frame ()
"Can compose a valid STOMP frame from a LISP datastructure"
(let ((frame '()) (headers '()))
(map-put frame 'command "SEND")
(map-put headers "header" "value")
(map-put frame 'headers headers)
(map-put frame 'content "Some content")
(should (equal "SEND\nheader:value\n\nSome content\u0000" (stomp-frame-to-string frame)))))
(ert-deftest can-get-frame-header ()
"stomp-frame-header should be able to read arbitrary headers
from a frame"
(setq frame '((headers . (("bla" . "ok") ("random" . "barry")))))
(should (equal "ok" (stomp-frame-header "bla" frame)))
(should (equal "barry" (stomp-frame-header "random" frame))))
(ert-deftest will-return-nil-for-non-existent-header ()
"stomp-frame-header should return nil if a header does not exist"
(setq frame '((headers . (("okay" . "bye")))))
(should (equal nil (stomp-frame-header "bla" frame))))
(ert-deftest will-signal-invald-frame ()
"`stomp-frame-to-string` should signal an invalid frame error
when attempting to send a frame without a command defined in it"
(should-error (stomp-frame-to-string '()) :type 'stomp-invalid-frame))
(ert-deftest will-signal-wrong-type-filter-function ()
"`stomp-filter-function` should signal a `wrong-type-argument`
error when `callback` is not a callback"
(should-error (stomp-filter-function '(not a function) :type 'wrong-type-argument)))
(ert-deftest will-signal-invalid-header ()
"`stomp-read-headers` should signal a `stomp-invalid-frame`
error when a header containing colons is encountered"
(with-testing-buffer
"invalid_header_frame.txt"
(should-error (stomp-read-headers (current-buffer) (point-max)) :type 'stomp-invalid-header)))