diff options
author | Bryan Davis <bd808@wikimedia.org> | 2014-10-12 11:59:00 -0600 |
---|---|---|
committer | Bryan Davis <bd808@wikimedia.org> | 2014-10-12 12:34:22 -0600 |
commit | 8fea9c619d07deafee54763dfe20538406a9bf13 (patch) | |
tree | 5bf883cf6a543815776c0083cb6c4f6f25cbf0c7 /tests/phpunit/includes/json/FormatJsonTest.php | |
parent | 042c6b6fccb137a6b0d1a05edc523b236f85caaf (diff) | |
download | mediawikicore-8fea9c619d07deafee54763dfe20538406a9bf13.tar.gz mediawikicore-8fea9c619d07deafee54763dfe20538406a9bf13.zip |
FormatJson::stripComments
Add stripComments method that can be used to remove single line and
multiline comments from an otherwise valid JSON string. Inspired by the
comment removal code in redisJobRunnerService and discussions on irc
about the Extension registration RFC.
Change-Id: Ie743957bfbb7b1fca8cb78ad48c1efd953362fde
Diffstat (limited to 'tests/phpunit/includes/json/FormatJsonTest.php')
-rw-r--r-- | tests/phpunit/includes/json/FormatJsonTest.php | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/phpunit/includes/json/FormatJsonTest.php b/tests/phpunit/includes/json/FormatJsonTest.php index af68ab03ada6..456266f7be6a 100644 --- a/tests/phpunit/includes/json/FormatJsonTest.php +++ b/tests/phpunit/includes/json/FormatJsonTest.php @@ -222,6 +222,64 @@ class FormatJsonTest extends MediaWikiTestCase { $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"' ), + ); + } + + /** + * @covers FormatJson::stripComments + * @dataProvider provideStripComments + * @param string $json + * @param string $expect + */ + public function testStripComments( $json, $expect ) { + $this->assertSame( $expect, FormatJson::stripComments( $json ) ); + } + + public function provideParseStripComments() { + return array( + array( '/* blah */true', true ), + array( "// blah \ntrue", true ), + array( '[ "a" , /* blah */ "b" ]', array( 'a', 'b' ) ), + ); + } + + /** + * @covers FormatJson::parse + * @covers FormatJson::stripComments + * @dataProvider provideParseStripComments + * @param string $json + * @param mixed $expect + */ + public function testParseStripComments( $json, $expect ) { + $st = FormatJson::parse( $json, FormatJson::STRIP_COMMENTS ); + $this->assertType( 'Status', $st ); + $this->assertTrue( $st->isGood() ); + $this->assertEquals( $expect, $st->getValue() ); + } + /** * Generate a set of test cases for a particular combination of encoder options. * |