Add zsh-autosuggestion
[zsh.git] / .zsh / external / zsh-autosuggestions / test / widgets / accept_test.zsh
1 #!/usr/bin/env zsh
2
3 source "${0:a:h}/../test_helper.zsh"
4
5 oneTimeSetUp() {
6 source_autosuggestions
7 }
8
9 setUp() {
10 BUFFER=''
11 POSTDISPLAY=''
12 CURSOR=0
13 KEYMAP='main'
14 }
15
16 tearDown() {
17 restore _zsh_autosuggest_invoke_original_widget
18 }
19
20 testCursorAtEnd() {
21 BUFFER='echo'
22 POSTDISPLAY=' hello'
23 CURSOR=4
24
25 stub _zsh_autosuggest_invoke_original_widget
26
27 _zsh_autosuggest_accept 'original-widget'
28
29 assertTrue \
30 'original widget not invoked' \
31 'stub_called _zsh_autosuggest_invoke_original_widget'
32
33 assertEquals \
34 'BUFFER was not modified' \
35 'echo hello' \
36 "$BUFFER"
37
38 assertEquals \
39 'POSTDISPLAY was not cleared' \
40 '' \
41 "$POSTDISPLAY"
42 }
43
44 testCursorNotAtEnd() {
45 BUFFER='echo'
46 POSTDISPLAY=' hello'
47 CURSOR=2
48
49 stub _zsh_autosuggest_invoke_original_widget
50
51 _zsh_autosuggest_accept 'original-widget'
52
53 assertTrue \
54 'original widget not invoked' \
55 'stub_called _zsh_autosuggest_invoke_original_widget'
56
57 assertEquals \
58 'BUFFER was modified' \
59 'echo' \
60 "$BUFFER"
61
62 assertEquals \
63 'POSTDISPLAY was modified' \
64 ' hello' \
65 "$POSTDISPLAY"
66 }
67
68 testViCursorAtEnd() {
69 BUFFER='echo'
70 POSTDISPLAY=' hello'
71 CURSOR=3
72 KEYMAP='vicmd'
73
74 stub _zsh_autosuggest_invoke_original_widget
75
76 _zsh_autosuggest_accept 'original-widget'
77
78 assertTrue \
79 'original widget not invoked' \
80 'stub_called _zsh_autosuggest_invoke_original_widget'
81
82 assertEquals \
83 'BUFFER was not modified' \
84 'echo hello' \
85 "$BUFFER"
86
87 assertEquals \
88 'POSTDISPLAY was not cleared' \
89 '' \
90 "$POSTDISPLAY"
91 }
92
93 testViCursorNotAtEnd() {
94 BUFFER='echo'
95 POSTDISPLAY=' hello'
96 CURSOR=2
97 KEYMAP='vicmd'
98
99 stub _zsh_autosuggest_invoke_original_widget
100
101 _zsh_autosuggest_accept 'original-widget'
102
103 assertTrue \
104 'original widget not invoked' \
105 'stub_called _zsh_autosuggest_invoke_original_widget'
106
107 assertEquals \
108 'BUFFER was modified' \
109 'echo' \
110 "$BUFFER"
111
112 assertEquals \
113 'POSTDISPLAY was modified' \
114 ' hello' \
115 "$POSTDISPLAY"
116 }
117
118 testRetval() {
119 stub_and_eval \
120 _zsh_autosuggest_invoke_original_widget \
121 'return 1'
122
123 _zsh_autosuggest_widget_accept 'original-widget'
124
125 assertEquals \
126 'Did not return correct value from original widget' \
127 '1' \
128 "$?"
129 }
130
131 testWidget() {
132 stub _zsh_autosuggest_highlight_reset
133 stub _zsh_autosuggest_accept
134 stub _zsh_autosuggest_highlight_apply
135
136 # Call the function pointed to by the widget since we can't call
137 # the widget itself when zle is not active
138 ${widgets[autosuggest-accept]#*:} 'original-widget'
139
140 assertTrue \
141 'autosuggest-accept widget does not exist' \
142 'zle -l autosuggest-accept'
143
144 assertTrue \
145 'highlight_reset was not called' \
146 'stub_called _zsh_autosuggest_highlight_reset'
147
148 assertTrue \
149 'widget function was not called' \
150 'stub_called _zsh_autosuggest_accept'
151
152 assertTrue \
153 'highlight_apply was not called' \
154 'stub_called _zsh_autosuggest_highlight_apply'
155
156 restore _zsh_autosuggest_highlight_reset
157 restore _zsh_autosuggest_accept
158 restore _zsh_autosuggest_highlight_apply
159 }
160
161 run_tests "$0"