Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/msgcat.test @ 31

Last change on this file since 31 was 25, checked in by landauf, 17 years ago

added tcl to libs

File size: 16.2 KB
Line 
1# This file contains a collection of tests for the msgcat package.
2# Sourcing this file into Tcl runs the tests and
3# generates output for errors.  No output means no errors were found.
4#
5# Copyright (c) 1998 Mark Harrison.
6# Copyright (c) 1998-1999 by Scriptics Corporation.
7# Contributions from Don Porter, NIST, 2002.  (not subject to US copyright)
8#
9# See the file "license.terms" for information on usage and redistribution
10# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11#
12# Note that after running these tests, entries will be left behind in the
13# message catalogs for locales foo, foo_BAR, and foo_BAR_baz.
14#
15# RCS: @(#) $Id: msgcat.test,v 1.20 2006/09/11 15:58:01 andreas_kupries Exp $
16
17package require Tcl 8.2
18if {[catch {package require tcltest 2}]} {
19    puts stderr "Skipping tests in [info script].  tcltest 2 required."
20    return
21}
22if {[catch {package require msgcat 1.4.2}]} {
23    puts stderr "Skipping tests in [info script].  No msgcat 1.4.2 found to test."
24    return
25}
26
27namespace eval ::msgcat::test {
28    namespace import ::msgcat::*
29    namespace import ::tcltest::test
30    namespace import ::tcltest::cleanupTests
31    namespace import ::tcltest::temporaryDirectory
32    namespace import ::tcltest::make*
33    namespace import ::tcltest::remove*
34
35    # Tests msgcat-0.*: locale initialization
36
37    proc PowerSet {l} {
38        if {[llength $l] == 0} {return [list [list]]}
39        set element [lindex $l 0]
40        set rest [lrange $l 1 end]
41        set result [list]
42        foreach x [PowerSet $rest] {
43            lappend result [linsert $x 0 $element]
44            lappend result $x
45        }
46        return $result
47    }
48
49    variable envVars {LC_ALL LC_MESSAGES LANG}
50    variable count 0
51    variable body
52    variable result
53    variable setVars
54    foreach setVars [PowerSet $envVars] {
55        set result [string tolower [lindex $setVars 0]]
56        if {[string length $result] == 0} {
57            if {[info exists ::tcl::mac::locale]} {
58                set result [string tolower $::tcl::mac::locale]
59            } else {
60                set result c
61            }
62        }
63        test msgcat-0.$count [list \
64            locale initialization from environment variables $setVars \
65        ] -setup {
66            variable var
67            foreach var $envVars {
68                catch {variable $var $::env($var)}
69                catch {unset ::env($var)}
70            }
71            foreach var $setVars {
72                set ::env($var) $var
73            }
74            interp create [namespace current]::i
75            i eval [list package ifneeded msgcat [package provide msgcat] \
76                    [package ifneeded msgcat [package provide msgcat]]]
77            i eval package require msgcat
78        } -cleanup {
79            interp delete [namespace current]::i
80            foreach var $envVars {
81                catch {unset ::env($var)}
82                catch {set ::env($var) [set [namespace current]::$var]}
83            }
84        } -body {i eval msgcat::mclocale} -result $result
85        incr count
86    }
87    catch {unset result}
88   
89    # Could add tests of initialization from Windows registry here.
90    # Use a fake registry package.
91
92    # Tests msgcat-1.*: [mclocale], [mcpreferences]
93
94    test msgcat-1.3 {mclocale set, single element} -setup {
95        variable locale [mclocale]
96    } -cleanup {
97        mclocale $locale
98    } -body {
99        mclocale en
100    } -result en
101
102    test msgcat-1.4 {mclocale get, single element} -setup {
103        variable locale [mclocale]
104        mclocale en
105    } -cleanup {
106        mclocale $locale
107    } -body {
108        mclocale
109    } -result en
110
111    test msgcat-1.5 {mcpreferences, single element} -setup {
112        variable locale [mclocale]
113        mclocale en
114    } -cleanup {
115        mclocale $locale
116    } -body {
117        mcpreferences
118    } -result {en {}}
119
120    test msgcat-1.6 {mclocale set, two elements} -setup {
121        variable locale [mclocale]
122    } -cleanup {
123        mclocale $locale
124    } -body {
125        mclocale en_US
126    } -result en_us
127
128    test msgcat-1.7 {mclocale get, two elements} -setup {
129        variable locale [mclocale]
130        mclocale en_US
131    } -cleanup {
132        mclocale $locale
133    } -body {
134        mclocale
135    } -result en_us
136
137    test msgcat-1.8 {mcpreferences, two elements} -setup {
138        variable locale [mclocale]
139        mclocale en_US
140    } -cleanup {
141        mclocale $locale
142    } -body {
143        mcpreferences
144    } -result {en_us en {}}
145
146    test msgcat-1.9 {mclocale set, three elements} -setup {
147        variable locale [mclocale]
148    } -cleanup {
149        mclocale $locale
150    } -body {
151        mclocale en_US_funky
152    } -result en_us_funky
153
154    test msgcat-1.10 {mclocale get, three elements} -setup {
155        variable locale [mclocale]
156        mclocale en_US_funky
157    } -cleanup {
158        mclocale $locale
159    } -body {
160        mclocale
161    } -result en_us_funky
162
163    test msgcat-1.11 {mcpreferences, three elements} -setup {
164        variable locale [mclocale]
165        mclocale en_US_funky
166    } -cleanup {
167        mclocale $locale
168    } -body {
169        mcpreferences
170    } -result {en_us_funky en_us en {}}
171
172    test msgcat-1.12 {mclocale set, reject evil input} -setup {
173        variable locale [mclocale]
174    } -cleanup {
175        mclocale $locale
176    } -body {
177        mclocale /path/to/evil/code
178    } -returnCodes error -match glob -result {invalid newLocale value *}
179
180    test msgcat-1.13 {mclocale set, reject evil input} -setup {
181        variable locale [mclocale]
182    } -cleanup {
183        mclocale $locale
184    } -body {
185        mclocale looks/ok/../../../../but/is/path/to/evil/code
186    } -returnCodes error -match glob -result {invalid newLocale value *}
187
188    # Tests msgcat-2.*: [mcset], [mcmset], namespace partitioning
189
190    test msgcat-2.1 {mcset, global scope} {
191        namespace eval :: ::msgcat::mcset  foo_BAR text1 text2
192    } {text2}
193
194    test msgcat-2.2 {mcset, global scope, default} {
195        namespace eval :: ::msgcat::mcset foo_BAR text3
196    } {text3}
197
198    test msgcat-2.2.1 {mcset, namespace overlap} {
199        namespace eval baz {::msgcat::mcset  foo_BAR con1 con1baz}
200    } {con1baz}
201
202    test msgcat-2.3 {mcset, namespace overlap} -setup {
203        namespace eval bar {::msgcat::mcset  foo_BAR con1 con1bar}
204        namespace eval baz {::msgcat::mcset  foo_BAR con1 con1baz}
205        variable locale [mclocale]
206        mclocale foo_BAR
207    } -cleanup {
208        mclocale $locale
209    } -body {
210        namespace eval bar {::msgcat::mc con1}
211    } -result con1bar
212
213    test msgcat-2.4 {mcset, namespace overlap} -setup {
214        namespace eval bar {::msgcat::mcset  foo_BAR con1 con1bar}
215        namespace eval baz {::msgcat::mcset  foo_BAR con1 con1baz}
216        variable locale [mclocale]
217        mclocale foo_BAR
218    } -cleanup {
219        mclocale $locale
220    } -body {
221        namespace eval baz {::msgcat::mc con1}
222    } -result con1baz
223
224    test msgcat-2.5 {mcmset, global scope} -setup {
225        namespace eval :: {
226            ::msgcat::mcmset  foo_BAR {
227                src1 trans1
228                src2 trans2
229            }
230        }
231        variable locale [mclocale]
232        mclocale foo_BAR
233    } -cleanup {
234        mclocale $locale
235    } -body {
236        namespace eval :: {
237            ::msgcat::mc src1
238        }
239    } -result trans1
240
241    test msgcat-2.6 {mcmset, namespace overlap} -setup {
242        namespace eval bar {::msgcat::mcmset  foo_BAR {con2 con2bar}}
243        namespace eval baz {::msgcat::mcmset  foo_BAR {con2 con2baz}}
244        variable locale [mclocale]
245        mclocale foo_BAR
246    } -cleanup {
247        mclocale $locale
248    } -body {
249        namespace eval bar {::msgcat::mc con2}
250    } -result con2bar
251
252    test msgcat-2.7 {mcmset, namespace overlap} -setup {
253        namespace eval bar {::msgcat::mcmset  foo_BAR {con2 con2bar}}
254        namespace eval baz {::msgcat::mcmset  foo_BAR {con2 con2baz}}
255        variable locale [mclocale]
256        mclocale foo_BAR
257    } -cleanup {
258        mclocale $locale
259    } -body {
260        namespace eval baz {::msgcat::mc con2}
261    } -result con2baz
262
263    # Tests msgcat-3.*: [mcset], [mc], catalog "inheritance"
264    #
265    # Test mcset and mc, ensuring that more specific locales
266    # (e.g. en_UK) will search less specific locales
267    # (e.g. en) for translation strings.
268    #
269    # Do this for the 15 permutations of
270    #     locales: {foo foo_BAR foo_BAR_baz}
271    #     strings: {ov0 ov1 ov2 ov3 ov4}
272    #     locale ROOT        defines ov0, ov1, ov2, ov3
273    #     locale foo         defines      ov1, ov2, ov3
274    #     locale foo_BAR     defines           ov2, ov3
275    #     locale foo_BAR_BAZ defines                ov3
276    #     (ov4 is defined in none)
277    # So,
278    #     ov3 should be resolved in foo, foo_BAR, foo_BAR_baz
279    #     ov2 should be resolved in foo, foo_BAR
280    #     ov2 should resolve to foo_BAR in foo_BAR_baz
281    #     ov1 should be resolved in foo
282    #     ov1 should resolve to foo in foo_BAR, foo_BAR_baz
283    #     ov4 should be resolved in none, and call mcunknown
284    #
285    variable count 2
286    variable result
287    array set result {
288        foo,ov0 ov0_ROOT foo,ov1 ov1_foo foo,ov2 ov2_foo
289        foo,ov3 ov3_foo foo,ov4 ov4
290        foo_BAR,ov0 ov0_ROOT foo_BAR,ov1 ov1_foo foo_BAR,ov2 ov2_foo_BAR
291        foo_BAR,ov3 ov3_foo_BAR foo_BAR,ov4 ov4
292        foo_BAR_baz,ov0 ov0_ROOT foo_BAR_baz,ov1 ov1_foo
293        foo_BAR_baz,ov2 ov2_foo_BAR
294        foo_BAR_baz,ov3 ov3_foo_BAR_baz foo_BAR_baz,ov4 ov4
295    }
296    variable loc
297    variable string
298    foreach loc {foo foo_BAR foo_BAR_baz} {
299        foreach string {ov0 ov1 ov2 ov3 ov4} {
300            test msgcat-3.$count {mcset, overlap} -setup {
301                mcset {} ov0 ov0_ROOT
302                mcset {} ov1 ov1_ROOT
303                mcset {} ov2 ov2_ROOT
304                mcset {} ov3 ov3_ROOT
305                mcset foo ov1 ov1_foo
306                mcset foo ov2 ov2_foo
307                mcset foo ov3 ov3_foo
308                mcset foo_BAR ov2 ov2_foo_BAR
309                mcset foo_BAR ov3 ov3_foo_BAR
310                mcset foo_BAR_baz ov3 ov3_foo_BAR_baz
311                variable locale [mclocale]
312                mclocale $loc
313            } -cleanup {
314                mclocale $locale
315            } -body {
316                mc $string
317            } -result $result($loc,$string)
318            incr count
319        }
320    }
321    catch {unset result}
322
323    # Tests msgcat-4.*: [mcunknown]
324
325    test msgcat-4.2 {mcunknown, default} -setup {
326        mcset foo unk1 "unknown 1"
327        variable locale [mclocale]
328        mclocale foo
329    } -cleanup {
330        mclocale $locale
331    } -body {
332        mc unk1
333    } -result {unknown 1}
334
335    test msgcat-4.3 {mcunknown, default} -setup {
336        mcset foo unk1 "unknown 1"
337        variable locale [mclocale]
338        mclocale foo
339    } -cleanup {
340        mclocale $locale
341    } -body {
342        mc unk2
343    } -result unk2
344
345    test msgcat-4.4 {mcunknown, overridden} -setup {
346        rename ::msgcat::mcunknown SavedMcunknown
347        proc ::msgcat::mcunknown {dom s} {
348            return unknown:$dom:$s
349        }
350        mcset foo unk1 "unknown 1"
351        variable locale [mclocale]
352        mclocale foo
353    } -cleanup {
354        mclocale $locale
355        rename ::msgcat::mcunknown {}
356        rename SavedMcunknown ::msgcat::mcunknown
357    } -body {
358        mc unk1
359    } -result {unknown 1}
360
361    test msgcat-4.5 {mcunknown, overridden} -setup {
362        rename ::msgcat::mcunknown SavedMcunknown
363        proc ::msgcat::mcunknown {dom s} {
364            return unknown:$dom:$s
365        }
366        mcset foo unk1 "unknown 1"
367        variable locale [mclocale]
368        mclocale foo
369    } -cleanup {
370        mclocale $locale
371        rename ::msgcat::mcunknown {}
372        rename SavedMcunknown ::msgcat::mcunknown
373    } -body {
374        mc unk2
375    } -result {unknown:foo:unk2}
376
377    test msgcat-4.6 {mcunknown, uplevel context} -setup {
378        rename ::msgcat::mcunknown SavedMcunknown
379        proc ::msgcat::mcunknown {dom s} {
380            return "unknown:$dom:$s:[expr {[info level] - 1}]"
381        }
382        mcset foo unk1 "unknown 1"
383        variable locale [mclocale]
384        mclocale foo
385    } -cleanup {
386        mclocale $locale
387        rename ::msgcat::mcunknown {}
388        rename SavedMcunknown ::msgcat::mcunknown
389    } -body {
390        mc unk2
391    } -result unknown:foo:unk2:[info level]
392
393    # Tests msgcat-5.*: [mcload]
394
395    variable locales {{} foo foo_BAR foo_BAR_baz}
396    set msgdir [makeDirectory msgdir]
397    foreach loc $locales {
398        if { $loc eq {} } {
399            set msg ROOT
400        } else {
401            set msg [string tolower $loc]
402        }
403        makeFile [list ::msgcat::mcset $loc abc abc-$loc] $msg.msg $msgdir
404    }
405    variable count 1
406    foreach loc {foo foo_BAR foo_BAR_baz} {
407        test msgcat-5.$count {mcload} -setup {
408            variable locale [mclocale]
409            mclocale $loc
410        } -cleanup {
411            mclocale $locale
412        } -body {
413            mcload $msgdir
414        } -result [expr { $count+1 }]
415        incr count
416    }
417
418    # Even though foo_BAR_notexist does not exist,
419    # foo_BAR, foo and the root should be loaded.
420        test msgcat-5.4 {mcload} -setup {
421            variable locale [mclocale]
422            mclocale foo_BAR_notexist
423        } -cleanup {
424            mclocale $locale
425        } -body {
426            mcload $msgdir
427        } -result 3
428
429        test msgcat-5.5 {mcload} -setup {
430            variable locale [mclocale]
431            mclocale no_FI_notexist
432        } -cleanup {
433            mclocale $locale
434        } -body {
435            mcload $msgdir
436        } -result 1
437
438        test msgcat-5.6 {mcload} -setup {
439            variable locale [mclocale]
440            mclocale foo
441            mcload $msgdir
442        } -cleanup {
443            mclocale $locale
444        } -body {
445            mc abc
446        } -result abc-foo
447
448        test msgcat-5.7 {mcload} -setup {
449            variable locale [mclocale]
450            mclocale foo_BAR
451            mcload $msgdir
452        } -cleanup {
453            mclocale $locale
454        } -body {
455            mc abc
456        } -result abc-foo_BAR
457
458        test msgcat-5.8 {mcload} -setup {
459            variable locale [mclocale]
460            mclocale foo_BAR_baz
461            mcload $msgdir
462        } -cleanup {
463            mclocale $locale
464        } -body {
465            mc abc
466        } -result abc-foo_BAR_baz
467
468        test msgcat-5.9 {mcload} -setup {
469            variable locale [mclocale]
470            mclocale no_FI_notexist
471            mcload $msgdir
472        } -cleanup {
473            mclocale $locale
474        } -body {
475            mc abc
476        } -result abc-
477
478        test msgcat-5.10 {mcload} -setup {
479            rename ::msgcat::mcunknown SavedMcunknown
480            proc ::msgcat::mcunknown {dom s} {
481                return unknown:$dom:$s
482            }
483            variable locale [mclocale]
484            mclocale no_FI_notexist
485            mcload $msgdir
486        } -cleanup {
487            mclocale $locale
488            rename ::msgcat::mcunknown {}
489            rename SavedMcunknown ::msgcat::mcunknown
490        } -body {
491            mc def
492        } -result unknown:no_fi_notexist:def
493
494    foreach loc $locales {
495        if { $loc eq {} } {
496            set msg ROOT
497        } else {
498            set msg [string tolower $loc]
499        }
500        removeFile $msg.msg $msgdir
501    }
502    removeDirectory msgdir
503
504    # Tests msgcat-6.*: [mcset], [mc] namespace inheritance
505#
506# Test mcset and mc, ensuring that resolution for messages
507# proceeds from the current ns to its parent and so on to the
508# global ns.
509#
510# Do this for the 12 permutations of
511#     locales: foo
512#     namespaces: foo foo::bar foo::bar::baz
513#     strings: {ov1 ov2 ov3 ov4}
514#     namespace ::foo            defines ov1, ov2, ov3
515#     namespace ::foo::bar       defines      ov2, ov3
516#     namespace ::foo::bar::baz  defines           ov3
517#
518#     ov4 is not defined in any namespace.
519#
520# So,
521#     ov3 should be resolved in ::foo::bar::baz, ::foo::bar, ::foo;
522#     ov2 should be resolved in ::foo, ::foo::bar
523#     ov1 should be resolved in ::foo
524#     ov4 should be resolved in none, and call mcunknown
525#
526
527    variable result
528    array set result {
529        foo,ov1 ov1_foo foo,ov2 ov2_foo foo,ov3 ov3_foo foo,ov4 ov4
530        foo::bar,ov1 ov1_foo foo::bar,ov2 ov2_foo_bar
531        foo::bar,ov3 ov3_foo_bar foo::bar,ov4 ov4 foo::bar::baz,ov1 ov1_foo
532        foo::bar::baz,ov2 ov2_foo_bar foo::bar::baz,ov3 ov3_foo_bar_baz
533        foo::bar::baz,ov4 ov4
534    }
535    variable count 1
536    variable ns
537    foreach ns {foo foo::bar foo::bar::baz} {
538        foreach string {ov1 ov2 ov3 ov4} {
539            test msgcat-6.$count {mcset, overlap} -setup {
540                namespace eval foo {
541                    ::msgcat::mcset foo ov1 ov1_foo
542                    ::msgcat::mcset foo ov2 ov2_foo
543                    ::msgcat::mcset foo ov3 ov3_foo
544                    namespace eval bar {
545                        ::msgcat::mcset foo ov2 ov2_foo_bar
546                        ::msgcat::mcset foo ov3 ov3_foo_bar
547                        namespace eval baz {
548                            ::msgcat::mcset foo ov3 "ov3_foo_bar_baz"
549                        }
550                    }
551                   
552                }
553                variable locale [mclocale]
554                mclocale foo
555            } -cleanup {
556                mclocale $locale
557                namespace delete foo
558            } -body {
559                namespace eval $ns [list ::msgcat::mc $string]
560            } -result $result($ns,$string)
561            incr count
562        }
563    }
564
565    # Tests msgcat-7.*: [mc] extra args processed by [format]
566
567    test msgcat-7.1 {mc extra args go through to format} -setup {
568        mcset foo format1 "this is a test"
569        mcset foo format2 "this is a %s"
570        mcset foo format3 "this is a %s %s"
571        variable locale [mclocale]
572        mclocale foo
573    } -cleanup {
574        mclocale $locale
575    } -body {
576        mc format1 "good test"
577    } -result "this is a test"
578
579    test msgcat-7.2 {mc extra args go through to format} -setup {
580        mcset foo format1 "this is a test"
581        mcset foo format2 "this is a %s"
582        mcset foo format3 "this is a %s %s"
583        variable locale [mclocale]
584        mclocale foo
585    } -cleanup {
586        mclocale $locale
587    } -body {
588        mc format2 "good test"
589    } -result "this is a good test"
590
591    test msgcat-7.3 {mc errors from format are propagated} -setup {
592        mcset foo format1 "this is a test"
593        mcset foo format2 "this is a %s"
594        mcset foo format3 "this is a %s %s"
595        variable locale [mclocale]
596        mclocale foo
597    } -cleanup {
598        mclocale $locale
599    } -body {
600        catch {mc format3 "good test"}
601    } -result 1
602
603    test msgcat-7.4 {mc, extra args are given to unknown} -setup {
604        mcset foo format1 "this is a test"
605        mcset foo format2 "this is a %s"
606        mcset foo format3 "this is a %s %s"
607        variable locale [mclocale]
608        mclocale foo
609    } -cleanup {
610        mclocale $locale
611    } -body {
612        mc "this is a %s" "good test"
613    } -result "this is a good test"
614
615    cleanupTests
616}
617namespace delete ::msgcat::test
618return
619
Note: See TracBrowser for help on using the repository browser.