2013-02-05 00:44:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the subclass for Lua library tests. It will automatically run all
|
|
|
|
* tests against LuaSandbox and LuaStandalone.
|
|
|
|
*
|
|
|
|
* Most of the time, you'll only need to override the following:
|
|
|
|
* - $moduleName: Name of the module being tested
|
|
|
|
* - getTestModules(): Add a mapping from $moduleName to the file containing
|
|
|
|
* the code.
|
|
|
|
*/
|
2015-02-19 22:12:49 +00:00
|
|
|
abstract class Scribunto_LuaEngineTestBase extends MediaWikiLangTestCase {
|
2019-08-03 18:19:46 +00:00
|
|
|
use Scribunto_LuaEngineTestHelper;
|
2013-02-05 00:44:44 +00:00
|
|
|
|
|
|
|
private static $staticEngineName = null;
|
|
|
|
private $engineName = null;
|
|
|
|
private $engine = null;
|
|
|
|
private $luaDataProvider = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name to display instead of the default
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $luaTestName = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the module being tested
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $moduleName = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to use for the data provider
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $dataProviderClass = 'Scribunto_LuaDataProvider';
|
|
|
|
|
2014-07-01 14:21:00 +00:00
|
|
|
/**
|
|
|
|
* Tests to skip. Associative array mapping test name to skip reason.
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-06-15 17:19:00 +00:00
|
|
|
protected $skipTests = [];
|
2014-07-01 14:21:00 +00:00
|
|
|
|
2016-05-17 14:52:05 +00:00
|
|
|
public function __construct(
|
2017-06-15 17:19:00 +00:00
|
|
|
$name = null, array $data = [], $dataName = '', $engineName = null
|
2016-05-17 14:52:05 +00:00
|
|
|
) {
|
2013-02-05 00:44:44 +00:00
|
|
|
if ( $engineName === null ) {
|
|
|
|
$engineName = self::$staticEngineName;
|
|
|
|
}
|
|
|
|
$this->engineName = $engineName;
|
|
|
|
parent::__construct( $name, $data, $dataName );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function suite( $className ) {
|
|
|
|
return self::makeSuite( $className );
|
|
|
|
}
|
|
|
|
|
2019-10-11 18:31:29 +00:00
|
|
|
protected function tearDown() : void {
|
2013-02-05 00:44:44 +00:00
|
|
|
if ( $this->luaDataProvider ) {
|
|
|
|
$this->luaDataProvider->destroy();
|
|
|
|
$this->luaDataProvider = null;
|
|
|
|
}
|
|
|
|
if ( $this->engine ) {
|
|
|
|
$this->engine->destroy();
|
|
|
|
$this->engine = null;
|
|
|
|
}
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2014-01-16 16:00:30 +00:00
|
|
|
/**
|
|
|
|
* Get the title used for unit tests
|
|
|
|
*
|
|
|
|
* @return Title
|
|
|
|
*/
|
|
|
|
protected function getTestTitle() {
|
|
|
|
return Title::newMainPage();
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:46:18 +00:00
|
|
|
public function toString(): string {
|
2013-02-05 00:44:44 +00:00
|
|
|
// When running tests written in Lua, return a nicer representation in
|
|
|
|
// the failure message.
|
|
|
|
if ( $this->luaTestName ) {
|
|
|
|
return $this->engineName . ': ' . $this->luaTestName;
|
|
|
|
}
|
|
|
|
return $this->engineName . ': ' . parent::toString();
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:21:38 +00:00
|
|
|
protected function getTestModules() {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [
|
2013-02-05 00:44:44 +00:00
|
|
|
'TestFramework' => __DIR__ . '/TestFramework.lua',
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2013-02-05 00:44:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 11:21:38 +00:00
|
|
|
public function provideLuaData() {
|
2013-02-05 00:44:44 +00:00
|
|
|
if ( !$this->luaDataProvider ) {
|
|
|
|
$class = static::$dataProviderClass;
|
|
|
|
$this->luaDataProvider = new $class ( $this->getEngine(), static::$moduleName );
|
|
|
|
}
|
|
|
|
return $this->luaDataProvider;
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:45:07 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideLuaData
|
|
|
|
* @param string $key
|
|
|
|
* @param string $testName
|
|
|
|
* @param mixed $expected
|
|
|
|
*/
|
2014-11-12 11:21:38 +00:00
|
|
|
public function testLua( $key, $testName, $expected ) {
|
2018-09-03 18:08:53 +00:00
|
|
|
$this->luaTestName = static::$moduleName . "[$key]: $testName";
|
2014-07-01 14:21:00 +00:00
|
|
|
if ( isset( $this->skipTests[$testName] ) ) {
|
|
|
|
$this->markTestSkipped( $this->skipTests[$testName] );
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$actual = $this->provideLuaData()->run( $key );
|
|
|
|
} catch ( Scribunto_LuaError $ex ) {
|
|
|
|
if ( substr( $ex->getLuaMessage(), 0, 6 ) === 'SKIP: ' ) {
|
|
|
|
$this->markTestSkipped( substr( $ex->getLuaMessage(), 6 ) );
|
|
|
|
} else {
|
|
|
|
throw $ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->assertSame( $expected, $actual );
|
|
|
|
}
|
2013-02-05 00:44:44 +00:00
|
|
|
$this->luaTestName = null;
|
|
|
|
}
|
|
|
|
}
|
2013-02-07 17:50:21 +00:00
|
|
|
|
2018-02-24 08:23:15 +00:00
|
|
|
class Scribunto_LuaEngineTestSkip extends PHPUnit\Framework\TestCase {
|
2013-02-07 17:50:21 +00:00
|
|
|
private $className = '';
|
|
|
|
private $message = '';
|
|
|
|
|
|
|
|
public function __construct( $className = '', $message = '' ) {
|
|
|
|
$this->className = $className;
|
|
|
|
$this->message = $message;
|
|
|
|
parent::__construct( 'testDummy' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDummy() {
|
|
|
|
if ( $this->className ) {
|
|
|
|
$this->markTestSkipped( $this->message );
|
|
|
|
} else {
|
|
|
|
// Dummy
|
|
|
|
$this->assertTrue( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:46:18 +00:00
|
|
|
public function toString(): string {
|
2013-02-07 17:50:21 +00:00
|
|
|
return $this->className;
|
|
|
|
}
|
|
|
|
}
|