2019-10-29 21:35:43 +00:00
|
|
|
/* global moment */
|
2024-05-24 12:31:30 +00:00
|
|
|
const
|
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
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
QUnit.test( '#getTimestampRegexp', ( assert ) => {
|
2024-05-24 12:20:50 +00:00
|
|
|
const 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
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
cases.forEach( ( 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
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
QUnit.test( '#getTimestampParser', ( assert ) => {
|
2024-05-24 12:20:50 +00:00
|
|
|
const 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
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
cases.forEach( ( caseItem ) => {
|
2024-05-24 12:20:50 +00:00
|
|
|
const tsParser = parser.getTimestampParser( 'en', caseItem.format, caseItem.digits, '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
|
|
|
} );
|
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
QUnit.test( '#getTimestampParser (at DST change)', ( assert ) => {
|
2024-05-24 12:20:50 +00:00
|
|
|
const 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
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
cases.forEach( ( caseItem ) => {
|
2024-05-24 12:20:50 +00:00
|
|
|
const regexp = parser.getTimestampRegexp( 'en', caseItem.format, '\\d', caseItem.timezoneAbbrs ),
|
2020-09-03 20:59:33 +00:00
|
|
|
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
|
|
|
} );
|
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
require( '../cases/comments.json' ).forEach( ( caseItem ) => {
|
2022-10-11 16:23:40 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const testName = '#getThreads (' + caseItem.name + ')';
|
2024-04-18 18:37:58 +00:00
|
|
|
QUnit.test( testName, ( assert ) => {
|
2024-05-24 12:20:50 +00:00
|
|
|
const dom = ve.createDocumentFromHtml( require( '../' + caseItem.dom ) ),
|
2020-05-08 11:03:49 +00:00
|
|
|
expected = require( caseItem.expected ),
|
|
|
|
config = require( caseItem.config ),
|
2022-10-17 14:45:09 +00:00
|
|
|
data = require( caseItem.data );
|
2020-05-08 11:03:49 +00:00
|
|
|
|
|
|
|
testUtils.overrideMwConfig( config );
|
2019-10-24 12:59:42 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const container = testUtils.getThreadContainer( dom );
|
|
|
|
const title = mw.Title.newFromText( caseItem.title );
|
|
|
|
const threadItemSet = new Parser( data ).parse( container, title );
|
|
|
|
const threads = threadItemSet.getThreads();
|
2019-10-24 12:59:42 +00:00
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
threads.forEach( ( 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
|