os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/msgcat.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
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.
     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.11.2.4 2006/09/11 16:15:12 andreas_kupries Exp $
    16 
    17 package require Tcl 8.2
    18 if {[catch {package require tcltest 2}]} {
    19     puts stderr "Skipping tests in [info script].  tcltest 2 required."
    20     return
    21 }
    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."
    24     return
    25 }
    26 
    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*
    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 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)
   276     # So,
   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
   283     #
   284     variable count 2
   285     variable result
   286     array set result {
   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
   291     }
   292     variable loc
   293     variable string
   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]
   304 		mclocale $loc
   305 	    } -cleanup {
   306 		mclocale $locale
   307 	    } -body {
   308 		mc $string
   309 	    } -result $result($loc,$string)
   310 	    incr count
   311 	}
   312     }
   313     catch {unset result}
   314 
   315     # Tests msgcat-4.*: [mcunknown]
   316 
   317     test msgcat-4.2 {mcunknown, default} -setup {
   318 	mcset foo unk1 "unknown 1"
   319 	variable locale [mclocale]
   320 	mclocale foo
   321     } -cleanup {
   322 	mclocale $locale
   323     } -body {
   324 	mc unk1
   325     } -result {unknown 1}
   326 
   327     test msgcat-4.3 {mcunknown, default} -setup {
   328 	mcset foo unk1 "unknown 1"
   329 	variable locale [mclocale]
   330 	mclocale foo
   331     } -cleanup {
   332 	mclocale $locale
   333     } -body {
   334 	mc unk2
   335     } -result unk2
   336 
   337     test msgcat-4.4 {mcunknown, overridden} -setup {
   338 	rename ::msgcat::mcunknown SavedMcunknown
   339 	proc ::msgcat::mcunknown {dom s} {
   340             return unknown:$dom:$s
   341 	}
   342 	mcset foo unk1 "unknown 1"
   343 	variable locale [mclocale]
   344 	mclocale foo
   345     } -cleanup {
   346 	mclocale $locale
   347 	rename ::msgcat::mcunknown {}
   348 	rename SavedMcunknown ::msgcat::mcunknown
   349     } -body {
   350 	mc unk1
   351     } -result {unknown 1}
   352 
   353     test msgcat-4.5 {mcunknown, overridden} -setup {
   354 	rename ::msgcat::mcunknown SavedMcunknown
   355 	proc ::msgcat::mcunknown {dom s} {
   356             return unknown:$dom:$s
   357 	}
   358 	mcset foo unk1 "unknown 1"
   359 	variable locale [mclocale]
   360 	mclocale foo
   361     } -cleanup {
   362 	mclocale $locale
   363 	rename ::msgcat::mcunknown {}
   364 	rename SavedMcunknown ::msgcat::mcunknown
   365     } -body {
   366 	mc unk2
   367     } -result {unknown:foo:unk2}
   368 
   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}]"
   373 	}
   374 	mcset foo unk1 "unknown 1"
   375 	variable locale [mclocale]
   376 	mclocale foo
   377     } -cleanup {
   378 	mclocale $locale
   379 	rename ::msgcat::mcunknown {}
   380 	rename SavedMcunknown ::msgcat::mcunknown
   381     } -body {
   382 	mc unk2
   383     } -result unknown:foo:unk2:[info level]
   384 
   385     # Tests msgcat-5.*: [mcload]
   386 
   387     variable locales {foo foo_BAR foo_BAR_baz}
   388     makeDirectory msgdir
   389     foreach loc $locales {
   390 	makeFile "::msgcat::mcset $loc abc abc-$loc" \
   391 		[string tolower [file join msgdir $loc.msg]]
   392     }
   393     variable count 1
   394     foreach loc {foo foo_BAR foo_BAR_baz} {
   395 	test msgcat-5.$count {mcload} -setup {
   396 	    variable locale [mclocale]
   397 	    mclocale $loc
   398 	} -cleanup {
   399 	    mclocale $locale
   400 	} -body {
   401 	    mcload [file join [temporaryDirectory] msgdir]
   402 	} -result $count
   403 	incr count
   404     }
   405 
   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
   411 	} -cleanup {
   412 	    mclocale $locale
   413 	} -body {
   414 	    mcload [file join [temporaryDirectory] msgdir]
   415 	} -result 2
   416 
   417 	test msgcat-5.5 {mcload} -setup {
   418 	    variable locale [mclocale]
   419 	    mclocale no_FI_notexist
   420 	} -cleanup {
   421 	    mclocale $locale
   422 	} -body {
   423 	    mcload [file join [temporaryDirectory] msgdir]
   424 	} -result 0
   425 
   426 	test msgcat-5.6 {mcload} -setup {
   427 	    variable locale [mclocale]
   428 	    mclocale foo
   429 	} -cleanup {
   430 	    mclocale $locale
   431 	} -body {
   432 	    mc abc
   433 	} -result abc-foo
   434 
   435 	test msgcat-5.7 {mcload} -setup {
   436 	    variable locale [mclocale]
   437 	    mclocale foo_BAR
   438 	} -cleanup {
   439 	    mclocale $locale
   440 	} -body {
   441 	    mc abc
   442 	} -result abc-foo_BAR
   443 
   444 	test msgcat-5.8 {mcload} -setup {
   445 	    variable locale [mclocale]
   446 	    mclocale foo_BAR_baz
   447 	} -cleanup {
   448 	    mclocale $locale
   449 	} -body {
   450 	    mc abc
   451 	} -result abc-foo_BAR_baz
   452 
   453 	test msgcat-5.9 {mcload} -setup {
   454 	    rename ::msgcat::mcunknown SavedMcunknown
   455 	    proc ::msgcat::mcunknown {dom s} {
   456 		return unknown:$dom:$s
   457 	    }
   458 	    variable locale [mclocale]
   459 	    mclocale no_FI_notexist
   460 	} -cleanup {
   461 	    mclocale $locale
   462 	    rename ::msgcat::mcunknown {}
   463 	    rename SavedMcunknown ::msgcat::mcunknown
   464 	} -body {
   465 	    mc abc
   466 	} -result unknown:no_fi_notexist:abc
   467 
   468 
   469     foreach loc $locales {
   470 	removeFile [string tolower [file join msgdir $loc.msg]]
   471     }
   472     removeDirectory msgdir
   473 
   474     # Tests msgcat-6.*: [mcset], [mc] namespace inheritance
   475 #
   476 # Test mcset and mc, ensuring that resolution for messages
   477 # proceeds from the current ns to its parent and so on to the 
   478 # global ns.
   479 #
   480 # Do this for the 12 permutations of
   481 #     locales: foo
   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
   487 #
   488 #     ov4 is not defined in any namespace.
   489 #
   490 # So,
   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
   495 #
   496 
   497     variable result
   498     array set result {
   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
   504     }
   505     variable count 1
   506     variable ns
   507     foreach ns {foo foo::bar foo::bar::baz} {
   508 	foreach string {ov1 ov2 ov3 ov4} {
   509 	    test msgcat-6.$count {mcset, overlap} -setup {
   510 		namespace eval foo {
   511 		    ::msgcat::mcset foo ov1 ov1_foo
   512 		    ::msgcat::mcset foo ov2 ov2_foo
   513 		    ::msgcat::mcset foo ov3 ov3_foo
   514 		    namespace eval bar {
   515 			::msgcat::mcset foo ov2 ov2_foo_bar
   516 			::msgcat::mcset foo ov3 ov3_foo_bar
   517 			namespace eval baz {
   518 			    ::msgcat::mcset foo ov3 "ov3_foo_bar_baz"
   519 			}
   520 		    }
   521 		    
   522 		}
   523 		variable locale [mclocale]
   524 		mclocale foo
   525 	    } -cleanup {
   526 		mclocale $locale
   527 		namespace delete foo
   528 	    } -body {
   529 		namespace eval $ns [list ::msgcat::mc $string]
   530 	    } -result $result($ns,$string)
   531 	    incr count
   532 	}
   533     }
   534 
   535     # Tests msgcat-7.*: [mc] extra args processed by [format]
   536 
   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]
   542 	mclocale foo
   543     } -cleanup {
   544 	mclocale $locale
   545     } -body {
   546 	mc format1 "good test"
   547     } -result "this is a test"
   548 
   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]
   554 	mclocale foo
   555     } -cleanup {
   556 	mclocale $locale
   557     } -body {
   558 	mc format2 "good test"
   559     } -result "this is a good test"
   560 
   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]
   566 	mclocale foo
   567     } -cleanup {
   568 	mclocale $locale
   569     } -body {
   570 	catch {mc format3 "good test"}
   571     } -result 1
   572 
   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]
   578 	mclocale foo
   579     } -cleanup {
   580 	mclocale $locale
   581     } -body {
   582 	mc "this is a %s" "good test"
   583     } -result "this is a good test"
   584 
   585     cleanupTests
   586 }
   587 namespace delete ::msgcat::test
   588 return
   589