diff --git a/test/test.el b/test/test.el index f3d44d1..18b6152 100755 --- a/test/test.el +++ b/test/test.el @@ -8,21 +8,14 @@ (setq cwd default-directory) -(defun stomp-frame-buffer (test-file) - "Create a buffer with a stomp frame in it for testing purposes" - (find-file (format "%s/test/testdata/%s" cwd test-file))) - -(defun simple-frame-buffer () - "Simple buffer to be used for testing" - (stomp-frame-buffer "stomp_frame.txt")) - -(defun carriage-return-frame-buffer () - "Simple buffer with carriage returns" - (stomp-frame-buffer "stomp_frame_cr.txt")) - -(defun multiple-frame-buffer () - "Buffer with multiple frames" - (stomp-frame-buffer "multiple_frames.txt")) +(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 'let (list (list 'buffer + (list 'find-file + (list 'format "%s/test/testdata/%s" cwd file-name)))) + (let ((to-execute (list 'with-current-buffer 'buffer))) + (append to-execute body (list '(kill-buffer buffer)))))) (load (format "%sstomp.el" default-directory) nil t) @@ -33,30 +26,36 @@ (ert-deftest can-read-simple-frame () "Can read a simple message with headers and content" - (setq recieved (stomp-read-frame (simple-frame-buffer))) + (with-testing-buffer + "stomp_frame.txt" + (setq recieved (stomp-read-frame (current-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"))) + (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))) + (with-testing-buffer + "stomp_frame_cr.txt" + (setq recieved (stomp-read-frame (current-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"))) + (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"))))) + (with-testing-buffer + "multiple_frames.txt" + (let ((buffer (current-buffer))) + (stomp-delete-frame buffer) + (with-current-buffer buffer + (should (equal (buffer-string) "BLA\nsome-header:value\nok:bye\n\ncontent\n\u0000\n")))))) (ert-deftest can-compose-frame () "Can compose a valid STOMP frame from a LISP datastructure"