class FormatJsonTest extends MediaWikiTestCase {
public static function provideEncoderPrettyPrinting() {
- return array(
+ return [
// Four spaces
- array( true, ' ' ),
- array( ' ', ' ' ),
+ [ true, ' ' ],
+ [ ' ', ' ' ],
// Two spaces
- array( ' ', ' ' ),
+ [ ' ', ' ' ],
// One tab
- array( "\t", "\t" ),
- );
+ [ "\t", "\t" ],
+ ];
}
/**
* @dataProvider provideEncoderPrettyPrinting
*/
public function testEncoderPrettyPrinting( $pretty, $expectedIndent ) {
- $obj = array(
+ $obj = [
'emptyObject' => new stdClass,
- 'emptyArray' => array(),
+ 'emptyArray' => [],
'string' => 'foobar\\',
- 'filledArray' => array(
- array(
+ 'filledArray' => [
+ [
123,
456,
- ),
+ ],
// Nested json works without problems
'"7":["8",{"9":"10"}]',
// Whitespace clean up doesn't touch strings that look alike
"{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}",
- ),
- );
+ ],
+ ];
// No trailing whitespace, no trailing linefeed
$json = '{
}
public static function provideEncodeDefault() {
- return self::getEncodeTestCases( array() );
+ return self::getEncodeTestCases( [] );
}
/**
}
public static function provideEncodeUtf8() {
- return self::getEncodeTestCases( array( 'unicode' ) );
+ return self::getEncodeTestCases( [ 'unicode' ] );
}
/**
}
public static function provideEncodeXmlMeta() {
- return self::getEncodeTestCases( array( 'xmlmeta' ) );
+ return self::getEncodeTestCases( [ 'xmlmeta' ] );
}
/**
}
public static function provideEncodeAllOk() {
- return self::getEncodeTestCases( array( 'unicode', 'xmlmeta' ) );
+ return self::getEncodeTestCases( [ 'unicode', 'xmlmeta' ] );
}
/**
}
public static function provideParse() {
- return array(
- array( null ),
- array( true ),
- array( false ),
- array( 0 ),
- array( 1 ),
- array( 1.2 ),
- array( '' ),
- array( 'str' ),
- array( array( 0, 1, 2 ) ),
- array( array( 'a' => 'b' ) ),
- array( array( 'a' => 'b' ) ),
- array( array( 'a' => 'b', 'x' => array( 'c' => 'd' ) ) ),
- );
+ return [
+ [ null ],
+ [ true ],
+ [ false ],
+ [ 0 ],
+ [ 1 ],
+ [ 1.2 ],
+ [ '' ],
+ [ 'str' ],
+ [ [ 0, 1, 2 ] ],
+ [ [ 'a' => 'b' ] ],
+ [ [ 'a' => 'b' ] ],
+ [ [ 'a' => 'b', 'x' => [ 'c' => 'd' ] ] ],
+ ];
}
/**
$this->assertJson( $json );
$st = FormatJson::parse( $json );
- $this->assertType( 'Status', $st );
+ $this->assertInstanceOf( 'Status', $st );
$this->assertTrue( $st->isGood() );
$this->assertEquals( $expected, $st->getValue() );
$st = FormatJson::parse( $json, FormatJson::FORCE_ASSOC );
- $this->assertType( 'Status', $st );
+ $this->assertInstanceOf( 'Status', $st );
$this->assertTrue( $st->isGood() );
$this->assertEquals( $value, $st->getValue() );
}
* json-c parsed result. Defaults to the second argument's value.
*/
public static function provideParseTryFixing() {
- return array(
- array( "[,]", '[]', false ),
- array( "[ , ]", '[]', false ),
- array( "[ , }", false ),
- array( '[1],', false, true, '[1]' ),
- array( "[1,]", '[1]' ),
- array( "[1\n,]", '[1]' ),
- array( "[1,\n]", '[1]' ),
- array( "[1,]\n", '[1]' ),
- array( "[1\n,\n]\n", '[1]' ),
- array( '["a,",]', '["a,"]' ),
- array( "[[1,]\n,[2,\n],[3\n,]]", '[[1],[2],[3]]' ),
+ return [
+ [ "[,]", '[]', false ],
+ [ "[ , ]", '[]', false ],
+ [ "[ , }", false ],
+ [ '[1],', false, true, '[1]' ],
+ [ "[1,]", '[1]' ],
+ [ "[1\n,]", '[1]' ],
+ [ "[1,\n]", '[1]' ],
+ [ "[1,]\n", '[1]' ],
+ [ "[1\n,\n]\n", '[1]' ],
+ [ '["a,",]', '["a,"]' ],
+ [ "[[1,]\n,[2,\n],[3\n,]]", '[[1],[2],[3]]' ],
// I wish we could parse this, but would need quote parsing
- array( '[[1,],[2,],[3,]]', false, true, '[[1],[2],[3]]' ),
- array( '[1,,]', false, false, '[1]' ),
- );
+ [ '[[1,],[2,],[3,]]', false, true, '[[1],[2],[3]]' ],
+ [ '[1,,]', false, false, '[1]' ],
+ ];
}
/**
}
$st = FormatJson::parse( $value, FormatJson::TRY_FIXING );
- $this->assertType( 'Status', $st );
+ $this->assertInstanceOf( 'Status', $st );
if ( $expected === false ) {
$this->assertFalse( $st->isOK(), 'Expected isOK() == false' );
} else {
}
public static function provideParseErrors() {
- return array(
- array( 'aaa' ),
- array( '{"j": 1 ] }' ),
- );
+ return [
+ [ 'aaa' ],
+ [ '{"j": 1 ] }' ],
+ ];
}
/**
*/
public function testParseErrors( $value ) {
$st = FormatJson::parse( $value );
- $this->assertType( 'Status', $st );
+ $this->assertInstanceOf( 'Status', $st );
$this->assertFalse( $st->isOK() );
}
public function provideStripComments() {
- return array(
- array( '{"a":"b"}', '{"a":"b"}' ),
- array( "{\"a\":\"b\"}\n", "{\"a\":\"b\"}\n" ),
- array( '/*c*/{"c":"b"}', '{"c":"b"}' ),
- array( '{"a":"c"}/*c*/', '{"a":"c"}' ),
- array( '/*c//d*/{"c":"b"}', '{"c":"b"}' ),
- array( '{/*c*/"c":"b"}', '{"c":"b"}' ),
- array( "/*\nc\r\n*/{\"c\":\"b\"}", '{"c":"b"}' ),
- array( "//c\n{\"c\":\"b\"}", '{"c":"b"}' ),
- array( "//c\r\n{\"c\":\"b\"}", '{"c":"b"}' ),
- array( '{"a":"c"}//c', '{"a":"c"}' ),
- array( "{\"a-c\"://c\n\"b\"}", '{"a-c":"b"}' ),
- array( '{"/*a":"b"}', '{"/*a":"b"}' ),
- array( '{"a":"//b"}', '{"a":"//b"}' ),
- array( '{"a":"b/*c*/"}', '{"a":"b/*c*/"}' ),
- array( "{\"\\\"/*a\":\"b\"}", "{\"\\\"/*a\":\"b\"}" ),
- array( '', '' ),
- array( '/*c', '' ),
- array( '//c', '' ),
- array( '"http://example.com"', '"http://example.com"' ),
- array( "\0", "\0" ),
- array( '"Blåbærsyltetøy"', '"Blåbærsyltetøy"' ),
- );
+ return [
+ [ '{"a":"b"}', '{"a":"b"}' ],
+ [ "{\"a\":\"b\"}\n", "{\"a\":\"b\"}\n" ],
+ [ '/*c*/{"c":"b"}', '{"c":"b"}' ],
+ [ '{"a":"c"}/*c*/', '{"a":"c"}' ],
+ [ '/*c//d*/{"c":"b"}', '{"c":"b"}' ],
+ [ '{/*c*/"c":"b"}', '{"c":"b"}' ],
+ [ "/*\nc\r\n*/{\"c\":\"b\"}", '{"c":"b"}' ],
+ [ "//c\n{\"c\":\"b\"}", '{"c":"b"}' ],
+ [ "//c\r\n{\"c\":\"b\"}", '{"c":"b"}' ],
+ [ '{"a":"c"}//c', '{"a":"c"}' ],
+ [ "{\"a-c\"://c\n\"b\"}", '{"a-c":"b"}' ],
+ [ '{"/*a":"b"}', '{"/*a":"b"}' ],
+ [ '{"a":"//b"}', '{"a":"//b"}' ],
+ [ '{"a":"b/*c*/"}', '{"a":"b/*c*/"}' ],
+ [ "{\"\\\"/*a\":\"b\"}", "{\"\\\"/*a\":\"b\"}" ],
+ [ '', '' ],
+ [ '/*c', '' ],
+ [ '//c', '' ],
+ [ '"http://example.com"', '"http://example.com"' ],
+ [ "\0", "\0" ],
+ [ '"Blåbærsyltetøy"', '"Blåbærsyltetøy"' ],
+ ];
}
/**
}
public function provideParseStripComments() {
- return array(
- array( '/* blah */true', true ),
- array( "// blah \ntrue", true ),
- array( '[ "a" , /* blah */ "b" ]', array( 'a', 'b' ) ),
- );
+ return [
+ [ '/* blah */true', true ],
+ [ "// blah \ntrue", true ],
+ [ '[ "a" , /* blah */ "b" ]', [ 'a', 'b' ] ],
+ ];
}
/**
*/
public function testParseStripComments( $json, $expect ) {
$st = FormatJson::parse( $json, FormatJson::STRIP_COMMENTS );
- $this->assertType( 'Status', $st );
+ $this->assertInstanceOf( 'Status', $st );
$this->assertTrue( $st->isGood() );
$this->assertEquals( $expect, $st->getValue() );
}
* @return array Arrays of unencoded strings and corresponding encoded strings
*/
private static function getEncodeTestCases( array $unescapedGroups ) {
- $groups = array(
- 'always' => array(
+ $groups = [
+ 'always' => [
// Forward slash (always unescaped)
'/' => '/',
// Line terminators
"\xe2\x80\xa8" => '\u2028',
"\xe2\x80\xa9" => '\u2029',
- ),
- 'unicode' => array(
+ ],
+ 'unicode' => [
"\xc3\xa9" => '\u00e9',
"\xf0\x9d\x92\x9e" => '\ud835\udc9e', // U+1D49E, outside the BMP
- ),
- 'xmlmeta' => array(
+ ],
+ 'xmlmeta' => [
'<' => '\u003C', // JSON_HEX_TAG uses uppercase hex digits
'>' => '\u003E',
'&' => '\u0026',
- ),
- );
+ ],
+ ];
- $cases = array();
+ $cases = [];
foreach ( $groups as $name => $rules ) {
$leaveUnescaped = in_array( $name, $unescapedGroups );
foreach ( $rules as $from => $to ) {
- $cases[] = array( $from, '"' . ( $leaveUnescaped ? $from : $to ) . '"' );
+ $cases[] = [ $from, '"' . ( $leaveUnescaped ? $from : $to ) . '"' ];
}
}