[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var has = require('has');
|
---|
| 4 | var inspect = require('object-inspect');
|
---|
| 5 |
|
---|
| 6 | var getRegexLiteral = function (stringRegex) {
|
---|
| 7 | try {
|
---|
| 8 | /* jshint evil: true */
|
---|
| 9 | /* eslint-disable no-new-func */
|
---|
| 10 | return Function('return ' + stringRegex + ';')();
|
---|
| 11 | /* eslint-enable no-new-func */
|
---|
| 12 | /* jshint evil: false */
|
---|
| 13 | } catch (e) { /**/ }
|
---|
| 14 | return null;
|
---|
| 15 | };
|
---|
| 16 |
|
---|
| 17 | module.exports = function runTests(flags, t) {
|
---|
| 18 | t.equal(flags(/a/g), 'g', 'flags(/a/g) !== "g"');
|
---|
| 19 | t.equal(flags(/a/gmi), 'gim', 'flags(/a/gmi) !== "gim"');
|
---|
| 20 | t.equal(flags(new RegExp('a', 'gmi')), 'gim', 'flags(new RegExp("a", "gmi")) !== "gim"');
|
---|
| 21 | t.equal(flags(/a/), '', 'flags(/a/) !== ""');
|
---|
| 22 | t.equal(flags(new RegExp('a')), '', 'flags(new RegExp("a")) !== ""');
|
---|
| 23 |
|
---|
| 24 | t.test('sticky flag', { skip: !has(RegExp.prototype, 'sticky') }, function (st) {
|
---|
| 25 | st.equal(flags(getRegexLiteral('/a/y')), 'y', 'flags(/a/y) !== "y"');
|
---|
| 26 | st.equal(flags(new RegExp('a', 'y')), 'y', 'flags(new RegExp("a", "y")) !== "y"');
|
---|
| 27 | st.end();
|
---|
| 28 | });
|
---|
| 29 |
|
---|
| 30 | t.test('unicode flag', { skip: !has(RegExp.prototype, 'unicode') }, function (st) {
|
---|
| 31 | st.equal(flags(getRegexLiteral('/a/u')), 'u', 'flags(/a/u) !== "u"');
|
---|
| 32 | st.equal(flags(new RegExp('a', 'u')), 'u', 'flags(new RegExp("a", "u")) !== "u"');
|
---|
| 33 | st.end();
|
---|
| 34 | });
|
---|
| 35 |
|
---|
| 36 | t.test('dotAll flag', { skip: !has(RegExp.prototype, 'dotAll') }, function (st) {
|
---|
| 37 | st.equal(flags(getRegexLiteral('/a/s')), 's', 'flags(/a/s) !== "s"');
|
---|
| 38 | st.equal(flags(new RegExp('a', 's')), 's', 'flags(new RegExp("a", "s")) !== "s"');
|
---|
| 39 | st.end();
|
---|
| 40 | });
|
---|
| 41 |
|
---|
| 42 | t.test('sorting', function (st) {
|
---|
| 43 | st.equal(flags(/a/gim), 'gim', 'flags(/a/gim) !== "gim"');
|
---|
| 44 | st.equal(flags(/a/mig), 'gim', 'flags(/a/mig) !== "gim"');
|
---|
| 45 | st.equal(flags(/a/mgi), 'gim', 'flags(/a/mgi) !== "gim"');
|
---|
| 46 | if (has(RegExp.prototype, 'sticky')) {
|
---|
| 47 | st.equal(flags(getRegexLiteral('/a/gyim')), 'gimy', 'flags(/a/gyim) !== "gimy"');
|
---|
| 48 | }
|
---|
| 49 | if (has(RegExp.prototype, 'unicode')) {
|
---|
| 50 | st.equal(flags(getRegexLiteral('/a/ugmi')), 'gimu', 'flags(/a/ugmi) !== "gimu"');
|
---|
| 51 | }
|
---|
| 52 | if (has(RegExp.prototype, 'dotAll')) {
|
---|
| 53 | st.equal(flags(getRegexLiteral('/a/sgmi')), 'gims', 'flags(/a/sgmi) !== "gims"');
|
---|
| 54 | }
|
---|
| 55 | st.end();
|
---|
| 56 | });
|
---|
| 57 |
|
---|
| 58 | t.test('basic examples', function (st) {
|
---|
| 59 | st.equal(flags(/a/g), 'g', '(/a/g).flags !== "g"');
|
---|
| 60 | st.equal(flags(/a/gmi), 'gim', '(/a/gmi).flags !== "gim"');
|
---|
| 61 | st.equal(flags(new RegExp('a', 'gmi')), 'gim', 'new RegExp("a", "gmi").flags !== "gim"');
|
---|
| 62 | st.equal(flags(/a/), '', '(/a/).flags !== ""');
|
---|
| 63 | st.equal(flags(new RegExp('a')), '', 'new RegExp("a").flags !== ""');
|
---|
| 64 |
|
---|
| 65 | st.end();
|
---|
| 66 | });
|
---|
| 67 |
|
---|
| 68 | t.test('generic flags', function (st) {
|
---|
| 69 | st.equal(flags({}), '');
|
---|
| 70 | st.equal(flags({ ignoreCase: true }), 'i');
|
---|
| 71 | st.equal(flags({ dotAll: 1, global: 0, sticky: 1, unicode: 1 }), 'suy');
|
---|
| 72 | st.equal(flags({ __proto__: { multiline: true } }), 'm');
|
---|
| 73 |
|
---|
| 74 | st.end();
|
---|
| 75 | });
|
---|
| 76 |
|
---|
| 77 | t.test('throws properly', function (st) {
|
---|
| 78 | var nonObjects = ['', false, true, 42, NaN, null, undefined];
|
---|
| 79 | st.plan(nonObjects.length);
|
---|
| 80 | var throwsOnNonObject = function (nonObject) {
|
---|
| 81 | st['throws'](flags.bind(null, nonObject), TypeError, inspect(nonObject) + ' is not an Object');
|
---|
| 82 | };
|
---|
| 83 | nonObjects.forEach(throwsOnNonObject);
|
---|
| 84 | });
|
---|
| 85 | };
|
---|