MediaWiki  1.34.0
LuaEnvironmentComparisonTest.php
Go to the documentation of this file.
1 <?php
2 
9 class Scribunto_LuaEnvironmentComparisonTest extends PHPUnit\Framework\TestCase {
10  use MediaWikiCoversValidator;
11  use PHPUnit4And6Compat;
12 
13  public $sandboxOpts = [
14  'memoryLimit' => 50000000,
15  'cpuLimit' => 30,
16  'allowEnvFuncs' => true,
17  ];
18  public $standaloneOpts = [
19  'errorFile' => null,
20  'luaPath' => null,
21  'memoryLimit' => 50000000,
22  'cpuLimit' => 30,
23  'allowEnvFuncs' => true,
24  ];
25 
26  protected $engines = [];
27 
28  private function makeEngine( $class, $opts ) {
29  $parser = new Parser;
30  $options = new ParserOptions;
31  $options->setTemplateCallback( [ $this, 'templateCallback' ] );
32  $parser->startExternalParse( Title::newMainPage(), $options, Parser::OT_HTML, true );
33  $engine = new $class ( [ 'parser' => $parser ] + $opts );
34  $parser->scribunto_engine = $engine;
35  $engine->setTitle( $parser->getTitle() );
36  $engine->getInterpreter();
37  return $engine;
38  }
39 
40  protected function setUp() {
41  parent::setUp();
42 
43  try {
44  $this->engines['LuaSandbox'] = $this->makeEngine(
45  'Scribunto_LuaSandboxEngine', $this->sandboxOpts
46  );
48  $this->markTestSkipped( "LuaSandbox interpreter not available" );
49  return;
50  }
51 
52  try {
53  $this->engines['LuaStandalone'] = $this->makeEngine(
54  'Scribunto_LuaStandaloneEngine', $this->standaloneOpts
55  );
57  $this->markTestSkipped( "LuaStandalone interpreter not available" );
58  return;
59  }
60  }
61 
62  protected function tearDown() {
63  foreach ( $this->engines as $engine ) {
64  $engine->destroy();
65  }
66  $this->engines = [];
67  parent::tearDown();
68  }
69 
70  private function getGlobalEnvironment( $engine ) {
71  static $script = <<<LUA
72  xxxseen = {}
73  function xxxGetTable( t )
74  if xxxseen[t] then
75  return 'table'
76  end
77  local ret = {}
78  xxxseen[t] = ret
79  for k, v in pairs( t ) do
80  if k ~= '_G' and string.sub( k, 1, 3 ) ~= 'xxx' then
81  if type( v ) == 'table' then
82  ret[k] = xxxGetTable( v )
83  elseif type( v ) == 'string'
84  or type( v ) == 'number'
85  or type( v ) == 'boolean'
86  or type( v ) == 'nil'
87  then
88  ret[k] = v
89  else
90  ret[k] = type( v )
91  end
92  end
93  end
94  return ret
95  end
96  return xxxGetTable( _G )
97 LUA;
98  $func = $engine->getInterpreter()->loadString( $script, 'script' );
99  return $engine->getInterpreter()->callFunction( $func );
100  }
101 
102  public function testGlobalEnvironment() {
103  // Grab the first engine as the "standard"
104  $firstEngine = reset( $this->engines );
105  $firstName = key( $this->engines );
106  $firstEnv = $this->getGlobalEnvironment( $firstEngine );
107 
108  // Test all others against it
109  foreach ( $this->engines as $secondName => $secondEngine ) {
110  if ( $secondName !== $firstName ) {
111  $secondEnv = $this->getGlobalEnvironment( $secondEngine );
112  $this->assertEquals( $firstEnv, $secondEnv,
113  "Environments for $firstName and $secondName are not equivalent" );
114  }
115  }
116  }
117 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:42
Scribunto_LuaEnvironmentComparisonTest\testGlobalEnvironment
testGlobalEnvironment()
Definition: LuaEnvironmentComparisonTest.php:102
ParserOptions\setTemplateCallback
setTemplateCallback( $x)
Callback for template fetching; first argument to call_user_func().
Definition: ParserOptions.php:837
Title\newMainPage
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:649
Scribunto_LuaEnvironmentComparisonTest\$engines
$engines
Definition: LuaEnvironmentComparisonTest.php:26
Scribunto_LuaEnvironmentComparisonTest\getGlobalEnvironment
getGlobalEnvironment( $engine)
Definition: LuaEnvironmentComparisonTest.php:70
Scribunto_LuaInterpreterNotFoundError
Definition: LuaInterpreter.php:64
Scribunto_LuaEnvironmentComparisonTest\$standaloneOpts
$standaloneOpts
Definition: LuaEnvironmentComparisonTest.php:18
Scribunto_LuaEnvironmentComparisonTest\setUp
setUp()
Definition: LuaEnvironmentComparisonTest.php:40
Scribunto_LuaEnvironmentComparisonTest\tearDown
tearDown()
Definition: LuaEnvironmentComparisonTest.php:62
Scribunto_LuaEnvironmentComparisonTest
@group Lua @group LuaSandbox @group LuaStandalone @coversNothing
Definition: LuaEnvironmentComparisonTest.php:9
OT_HTML
const OT_HTML
Definition: Defines.php:164
Scribunto_LuaEnvironmentComparisonTest\makeEngine
makeEngine( $class, $opts)
Definition: LuaEnvironmentComparisonTest.php:28
Scribunto_LuaEnvironmentComparisonTest\$sandboxOpts
$sandboxOpts
Definition: LuaEnvironmentComparisonTest.php:13