From ae17e87436def764f99b24add4debb5d7a481e1a Mon Sep 17 00:00:00 2001 From: Hugo Heagren Date: Tue, 21 Jun 2022 12:45:50 +0100 Subject: [PATCH 2/2] test-ol: tests for default-description param when inserting links Add tests for various values of `:default-description' in `org-link-parameters'. --- testing/lisp/test-ol.el | 92 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/testing/lisp/test-ol.el b/testing/lisp/test-ol.el index 429bb52ee..9114c6497 100644 --- a/testing/lisp/test-ol.el +++ b/testing/lisp/test-ol.el @@ -625,5 +625,97 @@ See https://github.com/yantar92/org/issues/4." (test-ol-parse-link-in-text "The http://foo.com/(something)?after=parens link")))) +;;; Insert Links + +(defmacro test-ol-with-link-parameters-as (type parameters &rest body) + "Pass TYPE/PARAMETERS to `org-link-parameters' and execute BODY. + +Save the original value of `org-link-parameters', execute +`org-link-set-parameters' with the relevant args, execute BODY +and restore `org-link-parameters'. + +TYPE is as in `org-link-set-parameters'. PARAMETERS is a plist to +be passed to `org-link-set-parameters'." + ;; Copy all keys in `parameters' and their original values to + ;; `orig-parameters'. For `parity': nil = odd, non-nill = even + `(let (parity orig-parameters) + (dolist (p ',parameters) + (unless parity + (setq orig-parameters + (plist-put orig-parameters p (org-link-get-parameter ,type p)))) + (setq parity (not parity))) + ;; Set `parameters' values + (condition-case err + (let ((_ (org-link-set-parameters ,type ,@parameters)) + ;; Do body + (rtn (progn ,@body))) + ;; Restore original values + (apply 'org-link-set-parameters ,type orig-parameters) + ;; Return whatever the body returned + rtn) + ;; In case of error, restore state anyway AND really error + (error + (apply 'org-link-set-parameters ,type orig-parameters) + (signal (car err) (cdr err)))))) + +(defun test-ol-insert-link-get-desc (&optional link-location description) + "Insert link in temp buffer, return description. + +LINK-LOCATION and DESCRIPTION are passed to +`org-insert-link' (COMPLETE-FILE is always nil)." + (org-test-with-temp-text "" + (org-insert-link nil link-location description) + (save-match-data + (when (and + (org-in-regexp org-link-bracket-re 1) + (match-end 2)) + (match-string-no-properties 2))))) + +(defun test-ol/return-foobar (_link-test _desc) + "Return string \"foobar\". + +Take (and ignore) arguments conforming to `:default-description' +API in `org-link-parameters'. Used in test +`test-ol/insert-link-default-description', for the case where +`:default-description' is a function symbol." + "foobar") + +(ert-deftest test-ol/insert-link-default-description () + "Test `:default-description' parameter handling." + ;; String case + (should + (string= + "foobar" + (test-ol-with-link-parameters-as + "id" (:default-description "foobar") + (test-ol-insert-link-get-desc "id:foo-bar")))) + ;; Lambda case + (should + (string= + "foobar" + (test-ol-with-link-parameters-as + "id" (:default-description (lambda (_link-test _desc) "foobar")) + (test-ol-insert-link-get-desc "id:foo-bar")))) + ;; Function symbol case + (should + (string= + "foobar" + (test-ol-with-link-parameters-as + "id" (:default-description #'test-ol/return-foobar) + (test-ol-insert-link-get-desc "id:foo-bar")))) + ;; `:default-description' parameter is defined, but doesn't return a + ;; string + (should-error + (test-ol-with-link-parameters-as + "id" (:default-description #'ignore) + (test-ol-insert-link-get-desc "id:foo-bar"))) + ;; Description argument should override `:default-description' + (should + (string= + "notfoobar" + (test-ol-with-link-parameters-as + "id" (:default-description "foobar") + (test-ol-insert-link-get-desc "id:foo-bar" "notfoobar"))))) + (provide 'test-ol) ;;; test-ol.el ends here -- 2.20.1