Merge "Add mw.text.unstripNoWiki, mw.text.killMarkers, fix mw.text.unstrip"

This commit is contained in:
jenkins-bot 2014-12-01 21:42:17 +00:00 committed by Gerrit Code Review
commit 6f7349dcc8
4 changed files with 69 additions and 5 deletions

View file

@ -6,6 +6,8 @@ class Scribunto_LuaTextLibrary extends Scribunto_LuaLibraryBase {
$lib = array( $lib = array(
'unstrip' => array( $this, 'textUnstrip' ), 'unstrip' => array( $this, 'textUnstrip' ),
'unstripNoWiki' => array( $this, 'textUnstripNoWiki' ),
'killMarkers' => array( $this, 'killMarkers' ),
'getEntityTable' => array( $this, 'getEntityTable' ), 'getEntityTable' => array( $this, 'getEntityTable' ),
); );
$opts = array( $opts = array(
@ -37,7 +39,18 @@ class Scribunto_LuaTextLibrary extends Scribunto_LuaLibraryBase {
function textUnstrip( $s ) { function textUnstrip( $s ) {
$this->checkType( 'unstrip', 1, $s, 'string' ); $this->checkType( 'unstrip', 1, $s, 'string' );
return array( $this->getParser()->mStripState->unstripBoth( $s ) ); $stripState = $this->getParser()->mStripState;
return array( $stripState->killMarkers( $stripState->unstripNoWiki( $s ) ) );
}
function textUnstripNoWiki( $s ) {
$this->checkType( 'unstripNoWiki', 1, $s, 'string' );
return array( $this->getParser()->mStripState->unstripNoWiki( $s ) );
}
function killMarkers( $s ) {
$this->checkType( 'killMarkers', 1, $s, 'string' );
return array( $this->getParser()->mStripState->killMarkers( $s ) );
} }
function getEntityTable() { function getEntityTable() {

View file

@ -199,6 +199,14 @@ function mwtext.unstrip( s )
return php.unstrip( s ) return php.unstrip( s )
end end
function mwtext.unstripNoWiki( s )
return php.unstripNoWiki( s )
end
function mwtext.killMarkers( s )
return php.killMarkers( s )
end
function mwtext.split( text, pattern, plain ) function mwtext.split( text, pattern, plain )
local ret = {} local ret = {}
for m in mwtext.gsplit( text, pattern, plain ) do for m in mwtext.gsplit( text, pattern, plain ) do

View file

@ -7,10 +7,17 @@ class Scribunto_LuaTextLibraryTests extends Scribunto_LuaEngineTestBase {
parent::setUp(); parent::setUp();
// For unstrip test // For unstrip test
$parser = $this->getEngine()->getParser();
$markers = array(
'nowiki' => $parser->uniqPrefix() . '-test-nowiki-' . Parser::MARKER_SUFFIX,
'general' => $parser->uniqPrefix() . '-test-general-' . Parser::MARKER_SUFFIX,
);
$parser->mStripState->addNoWiki( $markers['nowiki'], 'NoWiki' );
$parser->mStripState->addGeneral( $markers['general'], 'General' );
$interpreter = $this->getEngine()->getInterpreter(); $interpreter = $this->getEngine()->getInterpreter();
$interpreter->callFunction( $interpreter->callFunction(
$interpreter->loadString( 'mw.text.stripTest = ...', 'fortest' ), $interpreter->loadString( 'mw.text.stripTest = ...', 'fortest' ),
$this->getEngine()->getParser()->insertStripItem( 'ok' ) $markers
); );
} }

View file

@ -13,6 +13,19 @@ setmetatable( tagattrs, { __pairs = function ( t )
end end
end } ) end } )
-- For data provider, make sure this is defined
mw.text.stripTest = mw.text.stripTest or { nowiki = '!!!', general = '!!!' }
-- Can't directly expect the value from mw.text.stripTest, because when
-- 'expect' is processed by the data provider it's the dummy entry above.
local function stripTest( func, marker )
local result = func( marker )
if result == marker then
result = 'strip-marker'
end
return result
end
-- Tests -- Tests
local tests = { local tests = {
{ name = 'trim', { name = 'trim',
@ -107,9 +120,32 @@ local tests = {
expect = { '<b present key="value" n="42">foo</b>' } expect = { '<b present key="value" n="42">foo</b>' }
}, },
{ name = 'unstrip', { name = 'unstrip (nowiki)',
func = mw.text.unstrip, args = { mw.text.stripTest }, func = stripTest,
expect = { 'ok' } args = { mw.text.unstrip, mw.text.stripTest.nowiki },
expect = { 'NoWiki' }
},
{ name = 'unstrip (general)',
func = stripTest,
args = { mw.text.unstrip, mw.text.stripTest.general },
expect = { '' }
},
{ name = 'unstripNoWiki (nowiki)',
func = stripTest,
args = { mw.text.unstripNoWiki, mw.text.stripTest.nowiki },
expect = { 'NoWiki' }
},
{ name = 'unstripNoWiki (general)',
func = stripTest,
args = { mw.text.unstripNoWiki, mw.text.stripTest.general },
expect = { 'strip-marker' }
},
{ name = 'killMarkers',
func = mw.text.killMarkers,
args = { 'a' .. mw.text.stripTest.nowiki .. 'b' .. mw.text.stripTest.general .. 'c' },
expect = { 'abc' }
}, },
{ name = 'split, simple', { name = 'split, simple',