Skip to content

Commit

Permalink
Merge branch 'master' into tests-no-useless-escape
Browse files Browse the repository at this point in the history
  • Loading branch information
NovemLinguae authored Dec 13, 2024
2 parents 2a399d0 + 2a38b76 commit a5cb048
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 50 deletions.
14 changes: 9 additions & 5 deletions tests/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
"env": {
"jest": true
},
"globals": {
"assert": "readonly",
"document": "readonly",
"jQuery": "readonly",
"Morebits": "readonly",
"mw": "readonly",
"Twinkle": "readonly",
"window": "readonly"
},
"root": true,
"rules": {
"array-bracket-spacing": "off",
"computed-property-spacing": "off",
"no-shadow": "off",
"object-curly-spacing": "off",
"space-in-parens": "off",

"camelcase": "warn",
"new-cap": "warn",
"no-undef": "warn",
"strict": "warn"
}
}
2 changes: 2 additions & 0 deletions tests/jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

// Tweak some mw.configs as needed by tests
mw.config.set({
wgPageName: 'Macbeth,_King_of_Scotland',
Expand Down
2 changes: 2 additions & 0 deletions tests/morebits.array.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

describe('Morebits.array', () => {
test('chunk', () => {
assert.deepEqual(Morebits.array.chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3), [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]], '10 over 3');
Expand Down
6 changes: 4 additions & 2 deletions tests/morebits.batchOperation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

describe('Morebits.batchOperation', () => {
test('Contruction', () => {
assert.true(new Morebits.batchOperation() instanceof Morebits.batchOperation, 'Correct instance');
assert.true(new Morebits.BatchOperation() instanceof Morebits.batchOperation, 'Correct instance');
});
const batch = new Morebits.batchOperation();
const batch = new Morebits.BatchOperation();
const pageList = ['Macbeth', 'Hamlet', 'Romeo and Juliet', 'Much Ado About Nothing', 'Tempest'];
batch.setPageList(pageList);
const chunkLength = 2;
Expand Down
2 changes: 2 additions & 0 deletions tests/morebits.createHtml.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

describe('Morebits.createHtml', () => {

test('createHtml', () => {
Expand Down
60 changes: 31 additions & 29 deletions tests/morebits.date.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
'use strict';

describe('Morebits.date', () => {
const now = Date.now();
const ts_mw = '16:26, 7 November 2020 (UTC)', ts_iso = '2020-11-07T16:26:00.000Z', naive = 20201107162600;
const timestampMediaWiki = '16:26, 7 November 2020 (UTC)', timestampIso = '2020-11-07T16:26:00.000Z', naive = 20201107162600;
test('Construction', () => {
// getTime and toISOString imply testing of inherited methods
// Allow off-by-one values in milliseconds for not-quite-simultaneous date contructions
expect(Math.abs(new Morebits.date().getTime() - Date.now())).toBeLessThanOrEqual(1);
expect(Math.abs(new Morebits.Date().getTime() - Date.now())).toBeLessThanOrEqual(1);

assert.strictEqual(new Morebits.date(now).getTime(), new Date(now).getTime(), 'Constructor from timestring');
assert.strictEqual(new Morebits.date(2020, 11, 7, 16, 26).getTime(), new Date(2020, 11, 7, 16, 26).getTime(), 'Constructor from parts');
assert.strictEqual(new Morebits.date(ts_mw).toISOString(), ts_iso, 'enWiki timestamp format');
assert.strictEqual(new Morebits.date(naive).toISOString(), ts_iso, 'MediaWiki 14-digit number');
assert.strictEqual(new Morebits.date(naive.toString()).toISOString(), ts_iso, 'MediaWiki 14-digit string');
assert.strictEqual(new Morebits.date(parseInt(naive / 10, 10)).toISOString(), new Date(parseInt(naive / 10, 10)).toISOString(), 'native 13 digit');
assert.strictEqual(new Morebits.date(naive * 10).toISOString(), new Date(naive * 10).toISOString(), 'native 15 digit');
assert.strictEqual(new Morebits.Date(now).getTime(), new Date(now).getTime(), 'Constructor from timestring');
assert.strictEqual(new Morebits.Date(2020, 11, 7, 16, 26).getTime(), new Date(2020, 11, 7, 16, 26).getTime(), 'Constructor from parts');
assert.strictEqual(new Morebits.Date(timestampMediaWiki).toISOString(), timestampIso, 'enWiki timestamp format');
assert.strictEqual(new Morebits.Date(naive).toISOString(), timestampIso, 'MediaWiki 14-digit number');
assert.strictEqual(new Morebits.Date(naive.toString()).toISOString(), timestampIso, 'MediaWiki 14-digit string');
assert.strictEqual(new Morebits.Date(parseInt(naive / 10, 10)).toISOString(), new Date(parseInt(naive / 10, 10)).toISOString(), 'native 13 digit');
assert.strictEqual(new Morebits.Date(naive * 10).toISOString(), new Date(naive * 10).toISOString(), 'native 15 digit');
});
const date = new Morebits.date(ts_mw);
const date = new Morebits.Date(timestampMediaWiki);
test('Methods', () => {
assert.true(date.isValid(), 'Valid');
// Logs a message; not a failure, but annoying
assert.false(new Morebits.date('no').isValid(), 'Invalid');
assert.false(new Morebits.Date('no').isValid(), 'Invalid');

// Ideally we would test the differences between UTC and non-UTC dates
assert.strictEqual(date.getUTCDayName(), 'Saturday', 'getUTCDayName');
assert.strictEqual(date.getUTCDayNameAbbrev(), 'Sat', 'getUTCDayNameAbbrev');
assert.strictEqual(date.getUTCMonthName(), 'November', 'getUTCMonthName');
assert.strictEqual(date.getUTCMonthNameAbbrev(), 'Nov', 'getUTCMonthNameAbbrev');

assert.true(new Morebits.date(now).isAfter(date), 'isAfter');
assert.true(new Morebits.Date(now).isAfter(date), 'isAfter');
assert.true(date.isBefore(new Date(now)), 'isBefore');
});
test('RegEx headers', () => {
Expand All @@ -41,25 +43,25 @@ describe('Morebits.date', () => {
assert.false(date.monthHeaderRegex().test('==December 2020=='), 'Wrong month');
});
test('add/subtract', () => {
assert.strictEqual(new Morebits.date(ts_mw).add(1, 'day').toISOString(), '2020-11-08T16:26:00.000Z', 'Add 1 day');
assert.strictEqual(new Morebits.date(ts_mw).add(1, 'DaY').toISOString(), '2020-11-08T16:26:00.000Z', 'Loudly add 1 day');
assert.strictEqual(new Morebits.date(ts_mw).add('1', 'day').toISOString(), '2020-11-08T16:26:00.000Z', "Add 1 day but it's a string");
assert.strictEqual(new Morebits.date(ts_mw).subtract(1, 'day').toISOString(), '2020-11-06T16:26:00.000Z', 'Subtract 1 day');
assert.strictEqual(new Morebits.date(ts_mw).add(2, 'weeks').toISOString(), '2020-11-21T16:26:00.000Z', 'Add 2 weeks');
assert.strictEqual(new Morebits.date(ts_mw).add(2, 'weeks').subtract(2, 'weeks').toISOString(), ts_iso, '2 weeks roundtrip'); // Note, this intentionally twice-crosses a US DST
assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'day').toISOString(), '2020-11-08T16:26:00.000Z', 'Add 1 day');
assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'DaY').toISOString(), '2020-11-08T16:26:00.000Z', 'Loudly add 1 day');
assert.strictEqual(new Morebits.Date(timestampMediaWiki).add('1', 'day').toISOString(), '2020-11-08T16:26:00.000Z', "Add 1 day but it's a string");
assert.strictEqual(new Morebits.Date(timestampMediaWiki).subtract(1, 'day').toISOString(), '2020-11-06T16:26:00.000Z', 'Subtract 1 day');
assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(2, 'weeks').toISOString(), '2020-11-21T16:26:00.000Z', 'Add 2 weeks');
assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(2, 'weeks').subtract(2, 'weeks').toISOString(), timestampIso, '2 weeks roundtrip'); // Note, this intentionally twice-crosses a US DST

assert.strictEqual(new Morebits.date(ts_mw).add(1, 'second').toISOString(), '2020-11-07T16:26:01.000Z', 'Add 1 second');
assert.strictEqual(new Morebits.date(ts_mw).add(1, 'minute').toISOString(), '2020-11-07T16:27:00.000Z', 'Add 1 minute');
assert.strictEqual(new Morebits.date(ts_mw).add(1, 'hour').toISOString(), '2020-11-07T17:26:00.000Z', 'Add 1 hour');
assert.strictEqual(new Morebits.date(ts_mw).add(1, 'month').toISOString(), '2020-12-07T16:26:00.000Z', 'Add 1 month');
assert.strictEqual(new Morebits.date(ts_mw).add(1, 'year').toISOString(), '2021-11-07T16:26:00.000Z', 'Add 1 year');
assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'second').toISOString(), '2020-11-07T16:26:01.000Z', 'Add 1 second');
assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'minute').toISOString(), '2020-11-07T16:27:00.000Z', 'Add 1 minute');
assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'hour').toISOString(), '2020-11-07T17:26:00.000Z', 'Add 1 hour');
assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'month').toISOString(), '2020-12-07T16:26:00.000Z', 'Add 1 month');
assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'year').toISOString(), '2021-11-07T16:26:00.000Z', 'Add 1 year');

assert.throws(() => new Morebits.date(ts_mw).add('forty-two'), 'throws: non-number provided');
assert.throws(() => new Morebits.date(ts_mw).add(1), 'throws: no unit');
assert.throws(() => new Morebits.date(ts_mw).subtract(1, 'dayo'), 'throws: bad unit');
assert.throws(() => new Morebits.Date(timestampMediaWiki).add('forty-two'), 'throws: non-number provided');
assert.throws(() => new Morebits.Date(timestampMediaWiki).add(1), 'throws: no unit');
assert.throws(() => new Morebits.Date(timestampMediaWiki).subtract(1, 'dayo'), 'throws: bad unit');
});
test('Formats', () => {
assert.strictEqual(new Morebits.date(now).format('YYYY-MM-DDTHH:mm:ss.SSSZ', 'utc'), new Date(now).toISOString(), 'ISO format');
assert.strictEqual(new Morebits.Date(now).format('YYYY-MM-DDTHH:mm:ss.SSSZ', 'utc'), new Date(now).toISOString(), 'ISO format');
assert.strictEqual(date.format('dddd D MMMM YY h:mA', 'utc'), 'Saturday 7 November 20 4:26PM', 'Some weirder stuff');
assert.strictEqual(date.format('MMt[h month], [d]a[y] D, h [o\'clock] A', 'utc'), '11th month, day 7, 4 o\'clock PM', 'Format escapes');
assert.strictEqual(date.format('dddd D MMMM YY h:mA', 600), 'Sunday 8 November 20 2:26AM', 'non-UTC formatting');
Expand All @@ -68,7 +70,7 @@ describe('Morebits.date', () => {
test('Calendar', () => {
assert.strictEqual(date.calendar('utc'), '2020-11-07', 'Old calendar');
assert.strictEqual(date.calendar(600), '2020-11-08', 'Old non-UTC');
assert.strictEqual(new Morebits.date(now).calendar('utc'), 'Today at ' + new Morebits.date(now).format('h:mm A', 'utc'), 'New calendar');
assert.strictEqual(new Morebits.date(now).subtract(1, 'day').calendar('utc'), 'Yesterday at ' + new Morebits.date(now).format('h:mm A', 'utc'), 'Close calendar');
assert.strictEqual(new Morebits.Date(now).calendar('utc'), 'Today at ' + new Morebits.Date(now).format('h:mm A', 'utc'), 'New calendar');
assert.strictEqual(new Morebits.Date(now).subtract(1, 'day').calendar('utc'), 'Yesterday at ' + new Morebits.Date(now).format('h:mm A', 'utc'), 'Close calendar');
});
});
2 changes: 2 additions & 0 deletions tests/morebits.ip.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

describe('Morebits.ip', () => {
test('sanitizeIPv6', () => {
assert.strictEqual(Morebits.ip.sanitizeIPv6('2001:0db8:0010:0000:0000:0000:0000:0001'), '2001:DB8:10:0:0:0:0:1', 'Shorten IPv6');
Expand Down
2 changes: 2 additions & 0 deletions tests/morebits.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

describe('constants', () => {
test('userIsSysop', () => {
assert.true(Morebits.userIsSysop, 'Is sysop');
Expand Down
6 changes: 4 additions & 2 deletions tests/morebits.quickForm.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

/**
* Simple helper to render a quickform element
*
* @param data
*/
function renderElement(data) {
return new Morebits.quickForm.element(data).render();
return new Morebits.QuickForm.Element(data).render();
}

describe('quickform', () => {
Expand Down Expand Up @@ -75,7 +77,7 @@ describe('quickform', () => {
};

const getRenderedForm = () => {
const form = new Morebits.quickForm(() => {});
const form = new Morebits.QuickForm(() => {});
form.append(inputConfig);
form.append(checkboxesConfig);
form.append(checkboxListConfig);
Expand Down
2 changes: 2 additions & 0 deletions tests/morebits.string.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

describe('Morebits.string', () => {
test('escapeRegExp', () => {
assert.strictEqual(Morebits.string.escapeRegExp('Four score and seven years ago'), 'Four[_ ]score[_ ]and[_ ]seven[_ ]years[_ ]ago', 'Spaces');
Expand Down
8 changes: 5 additions & 3 deletions tests/morebits.taskManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

describe('Morebits.taskManager', () => {
test('Contruction', () => {
const tm = new Morebits.taskManager();
const tm = new Morebits.TaskManager();
assert.true(tm instanceof Morebits.taskManager, 'Correct instance');
});

Expand All @@ -19,7 +21,7 @@ describe('Morebits.taskManager', () => {

test('Simple', () => {
generateFuncs();
const simple = new Morebits.taskManager();
const simple = new Morebits.TaskManager();
simple.add(data.one, []);
simple.add(data.two, [data.one]);
simple.add(data.three, [data.two]);
Expand All @@ -30,7 +32,7 @@ describe('Morebits.taskManager', () => {
});
test('Complex', () => {
generateFuncs();
const complex = new Morebits.taskManager();
const complex = new Morebits.TaskManager();
complex.add(data.one, [data.two]);
complex.add(data.two, [data.three, data.four]);
complex.add(data.three, []);
Expand Down
12 changes: 7 additions & 5 deletions tests/morebits.unbinder.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
'use strict';

describe('Morebits.unbinder', () => {
test('Construction', () => {
assert.throws(() => new Morebits.unbinder(), 'throws: no string');
assert.throws(() => new Morebits.unbinder([42]), 'throws: not a string');
const u = new Morebits.unbinder('Hello world');
assert.throws(() => new Morebits.Unbinder(), 'throws: no string');
assert.throws(() => new Morebits.Unbinder([42]), 'throws: not a string');
const u = new Morebits.Unbinder('Hello world');
assert.true(u instanceof Morebits.unbinder, 'Correct instance');
assert.throws(() => u.unbind(), 'throws: Missing prefix');
assert.throws(() => u.unbind('w'), 'throws: Missing postfix');
});
test('Run', () => {
let u = new Morebits.unbinder('Hello world <!-- world --> world');
let u = new Morebits.Unbinder('Hello world <!-- world --> world');
u.unbind('<!--', '-->');
u.content = u.content.replace(/world/g, 'earth');
assert.strictEqual(u.rebind(), 'Hello earth <!-- world --> earth', 'Simple replace');

u = new Morebits.unbinder('Hello world <!-- world --> world [link link] [[link|link]]');
u = new Morebits.Unbinder('Hello world <!-- world --> world [link link] [[link|link]]');
assert.true(u instanceof Morebits.unbinder, 'Correct instance');
u.unbind('<!--', '-->');
u.unbind('\\[\\[', '\\]\\]');
Expand Down
10 changes: 6 additions & 4 deletions tests/morebits.wikitext.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

describe('Morebits.wikitext', () => {
test('parseTemplate', () => {
// Function to help build a template from a sample object
Expand Down Expand Up @@ -81,13 +83,13 @@ describe('Morebits.wikitext', () => {

test('Morebits.wikitext.page', () => {
const text = '{{short description}}{{about}}[[File:Fee.svg]]O, [[Juliet|she]] doth {{plural|teach}} the torches to burn bright!';
const page = new Morebits.wikitext.page(text);
const page = new Morebits.wikitext.Page(text);
assert.true(page instanceof Morebits.wikitext.page, 'Correct instance');
assert.strictEqual(page.getText(), text, 'Got text');

// Throws
assert.throws(() => new Morebits.wikitext.page(text).insertAfterTemplates(), 'throws: no tag');
assert.throws(() => new Morebits.wikitext.page(text).insertAfterTemplates('tag'), 'throws: no regex');
assert.throws(() => new Morebits.wikitext.Page(text).insertAfterTemplates(), 'throws: no tag');
assert.throws(() => new Morebits.wikitext.Page(text).insertAfterTemplates('tag'), 'throws: no regex');

// Define all the tests individually, with the appropriate method,
// input, expected output, and (spreaded) parameters.
Expand Down Expand Up @@ -354,7 +356,7 @@ describe('Morebits.wikitext', () => {
];

tests.forEach((test) => {
const page = new Morebits.wikitext.page(test.input);
const page = new Morebits.wikitext.Page(test.input);
assert.strictEqual(page[test.method](...test.params).getText(), test.expected, test.method + ' - ' + test.name);
});
});
Expand Down
2 changes: 2 additions & 0 deletions tests/twinkleblock.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

describe('modules/twinkleblock', () => {
describe('combineFormDataAndFieldTemplateOptions', () => {
// https://github.com/wikimedia-gadgets/twinkle/issues/2106
Expand Down
2 changes: 2 additions & 0 deletions tests/twinkletag.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

describe('modules/twinkletag', () => {
describe('checkIncompatible', () => {
test('no conflicts, 0 tags to check', () => {
Expand Down
2 changes: 2 additions & 0 deletions tests/twinklewarn.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

describe('modules/twinklewarn', () => {
describe('getTemplateProperty', () => {
const templates = {
Expand Down
2 changes: 2 additions & 0 deletions tests/twinklexfd.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

describe('modules/twinklexfd', () => {
describe('insertRMTR', () => {
test('0 rows, 1 line breaks', () => {
Expand Down

0 comments on commit a5cb048

Please sign in to comment.