mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TemplateData
synced 2024-09-24 10:49:29 +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() {
|
public static function provideParse() {
|
||||||
return array(
|
$cases = array(
|
||||||
array(
|
array(
|
||||||
'
|
'input' => '[]
|
||||||
{}
|
|
||||||
',
|
',
|
||||||
'
|
'status' => 'Property "templatedata" is expected to be of type "object".'
|
||||||
{}
|
|
||||||
',
|
|
||||||
'Empty object'
|
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'
|
'input' => '{
|
||||||
{
|
"params": {}
|
||||||
|
}
|
||||||
|
',
|
||||||
|
'output' => '{
|
||||||
|
"description": null,
|
||||||
|
"params": {}
|
||||||
|
}
|
||||||
|
',
|
||||||
|
'status' => true,
|
||||||
|
'msg' => 'Minimal valid blob'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'input' => '{
|
||||||
|
"params": {},
|
||||||
"foo": "bar"
|
"foo": "bar"
|
||||||
}
|
}
|
||||||
',
|
',
|
||||||
'
|
'status' => 'Unexpected property "foo".',
|
||||||
{}
|
'msg' => 'Unknown properties'
|
||||||
',
|
|
||||||
'Unknown properties are stripped'
|
|
||||||
),
|
),
|
||||||
array(
|
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": {
|
"params": {
|
||||||
"foo": {}
|
"foo": {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
',
|
',
|
||||||
'
|
'output' => '{
|
||||||
{
|
|
||||||
"description": null,
|
"description": null,
|
||||||
"params": {
|
"params": {
|
||||||
"foo": {
|
"foo": {
|
||||||
|
@ -56,11 +74,10 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
',
|
',
|
||||||
'Optional properties are added if missing'
|
'msg' => 'Optional properties are added if missing'
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'
|
'input' => '{
|
||||||
{
|
|
||||||
"description": "User badge MediaWiki developers.",
|
"description": "User badge MediaWiki developers.",
|
||||||
"params": {
|
"params": {
|
||||||
"nickname": {
|
"nickname": {
|
||||||
|
@ -75,8 +92,7 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
',
|
',
|
||||||
'
|
'output' => '{
|
||||||
{
|
|
||||||
"description": {
|
"description": {
|
||||||
"en": "User badge MediaWiki developers."
|
"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(
|
array(
|
||||||
'
|
'input' => '{
|
||||||
{
|
|
||||||
"description": {
|
"description": {
|
||||||
"en": "User badge MediaWiki developers."
|
"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(
|
array(
|
||||||
'
|
'input' => '{
|
||||||
{
|
|
||||||
"description": "Document the documenter.",
|
"description": "Document the documenter.",
|
||||||
"params": {
|
"params": {
|
||||||
"1d": {
|
"1d": {
|
||||||
|
@ -141,8 +155,7 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
',
|
',
|
||||||
'
|
'output' => '{
|
||||||
{
|
|
||||||
"description": {
|
"description": {
|
||||||
"en": "Document the documenter."
|
"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
|
* @dataProvider provideParse
|
||||||
*/
|
*/
|
||||||
public function testParse( $input, $expected, $msg = null ) {
|
public function testParse( Array $cases ) {
|
||||||
if ( !$msg ) {
|
|
||||||
$msg = $expected;
|
// Expand defaults
|
||||||
$expected = $input;
|
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();
|
$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();
|
$status = $t->getStatus();
|
||||||
|
if ( !$status->isGood() ) {
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$status->isGood(),
|
$cases['status'],
|
||||||
$isGood,
|
$status->getHtml(),
|
||||||
$msg
|
'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