diff options
| author | Stanislav Malyshev <stas@php.net> | 2010-01-04 21:44:45 +0000 |
|---|---|---|
| committer | Stanislav Malyshev <stas@php.net> | 2010-01-04 21:44:45 +0000 |
| commit | 8f4337f2551e28d98290752e9ca99fc7f87d93b5 (patch) | |
| tree | 65f4e75f335d8736f9df347484bfe14b9a0b0a8e /ext/intl/tests | |
| parent | 795f716832379f43aa7daba605b0c6e136dab24e (diff) | |
| download | php-git-8f4337f2551e28d98290752e9ca99fc7f87d93b5.tar.gz | |
add ICU resource bundle support, contributed by Hans-Peter Oeri
Diffstat (limited to 'ext/intl/tests')
| -rwxr-xr-x | ext/intl/tests/_files/es-bundle.txt | 21 | ||||
| -rwxr-xr-x | ext/intl/tests/_files/res_index.txt | 6 | ||||
| -rwxr-xr-x | ext/intl/tests/_files/resourcebundle.txt | 21 | ||||
| -rwxr-xr-x | ext/intl/tests/_files/resourcebundle/es.res | bin | 0 -> 384 bytes | |||
| -rwxr-xr-x | ext/intl/tests/_files/resourcebundle/res_index.res | bin | 0 -> 128 bytes | |||
| -rwxr-xr-x | ext/intl/tests/_files/resourcebundle/root.res | bin | 0 -> 388 bytes | |||
| -rwxr-xr-x | ext/intl/tests/rb_build.php | 33 | ||||
| -rw-r--r-- | ext/intl/tests/resourcebundle.inc | 13 | ||||
| -rw-r--r-- | ext/intl/tests/resourcebundle_arrayaccess.phpt | 48 | ||||
| -rw-r--r-- | ext/intl/tests/resourcebundle_create.phpt | 62 | ||||
| -rw-r--r-- | ext/intl/tests/resourcebundle_individual.phpt | 55 | ||||
| -rw-r--r-- | ext/intl/tests/resourcebundle_iterator.phpt | 71 | ||||
| -rwxr-xr-x | ext/intl/tests/resourcebundle_locales.phpt | 23 | ||||
| -rwxr-xr-x | ext/intl/tests/ut_common.inc | 26 |
14 files changed, 378 insertions, 1 deletions
diff --git a/ext/intl/tests/_files/es-bundle.txt b/ext/intl/tests/_files/es-bundle.txt new file mode 100755 index 0000000000..46399a7717 --- /dev/null +++ b/ext/intl/tests/_files/es-bundle.txt @@ -0,0 +1,21 @@ +es { + teststring:string { "Hola Mundo!" } + + testint:int { 2 } + + testvector:intvector { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } + + testbin:bin { a1b2c3d4e5f67890 } + + testtable:table { + major:int { 3 } + minor:int { 4 } + patch:int { 7 } + } + + testarray:array { + "cadena 1", + "cadena 2", + "cadena 3" + } +} diff --git a/ext/intl/tests/_files/res_index.txt b/ext/intl/tests/_files/res_index.txt new file mode 100755 index 0000000000..a39bea58e5 --- /dev/null +++ b/ext/intl/tests/_files/res_index.txt @@ -0,0 +1,6 @@ +res_index:table(nofallback) {
+ InstalledLocales {
+es {""} +root {""}
+ }
+}
\ No newline at end of file diff --git a/ext/intl/tests/_files/resourcebundle.txt b/ext/intl/tests/_files/resourcebundle.txt new file mode 100755 index 0000000000..5b081da6cc --- /dev/null +++ b/ext/intl/tests/_files/resourcebundle.txt @@ -0,0 +1,21 @@ +root { + teststring:string { "Hello World!" } + + testint:int { 2 } + + testvector:intvector { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } + + testbin:bin { a1b2c3d4e5f67890 } + + testtable:table { + major:int { 3 } + minor:int { 4 } + patch:int { 7 } + } + + testarray:array { + "string 1", + "string 2", + "string 3" + } +} diff --git a/ext/intl/tests/_files/resourcebundle/es.res b/ext/intl/tests/_files/resourcebundle/es.res Binary files differnew file mode 100755 index 0000000000..f9d891d6ba --- /dev/null +++ b/ext/intl/tests/_files/resourcebundle/es.res diff --git a/ext/intl/tests/_files/resourcebundle/res_index.res b/ext/intl/tests/_files/resourcebundle/res_index.res Binary files differnew file mode 100755 index 0000000000..9dd3df2de3 --- /dev/null +++ b/ext/intl/tests/_files/resourcebundle/res_index.res diff --git a/ext/intl/tests/_files/resourcebundle/root.res b/ext/intl/tests/_files/resourcebundle/root.res Binary files differnew file mode 100755 index 0000000000..62cb48c457 --- /dev/null +++ b/ext/intl/tests/_files/resourcebundle/root.res diff --git a/ext/intl/tests/rb_build.php b/ext/intl/tests/rb_build.php new file mode 100755 index 0000000000..6a7eeae5eb --- /dev/null +++ b/ext/intl/tests/rb_build.php @@ -0,0 +1,33 @@ +<?php
+// THIS SCRIPT WILL REBUILD ResourceBundle bundles from source files
+
+// DEFINE YOUR ICU TOOLS PATH HERE
+define("ICU_DIR", "C:/PROJECTS/ICU40/BIN/");
+
+$here = dirname(__FILE__);
+
+$dir = new GlobIterator("$here/_files/*.txt", FilesystemIterator::KEY_AS_FILENAME);
+
+foreach($dir as $file) {
+ passthru( ICU_DIR."genrb -s $here/_files/ -d $here/_files/resourcebundle ".$file->getFileName());
+}
+
+$dir = new GlobIterator("$here/_files/resourcebundle/*.res", FilesystemIterator::KEY_AS_FILENAME);
+foreach($dir as $file) {
+ if($file->getFileName() == "res_index.res") continue;
+ $list[] = str_replace(".res", "", $file->getFileName());
+}
+
+$filelist = join(" {\"\"}\n", $list);
+$res_index = <<<END
+res_index:table(nofallback) {
+ InstalledLocales {
+$filelist {""}
+ }
+}
+END;
+file_put_contents("$here/_files/res_index.txt", $res_index);
+
+passthru( ICU_DIR."genrb -s $here/_files/ -d $here/_files/resourcebundle res_index.txt");
+
+// passthru(ICU_DIR."icupkg -tl -a $here/rb.txt -s $here/_files -d $here/_files new $here/_files/resourcebundle.dat");
\ No newline at end of file diff --git a/ext/intl/tests/resourcebundle.inc b/ext/intl/tests/resourcebundle.inc new file mode 100644 index 0000000000..2ec138bb7d --- /dev/null +++ b/ext/intl/tests/resourcebundle.inc @@ -0,0 +1,13 @@ +<?php +define('BUNDLE', dirname(__FILE__)."/_files/resourcebundle"); + +function debug( $res ) { + if (is_null( $res )) { + $ret = "NULL\n"; + } + else { + $ret = print_r( $res, true ). "\n"; + } + return $ret . sprintf( "%5d: %s\n", intl_get_error_code(), intl_get_error_message() ); +} + diff --git a/ext/intl/tests/resourcebundle_arrayaccess.phpt b/ext/intl/tests/resourcebundle_arrayaccess.phpt new file mode 100644 index 0000000000..ef2c0a8b98 --- /dev/null +++ b/ext/intl/tests/resourcebundle_arrayaccess.phpt @@ -0,0 +1,48 @@ +--TEST-- +Test ResourceBundle array access and count - existing/missing keys +--FILE-- +<?php + include "resourcebundle.inc"; + + // fall back + $r = new ResourceBundle( 'en_US', BUNDLE ); + + printf( "length: %d\n", count($r) ); + printf( "teststring: %s\n", $r['teststring'] ); + printf( "testint: %d\n", $r['testint'] ); + + print_r( $r['testvector'] ); + + printf( "testbin: %s\n", bin2hex($r['testbin']) ); + + $r2 = $r['testtable']; + printf( "testtable: %d\n", $r2['major'] ); + + $r2 = $r['testarray']; + printf( "testarray: %s\n", $r2[2] ); + + $t = $r['nonexisting']; + echo debug( $t ); +?> +--EXPECT-- +length: 6 +teststring: Hello World! +testint: 2 +Array +( + [0] => 1 + [1] => 2 + [2] => 3 + [3] => 4 + [4] => 5 + [5] => 6 + [6] => 7 + [7] => 8 + [8] => 9 + [9] => 0 +) +testbin: a1b2c3d4e5f67890 +testtable: 3 +testarray: string 3 +NULL + 2: Cannot load resource element 'nonexisting': U_MISSING_RESOURCE_ERROR diff --git a/ext/intl/tests/resourcebundle_create.phpt b/ext/intl/tests/resourcebundle_create.phpt new file mode 100644 index 0000000000..4d96d3eff7 --- /dev/null +++ b/ext/intl/tests/resourcebundle_create.phpt @@ -0,0 +1,62 @@ +--TEST-- +Test ResourceBundle::__construct() - existing/missing bundles/locales +--SKIPIF-- +<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?> +--FILE-- +<?php + +include "resourcebundle.inc"; + +function ut_main() { + $str_res = ''; + // all fine + $r1 = ut_resourcebundle_create( 'root', BUNDLE ); + $str_res .= debug( $r1 ); + $str_res .= print_r( $r1['teststring'], true)."\n"; + + // non-root one + $r1 = ut_resourcebundle_create( 'es', BUNDLE ); + $str_res .= debug( $r1 ); + $str_res .= print_r( $r1['teststring'], true)."\n"; + + // fall back + $r1 = ut_resourcebundle_create( 'en_US', BUNDLE ); + $str_res .= debug( $r1 ); + $str_res .= print_r( $r1['testsring'], true); + + // fall out + $r2 = ut_resourcebundle_create( 'en_US', BUNDLE, false ); + $str_res .= debug( $r2 ); + + // missing + $r3 = ut_resourcebundle_create( 'en_US', 'nonexisting' ); + $str_res .= debug( $r3 ); + + return $str_res; +} + + include_once( 'ut_common.inc' ); + ut_run(); +?> +--EXPECTF-- +ResourceBundle Object +( +) + + 0: U_ZERO_ERROR +Hello World! +ResourceBundle Object +( +) + + 0: U_ZERO_ERROR +Hola Mundo! +ResourceBundle Object +( +) + + -127: U_USING_DEFAULT_WARNING +NULL + -127: resourcebundle_ctor: Cannot load libICU resource '%s/resourcebundle' without fallback from en_US to root: U_USING_DEFAULT_WARNING +NULL + 2: resourcebundle_ctor: Cannot load libICU resource bundle: U_MISSING_RESOURCE_ERROR diff --git a/ext/intl/tests/resourcebundle_individual.phpt b/ext/intl/tests/resourcebundle_individual.phpt new file mode 100644 index 0000000000..182cbf325a --- /dev/null +++ b/ext/intl/tests/resourcebundle_individual.phpt @@ -0,0 +1,55 @@ +--TEST-- +Test ResourceBundle::get() and length() - existing/missing keys +--FILE-- +<?php + include "resourcebundle.inc"; + +function ut_main() { + $str_res = ''; + // fall back + $r = ut_resourcebundle_create( 'en_US', BUNDLE ); + + $str_res .= sprintf( "length: %d\n", ut_resourcebundle_count($r) ); + $str_res .= sprintf( "teststring: %s\n", ut_resourcebundle_get($r, 'teststring' ) ); + $str_res .= sprintf( "testint: %d\n", ut_resourcebundle_get($r, 'testint' ) ); + + $str_res .= print_r( ut_resourcebundle_get($r, 'testvector' ), true ); + + $str_res .= sprintf( "testbin: %s\n", bin2hex(ut_resourcebundle_get( $r,'testbin' )) ); + + $r2 = ut_resourcebundle_get($r, 'testtable' ); + $str_res .= sprintf( "testtable: %d\n", ut_resourcebundle_get($r2, 'major' ) ); + + $r2 = ut_resourcebundle_get($r,'testarray' ); + $str_res .= sprintf( "testarray: %s\n", ut_resourcebundle_get($r2, 2 ) ); + + $t = ut_resourcebundle_get( $r, 'nonexisting' ); + $str_res .= debug( $t ); + + return $str_res; +} + include_once( 'ut_common.inc' ); + ut_run(); +?> +--EXPECT-- +length: 6 +teststring: Hello World! +testint: 2 +Array +( + [0] => 1 + [1] => 2 + [2] => 3 + [3] => 4 + [4] => 5 + [5] => 6 + [6] => 7 + [7] => 8 + [8] => 9 + [9] => 0 +) +testbin: a1b2c3d4e5f67890 +testtable: 3 +testarray: string 3 +NULL + 2: Cannot load resource element 'nonexisting': U_MISSING_RESOURCE_ERROR diff --git a/ext/intl/tests/resourcebundle_iterator.phpt b/ext/intl/tests/resourcebundle_iterator.phpt new file mode 100644 index 0000000000..31b0768bfb --- /dev/null +++ b/ext/intl/tests/resourcebundle_iterator.phpt @@ -0,0 +1,71 @@ +--TEST-- +Test ResourceBundle iterator +--FILE-- +<?php + include "resourcebundle.inc"; + + // fall back + $r = new ResourceBundle( 'en_US', BUNDLE ); + + foreach ($r as $onekey => $oneval) { + echo "Here comes $onekey:\n"; + switch (gettype($oneval)) { + case 'string': + echo bin2hex( $oneval ) . "\n"; + break; + + case 'integer': + echo "$oneval\n"; + break; + + default: + print_r( $oneval ); + } + echo "\n"; + } + + echo "Testarray Contents:\n"; + $r = $r->get( 'testarray' ); + foreach ($r as $onekey => $oneval) { + echo "$onekey => $oneval\n"; + } +?> +--EXPECTF-- +Here comes testarray: +ResourceBundle Object +( +) + +Here comes testbin: +a1b2c3d4e5f67890 + +Here comes testint: +2 + +Here comes teststring: +48656c6c6f20576f726c6421 + +Here comes testtable: +ResourceBundle Object +( +) + +Here comes testvector: +Array +( + [0] => 1 + [1] => 2 + [2] => 3 + [3] => 4 + [4] => 5 + [5] => 6 + [6] => 7 + [7] => 8 + [8] => 9 + [9] => 0 +) + +Testarray Contents: +0 => string 1 +1 => string 2 +2 => string 3 diff --git a/ext/intl/tests/resourcebundle_locales.phpt b/ext/intl/tests/resourcebundle_locales.phpt new file mode 100755 index 0000000000..e14a7e5aa1 --- /dev/null +++ b/ext/intl/tests/resourcebundle_locales.phpt @@ -0,0 +1,23 @@ +--TEST-- +Test ResourceBundle::getLocales +--SKIPIF-- +<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?> +--FILE-- +<?php + +include "resourcebundle.inc"; + +function ut_main() { + $str_res = ''; + + $str_res .= join("\n", ut_resourcebundle_locales(BUNDLE)); + + return $str_res; +} + + include_once( 'ut_common.inc' ); + ut_run(); +?> +--EXPECT-- +es +root diff --git a/ext/intl/tests/ut_common.inc b/ext/intl/tests/ut_common.inc index c59d1770ee..09be22bf5a 100755 --- a/ext/intl/tests/ut_common.inc +++ b/ext/intl/tests/ut_common.inc @@ -389,4 +389,28 @@ function ut_datefmt_localtime( $fmt , $value , &$parse_pos=0 ) { return $GLOBALS['oo-mode'] ? $fmt->localtime( $value , $parse_pos ) : datefmt_localtime( $fmt , $value , $parse_pos ); } -?> + +function ut_resourcebundle_create( $locale, $bundle, $fallback=true ) +{ + return $GLOBALS['oo-mode'] ? new ResourceBundle($locale, $bundle, $fallback): resourcebundle_create($locale, $bundle, $fallback); +} +function ut_resourcebundle_count($bundle ) +{ + return $GLOBALS['oo-mode'] ? $bundle->count():resourcebundle_count($bundle); +} +function ut_resourcebundle_locales($bundle ) +{ + return $GLOBALS['oo-mode'] ? ResourceBundle::getLocales($bundle):resourcebundle_locales($bundle); +} +function ut_resourcebundle_get($bundle, $idx ) +{ + return $GLOBALS['oo-mode'] ? $bundle->get($idx):resourcebundle_get($bundle, $idx); +} +function ut_resourcebundle_get_error_code($bundle ) +{ + return $GLOBALS['oo-mode'] ? $bundle->getErrorCode():resourcebundle_get_error_code($bundle); +} +function ut_resourcebundle_get_error_message($bundle ) +{ + return $GLOBALS['oo-mode'] ? $bundle->getErrorMessage():resourcebundle_get_error_message($bundle); +} |
