mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-25 16:58:35 +00:00
aa4d72e3ff
The following continue to be ignored: * Generic.Arrays.DisallowLongArraySyntax.Found, because I'm not sure Scribunto is ready to abandon old version support in master. * MediaWiki.ControlStructures.AssignmentInControlStructures.AssignmentInControlStructures, because it's overly strict for its purpose. Squiz.Classes.ValidClassName.NotCamelCaps isn't ignored globally, we just ignore it explicitly every place it's needed. Change-Id: I307668da6ef7b3e23da19b1fd1e08914239b99b3
113 lines
3 KiB
PHP
113 lines
3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group Lua
|
|
* @group LuaSandbox
|
|
* @group LuaStandalone
|
|
*/
|
|
// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps
|
|
class Scribunto_LuaEnvironmentComparisonTest extends MediaWikiTestCase {
|
|
public $sandboxOpts = array(
|
|
'memoryLimit' => 50000000,
|
|
'cpuLimit' => 30,
|
|
'allowEnvFuncs' => true,
|
|
);
|
|
public $standaloneOpts = array(
|
|
'errorFile' => null,
|
|
'luaPath' => null,
|
|
'memoryLimit' => 50000000,
|
|
'cpuLimit' => 30,
|
|
'allowEnvFuncs' => true,
|
|
);
|
|
|
|
protected $engines = array();
|
|
|
|
private function makeEngine( $class, $opts ) {
|
|
$parser = new Parser;
|
|
$options = new ParserOptions;
|
|
$options->setTemplateCallback( array( $this, 'templateCallback' ) );
|
|
$parser->startExternalParse( Title::newMainPage(), $options, Parser::OT_HTML, true );
|
|
$engine = new $class ( array( 'parser' => $parser ) + $opts );
|
|
$parser->scribunto_engine = $engine;
|
|
$engine->setTitle( $parser->getTitle() );
|
|
$engine->getInterpreter();
|
|
return $engine;
|
|
}
|
|
|
|
protected function setUp() {
|
|
parent::setUp();
|
|
|
|
try {
|
|
$this->engines['LuaSandbox'] = $this->makeEngine(
|
|
'Scribunto_LuaSandboxEngine', $this->sandboxOpts
|
|
);
|
|
} catch ( Scribunto_LuaInterpreterNotFoundError $e ) {
|
|
$this->markTestSkipped( "LuaSandbox interpreter not available" );
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$this->engines['LuaStandalone'] = $this->makeEngine(
|
|
'Scribunto_LuaStandaloneEngine', $this->standaloneOpts
|
|
);
|
|
} catch ( Scribunto_LuaInterpreterNotFoundError $e ) {
|
|
$this->markTestSkipped( "LuaStandalone interpreter not available" );
|
|
return;
|
|
}
|
|
}
|
|
|
|
protected function tearDown() {
|
|
foreach ( $this->engines as $engine ) {
|
|
$engine->destroy();
|
|
}
|
|
$this->engines = array();
|
|
parent::tearDown();
|
|
}
|
|
|
|
private function getGlobalEnvironment( $engine ) {
|
|
static $script = <<<LUA
|
|
xxxseen = {}
|
|
function xxxGetTable( t )
|
|
if xxxseen[t] then
|
|
return 'table'
|
|
end
|
|
local ret = {}
|
|
xxxseen[t] = ret
|
|
for k, v in pairs( t ) do
|
|
if k ~= '_G' and string.sub( k, 1, 3 ) ~= 'xxx' then
|
|
if type( v ) == 'table' then
|
|
ret[k] = xxxGetTable( v )
|
|
elseif type( v ) == 'string'
|
|
or type( v ) == 'number'
|
|
or type( v ) == 'boolean'
|
|
or type( v ) == 'nil'
|
|
then
|
|
ret[k] = v
|
|
else
|
|
ret[k] = type( v )
|
|
end
|
|
end
|
|
end
|
|
return ret
|
|
end
|
|
return xxxGetTable( _G )
|
|
LUA;
|
|
$func = $engine->getInterpreter()->loadString( $script, 'script' );
|
|
return $engine->getInterpreter()->callFunction( $func );
|
|
}
|
|
|
|
public function testGlobalEnvironment() {
|
|
// Grab the first engine as the "standard"
|
|
reset( $this->engines );
|
|
list( $firstName, $firstEngine ) = each( $this->engines );
|
|
$firstEnv = $this->getGlobalEnvironment( $firstEngine );
|
|
|
|
// Test all others against it
|
|
while ( list( $secondName, $secondEngine ) = each( $this->engines ) ) {
|
|
$secondEnv = $this->getGlobalEnvironment( $secondEngine );
|
|
$this->assertEquals( $firstEnv, $secondEnv,
|
|
"Environments for $firstName and $secondName are not equivalent" );
|
|
}
|
|
}
|
|
}
|