From 34fc672f6d703feef83834a92f3a10e3c3e6a651 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Fri, 3 Apr 2020 11:26:59 -0400 Subject: [PATCH] Add tests and type checking for mw.addWarning() Testing was overlooked when this was added in Ibdd2506f. Change-Id: Ie17020e3082668180dfa1d6532946891ea7951ea --- includes/engines/LuaCommon/LuaEngine.php | 3 +++ .../phpunit/engines/LuaCommon/CommonTests.lua | 9 ++++++++ .../engines/LuaCommon/LuaCommonTest.php | 23 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/includes/engines/LuaCommon/LuaEngine.php b/includes/engines/LuaCommon/LuaEngine.php index 969c6b8b..5f136864 100644 --- a/includes/engines/LuaCommon/LuaEngine.php +++ b/includes/engines/LuaCommon/LuaEngine.php @@ -874,6 +874,9 @@ abstract class Scribunto_LuaEngine extends ScribuntoEngineBase { * @param string $text wikitext */ public function addWarning( $text ) { + $args = func_get_args(); + $this->checkString( 'addWarning', $args, 0 ); + $this->getParser()->getOutput()->addWarning( $text ); } diff --git a/tests/phpunit/engines/LuaCommon/CommonTests.lua b/tests/phpunit/engines/LuaCommon/CommonTests.lua index fef18edf..80fd5f85 100644 --- a/tests/phpunit/engines/LuaCommon/CommonTests.lua +++ b/tests/phpunit/engines/LuaCommon/CommonTests.lua @@ -384,4 +384,13 @@ return testframework.getTestProvider( { func = test.loadData.rawset, expect = { 'ugh', 'foo bar', 'foo bar' }, }, + + { name = 'mw.addWarning', + func = mw.addWarning, args = { 'warn' }, + expect = {}, + }, + { name = 'mw.addWarning, bad type', + func = mw.addWarning, args = { true }, + expect = "bad argument #1 to 'addWarning' (string expected)", + }, } ) diff --git a/tests/phpunit/engines/LuaCommon/LuaCommonTest.php b/tests/phpunit/engines/LuaCommon/LuaCommonTest.php index eac72fda..e8be6108 100644 --- a/tests/phpunit/engines/LuaCommon/LuaCommonTest.php +++ b/tests/phpunit/engines/LuaCommon/LuaCommonTest.php @@ -828,4 +828,27 @@ class Scribunto_LuaCommonTest extends Scribunto_LuaEngineTestBase { $text = $parser->mStripState->unstripBoth( $text ); $this->assertSame( '>false<', $text ); } + + public function testAddWarning() { + $engine = $this->getEngine(); + $parser = $engine->getParser(); + $pp = $parser->getPreprocessor(); + + $this->extraModules['Module:TestAddWarning'] = ' + local p = {} + + p.foo = function () + mw.addWarning( "Don\'t panic!" ) + return "ok" + end + + return p + '; + + $frame = $pp->newFrame(); + $text = $frame->expand( $pp->preprocessToObj( ">{{#invoke:TestAddWarning|foo}}<" ) ); + $text = $parser->mStripState->unstripBoth( $text ); + $this->assertSame( '>ok<', $text ); + $this->assertSame( [ 'Don\'t panic!' ], $parser->getOutput()->getWarnings() ); + } }