-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtest_dialog.vim
More file actions
190 lines (172 loc) · 6.61 KB
/
test_dialog.vim
File metadata and controls
190 lines (172 loc) · 6.61 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"======================================================================
"
" test_dialog.vim - interactive test for quickui#dialog
"
" Usage: :source tools/test/test_dialog.vim | call Test_dialog_basic()
"
"======================================================================
" vim: set ts=4 sw=4 tw=78 noet :
"----------------------------------------------------------------------
" basic test: all control types
"----------------------------------------------------------------------
function! Test_dialog_basic()
let items = [
\ {'type': 'label', 'text': 'Test all controls:'},
\ {'type': 'input', 'name': 'name', 'prompt': 'Name:',
\ 'value': 'test'},
\ {'type': 'radio', 'name': 'choice', 'prompt': 'Pick:',
\ 'items': ['A', 'B', 'C']},
\ {'type': 'check', 'name': 'flag', 'text': 'Enable'},
\ {'type': 'button', 'name': 'confirm',
\ 'items': [' &OK ', ' &Cancel ']},
\ ]
let result = quickui#dialog#open(items, {'title': 'Test'})
echo result
endfunc
"----------------------------------------------------------------------
" focus test: control-level focus field
"----------------------------------------------------------------------
function! Test_dialog_focus()
let items = [
\ {'type': 'input', 'name': 'user', 'prompt': 'User:',
\ 'value': 'admin'},
\ {'type': 'input', 'name': 'pass', 'prompt': 'Pass:'},
\ {'type': 'button', 'name': 'confirm',
\ 'items': [' &Login ', ' &Cancel '], 'focus': 0},
\ ]
let result = quickui#dialog#open(items, {'title': 'Focus Test'})
echo result
endfunc
"----------------------------------------------------------------------
" multi button rows test
"----------------------------------------------------------------------
function! Test_dialog_multi_button()
let items = [
\ {'type': 'label', 'text': 'Multiple button rows:'},
\ {'type': 'button', 'name': 'action',
\ 'items': [' &Apply ', ' &Reset ']},
\ {'type': 'button', 'name': 'confirm',
\ 'items': [' &OK ', ' &Cancel ']},
\ ]
let result = quickui#dialog#open(items, {'title': 'Multi Button'})
echo result
endfunc
"----------------------------------------------------------------------
" full form test (from design doc example)
"----------------------------------------------------------------------
function! Test_dialog_form()
let items = [
\ {'type': 'label', 'text': 'Please fill in the user form:'},
\ {'type': 'input', 'name': 'username', 'prompt': 'Name:',
\ 'value': 'skywind'},
\ {'type': 'input', 'name': 'email', 'prompt': 'Email:'},
\ {'type': 'radio', 'name': 'role', 'prompt': 'Role:',
\ 'items': ['&Dev', '&QA', '&PM'], 'value': 0},
\ {'type': 'check', 'name': 'admin', 'text': 'Administrator'},
\ {'type': 'check', 'name': 'notify', 'text': 'Send notification',
\ 'value': 1},
\ {'type': 'button', 'name': 'confirm',
\ 'items': [' &OK ', ' &Cancel ']},
\ ]
let opts = {'title': 'User Form', 'w': 50}
let result = quickui#dialog#open(items, opts)
if result.button_index >= 0
echo 'User: ' . result.username
echo 'Email: ' . result.email
echo 'Role: ' . result.role
echo 'Admin: ' . result.admin
else
echo 'Cancelled'
endif
endfunc
"----------------------------------------------------------------------
" label only test
"----------------------------------------------------------------------
function! Test_dialog_label_only()
let items = [
\ {'type': 'label', 'text': "This dialog has no interactive\ncontrols. Press ESC to close."},
\ ]
let result = quickui#dialog#open(items, {'title': 'Info'})
echo result
endfunc
"----------------------------------------------------------------------
" radio vertical test
"----------------------------------------------------------------------
function! Test_dialog_radio_vertical()
let items = [
\ {'type': 'label', 'text': 'Pick a very long option:'},
\ {'type': 'radio', 'name': 'lang', 'prompt': 'Language:',
\ 'items': ['&Python', '&JavaScript', '&TypeScript', '&Rust', '&Go'],
\ 'vertical': 1},
\ {'type': 'button', 'name': 'confirm',
\ 'items': [' &OK ', ' &Cancel ']},
\ ]
let result = quickui#dialog#open(items, {'title': 'Vertical Radio'})
echo result
endfunc
"----------------------------------------------------------------------
" input with history test
"----------------------------------------------------------------------
function! Test_dialog_history()
let items = [
\ {'type': 'input', 'name': 'search', 'prompt': 'Search:',
\ 'history': 'dialog_test_search'},
\ {'type': 'button', 'name': 'confirm',
\ 'items': [' &OK ', ' &Cancel ']},
\ ]
let result = quickui#dialog#open(items, {'title': 'Search'})
echo result
endfunc
"----------------------------------------------------------------------
" form with dropdown + validator
"----------------------------------------------------------------------
function! s:validate_project(result) abort
if a:result.project_name =~# '^\s*$'
return 'Project name cannot be empty'
endif
if a:result.project_name =~# '[^a-zA-Z0-9_\-]'
return 'Project name: only letters, digits, _ and - are allowed'
endif
if a:result.email =~# '^\s*$'
return 'Email cannot be empty'
endif
if a:result.email !~# '@'
return 'Email must contain @'
endif
return ''
endfunc
function! Test_dialog_project_form()
let items = [
\ {'type': 'label', 'text': 'Create New Project:'},
\ {'type': 'input', 'name': 'project_name', 'prompt': 'Project:',
\ 'value': 'my-app', 'focus': 0},
\ {'type': 'input', 'name': 'email', 'prompt': 'Email:',
\ 'value': 'dev@example.com'},
\ {'type': 'dropdown', 'name': 'language', 'prompt': 'Language:',
\ 'items': ['Python', 'JavaScript', 'Go', 'Rust', 'C++'], 'value': 0},
\ {'type': 'dropdown', 'name': 'build', 'prompt': 'Build:',
\ 'items': ['Make', 'CMake', 'Cargo', 'npm', 'pip'], 'value': 0},
\ {'type': 'radio', 'name': 'license', 'prompt': 'License:',
\ 'items': ['&MIT', '&Apache', '&GPL', '&Proprietary'], 'value': 0},
\ {'type': 'check', 'name': 'git_init', 'text': 'Initialize git repo',
\ 'value': 1},
\ {'type': 'check', 'name': 'ci', 'text': 'Add CI config',
\ 'value': 0},
\ {'type': 'button', 'name': 'confirm',
\ 'items': [' &Create ', ' Cancel ']},
\ ]
let opts = {'title': 'New Project', 'w': 50,
\ 'validator': function('s:validate_project')}
let result = quickui#dialog#open(items, opts)
if result.button_index >= 0
echo 'Project: ' . result.project_name
echo 'Email: ' . result.email
echo 'Language: ' . items[3].items[result.language]
echo 'Build: ' . items[4].items[result.build]
echo 'License: ' . items[5].items[result.license]
echo 'Git: ' . (result.git_init ? 'yes' : 'no')
echo 'CI: ' . (result.ci ? 'yes' : 'no')
else
echo 'Cancelled'
endif
endfunc