diff --git a/ChangeLog b/ChangeLog index af4cf18b..e88f4f2a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2026-02-08 Mats Lidell + +* hywiki.el (hywiki-word-create): Eliminate redundant term. + (hywiki-create-page): Let page-file be the name of the + file. User-error when page-file is not created. + (hywiki-display-referent-type): Remove unreachable error check. + +* test/hywiki-tests.el (hywiki-tests--find-page) + (hywiki-tests--get-page/wikiword-list, hywiki-tests--org-link-store) + (hywiki-tests--create-referent, hywiki-tests--get-buttonize-characters) + (hywiki-tests--non-hook-context-p, hywiki-tests--word-create) + (hywiki-tests--create-page): New tests. + (hywiki-tests--add-referent): Extend test for more coverage. + (hywiki-tests--display-referent-type): Error test case. + (hywiki-tests--add-global-button): Enable local vars. Avoid prompting + user during test. + 2026-02-07 Bob Weiner * test/hywiki-tests.el (hywiki-tests--verify-removal-of-delimiter-updates-face): diff --git a/hywiki.el b/hywiki.el index a118b7a6..295a5578 100644 --- a/hywiki.el +++ b/hywiki.el @@ -874,20 +874,21 @@ See the Info documentation at \"(hyperbole)HyWiki\". (defun hywiki-display-referent-type (wikiword referent) "Display WIKIWORD REFERENT, a cons of ( . ). Function used to display is \"hywiki-display-\"." - (let* ((referent-type (car referent)) ;; a symbol - (referent-value (cdr referent)) - (display-function (intern-soft (concat "hywiki-display-" - (symbol-name referent-type))))) - (when (equal (hywiki-get-singular-wikiword wikiword) (hywiki-word-at-point)) - ;; Set referent attributes of current implicit button - (hattr:set 'hbut:current 'referent-type referent-type) - (hattr:set 'hbut:current 'referent-value referent-value)) - (cond ((fboundp display-function) - (funcall display-function wikiword referent-value)) - ((symbolp referent-type) - (error "(hywiki-display-referent-type): No hywiki-display function for referent type '%s'" referent-type)) - (t - (error "(hywiki-display-referent-type): Referent type must be a symbol, not %s" referent-type))))) + (let ((referent-type (and (consp referent) (car referent)))) + (unless (and referent-type (symbolp referent-type)) + (error "(hywiki-display-referent-type): Referent type must be a symbol, not: referent-type = %S; referent = %S" + referent referent-type)) + (let* ((referent-value (cdr referent)) + (display-function (intern-soft (concat "hywiki-display-" + (symbol-name referent-type))))) + (when (equal (hywiki-get-singular-wikiword wikiword) (hywiki-word-at-point)) + ;; Set referent attributes of current implicit button + (hattr:set 'hbut:current 'referent-type referent-type) + (hattr:set 'hbut:current 'referent-value referent-value)) + (cond ((fboundp display-function) + (funcall display-function wikiword referent-value)) + (t + (error "(hywiki-display-referent-type): No hywiki-display function for referent type '%s'" referent-type)))))) (defun hywiki-display-referent (&optional wikiword prompt-flag) "Display HyWiki WIKIWORD referent or a regular file with WIKIWORD nil. @@ -1318,11 +1319,11 @@ with the page." (unless (stringp wikiword) (setq wikiword (hywiki-word-read-new "Create/Edit HyWikiWord: "))) (setq hkey-value wikiword) - (let ((page-file (hywiki-add-page wikiword t))) + (let ((page-file (cdr (hywiki-add-page wikiword t)))) (if (or message-flag (called-interactively-p 'interactive)) - (when page-file - (message "HyWikiWord '%s' page: \"%s\"" wikiword page-file)) - (user-error "(hywiki-create-page): Invalid HyWikiWord: '%s'; must be capitalized, all alpha" wikiword)) + (if page-file + (message "HyWikiWord '%s' page: \"%s\"" wikiword page-file) + (user-error "(hywiki-create-page): Invalid HyWikiWord: '%s'; must be capitalized, all alpha" wikiword))) page-file)) (defun hywiki-add-page (page-name &optional force-flag) @@ -1400,8 +1401,7 @@ referent." "referent" "page")))) current-prefix-arg)) - (if (or (and hywiki-referent-prompt-flag (null arg)) - arg) + (if (or arg hywiki-referent-prompt-flag) (hywiki-create-referent wikiword t) (hywiki-create-page wikiword t))) diff --git a/test/hywiki-tests.el b/test/hywiki-tests.el index 30868e50..da5b8de3 100644 --- a/test/hywiki-tests.el +++ b/test/hywiki-tests.el @@ -1178,7 +1178,12 @@ Note special meaning of `hywiki-allow-plurals-flag'." (progn (should-not (hywiki-add-referent "notawikiword" referent)) (should (hywiki-add-referent "WikiWord" referent)) - (should (equal referent (hywiki-get-referent "WikiWord")))) + (should (equal referent (hywiki-get-referent "WikiWord"))) + (should (hywiki-add-referent "WikiPage#section" referent)) + (should (equal referent (hywiki-get-referent "WikiPage"))) + (mocklet ((hash-add => nil)) + (let ((err (should-error (hywiki-add-referent "WikiPage" referent) :type 'error))) + (should (string-match-p "Failed: (hash-add (page . /tmp/a.org) WikiPage" (cadr err)))))) (hywiki-tests--delete-hywiki-dir-and-buffer hywiki-directory)))) (ert-deftest hywiki-tests--add-activity () @@ -1235,7 +1240,8 @@ Note special meaning of `hywiki-allow-plurals-flag'." (ert-deftest hywiki-tests--add-global-button () "Verify `hywiki-add-global-button'." - (let ((hywiki-directory (make-temp-file "hywiki" t))) + (let ((hywiki-directory (make-temp-file "hywiki" t)) + (enable-local-variables :all)) (unwind-protect (mocklet ((hargs:read-match => "gbtn")) (should (equal '(global-button . "gbtn") (hywiki-add-global-button "WikiWord")))) @@ -2155,6 +2161,131 @@ expected result." (list #'hywiki-tags-view t nil bn))) (should (= (line-number-at-pos) 3)))))) +(ert-deftest hywiki-tests--display-referent-type () + "Verify error case for `hywiki-display-referent-type'." + (with-temp-buffer + (let ((err (should-error (hywiki-display-referent-type "WikiWord" (cons 'unknown-type 'value)) :type 'error))) + (should (string-match-p "No hywiki-display function for referent type .unknown-type." (cadr err)))))) + +(ert-deftest hywiki-tests--create-referent () + "Verify `hywiki-create-referent'." + (let* ((hywiki-directory (make-temp-file "hywiki" t))) + (unwind-protect + (progn + (mocklet ((hui:menu-act => '(referent))) + (should (equal '(referent) (hywiki-create-referent "WikiWord"))) + (ert-with-message-capture cap + (should (hywiki-create-referent "WikiWord" t)) + (string-match-p "HyWikiWord .WikiWord. referent: (referent)" cap)) + (mocklet ((hywiki-word-read-new => "WikiWord")) + (should (equal '(referent) (hywiki-create-referent nil))))) + (mocklet ((hui:menu-act => nil)) + (let ((err (should-error (hywiki-create-referent "WikiWord") :type 'user-error))) + (should (string-match-p "Invalid HyWikiWord: .WikiWord.; must be capitalized, all alpha" (cadr err)))))) + (hywiki-tests--delete-hywiki-dir-and-buffer hywiki-directory)))) + +(ert-deftest hywiki-tests--find-page () + "Verify `hywiki-find-page' runs hook and calls `hywiki-display-page'." + (let* (run-hook (hook (lambda () (setq run-hook t)))) + (unwind-protect + (mocklet (((hywiki-display-page "WikiWord") => "WikiWord")) + (with-temp-buffer + (add-hook 'hywiki-find-page-hook hook) + (should (string= "WikiWord" (hywiki-find-page "WikiWord")))) + (should run-hook)) + (remove-hook 'hywiki-find-page-hook hook)))) + +(ert-deftest hywiki-tests--get-page/wikiword-list () + "Verify `hywiki-get-page-list' and `hywiki-get-wikiword-list'." + (let* ((hywiki-directory (make-temp-file "hywiki" t)) + (wiki-page (cdr (hywiki-add-page "WikiWord")))) + (unwind-protect + (progn + (hywiki-add-find "WikiPage") + (should (equal '("WikiWord") (hywiki-get-page-list))) + (should (set:equal '("WikiPage" "WikiWord") (hywiki-get-wikiword-list)))) + (hy-delete-files-and-buffers (list wiki-page)) + (hywiki-tests--delete-hywiki-dir-and-buffer hywiki-directory)))) + +(ert-deftest hywiki-tests--org-link-store () + "Verify storing org links with `hywiki-org-link-store'." + (hywiki-tests--preserve-hywiki-mode + (insert "WikiWord\n") + (goto-char 3) + (let ((hywiki-org-link-type-required t)) + (mocklet (((org-link-store-props + :type hywiki-org-link-type + :link (concat hywiki-org-link-type ":WikiWord") + :description "WikiWord"))) + (hywiki-org-link-store))) + (let (hywiki-org-link-type-required) + (mocklet (((org-link-store-props + :type hywiki-org-link-type + :link "WikiWord" + :description "WikiWord"))) + (hywiki-org-link-store))))) + +(ert-deftest hywiki-tests--get-buttonize-characters () + "Verify `hywiki-get-buttonize-characters'." + (should (string= "!&+,./;=?@\\^`|~" (hywiki-get-buttonize-characters)))) + +(ert-deftest hywiki-tests--non-hook-context-p () + "Verify `hywiki-non-hook-context-p'." + (ert-info ("General case") + (should-not (hywiki-non-hook-context-p))) + (ert-info ("Minibuffer active and selected") + (mocklet ((minibuffer-window-active-p => t)) + (should (hywiki-non-hook-context-p)))) + ;; (ert-info ("Minibuffer active and debugging") + ;; (let ((edebug-active t)) + ;; (mocklet ((active-minibuffer-window => t)) + ;; (should (hywiki-non-hook-context-p))))) + (with-temp-buffer + (insert "\"string\"") + (goto-char 3) + (ert-info ("Fundamental-mode") + (fundamental-mode) + (should-not (hywiki-non-hook-context-p))) + (ert-info ("Prog-mode and point in a string") + (python-mode) + (should-not (hywiki-non-hook-context-p))) + (ert-info ("Highlight all in prog-mode, match current mode") + (let ((hywiki-highlight-all-in-prog-modes '(python-mode))) + (should-not (hywiki-non-hook-context-p)))) + (ert-info ("Prog-mode outside string") + (goto-char 1) + (should (hywiki-non-hook-context-p))))) + +(ert-deftest hywiki-tests--create-page () + "Verify `hywiki-create-page'." + (mocklet (((hywiki-add-page "WikiWord" t) => '(page . "WikiWord.org"))) + (should (string= "WikiWord.org" (hywiki-create-page "WikiWord"))) + (should (string= "WikiWord.org" (hywiki-create-page "WikiWord" t))) + (mocklet (((hywiki-word-read-new "Create/Edit HyWikiWord: ") => "WikiWord")) + (should (string= "WikiWord.org" (hywiki-create-page nil))) + (unless noninteractive ;FIXME: Disabled in batch - called-interactively-p issue? + (should (string= "WikiWord.org" (call-interactively #'hywiki-create-page)))))) + + ;; Error case - WikiWord is not created + (mocklet (((hywiki-add-page "wikiword" t) => nil)) + (should-not (hywiki-create-page "wikiword")) + (let ((err (should-error (hywiki-create-page "wikiword" t) :type 'error))) + (should (string-match-p "(hywiki-create-page): Invalid HyWikiWord: .wikiword.; must be capitalized, all alpha" (cadr err)))))) + +(ert-deftest hywiki-tests--word-create () + "Verify `hywiki-word-create'." + (mocklet (((hywiki-create-referent "WikiWord" t) => 'referent) + (hywiki-create-page not-called)) + (let ((hywiki-referent-prompt-flag t)) + (should (equal 'referent (hywiki-word-create "WikiWord"))) + (should (equal 'referent (hywiki-word-create "WikiWord" t)))) + (let (hywiki-referent-prompt-flag) + (should (equal 'referent (hywiki-word-create "WikiWord" t))))) + (mocklet ((hywiki-create-referent not-called) + ((hywiki-create-page "WikiWord" t) => 'page)) + (let (hywiki-referent-prompt-flag) + (should (equal 'page (hywiki-word-create "WikiWord")))))) + (provide 'hywiki-tests) ;; This file can't be byte-compiled without the `el-mock' package