mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TemplateData
synced 2024-09-24 02:39:25 +00:00
tests: Reformat tests, evaluate status in addition to I/O
Previously we only compared output, we now evaluate status as well. For example test {} => {} was misleading since the reason it roundtripped was because it is *invalid* and we replace invalid blobs with an empty object in blob storage. But in the API this'll never get used because getStatus will have a fatal error in it. Which overrides the data. The tests were previously looking in the data whilst ignoring the return status. Change-Id: I9809f126c1615c9bc9c28ce2d8bb7953058cbf6e
This commit is contained in:
parent
2c89726e6e
commit
1a38b42241
|
@ -11,37 +11,55 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public static function provideParse() {
|
||||
return array(
|
||||
$cases = array(
|
||||
array(
|
||||
'
|
||||
{}
|
||||
'input' => '[]
|
||||
',
|
||||
'
|
||||
{}
|
||||
',
|
||||
'Empty object'
|
||||
'status' => 'Property "templatedata" is expected to be of type "object".'
|
||||
),
|
||||
array(
|
||||
'
|
||||
{
|
||||
'input' => '{
|
||||
"params": {}
|
||||
}
|
||||
',
|
||||
'output' => '{
|
||||
"description": null,
|
||||
"params": {}
|
||||
}
|
||||
',
|
||||
'status' => true,
|
||||
'msg' => 'Minimal valid blob'
|
||||
),
|
||||
array(
|
||||
'input' => '{
|
||||
"params": {},
|
||||
"foo": "bar"
|
||||
}
|
||||
',
|
||||
'
|
||||
{}
|
||||
',
|
||||
'Unknown properties are stripped'
|
||||
'status' => 'Unexpected property "foo".',
|
||||
'msg' => 'Unknown properties'
|
||||
),
|
||||
array(
|
||||
'
|
||||
{
|
||||
'input' => '{}',
|
||||
'status' => 'Required property "params" not found.',
|
||||
'msg' => 'Empty object'
|
||||
),
|
||||
array(
|
||||
'input' => '{
|
||||
"foo": "bar"
|
||||
}
|
||||
',
|
||||
'status' => 'Unexpected property "foo".',
|
||||
'msg' => 'Unknown properties invalidate the blob'
|
||||
),
|
||||
array(
|
||||
'input' => '{
|
||||
"params": {
|
||||
"foo": {}
|
||||
}
|
||||
}
|
||||
',
|
||||
'
|
||||
{
|
||||
'output' => '{
|
||||
"description": null,
|
||||
"params": {
|
||||
"foo": {
|
||||
|
@ -56,11 +74,10 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
|||
}
|
||||
}
|
||||
',
|
||||
'Optional properties are added if missing'
|
||||
'msg' => 'Optional properties are added if missing'
|
||||
),
|
||||
array(
|
||||
'
|
||||
{
|
||||
'input' => '{
|
||||
"description": "User badge MediaWiki developers.",
|
||||
"params": {
|
||||
"nickname": {
|
||||
|
@ -75,8 +92,7 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
|||
}
|
||||
}
|
||||
',
|
||||
'
|
||||
{
|
||||
'output' => '{
|
||||
"description": {
|
||||
"en": "User badge MediaWiki developers."
|
||||
},
|
||||
|
@ -97,11 +113,10 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
|||
}
|
||||
}
|
||||
',
|
||||
'InterfaceText is expanded to langcode-keyed object, assuming content language'
|
||||
'msg' => 'InterfaceText is expanded to langcode-keyed object, assuming content language'
|
||||
),
|
||||
array(
|
||||
'
|
||||
{
|
||||
'input' => '{
|
||||
"description": {
|
||||
"en": "User badge MediaWiki developers."
|
||||
},
|
||||
|
@ -122,11 +137,10 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
|||
}
|
||||
}
|
||||
',
|
||||
'Fully normalised json should be valid input and stay unchanged'
|
||||
'msg' => 'Fully normalised json should be valid input and stay unchanged'
|
||||
),
|
||||
array(
|
||||
'
|
||||
{
|
||||
'input' => '{
|
||||
"description": "Document the documenter.",
|
||||
"params": {
|
||||
"1d": {
|
||||
|
@ -141,8 +155,7 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
|||
}
|
||||
}
|
||||
',
|
||||
'
|
||||
{
|
||||
'output' => '{
|
||||
"description": {
|
||||
"en": "Document the documenter."
|
||||
},
|
||||
|
@ -172,75 +185,56 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
|||
}
|
||||
}
|
||||
',
|
||||
'The inherits property copies over properties from another parameter (preserving overides)'
|
||||
'msg' => 'The inherits property copies over properties from another parameter (preserving overides)'
|
||||
),
|
||||
);
|
||||
$calls = array();
|
||||
foreach ( $cases as $case ) {
|
||||
$calls[] = array( $case );
|
||||
}
|
||||
return $calls;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideParse
|
||||
*/
|
||||
public function testParse( $input, $expected, $msg = null ) {
|
||||
if ( !$msg ) {
|
||||
$msg = $expected;
|
||||
$expected = $input;
|
||||
public function testParse( Array $cases ) {
|
||||
|
||||
// Expand defaults
|
||||
if ( !isset( $cases['status'] ) ) {
|
||||
$cases['status'] = true;
|
||||
}
|
||||
$t = TemplateDataBlob::newFromJSON( $input );
|
||||
if ( !isset( $cases['msg'] ) ) {
|
||||
$cases['msg'] = is_string( $cases['status'] ) ? $cases['status'] : 'TemplateData assertion';
|
||||
}
|
||||
if ( !isset( $cases['output'] ) ) {
|
||||
if ( is_string( $cases['status'] ) ) {
|
||||
$cases['output'] = '{}';
|
||||
} else {
|
||||
$cases['output'] = $cases['input'];
|
||||
}
|
||||
}
|
||||
|
||||
$t = TemplateDataBlob::newFromJSON( $cases['input'] );
|
||||
$actual = $t->getJSON();
|
||||
$this->assertJsonStringEqualsJsonString(
|
||||
$expected,
|
||||
$actual,
|
||||
$msg
|
||||
);
|
||||
}
|
||||
|
||||
public static function provideStatus() {
|
||||
return array(
|
||||
array(
|
||||
'
|
||||
[]
|
||||
',
|
||||
false,
|
||||
'Not an object'
|
||||
),
|
||||
array(
|
||||
'
|
||||
{
|
||||
"params": {}
|
||||
}
|
||||
',
|
||||
true,
|
||||
'Minimal valid blob'
|
||||
),
|
||||
array(
|
||||
'
|
||||
{
|
||||
"params": {},
|
||||
"foo": "bar"
|
||||
}
|
||||
',
|
||||
false,
|
||||
'Unknown properties'
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideStatus
|
||||
*/
|
||||
public function testStatus( $inputJSON, $isGood, $msg ) {
|
||||
// Make sure we don't have two errors cancelling each other out
|
||||
if ( json_decode( $inputJSON ) === null ) {
|
||||
throw new Exception( 'Test case provided invalid JSON.' );
|
||||
}
|
||||
|
||||
$t = TemplateDataBlob::newFromJSON( $inputJSON );
|
||||
$status = $t->getStatus();
|
||||
|
||||
$this->assertEquals(
|
||||
$status->isGood(),
|
||||
$isGood,
|
||||
$msg
|
||||
if ( !$status->isGood() ) {
|
||||
$this->assertEquals(
|
||||
$cases['status'],
|
||||
$status->getHtml(),
|
||||
'Status: ' . $cases['msg']
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
$cases['status'],
|
||||
$status->isGood(),
|
||||
'Status: ' . $cases['msg']
|
||||
);
|
||||
}
|
||||
$this->assertJsonStringEqualsJsonString(
|
||||
$cases['output'],
|
||||
$actual,
|
||||
$cases['msg']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue