nspkg / tests / coracle / callback_compatibility_test.v
288 lines · 269 sloc · 10.63 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module coracle
2
3import nspkg as facade
4import os
5
6struct CallbackState {
7mut:
8 calls []string
9}
10
11fn callback_mark(context voidptr, name string) {
12 mut state := unsafe { &CallbackState(context) }
13 state.calls << name
14}
15
16fn callback_log(context voidptr, level LogLevel, message &u8, arguments voidptr) {
17 if unsafe { cstring_to_vstring(message) } != 'cb-test' { return }
18 assert level == .warning && arguments == unsafe { nil }
19 callback_mark(context, 'log')
20}
21
22fn callback_download(context voidptr, filename &u8, kind DownloadEventType, data voidptr) {
23 assert unsafe { cstring_to_vstring(filename) } == 'file'
24 assert kind == .progress && data == unsafe { nil }
25 callback_mark(context, 'download')
26}
27
28fn callback_fetch(context voidptr, url &u8, directory &u8, force i32) i32 {
29 assert unsafe { cstring_to_vstring(url) } == 'url'
30 assert unsafe { cstring_to_vstring(directory) } == 'directory'
31 assert force == 0
32 callback_mark(context, 'fetch')
33 return 42
34}
35
36fn callback_event(context voidptr, event &Event) {
37 assert event == unsafe { nil }
38 callback_mark(context, 'event')
39}
40
41fn callback_question(context voidptr, question &Question) {
42 assert question == unsafe { nil }
43 callback_mark(context, 'question')
44}
45
46fn callback_progress(context voidptr, kind Progress, name &u8, percent i32, total uint, current uint) {
47 assert kind == .add_start && percent == 42 && total == 9 && current == 3
48 assert unsafe { cstring_to_vstring(name) } == 'pkg'
49 callback_mark(context, 'progress')
50}
51
52fn callback_public_log(context voidptr, level facade.LogLevel, message &u8, arguments voidptr) {
53 if unsafe { cstring_to_vstring(message) } != 'cb-test' { return }
54 assert level == .warning && arguments == unsafe { nil }
55 callback_mark(context, 'log')
56}
57
58fn callback_public_download(context voidptr, filename &u8, kind facade.DownloadEventType, data voidptr) {
59 assert unsafe { cstring_to_vstring(filename) } == 'file'
60 assert kind == .progress && data == unsafe { nil }
61 callback_mark(context, 'download')
62}
63
64fn callback_public_event(context voidptr, event &facade.Event) {
65 assert event == unsafe { nil }
66 callback_mark(context, 'event')
67}
68
69fn callback_public_question(context voidptr, question &facade.Question) {
70 assert question == unsafe { nil }
71 callback_mark(context, 'question')
72}
73
74fn callback_public_progress(context voidptr, kind facade.Progress, name &u8, percent i32, total uint, current uint) {
75 assert kind == .add_start && percent == 42 && total == 9 && current == 3
76 assert unsafe { cstring_to_vstring(name) } == 'pkg'
77 callback_mark(context, 'progress')
78}
79
80fn callback_reference_read(handle &Handle, kind string, context voidptr, expected voidptr) !string {
81 if _ := handle.register_syncdb('', 0) {
82 assert false
83 }
84 value := match kind {
85 'log' { voidptr(handle.option_get_logcb()) }
86 'download' { voidptr(handle.option_get_dlcb()) }
87 'fetch' { voidptr(handle.option_get_fetchcb()) }
88 'event' { voidptr(handle.option_get_eventcb()) }
89 'question' { voidptr(handle.option_get_questioncb()) }
90 else { voidptr(handle.option_get_progresscb()) }
91 }
92 code := int(handle.errno())
93 if _ := handle.register_syncdb('', 0) {
94 assert false
95 }
96 actual_context := match kind {
97 'log' { handle.option_get_logcb_ctx() }
98 'download' { handle.option_get_dlcb_ctx() }
99 'fetch' { handle.get_fetchcb_ctx() }
100 'event' { handle.option_get_eventcb_ctx() }
101 'question' { handle.option_get_questioncb_ctx() }
102 else { handle.option_get_progresscb_ctx() }
103 }
104 assert actual_context == context
105 assert value == expected
106 return '${kind}:${value != unsafe { nil }}:${actual_context != unsafe { nil }}:${code}:${int(handle.errno())}'
107}
108
109fn callback_public_read(handle &facade.Handle, kind string, context voidptr, expected voidptr) !string {
110 if _ := handle.register_syncdb('', 0) {
111 assert false
112 }
113 value := match kind {
114 'log' { voidptr(handle.option_get_logcb()) }
115 'download' { voidptr(handle.option_get_dlcb()) }
116 'fetch' { voidptr(handle.option_get_fetchcb()) }
117 'event' { voidptr(handle.option_get_eventcb()) }
118 'question' { voidptr(handle.option_get_questioncb()) }
119 else { voidptr(handle.option_get_progresscb()) }
120 }
121 code := int(handle.errno())
122 if _ := handle.register_syncdb('', 0) {
123 assert false
124 }
125 actual_context := match kind {
126 'log' { handle.option_get_logcb_ctx() }
127 'download' { handle.option_get_dlcb_ctx() }
128 'fetch' { handle.get_fetchcb_ctx() }
129 'event' { handle.option_get_eventcb_ctx() }
130 'question' { handle.option_get_questioncb_ctx() }
131 else { handle.option_get_progresscb_ctx() }
132 }
133 assert actual_context == context
134 assert value == expected
135 return '${kind}:${value != unsafe { nil }}:${actual_context != unsafe { nil }}:${code}:${int(handle.errno())}'
136}
137
138fn callback_reference_cycle(handle &Handle, kind string, first voidptr, second voidptr) ![]string {
139 mut trace := [callback_reference_read(handle, kind, unsafe { nil }, unsafe { nil })!]
140 match kind {
141 'log' { handle.option_set_logcb(callback_log, first)! }
142 'download' { handle.option_set_dlcb(callback_download, first)! }
143 'fetch' { handle.option_set_fetchcb(callback_fetch, first)! }
144 'event' { handle.option_set_eventcb(callback_event, first)! }
145 'question' { handle.option_set_questioncb(callback_question, first)! }
146 else { handle.option_set_progresscb(callback_progress, first)! }
147 }
148 value := match kind {
149 'log' { voidptr(handle.option_get_logcb()) }
150 'download' { voidptr(handle.option_get_dlcb()) }
151 'fetch' { voidptr(handle.option_get_fetchcb()) }
152 'event' { voidptr(handle.option_get_eventcb()) }
153 'question' { voidptr(handle.option_get_questioncb()) }
154 else { voidptr(handle.option_get_progresscb()) }
155 }
156 assert value != unsafe { nil }
157 trace << callback_reference_read(handle, kind, first, value)!
158 match kind {
159 'log' {
160 handle.option_set_logcb(handle.option_get_logcb(), second)!
161 cb := handle.option_get_logcb()
162 cb(second, .warning, c'cb-test', unsafe { nil })
163 }
164 'download' {
165 handle.option_set_dlcb(handle.option_get_dlcb(), second)!
166 cb := handle.option_get_dlcb()
167 cb(second, c'file', .progress, unsafe { nil })
168 }
169 'fetch' {
170 handle.option_set_fetchcb(handle.option_get_fetchcb(), second)!
171 cb := handle.option_get_fetchcb()
172 assert cb(second, c'url', c'directory', 0) == 42
173 }
174 'event' {
175 handle.option_set_eventcb(handle.option_get_eventcb(), second)!
176 cb := handle.option_get_eventcb()
177 cb(second, unsafe { nil })
178 }
179 'question' {
180 handle.option_set_questioncb(handle.option_get_questioncb(), second)!
181 cb := handle.option_get_questioncb()
182 cb(second, unsafe { nil })
183 }
184 else {
185 handle.option_set_progresscb(handle.option_get_progresscb(), second)!
186 cb := handle.option_get_progresscb()
187 cb(second, .add_start, c'pkg', 42, 9, 3)
188 }
189 }
190 trace << callback_reference_read(handle, kind, second, value)!
191 match kind {
192 'log' { handle.option_set_logcb(unsafe { nil }, second)! }
193 'download' { handle.option_set_dlcb(unsafe { nil }, second)! }
194 'fetch' { handle.option_set_fetchcb(unsafe { nil }, second)! }
195 'event' { handle.option_set_eventcb(unsafe { nil }, second)! }
196 'question' { handle.option_set_questioncb(unsafe { nil }, second)! }
197 else { handle.option_set_progresscb(unsafe { nil }, second)! }
198 }
199 trace << callback_reference_read(handle, kind, second, unsafe { nil })!
200 return trace
201}
202
203fn callback_public_cycle(handle &facade.Handle, kind string, first voidptr, second voidptr) ![]string {
204 mut trace := [callback_public_read(handle, kind, unsafe { nil }, unsafe { nil })!]
205 match kind {
206 'log' { handle.option_set_logcb(callback_public_log, first)! }
207 'download' { handle.option_set_dlcb(callback_public_download, first)! }
208 'fetch' { handle.option_set_fetchcb(callback_fetch, first)! }
209 'event' { handle.option_set_eventcb(callback_public_event, first)! }
210 'question' { handle.option_set_questioncb(callback_public_question, first)! }
211 else { handle.option_set_progresscb(callback_public_progress, first)! }
212 }
213 value := match kind {
214 'log' { voidptr(handle.option_get_logcb()) }
215 'download' { voidptr(handle.option_get_dlcb()) }
216 'fetch' { voidptr(handle.option_get_fetchcb()) }
217 'event' { voidptr(handle.option_get_eventcb()) }
218 'question' { voidptr(handle.option_get_questioncb()) }
219 else { voidptr(handle.option_get_progresscb()) }
220 }
221 assert value != unsafe { nil }
222 trace << callback_public_read(handle, kind, first, value)!
223 match kind {
224 'log' {
225 handle.option_set_logcb(handle.option_get_logcb(), second)!
226 cb := handle.option_get_logcb()
227 cb(second, .warning, c'cb-test', unsafe { nil })
228 }
229 'download' {
230 handle.option_set_dlcb(handle.option_get_dlcb(), second)!
231 cb := handle.option_get_dlcb()
232 cb(second, c'file', .progress, unsafe { nil })
233 }
234 'fetch' {
235 handle.option_set_fetchcb(handle.option_get_fetchcb(), second)!
236 cb := handle.option_get_fetchcb()
237 assert cb(second, c'url', c'directory', 0) == 42
238 }
239 'event' {
240 handle.option_set_eventcb(handle.option_get_eventcb(), second)!
241 cb := handle.option_get_eventcb()
242 cb(second, unsafe { nil })
243 }
244 'question' {
245 handle.option_set_questioncb(handle.option_get_questioncb(), second)!
246 cb := handle.option_get_questioncb()
247 cb(second, unsafe { nil })
248 }
249 else {
250 handle.option_set_progresscb(handle.option_get_progresscb(), second)!
251 cb := handle.option_get_progresscb()
252 cb(second, .add_start, c'pkg', 42, 9, 3)
253 }
254 }
255 trace << callback_public_read(handle, kind, second, value)!
256 match kind {
257 'log' { handle.option_set_logcb(unsafe { nil }, second)! }
258 'download' { handle.option_set_dlcb(unsafe { nil }, second)! }
259 'fetch' { handle.option_set_fetchcb(unsafe { nil }, second)! }
260 'event' { handle.option_set_eventcb(unsafe { nil }, second)! }
261 'question' { handle.option_set_questioncb(unsafe { nil }, second)! }
262 else { handle.option_set_progresscb(unsafe { nil }, second)! }
263 }
264 trace << callback_public_read(handle, kind, second, unsafe { nil })!
265 return trace
266}
267
268fn test_public_callback_options_restore_context_and_clear_errno_like_c() ! {
269 root := os.join_path(os.vtmp_dir(), 'alpm-callback-options-${os.getpid()}')
270 os.mkdir_all(root)!
271 defer { os.rmdir_all(root) or { eprintln(err) } }
272 old := initialize('/', root)!
273 defer { old.release() or { eprintln(err) } }
274 public := facade.initialize('/', root)!
275 defer { public.release() or { eprintln(err) } }
276 first := &CallbackState{}
277 second := &CallbackState{}
278 for kind in ['log', 'download', 'fetch', 'event', 'question', 'progress'] {
279 expected := callback_reference_cycle(old, kind, first, second)!
280 actual := callback_public_cycle(public, kind, first, second)!
281 assert actual == expected, kind
282 assert expected == ['${kind}:false:false:0:0', '${kind}:true:true:0:0',
283 '${kind}:true:true:0:0', '${kind}:false:true:0:0']
284 }
285 assert first.calls.len == 0
286 assert second.calls == ['log', 'log', 'download', 'download', 'fetch', 'fetch', 'event', 'event',
287 'question', 'question', 'progress', 'progress']
288}
289