Content deleted Content added
Pathoschild (talk | contribs) + warning about false positives with date tokens |
Pathoschild (talk | contribs) add unit tests for most custom regex patterns |
||
Line 621:
}
return text;
},
/**
* Run JavaScript unit tests and print the results to the console.
*/
runUnitTests: function() {
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.util.js', { dataType:'script', cache:true }).then(function() {
pathoschild.util.RunTests(function() {
var expect = chai.expect;
describe('regex_to_string', function() {
var regex_to_string = ohc.dateutil.regex_to_string;
it('converts a simple /./ pattern', function() {
expect(regex_to_string(/./))
.to.equal('.');
});
it('converts a simple /.+/ pattern', function() {
expect(regex_to_string(/.+/))
.to.equal('.+');
});
it('converts a simple /\\/.+/ pattern with an escaped forward slash', function() {
expect(regex_to_string(/\/.+/))
.to.equal('\\/.+');
});
it('converts a simple /\\/.+/ig pattern with an escaped forward slash and regex modifiers', function() {
expect(regex_to_string(/\/.+/ig))
.to.equal('\\/.+');
});
});
describe('regex', function() {
var regex = ohc.dateutil.regex;
describe('special tokens', function() {
// literal @
it('/@@/ matches a literal @', function() {
expect(regex('2014-01-01 @ @@', /@@/, 'X'))
.to.equal('2014-01-01 X XX');
});
// ordinal suffix
it('/@th/ matches valid ordinal suffixes (st, nd, rd, th)', function() {
expect(regex('st nd rd th', /\b@th\b/, 'X'))
.to.equal('X X X X');
});
it('/@th/ ignores invalid ordinal suffixes', function() {
expect(regex('first second third fourth raster 1 14', /\b@th\b/, 'X'))
.to.equal('first second third fourth raster 1 14');
});
});
describe('grouping day tokens', function() {
// day without leading zero
it('@SD (day without leading zero) captures valid values', function() {
expect(regex('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31', /\b@SD\b/, '[@SD]'))
.to.equal('[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28] [29] [30] [31]');
});
it('@SD (day without leading zero) ignores invalid values', function() {
expect(regex('0 01 010 2001 1st first', /\b@SD\b/, '[@SD]'))
.to.equal('0 01 010 2001 1st first');
});
// day with leading zero
it('@ZD (day with leading zero) captures valid values', function() {
expect(regex('01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31', /\b@ZD\b/, '[@ZD]'))
.to.equal('[01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28] [29] [30] [31]');
});
it('@ZD (day with leading zero) ignores invalid values', function() {
expect(regex('0 00 1 2 3 4 5 6 7 8 9 100 101 001 1st first', /\b@ZD\b/, '[@ZD]'))
.to.equal('0 00 1 2 3 4 5 6 7 8 9 100 101 001 1st first');
});
// day with optional leading zero
it('@DD (day with optional leading zero) captures valid values without leading zero', function() {
expect(regex('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31', /\b@DD\b/, '[@DD]'))
.to.equal('[01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28] [29] [30] [31]');
});
it('@DD (day with optional leading zero) captures valid values with leading zero', function() {
expect(regex('01 02 03 04 05 06 07 08 09', /\b@DD\b/, '[@DD]'))
.to.equal('[01] [02] [03] [04] [05] [06] [07] [08] [09]');
});
it('@DD (day with optional leading zero) ignores invalid values', function() {
expect(regex('0 00 010 100 101 001 1st first', /\b@DD\b/, '[@DD]'))
.to.equal('0 00 010 100 101 001 1st first');
});
// day with optional leading zero and optional ordinal suffix
it('@Day (day with optional leading zero and optional ordinal suffix) captures valid values without leading zero or ordinal', function() {
expect(regex('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31', /\b@Day\b/, '[@Day]'))
.to.equal('[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28] [29] [30] [31]');
});
it('@Day (day with optional leading zero and optional ordinal suffix) captures valid values without leading zero, but with ordinal', function() {
expect(regex('1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st', /\b@Day\b/, '[@Day]'))
.to.equal('[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28] [29] [30] [31]');
});
});
describe('non-grouping day tokens', function() {
// day without leading zero
it('@sd (day without leading zero) matches valid values', function() {
expect(regex('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31', /\b@sd\b/, 'X'))
.to.equal('X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X');
});
it('@sd (day without leading zero) ignores invalid values', function() {
expect(regex('0 01 010 2001 1st first', /\b@sd\b/, 'X'))
.to.equal('0 01 010 2001 1st first');
});
// day with leading zero
it('@zd (day with leading zero) matches valid values', function() {
expect(regex('01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31', /\b@zd\b/, 'X'))
.to.equal('X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X');
});
it('@zd (day with leading zero) ignores invalid values', function() {
expect(regex('0 00 1 2 3 4 5 6 7 8 9 100 101 001 1st first', /\b@zd\b/, 'X'))
.to.equal('0 00 1 2 3 4 5 6 7 8 9 100 101 001 1st first');
});
// day with optional leading zero
it('@dd (day with optional leading zero) matches valid values without leading zero', function() {
expect(regex('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31', /\b@dd\b/, 'X'))
.to.equal('X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X');
});
it('@dd (day with optional leading zero) matches valid values with leading zero', function() {
expect(regex('01 02 03 04 05 06 07 08 09', /\b@dd\b/, 'X'))
.to.equal('X X X X X X X X X');
});
it('@dd (day with optional leading zero) ignores invalid values', function() {
expect(regex('0 00 010 100 101 001 1st first', /\b@dd\b/, 'X'))
.to.equal('0 00 010 100 101 001 1st first');
});
// day with optional leading zero and optional ordinal suffix
it('@day (day with optional leading zero and optional ordinal suffix) matches valid values without leading zero or ordinal', function() {
expect(regex('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31', /\b@day\b/, 'X'))
.to.equal('X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X');
});
it('@day (day with optional leading zero and optional ordinal suffix) matches valid values without leading zero, but with ordinal', function() {
expect(regex('1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st', /\b@day\b/, 'X'))
.to.equal('X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X');
});
});
describe('grouping month tokens', function() {
// month without leading zero
it('@SM (month without leading zero) captures valid values', function() {
expect(regex('1 2 3 4 5 6 7 8 9 10 11 12', /\b@SM\b/, '[@SM]'))
.to.equal('[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]');
});
it('@SM (month without leading zero) ignores invalid values', function() {
expect(regex('0 01 02 03 04 05 06 07 08 09 010 13 21 2001', /\b@SM\b/, '[@SM]'))
.to.equal('0 01 02 03 04 05 06 07 08 09 010 13 21 2001');
});
// month with leading zero
it('@ZM (month with leading zero) captures valid values', function() {
expect(regex('01 02 03 04 05 06 07 08 09 10 11 12', /\b@ZM\b/, '[@ZM]'))
.to.equal('[01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12]');
});
it('@ZM (month with leading zero) ignores invalid values', function() {
expect(regex('0 1 2 3 4 5 6 7 8 9 010 13 21 2001', /\b@ZM\b/, '[@ZM]'))
.to.equal('0 1 2 3 4 5 6 7 8 9 010 13 21 2001');
});
// month with optional leading zero
it('@MM (month with optional leading zero) captures valid values without leading zero', function() {
expect(regex('1 2 3 4 5 6 7 8 9 10 11 12', /\b@MM\b/, '[@MM]'))
.to.equal('[01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12]');
});
it('@MM (month with optional leading zero) captures valid values with leading zero', function() {
expect(regex('01 02 03 04 05 06 07 08 09', /\b@MM\b/, '[@MM]'))
.to.equal('[01] [02] [03] [04] [05] [06] [07] [08] [09]');
});
it('@MM (month with optional leading zero) ignores invalid values', function() {
expect(regex('0 00 010 13 14 15 16 17 18 19 20 100 101 001', /\b@MM\b/, '[@MM]'))
.to.equal('0 00 010 13 14 15 16 17 18 19 20 100 101 001');
});
// full month name
it('@FullMonth (full month name) captures valid values', function() {
expect(regex('January February March April May June July August September October November December', /\b@FullMonth\b/, '[@FullMonth]'))
.to.equal('[January] [February] [March] [April] [May] [June] [July] [August] [September] [October] [November] [December]');
});
it('@FullMonth (full month name) ignores invalid values', function() {
expect(regex('0 1 2 3 4 5 6 7 8 9 10 11 12 jan Jan feb Feb mar Mar apr Apr jun Jun jul Jul aug Aug sep Sep oct Oct nov Nov dec Dec test', /\b@FullMonth\b/, '[@FullMonth]'))
.to.equal('0 1 2 3 4 5 6 7 8 9 10 11 12 jan Jan feb Feb mar Mar apr Apr jun Jun jul Jul aug Aug sep Sep oct Oct nov Nov dec Dec test');
});
// short month name
it('@Mon (short month name) captures valid values', function() {
expect(regex('Jan Jan. Feb Feb. Mar Mar. Apr Apr. May Jun Jun. Jul Jul. Aug Aug. Sep Sep. Oct Oct. Nov Nov. Dec Dec. ', /\b@Mon /, '[@Mon] '))
.to.equal('[Jan] [Jan] [Feb] [Feb] [Mar] [Mar] [Apr] [Apr] [May] [Jun] [Jun] [Jul] [Jul] [Aug] [Aug] [Sep] [Sep] [Oct] [Oct] [Nov] [Nov] [Dec] [Dec] ');
});
it('@Mon (short month name) ignores invalid values', function() {
expect(regex('0 1 2 3 4 5 6 7 8 9 10 11 12 january January february February march March april April june June july July august August september September october October november November december December test', /\b@Mon\b/, '[@Mon]'))
.to.equal('0 1 2 3 4 5 6 7 8 9 10 11 12 january January february February march March april April june June july July august August september September october October november November december December test');
});
// full or short name
it('@Month (full or short month name) captures valid long values', function() {
expect(regex('January February March April May June July August September October November December', /\b@Month\b/, '[@Month]'))
.to.equal('[January] [February] [March] [April] [May] [June] [July] [August] [September] [October] [November] [December]');
});
it('@Month (full or short month name) captures valid short values', function() {
expect(regex('Jan Jan. Feb Feb. Mar Mar. Apr Apr. May Jun Jun. Jul Jul. Aug Aug. Sep Sep. Oct Oct. Nov Nov. Dec Dec. ', /\b@Month /, '[@Month] '))
.to.equal('[January] [January] [February] [February] [March] [March] [April] [April] [May] [June] [June] [July] [July] [August] [August] [September] [September] [October] [October] [November] [November] [December] [December] ');
});
});
describe('non-grouping month tokens', function() {
// month without leading zero
it('@sm (month without leading zero) matches valid values', function() {
expect(regex('1 2 3 4 5 6 7 8 9 10 11 12', /\b@sm\b/, '[@sm]'))
.to.equal('[@sm] [@sm] [@sm] [@sm] [@sm] [@sm] [@sm] [@sm] [@sm] [@sm] [@sm] [@sm]');
});
it('@sm (month without leading zero) ignores invalid values', function() {
expect(regex('0 01 02 03 04 05 06 07 08 09 010 13 21 2001', /\b@sm\b/, 'X'))
.to.equal('0 01 02 03 04 05 06 07 08 09 010 13 21 2001');
});
// month with leading zero
it('@zm (month with leading zero) matches valid values', function() {
expect(regex('01 02 03 04 05 06 07 08 09 10 11 12', /\b@zm\b/, '[@zm]'))
.to.equal('[@zm] [@zm] [@zm] [@zm] [@zm] [@zm] [@zm] [@zm] [@zm] [@zm] [@zm] [@zm]');
});
it('@zm (month with leading zero) ignores invalid values', function() {
expect(regex('0 1 2 3 4 5 6 7 8 9 010 13 21 2001', /\b@zm\b/, '[@zm]'))
.to.equal('0 1 2 3 4 5 6 7 8 9 010 13 21 2001');
});
// month with optional leading zero
it('@mm (month with optional leading zero) matches valid values without leading zero', function() {
expect(regex('1 2 3 4 5 6 7 8 9 10 11 12', /\b@mm\b/, '[@mm]'))
.to.equal('[@mm] [@mm] [@mm] [@mm] [@mm] [@mm] [@mm] [@mm] [@mm] [@mm] [@mm] [@mm]');
});
it('@mm (month with optional leading zero) matches valid values with leading zero', function() {
expect(regex('01 02 03 04 05 06 07 08 09', /\b@mm\b/, '[@mm]'))
.to.equal('[@mm] [@mm] [@mm] [@mm] [@mm] [@mm] [@mm] [@mm] [@mm]');
});
it('@mm (month with optional leading zero) ignores invalid values', function() {
expect(regex('0 00 010 13 14 15 16 17 18 19 20 100 101 001', /\b@mm\b/, '[@mm]'))
.to.equal('0 00 010 13 14 15 16 17 18 19 20 100 101 001');
});
// full month name
it('@FullMonth (full month name) matches valid values', function() {
expect(regex('January February March April May June July August September October November December', /\b@fullmonth\b/, '[@fullmonth]'))
.to.equal('[@fullmonth] [@fullmonth] [@fullmonth] [@fullmonth] [@fullmonth] [@fullmonth] [@fullmonth] [@fullmonth] [@fullmonth] [@fullmonth] [@fullmonth] [@fullmonth]');
});
it('@fullmonth (full month name) ignores invalid values', function() {
expect(regex('0 1 2 3 4 5 6 7 8 9 10 11 12 jan Jan feb Feb mar Mar apr Apr jun Jun jul Jul aug Aug sep Sep oct Oct nov Nov dec Dec test', /\b@fullmonth\b/, '[@fullmonth]'))
.to.equal('0 1 2 3 4 5 6 7 8 9 10 11 12 jan Jan feb Feb mar Mar apr Apr jun Jun jul Jul aug Aug sep Sep oct Oct nov Nov dec Dec test');
});
// short month name
it('@mon (short month name) matches valid values', function() {
expect(regex('Jan Jan. Feb Feb. Mar Mar. Apr Apr. May Jun Jun. Jul Jul. Aug Aug. Sep Sep. Oct Oct. Nov Nov. Dec Dec. ', /\b@mon /, '[@mon] '))
.to.equal('[@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] [@mon] ');
});
it('@mon (short month name) ignores invalid values', function() {
expect(regex('0 1 2 3 4 5 6 7 8 9 10 11 12 january January february February march March april April june June july July august August september September october October november November december December test', /\b@mon\b/, '[@mon]'))
.to.equal('0 1 2 3 4 5 6 7 8 9 10 11 12 january January february February march March april April june June july July august August september September october October november November december December test');
});
// full or short name
it('@month (full or short month name) matches valid long values', function() {
expect(regex('January February March April May June July August September October November December', /\b@month\b/, '[@month]'))
.to.equal('[@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month]');
});
it('@month (full or short month name) matches valid short values', function() {
expect(regex('Jan Jan. Feb Feb. Mar Mar. Apr Apr. May Jun Jun. Jul Jul. Aug Aug. Sep Sep. Oct Oct. Nov Nov. Dec Dec. ', /\b@month /, '[@month] '))
.to.equal('[@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] [@month] ');
});
});
});
});
});
}
};
|