summaryrefslogtreecommitdiff
path: root/test/lisp/wid-edit-tests.el
blob: 2ddb656fa9e84c645d0babc212afc5cd35413358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
;;; wid-edit-tests.el --- tests for wid-edit.el -*- lexical-binding: t -*-

;; Copyright (C) 2019-2020 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'ert)
(require 'wid-edit)

(ert-deftest widget-at ()
  "Test `widget-at' behavior."
  (with-temp-buffer
    (should-not (widget-at))
    (let ((marco (widget-create 'link "link widget"))
          (polo  (widget-at (1- (point)))))
      (should (widgetp polo))
      (should (eq marco polo)))
    ;; Buttons and widgets are incompatible (bug#34506).
    (insert-text-button "text button")
    (should-not (widget-at (1- (point))))
    (insert-button "overlay button")
    (should-not (widget-at (1- (point))))))

;; The following three tests compare the effect of using either %n or \n at the
;; end of a format string, as well as using %n at the end or in the middle of
;; the format string.  (Bug#12533)

(ert-deftest widget-test-indentation-after-%n ()
  "Fail when %n is used at the end of a format string."
  :expected-result :failed
  (with-temp-buffer
    (let (wid indented)
      (widget-insert "Testing indentation.\n")
      ;; If we use %n at the end of the format string of the widget `other', we
      ;; screw up indentation of the following widgets.
      (setq wid (widget-create
                 '(repeat :indent 4
                   (cons
                    string (choice (other :tag "Other" :format "%t%n" c))))))
      (goto-char (widget-get wid :value-pos))
      ;; Since we indent the `repeat' widget, we skip the space characters
      ;; inserted.
      (skip-chars-forward " ")
      (setq indented (current-column)) ; Save the column to which we indented.
      (should (eq indented (or (widget-get wid :indent) 0)))
      ;; Insert an entry.  This simulates a click or RET at the INS button.
      (widget-apply (widget-at) :action)
      (goto-char (widget-get wid :value-pos))
      (skip-chars-forward " ")
      ;; This fails, because the button is not at the right column.
      (should (eq (current-column) indented)))))

(ert-deftest widget-test-indentation-after-newline ()
  "Pass when the newline is used at the end of a format string."
  (with-temp-buffer
    (let (wid indented)
      (widget-insert "Testing indentation.\n")
      (setq wid (widget-create
                 '(repeat :indent 4
                   (cons
                    string
                    (choice (other :tag "Other" :format "%t\n" c))))))
      (goto-char (widget-get wid :value-pos))
      (skip-chars-forward " ")
      (setq indented (current-column))
      (should (eq (current-column) (or (widget-get wid :indent) 0)))
      (widget-apply (widget-at) :action)
      (goto-char (widget-get wid :value-pos))
      (skip-chars-forward " ")
      ;; Because we used \n in the format string, this pass.
      (should (eq (current-column) indented)))))

(ert-deftest widget-test-newline-and-indent-same-widget ()
  "It's OK to use the %n escape sequence in the middle of the format string."
  (with-temp-buffer
    (let (wid indented)
      (widget-insert "Testing indentation.\n")
      (setq wid (widget-create
                 '(repeat :indent 4
                          :format "%{%t%}:%n%v%i\n"
                          (cons
                           string
                           (choice (other :tag "Other" :format "%t\n" c))))))
      (goto-char (widget-get wid :value-pos))
      (skip-chars-forward " ")
      (setq indented (current-column))
      (should (eq indented (or (widget-get wid :indent) 0)))
      (widget-apply (widget-at) :action)
      (goto-char (widget-get wid :value-pos))
      (skip-chars-forward " ")
      (should (eq (current-column) indented))

      ;; Also, the children are indented correctly.
      (let ((grandchild
             ;; This gets the `string' widget.
             (car (widget-get (car (widget-get wid :children)) :children))))
        (goto-char (widget-get grandchild :from))
        (should (eq (current-column)
                    (widget-get grandchild :indent)))))))

;;; wid-edit-tests.el ends here