File: uevent.test

package info (click to toggle)
tcllib 2.0%2Bdfsg-4
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 83,572 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (478 lines) | stat: -rw-r--r-- 14,379 bytes parent folder | download | duplicates (2)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# -*- tcl -*-
# uevent.test:  Tests for the UEVENT utilities.
#
# Copyright (c) 2008 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
# All rights reserved.
#
# UEVENT: @(#) $Id: uevent.test,v 1.2 2012/03/30 22:47:15 andreas_kupries Exp $

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 1.0

support {
    use log/logger.tcl logger
}
testing {
    useLocal uevent.tcl uevent
}

# -------------------------------------------------------------------------
## Serialize the tag/event/command database.

proc uestate {} {
    set res {}
    foreach tag [lsort -dict [uevent::list]] {
	set buf {}
	foreach event [lsort -dict [uevent::list $tag]] {
	    lappend buf $event [uevent::list $tag $event]
	}
	lappend res $tag $buf
    }
    return $res
}

# -------------------------------------------------------------------------

test uevent-1.0 {bind error, wrong#args, not enough} {
    catch {::uevent::bind} msg
    set msg
} [tcltest::wrongNumArgs {::uevent::bind} {tag event command} 0]

test uevent-1.1 {bind error, wrong#args, not enough} {
    catch {::uevent::bind foo} msg
    set msg
} [tcltest::wrongNumArgs {::uevent::bind} {tag event command} 1]

test uevent-1.2 {bind error, wrong#args, not enough} {
    catch {::uevent::bind foo bar} msg
    set msg
} [tcltest::wrongNumArgs {::uevent::bind} {tag event command} 2]

test uevent-1.3 {bind error, wrong#args, too many} {
    catch {::uevent::bind foo bar barf more} msg
    set msg
} [tcltest::tooManyArgs {::uevent::bind} {tag event command}]

# -------------------------------------------------------------------------

test uevent-2.0 {bind} {
    set     res {}
    lappend res [uestate]
    set t [::uevent::bind tag event command]
    lappend res [uestate]
    uevent::unbind $t
    set res
} {{} {tag {event command}}}

test uevent-2.1 {bind, multiple times of the same combination} {
    set     res {}
    lappend res [uestate]
    set ta [::uevent::bind tag event command]
    lappend res [uestate]
    set tb [::uevent::bind tag event command]
    lappend res [uestate]
    uevent::unbind $ta
    lappend res [uestate]
    lappend res [expr {$ta eq $tb}]
    set res
} {{} {tag {event command}} {tag {event command}} {} 1}

test uevent-2.2 {bind, same tag/event, different commands} {
    set     res {}
    lappend res [uestate]
    set ta [::uevent::bind tag event command1]
    lappend res [uestate]
    set tb [::uevent::bind tag event command2]
    lappend res [uestate]
    uevent::unbind $ta
    uevent::unbind $tb
    lappend res [uestate]
    lappend res [expr {$ta eq $tb}]
    set res
} {{} {tag {event command1}} {tag {event {command1 command2}}} {} 0}

test uevent-2.3 {bind, same tag/command, different events} {
    set     res {}
    lappend res [uestate]
    set ta [::uevent::bind tag event1 command]
    lappend res [uestate]
    set tb [::uevent::bind tag event2 command]
    lappend res [uestate]
    uevent::unbind $ta
    uevent::unbind $tb
    lappend res [uestate]
    lappend res [expr {$ta eq $tb}]
    set res
} {{} {tag {event1 command}} {tag {event1 command event2 command}} {} 0}

test uevent-2.4 {bind, same event/command, different tags} {
    set     res {}
    lappend res [uestate]
    set ta [::uevent::bind tag1 event command]
    lappend res [uestate]
    set tb [::uevent::bind tag2 event command]
    lappend res [uestate]
    uevent::unbind $ta
    uevent::unbind $tb
    lappend res [uestate]
    lappend res [expr {$ta eq $tb}]
    set res
} {{} {tag1 {event command}} {tag1 {event command} tag2 {event command}} {} 0}

# -------------------------------------------------------------------------

test uevent-3.0 {unbind error, wrong#args, not enough} {
    catch {::uevent::unbind} msg
    set msg
} [tcltest::wrongNumArgs {::uevent::unbind} {token} 0]

test uevent-3.1 {unbind error, wrong#args, too many} {
    catch {::uevent::unbind foo bar} msg
    set msg
} [tcltest::tooManyArgs {::uevent::unbind} {token}]

# -------------------------------------------------------------------------

test uevent-4.0 {unbind} {
    set ta [::uevent::bind tag1 event1 command1]
    set tb [::uevent::bind tag1 event1 command2]
    set tc [::uevent::bind tag1 event2 command1]
    set td [::uevent::bind tag2 event1 command1]
    ::uevent::unbind $ta
    set res [uestate]
    ::uevent::unbind $tb
    ::uevent::unbind $tc
    ::uevent::unbind $td
    set res
} {tag1 {event1 command2 event2 command1} tag2 {event1 command1}}

test uevent-4.1 {unbind} {
    set ta [::uevent::bind tag1 event1 command1]
    set tb [::uevent::bind tag1 event1 command2]
    set tc [::uevent::bind tag1 event2 command1]
    set td [::uevent::bind tag2 event1 command1]
    ::uevent::unbind $tb
    set res [uestate]
    ::uevent::unbind $ta
    ::uevent::unbind $tc
    ::uevent::unbind $td
    set res
} {tag1 {event1 command1 event2 command1} tag2 {event1 command1}}

test uevent-4.2 {unbind} {
    set ta [::uevent::bind tag1 event1 command1]
    set tb [::uevent::bind tag1 event1 command2]
    set tc [::uevent::bind tag1 event2 command1]
    set td [::uevent::bind tag2 event1 command1]
    ::uevent::unbind $tc
    set res [uestate]
    ::uevent::unbind $tb
    ::uevent::unbind $ta
    ::uevent::unbind $td
    set res
} {tag1 {event1 {command1 command2}} tag2 {event1 command1}}

test uevent-4.3 {unbind} {
    set ta [::uevent::bind tag1 event1 command1]
    set tb [::uevent::bind tag1 event1 command2]
    set tc [::uevent::bind tag1 event2 command1]
    set td [::uevent::bind tag2 event1 command1]
    ::uevent::unbind $td
    set res [uestate]
    ::uevent::unbind $tb
    ::uevent::unbind $tc
    ::uevent::unbind $ta
    set res
} {tag1 {event1 {command1 command2} event2 command1}}

# -------------------------------------------------------------------------

test uevent-5.0 {generate error, wrong#args, not enough} {
    catch {::uevent::generate} msg
    set msg
} [tcltest::wrongNumArgs {::uevent::generate} {tag event ?details?} 0]

test uevent-5.1 {generate error, wrong#args, not enough} {
    catch {::uevent::generate foo} msg
    set msg
} [tcltest::wrongNumArgs {::uevent::generate} {tag event ?details?} 1]

test uevent-5.2 {generate error, wrong#args, too many} {
    catch {::uevent::generate foo bar barf more} msg
    set msg
} [tcltest::tooManyArgs {::uevent::generate} {tag event ?details?}]

# -------------------------------------------------------------------------

proc EVENT {t e d} {
    global  res
    lappend res $t $e $d
    return
}

proc EVENT2 {t e d} {
    global  res
    lappend res 2/$t $e $d
    return
}

test uevent-6.0 {generate, single command} {
    set t [::uevent::bind tag event EVENT]
    set res {}
    uevent::generate tag event ClientData
    vwait ::res ; # Allow event to fire.
    uevent::unbind $t
    set res
} {tag event ClientData}

test uevent-6.1 {generate, single command, multiple issues} {
    set t [::uevent::bind tag event EVENT]
    set res {}
    uevent::generate tag event ClientData1
    uevent::generate tag event ClientData2
    uevent::generate tag event ClientData3
    vwait ::res ; # Allow events to fire.
    uevent::unbind $t
    set res
} {tag event ClientData1 tag event ClientData2 tag event ClientData3}

test uevent-6.2 {generate, multiple commands} {
    # Note: While we do not document the order of firing multiple
    # commands on one tag/event this test does capture the current
    # order and will trigger should we change it in the future, making
    # us aware of the incompatibility.
    set ta [::uevent::bind tag event EVENT]
    set tb [::uevent::bind tag event EVENT2]
    set res {}
    uevent::generate tag event ClientData
    vwait ::res ; # Allow events to fire.
    uevent::unbind $ta
    uevent::unbind $tb
    set res
} {tag event ClientData 2/tag event ClientData}


# -------------------------------------------------------------------------

proc WATCHT {a t} {
    global  res
    lappend res $a $t
    return
}

test uevent-7.0 {watch tag add, wrong#args, not enough} {
    catch {
	::uevent::watch::tag::add
    } msg
    set msg
} {wrong # args: should be "::uevent::watch::tag::add pattern cmdprefix"}

test uevent-7.1 {watch tag add, wrong#args, not enough} {
    catch {
	::uevent::watch::tag::add TAG
    } msg
    set msg
} {wrong # args: should be "::uevent::watch::tag::add pattern cmdprefix"}

test uevent-7.2 {watch tag add, wrong#args, too many} {
    catch {
	::uevent::watch::tag::add TAG CMD foo
    } msg
    set msg
} {wrong # args: should be "::uevent::watch::tag::add pattern cmdprefix"}

test uevent-7.3 {watch tag remove, wrong#args, not enough} {
    catch {
	::uevent::watch::tag::remove
    } msg
    set msg
} {wrong # args: should be "::uevent::watch::tag::remove token"}

test uevent-7.4 {watch tag remove, wrong#args, not enough} {
    catch {
	::uevent::watch::tag::remove TOKEN foo
    } msg
    set msg
} {wrong # args: should be "::uevent::watch::tag::remove token"}

test uevent-8.0 {watch tags, bind after watch, exact} {
    set res {}
    set tw [::uevent::watch::tag::add TAG WATCHT]
    set t1 [::uevent::bind TAGX E FOO]
    set t2 [::uevent::bind TAG  E FOO]
    ::uevent::unbind $t1
    ::uevent::unbind $t2
    ::uevent::watch::tag::remove $tw
    set res
} {bound TAG unbound TAG}

test uevent-8.1 {watch tags, watch after bind, exact} {
    set res {}
    set t1 [::uevent::bind TAGX E FOO]
    set t2 [::uevent::bind TAG  E FOO]
    set tw [::uevent::watch::tag::add TAG WATCHT]
    ::uevent::unbind $t1
    ::uevent::unbind $t2
    ::uevent::watch::tag::remove $tw
    set res
} {bound TAG unbound TAG}

test uevent-8.2 {watch tags, bind after watch, glob} {
    set res {}
    set tw [::uevent::watch::tag::add TAG* WATCHT]
    set t1 [::uevent::bind TAGX E FOO]
    set t2 [::uevent::bind TAG  E FOO]
    ::uevent::unbind $t1
    ::uevent::unbind $t2
    ::uevent::watch::tag::remove $tw
    set res
} {bound TAGX bound TAG unbound TAGX unbound TAG}

test uevent-8.3 {watch tags, watch after bind, glob} {
    set res {}
    set t1 [::uevent::bind TAGX E FOO]
    set t2 [::uevent::bind TAG  E FOO]
    set tw [::uevent::watch::tag::add TAG* WATCHT]
    ::uevent::unbind $t1
    ::uevent::unbind $t2
    ::uevent::watch::tag::remove $tw
    set res
} {bound TAG bound TAGX unbound TAGX unbound TAG}

# -------------------------------------------------------------------------

proc WATCHE {a t e} {
    global  res
    lappend res $a $t $e
    return
}
proc SORTE {} {
    global res
    set tmp {}
    foreach {a t e} $res {
	lappend tmp [list $a $t $e]
    }
    set res {}
    foreach item [lsort -dict $tmp] {
	foreach {a t e} $item break
	lappend res $a $t $e
    }
    return $res
}

test uevent-9.0 {watch event add, wrong#args, not enough} {
    catch {
	::uevent::watch::event::add
    } msg
    set msg
} {wrong # args: should be "::uevent::watch::event::add tpattern epattern cmdprefix"}

test uevent-9.1 {watch event add, wrong#args, not enough} {
    catch {
	::uevent::watch::event::add TAG
    } msg
    set msg
} {wrong # args: should be "::uevent::watch::event::add tpattern epattern cmdprefix"}

test uevent-9.2 {watch event add, wrong#args, not enough} {
    catch {
	::uevent::watch::event::add TAG EVENT
    } msg
    set msg
} {wrong # args: should be "::uevent::watch::event::add tpattern epattern cmdprefix"}

test uevent-9.3 {watch event add, wrong#args, too many} {
    catch {
	::uevent::watch::event::add TAG EVENT CMD foo
    } msg
    set msg
} {wrong # args: should be "::uevent::watch::event::add tpattern epattern cmdprefix"}

test uevent-9.4 {watch event remove, wrong#args, not enough} {
    catch {
	::uevent::watch::event::remove
    } msg
    set msg
} {wrong # args: should be "::uevent::watch::event::remove token"}

test uevent-9.5 {watch event remove, wrong#args, not enough} {
    catch {
	::uevent::watch::event::remove TOKEN foo
    } msg
    set msg
} {wrong # args: should be "::uevent::watch::event::remove token"}

test uevent-10.0 {watch events, bind after watch, exact} {
    set res {}
    set tw [::uevent::watch::event::add TAG E WATCHE]
    set t1 [::uevent::bind TAGX E FOO]
    set t2 [::uevent::bind TAG  E FOO]
    set t3 [::uevent::bind TAGX EX FOO]
    set t4 [::uevent::bind TAG  EX FOO]
    ::uevent::unbind $t1
    ::uevent::unbind $t2
    ::uevent::unbind $t3
    ::uevent::unbind $t4
    ::uevent::watch::event::remove $tw
    set res
} {bound TAG E unbound TAG E}

test uevent-10.1 {watch events, watch after bind, exact} {
    set res {}
    set t1 [::uevent::bind TAGX E FOO]
    set t2 [::uevent::bind TAG  E FOO]
    set t3 [::uevent::bind TAGX EX FOO]
    set t4 [::uevent::bind TAG  EX FOO]
    set tw [::uevent::watch::event::add TAG E WATCHE]
    ::uevent::unbind $t1
    ::uevent::unbind $t2
    ::uevent::unbind $t3
    ::uevent::unbind $t4
    ::uevent::watch::event::remove $tw
    set res
} {bound TAG E unbound TAG E}

test uevent-10.2 {watch events, bind after watch, glob} {
    set res {}
    set tw [::uevent::watch::event::add TAG* E* WATCHE]
    set t1 [::uevent::bind TAGX E FOO]
    set t2 [::uevent::bind TAG  E FOO]
    set t3 [::uevent::bind TAGX EX FOO]
    set t4 [::uevent::bind TAG  EX FOO]
    ::uevent::unbind $t1
    ::uevent::unbind $t2
    ::uevent::unbind $t3
    ::uevent::unbind $t4
    ::uevent::watch::event::remove $tw
    set res
} {bound TAGX E bound TAG E bound TAGX EX bound TAG EX unbound TAGX E unbound TAG E unbound TAGX EX unbound TAG EX}

test uevent-10.3 {watch events, watch after bind, glob} {
    set res {}
    set t1 [::uevent::bind TAGX E FOO]
    set t2 [::uevent::bind TAG  E FOO]
    set t3 [::uevent::bind TAGX EX FOO]
    set t4 [::uevent::bind TAG  EX FOO]
    set tw [::uevent::watch::event::add TAG* E* WATCHE]
    SORTE ;# ensure a canonical order
    ::uevent::unbind $t1
    ::uevent::unbind $t2
    ::uevent::unbind $t3
    ::uevent::unbind $t4
    ::uevent::watch::event::remove $tw
    set res
} {bound TAG E bound TAG EX bound TAGX E bound TAGX EX unbound TAGX E unbound TAG E unbound TAGX EX unbound TAG EX}

# -------------------------------------------------------------------------
rename EVENT  {}
rename EVENT2 {}
rename WATCHT {}
rename WATCHE {}
catch {unset res}
testsuiteCleanup