os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/msgcat.test
First public contribution.
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.
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)
9 # See the file "license.terms" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
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.
15 # RCS: @(#) $Id: msgcat.test,v 1.11.2.4 2006/09/11 16:15:12 andreas_kupries Exp $
17 package require Tcl 8.2
18 if {[catch {package require tcltest 2}]} {
19 puts stderr "Skipping tests in [info script]. tcltest 2 required."
22 if {[catch {package require msgcat 1.3.4}]} {
23 puts stderr "Skipping tests in [info script]. No msgcat 1.3.4 found to test."
27 namespace 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*
35 # Tests msgcat-0.*: locale initialization
38 if {[llength $l] == 0} {return [list [list]]}
39 set element [lindex $l 0]
40 set rest [lrange $l 1 end]
42 foreach x [PowerSet $rest] {
43 lappend result [linsert $x 0 $element]
49 variable envVars {LC_ALL LC_MESSAGES LANG}
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]
63 test msgcat-0.$count [list \
64 locale initialization from environment variables $setVars \
67 foreach var $envVars {
68 catch {variable $var $::env($var)}
69 catch {unset ::env($var)}
71 foreach var $setVars {
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
79 interp delete [namespace current]::i
80 foreach var $envVars {
81 catch {unset ::env($var)}
82 catch {set ::env($var) [set [namespace current]::$var]}
84 } -body {i eval msgcat::mclocale} -result $result
89 # Could add tests of initialization from Windows registry here.
90 # Use a fake registry package.
92 # Tests msgcat-1.*: [mclocale], [mcpreferences]
94 test msgcat-1.3 {mclocale set, single element} -setup {
95 variable locale [mclocale]
102 test msgcat-1.4 {mclocale get, single element} -setup {
103 variable locale [mclocale]
111 test msgcat-1.5 {mcpreferences, single element} -setup {
112 variable locale [mclocale]
120 test msgcat-1.6 {mclocale set, two elements} -setup {
121 variable locale [mclocale]
128 test msgcat-1.7 {mclocale get, two elements} -setup {
129 variable locale [mclocale]
137 test msgcat-1.8 {mcpreferences, two elements} -setup {
138 variable locale [mclocale]
146 test msgcat-1.9 {mclocale set, three elements} -setup {
147 variable locale [mclocale]
152 } -result en_us_funky
154 test msgcat-1.10 {mclocale get, three elements} -setup {
155 variable locale [mclocale]
161 } -result en_us_funky
163 test msgcat-1.11 {mcpreferences, three elements} -setup {
164 variable locale [mclocale]
170 } -result {en_us_funky en_us en}
172 test msgcat-1.12 {mclocale set, reject evil input} -setup {
173 variable locale [mclocale]
177 mclocale /path/to/evil/code
178 } -returnCodes error -match glob -result {invalid newLocale value *}
180 test msgcat-1.13 {mclocale set, reject evil input} -setup {
181 variable locale [mclocale]
185 mclocale looks/ok/../../../../but/is/path/to/evil/code
186 } -returnCodes error -match glob -result {invalid newLocale value *}
188 # Tests msgcat-2.*: [mcset], [mcmset], namespace partitioning
190 test msgcat-2.1 {mcset, global scope} {
191 namespace eval :: ::msgcat::mcset foo_BAR text1 text2
194 test msgcat-2.2 {mcset, global scope, default} {
195 namespace eval :: ::msgcat::mcset foo_BAR text3
198 test msgcat-2.2.1 {mcset, namespace overlap} {
199 namespace eval baz {::msgcat::mcset foo_BAR con1 con1baz}
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]
210 namespace eval bar {::msgcat::mc con1}
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]
221 namespace eval baz {::msgcat::mc con1}
224 test msgcat-2.5 {mcmset, global scope} -setup {
226 ::msgcat::mcmset foo_BAR {
231 variable locale [mclocale]
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]
249 namespace eval bar {::msgcat::mc con2}
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]
260 namespace eval baz {::msgcat::mc con2}
263 # Tests msgcat-3.*: [mcset], [mc], catalog "inheritance"
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.
269 # Do this for the 12 permutations of
270 # locales: {foo foo_BAR foo_BAR_baz}
271 # strings: {ov1 ov2 ov3 ov4}
272 # locale foo defines ov1, ov2, ov3
273 # locale foo_BAR defines ov2, ov3
274 # locale foo_BAR_BAZ defines ov3
275 # (ov4 is defined in none)
277 # ov3 should be resolved in foo, foo_BAR, foo_BAR_baz
278 # ov2 should be resolved in foo, foo_BAR
279 # ov2 should resolve to foo_BAR in foo_BAR_baz
280 # ov1 should be resolved in foo
281 # ov1 should resolve to foo in foo_BAR, foo_BAR_baz
282 # ov4 should be resolved in none, and call mcunknown
287 foo,ov1 ov1_foo foo,ov2 ov2_foo foo,ov3 ov3_foo foo,ov4 ov4
288 foo_BAR,ov1 ov1_foo foo_BAR,ov2 ov2_foo_BAR foo_BAR,ov3 ov3_foo_BAR
289 foo_BAR,ov4 ov4 foo_BAR_baz,ov1 ov1_foo foo_BAR_baz,ov2 ov2_foo_BAR
290 foo_BAR_baz,ov3 ov3_foo_BAR_baz foo_BAR_baz,ov4 ov4
294 foreach loc {foo foo_BAR foo_BAR_baz} {
295 foreach string {ov1 ov2 ov3 ov4} {
296 test msgcat-3.$count {mcset, overlap} -setup {
297 mcset foo ov1 ov1_foo
298 mcset foo ov2 ov2_foo
299 mcset foo ov3 ov3_foo
300 mcset foo_BAR ov2 ov2_foo_BAR
301 mcset foo_BAR ov3 ov3_foo_BAR
302 mcset foo_BAR_baz ov3 ov3_foo_BAR_baz
303 variable locale [mclocale]
309 } -result $result($loc,$string)
315 # Tests msgcat-4.*: [mcunknown]
317 test msgcat-4.2 {mcunknown, default} -setup {
318 mcset foo unk1 "unknown 1"
319 variable locale [mclocale]
325 } -result {unknown 1}
327 test msgcat-4.3 {mcunknown, default} -setup {
328 mcset foo unk1 "unknown 1"
329 variable locale [mclocale]
337 test msgcat-4.4 {mcunknown, overridden} -setup {
338 rename ::msgcat::mcunknown SavedMcunknown
339 proc ::msgcat::mcunknown {dom s} {
340 return unknown:$dom:$s
342 mcset foo unk1 "unknown 1"
343 variable locale [mclocale]
347 rename ::msgcat::mcunknown {}
348 rename SavedMcunknown ::msgcat::mcunknown
351 } -result {unknown 1}
353 test msgcat-4.5 {mcunknown, overridden} -setup {
354 rename ::msgcat::mcunknown SavedMcunknown
355 proc ::msgcat::mcunknown {dom s} {
356 return unknown:$dom:$s
358 mcset foo unk1 "unknown 1"
359 variable locale [mclocale]
363 rename ::msgcat::mcunknown {}
364 rename SavedMcunknown ::msgcat::mcunknown
367 } -result {unknown:foo:unk2}
369 test msgcat-4.6 {mcunknown, uplevel context} -setup {
370 rename ::msgcat::mcunknown SavedMcunknown
371 proc ::msgcat::mcunknown {dom s} {
372 return "unknown:$dom:$s:[expr {[info level] - 1}]"
374 mcset foo unk1 "unknown 1"
375 variable locale [mclocale]
379 rename ::msgcat::mcunknown {}
380 rename SavedMcunknown ::msgcat::mcunknown
383 } -result unknown:foo:unk2:[info level]
385 # Tests msgcat-5.*: [mcload]
387 variable locales {foo foo_BAR foo_BAR_baz}
389 foreach loc $locales {
390 makeFile "::msgcat::mcset $loc abc abc-$loc" \
391 [string tolower [file join msgdir $loc.msg]]
394 foreach loc {foo foo_BAR foo_BAR_baz} {
395 test msgcat-5.$count {mcload} -setup {
396 variable locale [mclocale]
401 mcload [file join [temporaryDirectory] msgdir]
406 # Even though foo_BAR_notexist does not exist,
407 # foo_BAR and foo should be loaded.
408 test msgcat-5.4 {mcload} -setup {
409 variable locale [mclocale]
410 mclocale foo_BAR_notexist
414 mcload [file join [temporaryDirectory] msgdir]
417 test msgcat-5.5 {mcload} -setup {
418 variable locale [mclocale]
419 mclocale no_FI_notexist
423 mcload [file join [temporaryDirectory] msgdir]
426 test msgcat-5.6 {mcload} -setup {
427 variable locale [mclocale]
435 test msgcat-5.7 {mcload} -setup {
436 variable locale [mclocale]
442 } -result abc-foo_BAR
444 test msgcat-5.8 {mcload} -setup {
445 variable locale [mclocale]
451 } -result abc-foo_BAR_baz
453 test msgcat-5.9 {mcload} -setup {
454 rename ::msgcat::mcunknown SavedMcunknown
455 proc ::msgcat::mcunknown {dom s} {
456 return unknown:$dom:$s
458 variable locale [mclocale]
459 mclocale no_FI_notexist
462 rename ::msgcat::mcunknown {}
463 rename SavedMcunknown ::msgcat::mcunknown
466 } -result unknown:no_fi_notexist:abc
469 foreach loc $locales {
470 removeFile [string tolower [file join msgdir $loc.msg]]
472 removeDirectory msgdir
474 # Tests msgcat-6.*: [mcset], [mc] namespace inheritance
476 # Test mcset and mc, ensuring that resolution for messages
477 # proceeds from the current ns to its parent and so on to the
480 # Do this for the 12 permutations of
482 # namespaces: foo foo::bar foo::bar::baz
483 # strings: {ov1 ov2 ov3 ov4}
484 # namespace ::foo defines ov1, ov2, ov3
485 # namespace ::foo::bar defines ov2, ov3
486 # namespace ::foo::bar::baz defines ov3
488 # ov4 is not defined in any namespace.
491 # ov3 should be resolved in ::foo::bar::baz, ::foo::bar, ::foo;
492 # ov2 should be resolved in ::foo, ::foo::bar
493 # ov1 should be resolved in ::foo
494 # ov4 should be resolved in none, and call mcunknown
499 foo,ov1 ov1_foo foo,ov2 ov2_foo foo,ov3 ov3_foo foo,ov4 ov4
500 foo::bar,ov1 ov1_foo foo::bar,ov2 ov2_foo_bar
501 foo::bar,ov3 ov3_foo_bar foo::bar,ov4 ov4 foo::bar::baz,ov1 ov1_foo
502 foo::bar::baz,ov2 ov2_foo_bar foo::bar::baz,ov3 ov3_foo_bar_baz
503 foo::bar::baz,ov4 ov4
507 foreach ns {foo foo::bar foo::bar::baz} {
508 foreach string {ov1 ov2 ov3 ov4} {
509 test msgcat-6.$count {mcset, overlap} -setup {
511 ::msgcat::mcset foo ov1 ov1_foo
512 ::msgcat::mcset foo ov2 ov2_foo
513 ::msgcat::mcset foo ov3 ov3_foo
515 ::msgcat::mcset foo ov2 ov2_foo_bar
516 ::msgcat::mcset foo ov3 ov3_foo_bar
518 ::msgcat::mcset foo ov3 "ov3_foo_bar_baz"
523 variable locale [mclocale]
529 namespace eval $ns [list ::msgcat::mc $string]
530 } -result $result($ns,$string)
535 # Tests msgcat-7.*: [mc] extra args processed by [format]
537 test msgcat-7.1 {mc extra args go through to format} -setup {
538 mcset foo format1 "this is a test"
539 mcset foo format2 "this is a %s"
540 mcset foo format3 "this is a %s %s"
541 variable locale [mclocale]
546 mc format1 "good test"
547 } -result "this is a test"
549 test msgcat-7.2 {mc extra args go through to format} -setup {
550 mcset foo format1 "this is a test"
551 mcset foo format2 "this is a %s"
552 mcset foo format3 "this is a %s %s"
553 variable locale [mclocale]
558 mc format2 "good test"
559 } -result "this is a good test"
561 test msgcat-7.3 {mc errors from format are propagated} -setup {
562 mcset foo format1 "this is a test"
563 mcset foo format2 "this is a %s"
564 mcset foo format3 "this is a %s %s"
565 variable locale [mclocale]
570 catch {mc format3 "good test"}
573 test msgcat-7.4 {mc, extra args are given to unknown} -setup {
574 mcset foo format1 "this is a test"
575 mcset foo format2 "this is a %s"
576 mcset foo format3 "this is a %s %s"
577 variable locale [mclocale]
582 mc "this is a %s" "good test"
583 } -result "this is a good test"
587 namespace delete ::msgcat::test