aboutsummaryrefslogtreecommitdiff
path: root/test/mastodon-media-tests.el
diff options
context:
space:
mode:
authorHolger Dürer <me@hdurer.net>2021-11-02 20:30:27 +0100
committerHolger Dürer <me@hdurer.net>2021-11-06 16:09:19 +0100
commit93950dbee4165c733fd8e0a4938fd7d0f462d908 (patch)
tree8a25fb5555800fc6c60159e8bbce2059b87f6ac8 /test/mastodon-media-tests.el
parentb9d5d2ee57855653c32fe2fe2a495e5a3a038acf (diff)
Reformat all code.
Basically, in Emacs for each file: select all text and `indent-region`. - This also removes one redundant comment, and - fixes an error with json decoding where the `json-read-from-string` was actually not within the intended `unless` clause (which explains the warning about "result of (string-equal "" json-string) will be ignored" which I never understood.
Diffstat (limited to 'test/mastodon-media-tests.el')
-rw-r--r--test/mastodon-media-tests.el266
1 files changed, 133 insertions, 133 deletions
diff --git a/test/mastodon-media-tests.el b/test/mastodon-media-tests.el
index 20993f9..b537dfe 100644
--- a/test/mastodon-media-tests.el
+++ b/test/mastodon-media-tests.el
@@ -3,143 +3,143 @@
(ert-deftest mastodon-media:get-avatar-rendering ()
"Should return text with all expected properties."
(with-mock
- (mock (image-type-available-p 'imagemagick) => t)
- (mock (create-image * (when (version< emacs-version "27.1") 'imagemagick) t :height 123) => :mock-image)
-
- (let* ((mastodon-media--avatar-height 123)
- (result (mastodon-media--get-avatar-rendering "http://example.org/img.png"))
- (result-no-properties (substring-no-properties result))
- (properties (text-properties-at 0 result)))
- (should (string= " " result-no-properties))
- (should (string= "http://example.org/img.png" (plist-get properties 'media-url)))
- (should (eq 'needs-loading (plist-get properties 'media-state)))
- (should (eq 'avatar (plist-get properties 'media-type)))
- (should (eq :mock-image (plist-get properties 'display))))))
+ (mock (image-type-available-p 'imagemagick) => t)
+ (mock (create-image * (when (version< emacs-version "27.1") 'imagemagick) t :height 123) => :mock-image)
+
+ (let* ((mastodon-media--avatar-height 123)
+ (result (mastodon-media--get-avatar-rendering "http://example.org/img.png"))
+ (result-no-properties (substring-no-properties result))
+ (properties (text-properties-at 0 result)))
+ (should (string= " " result-no-properties))
+ (should (string= "http://example.org/img.png" (plist-get properties 'media-url)))
+ (should (eq 'needs-loading (plist-get properties 'media-state)))
+ (should (eq 'avatar (plist-get properties 'media-type)))
+ (should (eq :mock-image (plist-get properties 'display))))))
(ert-deftest mastodon-media:get-media-link-rendering ()
"Should return text with all expected properties."
(with-mock
- (mock (create-image * nil t) => :mock-image)
-
- (let* ((mastodon-media--preview-max-height 123)
- (result (mastodon-media--get-media-link-rendering "http://example.org/img.png"))
- (result-no-properties (substring-no-properties result))
- (properties (text-properties-at 0 result)))
- (should (string= "[img] " result-no-properties))
- (should (string= "http://example.org/img.png" (plist-get properties 'media-url)))
- (should (eq 'needs-loading (plist-get properties 'media-state)))
- (should (eq 'media-link (plist-get properties 'media-type)))
- (should (eq :mock-image (plist-get properties 'display))))))
+ (mock (create-image * nil t) => :mock-image)
+
+ (let* ((mastodon-media--preview-max-height 123)
+ (result (mastodon-media--get-media-link-rendering "http://example.org/img.png"))
+ (result-no-properties (substring-no-properties result))
+ (properties (text-properties-at 0 result)))
+ (should (string= "[img] " result-no-properties))
+ (should (string= "http://example.org/img.png" (plist-get properties 'media-url)))
+ (should (eq 'needs-loading (plist-get properties 'media-state)))
+ (should (eq 'media-link (plist-get properties 'media-type)))
+ (should (eq :mock-image (plist-get properties 'display))))))
(ert-deftest mastodon-media:load-image-from-url:avatar-with-imagemagic ()
"Should make the right call to url-retrieve."
(let ((url "http://example.org/image.png")
(mastodon-media--avatar-height 123))
(with-mock
- (mock (image-type-available-p 'imagemagick) => t)
- (mock (create-image
- *
- (when (version< emacs-version "27.1") 'imagemagick)
- t :height 123) => '(image foo))
- (mock (copy-marker 7) => :my-marker )
- (mock (url-retrieve
- url
- #'mastodon-media--process-image-response
- `(:my-marker (:height 123) 1 ,url))
- => :called-as-expected)
-
- (with-temp-buffer
- (insert (concat "Start:"
- (mastodon-media--get-avatar-rendering "http://example.org/img.png")
- ":rest"))
-
- (should (eq :called-as-expected (mastodon-media--load-image-from-url url 'avatar 7 1)))))))
+ (mock (image-type-available-p 'imagemagick) => t)
+ (mock (create-image
+ *
+ (when (version< emacs-version "27.1") 'imagemagick)
+ t :height 123) => '(image foo))
+ (mock (copy-marker 7) => :my-marker )
+ (mock (url-retrieve
+ url
+ #'mastodon-media--process-image-response
+ `(:my-marker (:height 123) 1 ,url))
+ => :called-as-expected)
+
+ (with-temp-buffer
+ (insert (concat "Start:"
+ (mastodon-media--get-avatar-rendering "http://example.org/img.png")
+ ":rest"))
+
+ (should (eq :called-as-expected (mastodon-media--load-image-from-url url 'avatar 7 1)))))))
(ert-deftest mastodon-media:load-image-from-url:avatar-without-imagemagic ()
"Should make the right call to url-retrieve."
(let ((url "http://example.org/image.png"))
(with-mock
- (mock (image-type-available-p 'imagemagick) => nil)
- (mock (create-image * nil t) => '(image foo))
- (mock (copy-marker 7) => :my-marker )
- (mock (url-retrieve
- url
- #'mastodon-media--process-image-response
- `(:my-marker () 1 ,url))
- => :called-as-expected)
-
- (with-temp-buffer
- (insert (concat "Start:"
- (mastodon-media--get-avatar-rendering "http://example.org/img.png")
- ":rest"))
-
- (should (eq :called-as-expected (mastodon-media--load-image-from-url url 'avatar 7 1)))))))
+ (mock (image-type-available-p 'imagemagick) => nil)
+ (mock (create-image * nil t) => '(image foo))
+ (mock (copy-marker 7) => :my-marker )
+ (mock (url-retrieve
+ url
+ #'mastodon-media--process-image-response
+ `(:my-marker () 1 ,url))
+ => :called-as-expected)
+
+ (with-temp-buffer
+ (insert (concat "Start:"
+ (mastodon-media--get-avatar-rendering "http://example.org/img.png")
+ ":rest"))
+
+ (should (eq :called-as-expected (mastodon-media--load-image-from-url url 'avatar 7 1)))))))
(ert-deftest mastodon-media:load-image-from-url:media-link-with-imagemagic ()
"Should make the right call to url-retrieve."
(let ((url "http://example.org/image.png"))
(with-mock
- (mock (image-type-available-p 'imagemagick) => t)
- (mock (create-image * nil t) => '(image foo))
- (mock (copy-marker 7) => :my-marker )
- (mock (url-retrieve
- "http://example.org/image.png"
- #'mastodon-media--process-image-response
- '(:my-marker (:max-height 321) 5 "http://example.org/image.png"))
- => :called-as-expected)
- (with-temp-buffer
- (insert (concat "Start:"
- (mastodon-media--get-media-link-rendering url)
- ":rest"))
- (let ((mastodon-media--preview-max-height 321))
- (should (eq :called-as-expected (mastodon-media--load-image-from-url url 'media-link 7 5))))))))
+ (mock (image-type-available-p 'imagemagick) => t)
+ (mock (create-image * nil t) => '(image foo))
+ (mock (copy-marker 7) => :my-marker )
+ (mock (url-retrieve
+ "http://example.org/image.png"
+ #'mastodon-media--process-image-response
+ '(:my-marker (:max-height 321) 5 "http://example.org/image.png"))
+ => :called-as-expected)
+ (with-temp-buffer
+ (insert (concat "Start:"
+ (mastodon-media--get-media-link-rendering url)
+ ":rest"))
+ (let ((mastodon-media--preview-max-height 321))
+ (should (eq :called-as-expected (mastodon-media--load-image-from-url url 'media-link 7 5))))))))
(ert-deftest mastodon-media:load-image-from-url:media-link-without-imagemagic ()
"Should make the right call to url-retrieve."
(let ((url "http://example.org/image.png"))
(with-mock
- (mock (image-type-available-p 'imagemagick) => nil)
- (mock (create-image * nil t) => '(image foo))
- (mock (copy-marker 7) => :my-marker )
- (mock (url-retrieve
- "http://example.org/image.png"
- #'mastodon-media--process-image-response
- '(:my-marker () 5 "http://example.org/image.png"))
- => :called-as-expected)
-
- (with-temp-buffer
- (insert (concat "Start:"
- (mastodon-media--get-avatar-rendering url)
- ":rest"))
- (let ((mastodon-media--preview-max-height 321))
- (should (eq :called-as-expected (mastodon-media--load-image-from-url url 'media-link 7 5))))))))
+ (mock (image-type-available-p 'imagemagick) => nil)
+ (mock (create-image * nil t) => '(image foo))
+ (mock (copy-marker 7) => :my-marker )
+ (mock (url-retrieve
+ "http://example.org/image.png"
+ #'mastodon-media--process-image-response
+ '(:my-marker () 5 "http://example.org/image.png"))
+ => :called-as-expected)
+
+ (with-temp-buffer
+ (insert (concat "Start:"
+ (mastodon-media--get-avatar-rendering url)
+ ":rest"))
+ (let ((mastodon-media--preview-max-height 321))
+ (should (eq :called-as-expected (mastodon-media--load-image-from-url url 'media-link 7 5))))))))
(ert-deftest mastodon-media:load-image-from-url:url-fetching-fails ()
"Should cope with failures in url-retrieve."
(let ((url "http://example.org/image.png")
(mastodon-media--avatar-height 123))
(with-mock
- (mock (image-type-available-p 'imagemagick) => t)
- (mock (create-image
- *
- (when (version< emacs-version "27.1") 'imagemagick)
- t :height 123) => '(image foo))
- (stub url-retrieve => (error "url-retrieve failed"))
-
- (with-temp-buffer
- (insert (concat "Start:"
- (mastodon-media--get-avatar-rendering "http://example.org/img.png")
- ":rest"))
-
- (should (eq :loading-failed (mastodon-media--load-image-from-url url 'avatar 7 1)))
- ;; the media state was updated so we won't load this again:
- (should (eq 'loading-failed (get-text-property 7 'media-state)))))))
+ (mock (image-type-available-p 'imagemagick) => t)
+ (mock (create-image
+ *
+ (when (version< emacs-version "27.1") 'imagemagick)
+ t :height 123) => '(image foo))
+ (stub url-retrieve => (error "url-retrieve failed"))
+
+ (with-temp-buffer
+ (insert (concat "Start:"
+ (mastodon-media--get-avatar-rendering "http://example.org/img.png")
+ ":rest"))
+
+ (should (eq :loading-failed (mastodon-media--load-image-from-url url 'avatar 7 1)))
+ ;; the media state was updated so we won't load this again:
+ (should (eq 'loading-failed (get-text-property 7 'media-state)))))))
(ert-deftest mastodon-media:process-image-response ()
"Should process the HTTP response and adjust the source buffer."
(with-temp-buffer
(with-mock
- (let ((source-buffer (current-buffer))
+ (let ((source-buffer (current-buffer))
used-marker
saved-marker)
(insert "start:")
@@ -175,35 +175,35 @@
(ert-deftest mastodon-media:inline-images ()
"Should process all media in buffer."
(with-mock
- ;; Stub needed for the test setup:
- (stub create-image => '(image ignored))
-
- (let (marker-media-link marker-media-link-bad-url marker-false-media marker-avatar)
- (with-temp-buffer
- (insert "Some text before\n")
- (setq marker-media-link (copy-marker (point)))
- (insert (mastodon-media--get-media-link-rendering "http://example.org/i.jpg")
- " some more text ")
- (setq marker-media-link-bad-url (copy-marker (point)))
- (insert (mastodon-media--get-media-link-rendering "/files/small/missing.png")
- " some more text ")
- (setq marker-false-media (copy-marker (point)))
- (insert
- ;; text that looks almost like an avatar but lacks the media-url property
- (propertize "this won't be processed"
- 'media-state 'needs-loading
- 'media-type 'avatar)
- "even more text ")
- (setq marker-avatar (copy-marker (point)))
- (insert (mastodon-media--get-avatar-rendering "http://example.org/avatar.png")
- " end of text")
- (goto-char (point-min))
-
- ;; stub for the actual test:
- (stub mastodon-media--load-image-from-url)
- (mastodon-media--inline-images (point-min) (point-max))
-
- (should (eq 'loading (get-text-property marker-media-link 'media-state)))
- (should (eq 'invalid-url (get-text-property marker-media-link-bad-url 'media-state)))
- (should (eq 'loading (get-text-property marker-avatar 'media-state)))
- (should (eq 'needs-loading (get-text-property marker-false-media 'media-state)))))))
+ ;; Stub needed for the test setup:
+ (stub create-image => '(image ignored))
+
+ (let (marker-media-link marker-media-link-bad-url marker-false-media marker-avatar)
+ (with-temp-buffer
+ (insert "Some text before\n")
+ (setq marker-media-link (copy-marker (point)))
+ (insert (mastodon-media--get-media-link-rendering "http://example.org/i.jpg")
+ " some more text ")
+ (setq marker-media-link-bad-url (copy-marker (point)))
+ (insert (mastodon-media--get-media-link-rendering "/files/small/missing.png")
+ " some more text ")
+ (setq marker-false-media (copy-marker (point)))
+ (insert
+ ;; text that looks almost like an avatar but lacks the media-url property
+ (propertize "this won't be processed"
+ 'media-state 'needs-loading
+ 'media-type 'avatar)
+ "even more text ")
+ (setq marker-avatar (copy-marker (point)))
+ (insert (mastodon-media--get-avatar-rendering "http://example.org/avatar.png")
+ " end of text")
+ (goto-char (point-min))
+
+ ;; stub for the actual test:
+ (stub mastodon-media--load-image-from-url)
+ (mastodon-media--inline-images (point-min) (point-max))
+
+ (should (eq 'loading (get-text-property marker-media-link 'media-state)))
+ (should (eq 'invalid-url (get-text-property marker-media-link-bad-url 'media-state)))
+ (should (eq 'loading (get-text-property marker-avatar 'media-state)))
+ (should (eq 'needs-loading (get-text-property marker-false-media 'media-state)))))))