2019-10-29 21:35:43 +00:00
|
|
|
/* global moment */
|
2019-10-24 11:41:25 +00:00
|
|
|
var
|
2020-03-19 18:43:51 +00:00
|
|
|
testUtils = require( './testUtils.js' ),
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
Parser = require( 'ext.discussionTools.init' ).Parser;
|
2019-10-24 11:41:25 +00:00
|
|
|
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
QUnit.module( 'mw.dt.Parser', QUnit.newMwEnvironment() );
|
2019-10-24 11:41:25 +00:00
|
|
|
|
|
|
|
QUnit.test( '#getTimestampRegexp', function ( assert ) {
|
2020-07-20 21:15:03 +00:00
|
|
|
var cases = require( '../cases/timestamp-regex.json' ),
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
parser = new Parser( require( '../data-en.json' ) );
|
2019-10-24 11:41:25 +00:00
|
|
|
|
2020-05-08 11:03:49 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
2019-10-24 11:41:25 +00:00
|
|
|
assert.strictEqual(
|
2020-09-03 20:59:33 +00:00
|
|
|
parser.getTimestampRegexp( 'en', caseItem.format, '\\d', { UTC: 'UTC' } ),
|
2020-05-08 11:03:49 +00:00
|
|
|
caseItem.expected,
|
|
|
|
caseItem.message
|
2019-10-24 11:41:25 +00:00
|
|
|
);
|
2020-05-08 11:03:49 +00:00
|
|
|
} );
|
2019-10-24 11:41:25 +00:00
|
|
|
} );
|
2019-10-24 12:59:42 +00:00
|
|
|
|
2019-10-29 21:35:43 +00:00
|
|
|
QUnit.test( '#getTimestampParser', function ( assert ) {
|
2020-07-20 21:15:03 +00:00
|
|
|
var cases = require( '../cases/timestamp-parser.json' ),
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
parser = new Parser( require( '../data-en.json' ) );
|
2019-10-29 21:35:43 +00:00
|
|
|
|
2020-05-08 11:03:49 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
2020-09-03 20:59:33 +00:00
|
|
|
var tsParser = parser.getTimestampParser( 'en', caseItem.format, null, 'UTC', { UTC: 'UTC' } ),
|
2020-05-08 11:03:49 +00:00
|
|
|
expectedDate = moment( caseItem.expected );
|
2019-10-29 21:35:43 +00:00
|
|
|
|
2021-11-08 19:03:40 +00:00
|
|
|
assert.true(
|
2022-03-03 16:29:39 +00:00
|
|
|
tsParser( caseItem.data ).date.isSame( expectedDate ),
|
2020-05-08 11:03:49 +00:00
|
|
|
caseItem.message
|
2019-10-29 21:35:43 +00:00
|
|
|
);
|
2020-05-08 11:03:49 +00:00
|
|
|
} );
|
2019-10-29 21:35:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#getTimestampParser (at DST change)', function ( assert ) {
|
2020-07-20 21:15:03 +00:00
|
|
|
var cases = require( '../cases/timestamp-parser-dst.json' ),
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
parser = new Parser( require( '../data-en.json' ) );
|
2019-10-29 21:35:43 +00:00
|
|
|
|
2020-05-08 11:03:49 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
2020-09-03 20:59:33 +00:00
|
|
|
var regexp = parser.getTimestampRegexp( 'en', caseItem.format, '\\d', caseItem.timezoneAbbrs ),
|
|
|
|
tsParser = parser.getTimestampParser( 'en', caseItem.format, null, caseItem.timezone, caseItem.timezoneAbbrs ),
|
2022-03-03 16:29:39 +00:00
|
|
|
date = tsParser( caseItem.sample.match( regexp ) ).date;
|
2019-10-29 21:35:43 +00:00
|
|
|
|
2021-11-08 19:03:40 +00:00
|
|
|
assert.true(
|
2020-05-08 11:03:49 +00:00
|
|
|
date.isSame( caseItem.expected ),
|
|
|
|
caseItem.message
|
2019-10-29 21:35:43 +00:00
|
|
|
);
|
2021-11-08 19:03:40 +00:00
|
|
|
assert.true(
|
2020-05-08 11:03:49 +00:00
|
|
|
date.isSame( caseItem.expectedUtc ),
|
|
|
|
caseItem.message
|
2019-10-29 21:35:43 +00:00
|
|
|
);
|
2020-05-08 11:03:49 +00:00
|
|
|
} );
|
2019-10-29 21:35:43 +00:00
|
|
|
} );
|
|
|
|
|
2022-10-11 16:23:40 +00:00
|
|
|
require( '../cases/comments.json' ).forEach( function ( caseItem ) {
|
|
|
|
|
|
|
|
var testName = '#getThreads (' + caseItem.name + ')';
|
|
|
|
|
2022-10-12 22:05:30 +00:00
|
|
|
// Some parser tests are currently broken in JS
|
2022-10-11 16:23:40 +00:00
|
|
|
var skipTests = [
|
2022-10-12 22:05:30 +00:00
|
|
|
'Signature which is just a selflink'
|
2022-10-11 16:23:40 +00:00
|
|
|
];
|
|
|
|
if ( skipTests.indexOf( caseItem.name ) !== -1 ) {
|
|
|
|
QUnit.skip( testName );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUnit.test( testName, function ( assert ) {
|
2022-10-12 20:03:23 +00:00
|
|
|
var dom = ve.createDocumentFromHtml( require( '../' + caseItem.dom ) ),
|
2020-05-08 11:03:49 +00:00
|
|
|
expected = require( caseItem.expected ),
|
|
|
|
config = require( caseItem.config ),
|
2022-01-11 15:50:44 +00:00
|
|
|
data = require( caseItem.data ),
|
|
|
|
title = mw.Title.newFromText( caseItem.title );
|
2020-05-08 11:03:49 +00:00
|
|
|
|
2022-10-12 20:03:23 +00:00
|
|
|
var container = testUtils.getThreadContainer( dom );
|
2020-05-08 11:03:49 +00:00
|
|
|
testUtils.overrideMwConfig( config );
|
2019-10-24 12:59:42 +00:00
|
|
|
|
2022-10-12 20:03:23 +00:00
|
|
|
var threadItemSet = new Parser( data ).parse( container, title );
|
2022-02-19 06:31:34 +00:00
|
|
|
var threads = threadItemSet.getThreads();
|
2019-10-24 12:59:42 +00:00
|
|
|
|
2020-05-08 11:03:49 +00:00
|
|
|
threads.forEach( function ( thread, i ) {
|
2022-10-12 20:03:23 +00:00
|
|
|
testUtils.serializeComments( thread, container );
|
2019-10-24 12:59:42 +00:00
|
|
|
|
|
|
|
assert.deepEqual(
|
2020-05-08 11:03:49 +00:00
|
|
|
JSON.parse( JSON.stringify( thread ) ),
|
|
|
|
expected[ i ],
|
|
|
|
caseItem.name + ' section ' + i
|
2019-10-24 12:59:42 +00:00
|
|
|
);
|
2020-05-08 11:03:49 +00:00
|
|
|
} );
|
2019-11-24 16:28:01 +00:00
|
|
|
|
2020-03-04 20:25:35 +00:00
|
|
|
// Uncomment this to get updated content for the JSON files, for copy/paste:
|
2019-11-24 16:28:01 +00:00
|
|
|
// console.log( JSON.stringify( threads, null, 2 ) );
|
2020-05-08 11:03:49 +00:00
|
|
|
} );
|
2019-10-24 12:59:42 +00:00
|
|
|
} );
|
2021-11-13 15:27:38 +00:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// * findCommentsById
|
|
|
|
// * findCommentsByName
|
|
|
|
// * getThreadItems
|