File: tie_file.test

package info (click to toggle)
tcllib 1.8-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 13,628 kB
  • ctags: 4,897
  • sloc: tcl: 88,012; sh: 7,856; ansic: 4,174; xml: 1,765; yacc: 753; perl: 84; f90: 84; makefile: 60; python: 33; ruby: 13; php: 11
file content (430 lines) | stat: -rw-r--r-- 11,926 bytes parent folder | download
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
# Tests for the tie module.                              -*- tcl -*- 
#
# Copyright (c) 2004 Andreas Kupries <andreas_kupries@users.sourceforge.net>
# All rights reserved.
#
# RCS: @(#) $Id: tie_file.test,v 1.5 2005/09/28 04:51:24 andreas_kupries Exp $

if {![package vsatisfies [package provide Tcl] 8.4]} {
    puts "Aborting tests for snit."
    puts "Requiring Tcl 8.4, have [package present Tcl]"
    return
}

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import ::tcltest::*
}

if { [lsearch $auto_path [file dirname [info script]]] == -1 } {
    set auto_path [linsert $auto_path 0 [file dirname [info script]]]
}

# -------------------------------------------------------------------------
# Ensure we test _this_ local copy and not one installed somewhere else.
# We also ensure to use local copies of the prerequisite packages.

package forget tie::std::file
package forget tie
package forget snit

catch {namespace delete tie::std::file}
catch {namespace delete tie}
catch {namespace delete snit}

if {[catch {source [file join [file dirname [info script]] .. snit snit.tcl]} msg]} {
    puts "skipped [file tail [info script]] (snit.tcl): $msg"
    return
}
if {[catch {source [file join [file dirname [info script]] tie_file.tcl]} msg]} {
    puts "skipped [file tail [info script]] (tie_file.tcl): $msg"
    return
}
if {[catch {source [file join [file dirname [info script]] tie.tcl]} msg]} {
    puts "skipped [file tail [info script]] (tie.tcl): $msg"
    return
}

puts "tie::std::file [package present tie::std::file]"
puts "- tie          [package present tie]"
puts "- snit         [package present snit]"

# -------------------------------------------------------------------------
# Takes a dictionary, returns a list containing the same dictionary,
# however the keys are sorted alphabetically. This allows for a true
# comparison of dictionary results.

proc dictsort {dict} {
    array set a $dict
    set out [list]
    foreach key [lsort [array names a]] {
	lappend out $key $a($key)
    }
    return $out
}

# -------------------------------------------------------------------------
# Creation of array daat sources
# Errors: Undefined variable, scalar, local variable

test tie-file-1.0 {file creation, wrong#args} {
    catch {tie::std::file} msg
    set msg
} {Error in constructor: wrong # args: should be "::tie::std::file::Snit_constructor type selfns win self thepath"}

test tie-file-1.1 {file creation, wrong#args} {
    catch {tie::std::file x} msg
    set msg
} {Error in constructor: wrong # args: should be "::tie::std::file::Snit_constructor type selfns win self thepath"}

test tie-file-1.2 {file creation, wrong#args} {
    catch {tie::std::file x y z} msg
    set msg
} {Error in constructor: wrong # args: should be "::tie::std::file::Snit_constructor type selfns win self thepath"}

test tie-file-1.3 {file creation, ok args, unwritable file} {unixOnly} {
    set f [makeFile {} journal]
    file attributes $f -permissions ugo-w
    catch {tie::std::file x $f} msg
    removeFile journal
    string map [list $f @] $msg
} {Error in constructor: couldn't open "@": permission denied}

test tie-file-1.4 {file creation, ok args, unwritable file} {winOnly} {
    set f [makeFile {} journal]
    file attributes $f -readonly 1
    catch {tie::std::file x $f} msg
    file attributes $f -readonly 0
    removeFile journal
    string map [list $f @] $msg
} {Error in constructor: couldn't open "@": permission denied}

test tie-file-1.5 {file creation, ok args, unreadable file} {unixOnly} {
    set f [makeFile {} journal]
    file attributes $f -permissions ugo-r
    catch {tie::std::file x $f} msg
    removeFile journal
     string map [list $f @] $msg
} {Error in constructor: couldn't open "@": permission denied}

test tie-file-1.6 {file creation, ok args, uncreateable file} {unixOnly} {
    set d [makeDirectory jtest]
    set f [makeFile {}   jtest/journal]
    removeFile jtest/journal
    file attributes $d -permissions ugo-w

    catch {tie::std::file x $f} msg

    removeFile      jtest/journal
    removeDirectory jtest
    string map [list $f @] $msg
} {Error in constructor: couldn't open "@": permission denied}

test tie-file-1.7 {file creation, ok args, uncreateable file} {knownBug winOnly} {
    set d [makeDirectory jtest]
    set f [makeFile {}   jtest/journal]
    removeFile jtest/journal
    file attributes $d -readonly 1

    catch {tie::std::file x $f} msg

    removeFile      jtest/journal
    removeDirectory jtest
    string map [list $f @] $msg
} {Error in constructor: couldn't open "@": permission denied}

test tie-file-1.8 {file creation, fixed name, array} {
    set f [makeFile {} journal]
    set msg [tie::std::file x $f]
    x destroy
    removeFile journal
    set msg
} {::x}

test tie-file-1.9 {file creation, %AUTO%} {
    set f [makeFile {} journal]
    set msg [tie::std::file %AUTO% $f]
    $msg destroy
    removeFile journal
    string match ::file\[79] $msg
} 1

# -------------------------------------------------------------------------
## Methods

test tie-file-2.0 {file get, wrong#args} {
    set f [makeFile {array set {b 2 a 1}} journal]
    tie::std::file x $f
    catch {x get a} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodget type selfns win self"}

test tie-file-2.1 {file get} {
    set f [makeFile {array set {b 2 a 1}} journal]
    tie::std::file x $f
    set res [dictsort [x get]]
    x destroy
    removeFile journal
    set res
} {a 1 b 2}


test tie-file-3.0 {file set, wrong#args} {
    set f [makeFile {} journal]
    tie::std::file x $f
    set     res {}
    catch {x set} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodset type selfns win self dict"}

test tie-file-3.1 {file set, wrong#args} {
    set f [makeFile {} journal]
    tie::std::file x $f
    set     res {}
    catch {x set a b} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodset type selfns win self dict"}

test tie-file-3.2 {file set} {
    set f [makeFile {} journal]
    tie::std::file x $f
    set     res {}
    lappend res [x set {c 3 b 2 a 1}]
    lappend res [viewFile journal]
    x destroy
    removeFile journal
    set res
} {{} {
array set {c 3 b 2 a 1}}}


test tie-file-4.0 {file names, wrong#args} {
    set f [makeFile {array set {b 2 a 1}} journal]
    tie::std::file x $f
    catch {x names a} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodnames type selfns win self"}

test tie-file-4.1 {file names} {
    set f [makeFile {array set {b 2 a 1}} journal]
    tie::std::file x $f
    set res [lsort [x names]]
    x destroy
    removeFile journal
    set res
} {a b}


test tie-file-5.0 {file size, wrong#args} {
    set f [makeFile {array set {b 2 a 1}} journal]
    tie::std::file x $f
    catch {x size a} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodsize type selfns win self"}

test tie-file-5.1 {file size} {
    set f [makeFile {array set {b 2 a 1}} journal]
    tie::std::file x $f
    set res [x size]
    x destroy
    removeFile journal
    set res
} 2


test tie-file-6.0 {file unset, wrong#args} {
    set f [makeFile {array set {foo bar fox snarf a 3}} journal]
    tie::std::file x $f
    set     res {}
    catch {x unset a b} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodunset type selfns win self ?pattern?"}

test tie-file-6.1 {file unset, default pattern} {
    set f [makeFile {array set {foo bar fox snarf a 3}} journal]
    tie::std::file x $f
    set     res {}
    lappend res [x unset]
    lappend res [viewFile journal]
    x destroy
    removeFile journal
    set res
} {{} {array set {foo bar fox snarf a 3}
array unset *}}

test tie-file-6.2 {file unset, by pattern} {
    set f [makeFile {array set {foo bar fox snarf a 3}} journal]
    tie::std::file x $f
    set     res {}
    lappend res [x unset f*]
    lappend res [viewFile journal]
    x destroy
    removeFile journal
    set res
} {{} {array set {foo bar fox snarf a 3}
array unset f*}}


test tie-file-7.0 {file getv, wrong#args} {
    set f [makeFile {} journal]
    tie::std::file x $f
    set     res {}
    catch {x getv} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodgetv type selfns win self index"}

test tie-file-7.1 {file getv, wrong#args} {
    set f [makeFile {} journal]
    tie::std::file x $f
    set     res {}
    catch {x getv a b} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodgetv type selfns win self index"}

test tie-file-7.2 {file getv} {
    set f [makeFile {array set {a 3 b 4}} journal]
    tie::std::file x $f
    set     res {}
    lappend res [x getv a]
    x destroy
    removeFile journal
    set res
} 3


test tie-file-8.0 {file setv, wrong#args} {
    set f [makeFile {} journal]
    tie::std::file x $f
    set     res {}
    catch {x setv} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodsetv type selfns win self index value"}

test tie-file-8.1 {file setv, wrong#args} {
    set f [makeFile {} journal]
    tie::std::file x $f
    set     res {}
    catch {x setv a} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodsetv type selfns win self index value"}

test tie-file-8.2 {file setv, wrong#args} {
    set f [makeFile {} journal]
    tie::std::file x $f
    set     res {}
    catch {x setv a b c} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodsetv type selfns win self index value"}

test tie-file-8.3 {file setv} {
    set f [makeFile {} journal]
    tie::std::file x $f
    set     res {}
    lappend res [x setv fox snarf]
    lappend res [viewFile journal]
    x destroy
    removeFile journal
    set res
} {{} {
set fox snarf}}


test tie-file-9.0 {file unsetv, wrong#args} {
    set f [makeFile {} journal]
    tie::std::file x $f
    set     res {}
    catch {x unsetv} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodunsetv type selfns win self index"}

test tie-file-9.1 {file unsetv, wrong#args} {
    set f [makeFile {} journal]
    tie::std::file x $f
    set     res {}
    catch {x unsetv a b} msg
    x destroy
    removeFile journal
    set msg
} {wrong # args: should be "::tie::std::file::Snit_methodunsetv type selfns win self index"}

test tie-file-9.2 {file unsetv} {
    set f [makeFile {array set {a 3 b 4}} journal]
    tie::std::file x $f
    set     res {}
    lappend res [x unsetv a]
    lappend res [viewFile journal]
    x destroy
    removeFile journal
    set res
} {{} {array set {a 3 b 4}
unset a}}

# -------------------------------------------------------------------------
## File compaction

test tie-file-10.0 {file compaction} {
    set f [makeFile {
	set aa 3
	set ab 4
	set ac 5
	set ad 6
	set ae 7
	set f 8
	array unset a*
    } journal] ; # {}
    tie::std::file x $f
    x names ; # This loads the journal and causes compaction
    x destroy
    set res [viewFile journal]
    removeFile journal
    set res
} {array set {f 8}}

test tie-file-10.1 {file compaction, not} {
    set f [makeFile {
	set aa 3
	set ab 4
	set ac 5
	set ad 6
	set ae 7
    } journal] ; # {}
    tie::std::file x $f
    x names ; # This loads the journal and causes compaction
    x destroy
    set res [viewFile journal]
    removeFile journal
    set res
} {
	set aa 3
	set ab 4
	set ac 5
	set ad 6
	set ae 7
    }

::tcltest::cleanupTests
return