diff options
author | Kevin Israel <pleasestand@live.com> | 2014-04-02 21:51:09 -0400 |
---|---|---|
committer | Brad Jorsch <bjorsch@wikimedia.org> | 2014-04-16 10:00:10 -0400 |
commit | 1efdda25ee5cbaea2b7e2dd7afdbaf53063d4b68 (patch) | |
tree | 58db249ab53d92045c496fc08beef59d472012b8 /tests/phpunit/includes/json/FormatJsonTest.php | |
parent | 1360206275f5d9d141240356d3d9939b0ebcea89 (diff) | |
download | mediawikicore-1efdda25ee5cbaea2b7e2dd7afdbaf53063d4b68.tar.gz mediawikicore-1efdda25ee5cbaea2b7e2dd7afdbaf53063d4b68.zip |
FormatJson: Make it possible to change the indent string
This is to allow consistency with MediaWiki PHP and JS files (e.g. when
generating JSON i18n files), not because tabs are "better" than spaces for
indenting code (both have advantages and disadvantages).
Because PHP's json_encode() function hardcodes the indent string, using tabs
has a performance cost (in post-processing the output) and is less suitable
for web output; thus the API and ResourceLoader debug mode will continue to
use four spaces. Adjusting the maintenance scripts and JSON files is left to
separate change sets.
Bug: 63444
Change-Id: Ic915c50b0acd2e236940b70d5dd48ea87954c9d5
Diffstat (limited to 'tests/phpunit/includes/json/FormatJsonTest.php')
-rw-r--r-- | tests/phpunit/includes/json/FormatJsonTest.php | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/tests/phpunit/includes/json/FormatJsonTest.php b/tests/phpunit/includes/json/FormatJsonTest.php index 8359f0d2e735..307b3551e9f6 100644 --- a/tests/phpunit/includes/json/FormatJsonTest.php +++ b/tests/phpunit/includes/json/FormatJsonTest.php @@ -5,7 +5,22 @@ */ class FormatJsonTest extends MediaWikiTestCase { - public function testEncoderPrettyPrinting() { + public static function provideEncoderPrettyPrinting() { + return array( + // Four spaces + array( true, ' ' ), + array( ' ', ' ' ), + // Two spaces + array( ' ', ' ' ), + // One tab + array( "\t", "\t" ), + ); + } + + /** + * @dataProvider provideEncoderPrettyPrinting + */ + public function testEncoderPrettyPrinting( $pretty, $expectedIndent ) { $obj = array( 'emptyObject' => new stdClass, 'emptyArray' => array(), @@ -22,23 +37,24 @@ class FormatJsonTest extends MediaWikiTestCase { ), ); - // 4 space indent, no trailing whitespace, no trailing linefeed + // No trailing whitespace, no trailing linefeed $json = '{ - "emptyObject": {}, - "emptyArray": [], - "string": "foobar\\\\", - "filledArray": [ - [ - 123, - 456 - ], - "\"7\":[\"8\",{\"9\":\"10\"}]", - "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}" - ] + "emptyObject": {}, + "emptyArray": [], + "string": "foobar\\\\", + "filledArray": [ + [ + 123, + 456 + ], + "\"7\":[\"8\",{\"9\":\"10\"}]", + "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}" + ] }'; $json = str_replace( "\r", '', $json ); // Windows compat - $this->assertSame( $json, FormatJson::encode( $obj, true ) ); + $json = str_replace( "\t", $expectedIndent, $json ); + $this->assertSame( $json, FormatJson::encode( $obj, $pretty ) ); } public static function provideEncodeDefault() { |