Add the test for getAuthors() in JS too

Change-Id: Id7dabc535b6bb688602c0d55fc3696f662cb10c7
This commit is contained in:
Bartosz Dziewoński 2020-05-19 21:01:03 +02:00
parent 515af82061
commit c64bb6b5b7
5 changed files with 46 additions and 25 deletions

View file

@ -162,6 +162,7 @@
"data/nlwiki-data.json", "data/nlwiki-data.json",
"data/plwiki-config.json", "data/plwiki-config.json",
"data/plwiki-data.json", "data/plwiki-data.json",
"cases/authors.json",
"cases/comments.json", "cases/comments.json",
"cases/transcluded.json", "cases/transcluded.json",
"cases/modified.json", "cases/modified.json",

26
tests/cases/authors.json Normal file
View file

@ -0,0 +1,26 @@
[
{
"thread": {
"replies": [
{
"author": "Eve",
"replies": []
},
{
"author": "Bob",
"replies": [
{
"author": "Alice",
"replies": []
}
]
}
]
},
"expected": [
"Alice",
"Bob",
"Eve"
]
}
]

View file

@ -188,29 +188,10 @@ class CommentParserTest extends CommentTestCase {
} }
public function provideAuthors() : array { public function provideAuthors() : array {
return [ return array_map( function ( $caseItem ) {
[ // PHPUnit requires associative arrays, not stdClass objects
'thread' => (object)[ return (array)$caseItem;
'replies' => [ }, self::getJson( '../cases/authors.json', false ) );
(object)[
'author' => 'Eve',
'replies' => []
],
(object)[
'author' => 'Bob',
'replies' => [
(object)[
'author' => 'Alice',
'replies' => []
]
]
]
]
],
'expected' => [ 'Alice', 'Bob', 'Eve' ]
]
];
} }
/** /**

View file

@ -26,12 +26,13 @@ abstract class CommentTestCase extends MediaWikiTestCase {
* Get parsed JSON from path * Get parsed JSON from path
* *
* @param string $relativePath * @param string $relativePath
* @param bool $assoc See json_decode()
* @return array * @return array
*/ */
protected static function getJson( string $relativePath ) : array { protected static function getJson( string $relativePath, bool $assoc = true ) : array {
$json = json_decode( $json = json_decode(
file_get_contents( __DIR__ . '/' . $relativePath ), file_get_contents( __DIR__ . '/' . $relativePath ),
true $assoc
); );
return $json; return $json;
} }

View file

@ -137,3 +137,15 @@ QUnit.test( '#getTranscludedFrom', function ( assert ) {
// console.log( JSON.stringify( transcludedFrom, null, 2 ) ); // console.log( JSON.stringify( transcludedFrom, null, 2 ) );
} ); } );
} ); } );
QUnit.test( '#getAuthors', function ( assert ) {
var cases = require( '../cases/authors.json' );
cases.forEach( function ( caseItem ) {
var authors = parser.getAuthors( caseItem.thread );
assert.deepEqual(
authors,
caseItem.expected
);
} );
} );