mediawiki-extensions-Discus.../tests/qunit/parser.test.js
thiemowmde 10dcd1f847 Replace preg_replace_callback with strtr in CommentParser
It does the same as before.

I think performance is not a concern here, and wasn't my motivation
either. But I hope this makes the code easier to read and to reason
with.

I added a pure unit test case (without involving an actual Language
object) to cover the previously uncovered digits feature.

Change-Id: I6a0fc86035817eabb42b55e58183ae094c052aa6
2023-10-31 08:55:40 +01:00

91 lines
2.7 KiB
JavaScript

/* global moment */
var
testUtils = require( './testUtils.js' ),
Parser = require( 'ext.discussionTools.init' ).Parser;
QUnit.module( 'mw.dt.Parser', QUnit.newMwEnvironment() );
QUnit.test( '#getTimestampRegexp', function ( assert ) {
var cases = require( '../cases/timestamp-regex.json' ),
parser = new Parser( require( '../data-en.json' ) );
cases.forEach( function ( caseItem ) {
assert.strictEqual(
parser.getTimestampRegexp( 'en', caseItem.format, '\\d', { UTC: 'UTC' } ),
caseItem.expected,
caseItem.message
);
} );
} );
QUnit.test( '#getTimestampParser', function ( assert ) {
var cases = require( '../cases/timestamp-parser.json' ),
parser = new Parser( require( '../data-en.json' ) );
cases.forEach( function ( caseItem ) {
var tsParser = parser.getTimestampParser( 'en', caseItem.format, caseItem.digits, 'UTC', { UTC: 'UTC' } ),
expectedDate = moment( caseItem.expected );
assert.true(
tsParser( caseItem.data ).date.isSame( expectedDate ),
caseItem.message
);
} );
} );
QUnit.test( '#getTimestampParser (at DST change)', function ( assert ) {
var cases = require( '../cases/timestamp-parser-dst.json' ),
parser = new Parser( require( '../data-en.json' ) );
cases.forEach( function ( caseItem ) {
var regexp = parser.getTimestampRegexp( 'en', caseItem.format, '\\d', caseItem.timezoneAbbrs ),
tsParser = parser.getTimestampParser( 'en', caseItem.format, null, caseItem.timezone, caseItem.timezoneAbbrs ),
date = tsParser( caseItem.sample.match( regexp ) ).date;
assert.true(
date.isSame( caseItem.expected ),
caseItem.message
);
assert.true(
date.isSame( caseItem.expectedUtc ),
caseItem.message
);
} );
} );
require( '../cases/comments.json' ).forEach( function ( caseItem ) {
var testName = '#getThreads (' + caseItem.name + ')';
QUnit.test( testName, function ( assert ) {
var dom = ve.createDocumentFromHtml( require( '../' + caseItem.dom ) ),
expected = require( caseItem.expected ),
config = require( caseItem.config ),
data = require( caseItem.data );
testUtils.overrideMwConfig( config );
var container = testUtils.getThreadContainer( dom );
var title = mw.Title.newFromText( caseItem.title );
var threadItemSet = new Parser( data ).parse( container, title );
var threads = threadItemSet.getThreads();
threads.forEach( function ( thread, i ) {
testUtils.serializeComments( thread, container );
assert.deepEqual(
JSON.parse( JSON.stringify( thread ) ),
expected[ i ],
caseItem.name + ' section ' + i
);
} );
// Uncomment this to get updated content for the JSON files, for copy/paste:
// console.log( JSON.stringify( threads, null, 2 ) );
} );
} );
// TODO:
// * findCommentsById
// * findCommentsByName
// * getThreadItems