/extensions/jQueryMsg/test/jasmine/makeLanguageSpec.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 114 lines · 83 code · 13 blank · 18 comment · 3 complexity · bb1c71db26620da4618f07f0c1bad324 MD5 · raw file

  1. <?php
  2. /**
  3. * This PHP script defines the spec that the Javascript message parser should conform to.
  4. *
  5. * It does this by looking up the results of various string kinds of string parsing, with various languages,
  6. * in the current installation of MediaWiki. It then outputs a static specification, mapping expected inputs to outputs,
  7. * which can be used with the JasmineBDD framework. This specification can then be used by simply including it into
  8. * the SpecRunner.html file.
  9. *
  10. * This is similar to Michael Dale (mdale@mediawiki.org)'s parser tests, except that it doesn't look up the
  11. * API results while doing the test, so the Jasmine run is much faster(at the cost of being out of date in rare
  12. * circumstances. But mostly the parsing that we are doing in Javascript doesn't change much.)
  13. *
  14. */
  15. $maintenanceDir = dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) . '/maintenance';
  16. require( "$maintenanceDir/Maintenance.php" );
  17. class MakeLanguageSpec extends Maintenance {
  18. static $keyToTestArgs = array(
  19. 'undelete_short' => array(
  20. array( 0 ),
  21. array( 1 ),
  22. array( 2 ),
  23. array( 5 ),
  24. array( 21 ),
  25. array( 101 )
  26. ),
  27. 'category-subcat-count' => array(
  28. array( 0, 10 ),
  29. array( 1, 1 ),
  30. array( 1, 2 ),
  31. array( 3, 30 )
  32. )
  33. );
  34. public function __construct() {
  35. parent::__construct();
  36. $this->mDescription = "Create a JasmineBDD-compatible specification for message parsing";
  37. // add any other options here
  38. }
  39. public function execute() {
  40. list( $messages, $tests ) = $this->getMessagesAndTests();
  41. $this->writeJavascriptFile( $messages, $tests, "spec/mediawiki.language.parser.spec.data.js" );
  42. }
  43. private function getMessagesAndTests() {
  44. $messages = array();
  45. $tests = array();
  46. $wfMsgExtOptions = array( 'parsemag' );
  47. foreach ( array( 'en', 'fr', 'ar', 'jp', 'zh' ) as $languageCode ) {
  48. $wfMsgExtOptions['language'] = $languageCode;
  49. foreach ( self::$keyToTestArgs as $key => $testArgs ) {
  50. foreach ($testArgs as $args) {
  51. // get the raw template, without any transformations
  52. $template = wfMsgGetKey( $key, /* useDb */ true, $languageCode, /* transform */ false );
  53. // get the magic-parsed version with args
  54. $wfMsgExtArgs = array_merge( array( $key, $wfMsgExtOptions ), $args );
  55. $result = call_user_func_array( 'wfMsgExt', $wfMsgExtArgs );
  56. // record the template, args, language, and expected result
  57. // fake multiple languages by flattening them together
  58. $langKey = $languageCode . '_' . $key;
  59. $messages[ $langKey ] = $template;
  60. $tests[] = array(
  61. 'name' => $languageCode . " " . $key . " " . join( ",", $args ),
  62. 'key' => $langKey,
  63. 'args' => $args,
  64. 'result' => $result,
  65. 'lang' => $languageCode
  66. );
  67. }
  68. }
  69. }
  70. return array( $messages, $tests );
  71. }
  72. private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
  73. global $argv;
  74. $arguments = count($argv) ? $argv : $_SERVER[ 'argv' ];
  75. $json = new Services_JSON;
  76. $json->pretty = true;
  77. $javascriptPrologue = "// This file stores the results from the PHP parser for certain messages and arguments,\n"
  78. . "// so we can test the equivalent Javascript libraries.\n"
  79. . '// Last generated with ' . join(' ', $arguments) . ' at ' . gmdate('c') . "\n\n";
  80. $javascriptMessages = "mediaWiki.messages.set( " . $json->encode( $messages, true ) . " );\n";
  81. $javascriptTests = 'var jasmineMsgSpec = ' . $json->encode( $tests, true ) . ";\n";
  82. $fp = fopen( $dataSpecFile, 'w' );
  83. if ( !$fp ) {
  84. die( "couldn't open $dataSpecFile for writing" );
  85. }
  86. $success = fwrite( $fp, $javascriptPrologue . $javascriptMessages . $javascriptTests );
  87. if ( !$success ) {
  88. die( "couldn't write to $dataSpecFile" );
  89. }
  90. $success = fclose( $fp );
  91. if ( !$success ) {
  92. die( "couldn't close $dataSpecFile" );
  93. }
  94. }
  95. }
  96. $maintClass = "MakeLanguageSpec";
  97. require_once( "$maintenanceDir/doMaintenance.php" );