2012-04-19 07:40:56 +00:00
|
|
|
<?php
|
|
|
|
|
2013-02-05 00:44:44 +00:00
|
|
|
class Scribunto_LuaCommonTests extends Scribunto_LuaEngineTestBase {
|
|
|
|
protected static $moduleName = 'CommonTests';
|
2012-04-19 07:40:56 +00:00
|
|
|
|
2013-02-13 20:46:48 +00:00
|
|
|
private static $allowedGlobals = array(
|
|
|
|
// Functions
|
|
|
|
'assert',
|
|
|
|
'error',
|
|
|
|
'getfenv',
|
|
|
|
'getmetatable',
|
|
|
|
'ipairs',
|
|
|
|
'next',
|
|
|
|
'pairs',
|
|
|
|
'pcall',
|
|
|
|
'rawequal',
|
|
|
|
'rawget',
|
|
|
|
'rawset',
|
|
|
|
'require',
|
|
|
|
'select',
|
|
|
|
'setfenv',
|
|
|
|
'setmetatable',
|
|
|
|
'tonumber',
|
|
|
|
'tostring',
|
|
|
|
'type',
|
|
|
|
'unpack',
|
|
|
|
'xpcall',
|
|
|
|
|
|
|
|
// Packages
|
|
|
|
'_G',
|
|
|
|
'debug',
|
|
|
|
'math',
|
|
|
|
'mw',
|
|
|
|
'os',
|
|
|
|
'package',
|
|
|
|
'string',
|
|
|
|
'table',
|
|
|
|
|
|
|
|
// Misc
|
|
|
|
'_VERSION',
|
|
|
|
);
|
|
|
|
|
2012-04-19 07:40:56 +00:00
|
|
|
function setUp() {
|
2012-12-20 16:36:48 +00:00
|
|
|
parent::setUp();
|
2012-04-19 07:40:56 +00:00
|
|
|
|
2013-02-05 00:44:44 +00:00
|
|
|
// Note this depends on every iteration of the data provider running with a clean parser
|
|
|
|
$this->getEngine()->getParser()->getOptions()->setExpensiveParserFunctionLimit( 10 );
|
2012-12-19 20:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getTestModules() {
|
2013-02-05 00:44:44 +00:00
|
|
|
return parent::getTestModules() + array(
|
2012-12-19 20:10:01 +00:00
|
|
|
'CommonTests' => __DIR__ . '/CommonTests.lua',
|
|
|
|
);
|
2012-04-19 07:40:56 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 20:46:48 +00:00
|
|
|
function testNoLeakedGlobals() {
|
|
|
|
$interpreter = $this->getEngine()->getInterpreter();
|
|
|
|
|
|
|
|
list( $actualGlobals ) = $interpreter->callFunction(
|
|
|
|
$interpreter->loadString(
|
|
|
|
'local t = {} for k in pairs( _G ) do t[#t+1] = k end return t',
|
|
|
|
'getglobals'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$leakedGlobals = array_diff( $actualGlobals, self::$allowedGlobals );
|
|
|
|
$this->assertEquals( 0, count( $leakedGlobals ),
|
|
|
|
'The following globals are leaked: ' . join( ' ', $leakedGlobals )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-12-24 15:37:47 +00:00
|
|
|
function testModuleStringExtend() {
|
|
|
|
$engine = $this->getEngine();
|
|
|
|
$interpreter = $engine->getInterpreter();
|
|
|
|
|
|
|
|
$interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'string.testModuleStringExtend = "ok"', 'extendstring' )
|
|
|
|
);
|
|
|
|
$ret = $interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'return ("").testModuleStringExtend', 'teststring1' )
|
|
|
|
);
|
|
|
|
$this->assertSame( array( 'ok' ), $ret, 'string can be extended' );
|
|
|
|
|
|
|
|
$this->extraModules['Module:testModuleStringExtend'] = '
|
|
|
|
return {
|
|
|
|
test = function() return ("").testModuleStringExtend end
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$module = $engine->fetchModuleFromParser(
|
|
|
|
Title::makeTitle( NS_MODULE, 'testModuleStringExtend' )
|
|
|
|
);
|
|
|
|
$ext = $module->execute();
|
|
|
|
$ret = $interpreter->callFunction( $ext['test'] );
|
|
|
|
$this->assertSame( array( 'ok' ), $ret, 'string extension can be used from module' );
|
|
|
|
|
|
|
|
$this->extraModules['Module:testModuleStringExtend2'] = '
|
|
|
|
return {
|
|
|
|
test = function()
|
|
|
|
string.testModuleStringExtend = "fail"
|
|
|
|
return ("").testModuleStringExtend
|
|
|
|
end
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$module = $engine->fetchModuleFromParser(
|
|
|
|
Title::makeTitle( NS_MODULE, 'testModuleStringExtend2' )
|
|
|
|
);
|
|
|
|
$ext = $module->execute();
|
|
|
|
$ret = $interpreter->callFunction( $ext['test'] );
|
|
|
|
$this->assertSame( array( 'ok' ), $ret, 'string extension cannot be modified from module' );
|
|
|
|
$ret = $interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'return string.testModuleStringExtend', 'teststring2' )
|
|
|
|
);
|
|
|
|
$this->assertSame( array( 'ok' ), $ret, 'string extension cannot be modified from module' );
|
|
|
|
|
|
|
|
$ret = $engine->runConsole( array(
|
|
|
|
'prevQuestions' => array(),
|
|
|
|
'question' => '=("").testModuleStringExtend',
|
|
|
|
'content' => 'return {}',
|
|
|
|
'title' => Title::makeTitle( NS_MODULE, 'dummy' ),
|
|
|
|
) );
|
|
|
|
$this->assertSame( 'ok', $ret['return'], 'string extension can be used from console' );
|
|
|
|
|
|
|
|
$ret = $engine->runConsole( array(
|
|
|
|
'prevQuestions' => array( 'string.fail = "fail"' ),
|
|
|
|
'question' => '=("").fail',
|
|
|
|
'content' => 'return {}',
|
|
|
|
'title' => Title::makeTitle( NS_MODULE, 'dummy' ),
|
|
|
|
) );
|
|
|
|
$this->assertSame( 'nil', $ret['return'], 'string cannot be extended from console' );
|
|
|
|
|
|
|
|
$ret = $engine->runConsole( array(
|
|
|
|
'prevQuestions' => array( 'string.testModuleStringExtend = "fail"' ),
|
|
|
|
'question' => '=("").testModuleStringExtend',
|
|
|
|
'content' => 'return {}',
|
|
|
|
'title' => Title::makeTitle( NS_MODULE, 'dummy' ),
|
|
|
|
) );
|
|
|
|
$this->assertSame( 'ok', $ret['return'], 'string extension cannot be modified from console' );
|
|
|
|
$ret = $interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'return string.testModuleStringExtend', 'teststring3' )
|
|
|
|
);
|
|
|
|
$this->assertSame( array( 'ok' ), $ret, 'string extension cannot be modified from console' );
|
|
|
|
|
|
|
|
$interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'string.testModuleStringExtend = nil', 'unextendstring' )
|
|
|
|
);
|
|
|
|
}
|
2012-04-19 07:40:56 +00:00
|
|
|
}
|