| 1 |
'use strict'; |
| 2 |
|
| 3 |
var hasToStringTag = require('has-tostringtag/shams')(); |
| •4 |
var forEach = require('for-each'); |
| 5 |
var test = require('tape'); |
| 6 |
var isRegex = require('..'); |
| 7 |
|
| 8 |
test('not regexes', function (t) { |
| 9 |
t.notOk(isRegex(), 'undefined is not regex'); |
| 10 |
t.notOk(isRegex(null), 'null is not regex'); |
| 11 |
t.notOk(isRegex(false), 'false is not regex'); |
| 12 |
t.notOk(isRegex(true), 'true is not regex'); |
| 13 |
t.notOk(isRegex(42), 'number is not regex'); |
| 14 |
t.notOk(isRegex('foo'), 'string is not regex'); |
| 15 |
t.notOk(isRegex([]), 'array is not regex'); |
| 16 |
t.notOk(isRegex({}), 'object is not regex'); |
| 17 |
t.notOk(isRegex(function () {}), 'function is not regex'); |
| 18 |
t.end(); |
| 19 |
}); |
| 20 |
|
| 21 |
test('@@toStringTag', { skip: !hasToStringTag }, function (t) { |
| 22 |
var regex = /a/g; |
| 23 |
var fakeRegex = { |
| 24 |
toString: function () { return String(regex); }, |
| 25 |
valueOf: function () { return regex; } |
| 26 |
}; |
| 27 |
fakeRegex[Symbol.toStringTag] = 'RegExp'; |
| 28 |
t.notOk(isRegex(fakeRegex), 'fake RegExp with @@toStringTag "RegExp" is not regex'); |
| 29 |
t.end(); |
| 30 |
}); |
| 31 |
|
| 32 |
test('regexes', function (t) { |
| 33 |
t.ok(isRegex(/a/g), 'regex literal is regex'); |
| 34 |
t.ok(isRegex(new RegExp('a', 'g')), 'regex object is regex'); |
| 35 |
t.end(); |
| 36 |
}); |
| 37 |
|
| 38 |
test('does not mutate regexes', function (t) { |
| 39 |
t.test('lastIndex is a marker object', function (st) { |
| 40 |
var regex = /a/; |
| 41 |
var marker = {}; |
| 42 |
regex.lastIndex = marker; |
| 43 |
st.equal(regex.lastIndex, marker, 'lastIndex is the marker object'); |
| 44 |
st.ok(isRegex(regex), 'is regex'); |
| 45 |
st.equal(regex.lastIndex, marker, 'lastIndex is the marker object after isRegex'); |
| 46 |
st.end(); |
| 47 |
}); |
| 48 |
|
| 49 |
t.test('lastIndex is nonzero', function (st) { |
| 50 |
var regex = /a/; |
| 51 |
regex.lastIndex = 3; |
| 52 |
st.equal(regex.lastIndex, 3, 'lastIndex is 3'); |
| 53 |
st.ok(isRegex(regex), 'is regex'); |
| 54 |
st.equal(regex.lastIndex, 3, 'lastIndex is 3 after isRegex'); |
| 55 |
st.end(); |
| 56 |
}); |
| 57 |
|
| 58 |
t.end(); |
| 59 |
}); |
| 60 |
|
| 61 |
test('does not perform operations observable to Proxies', { skip: typeof Proxy !== 'function' }, function (t) { |
| 62 |
var Handler = function () { |
| 63 |
this.trapCalls = []; |
| 64 |
}; |
| 65 |
|
| 66 |
forEach([ |
| 67 |
'defineProperty', |
| 68 |
'deleteProperty', |
| 69 |
'get', |
| 70 |
'getOwnPropertyDescriptor', |
| 71 |
'getPrototypeOf', |
| 72 |
'has', |
| 73 |
'isExtensible', |
| 74 |
'ownKeys', |
| 75 |
'preventExtensions', |
| 76 |
'set', |
| 77 |
'setPrototypeOf' |
| 78 |
], function (trapName) { |
| 79 |
Handler.prototype[trapName] = function () { |
| 80 |
this.trapCalls.push(trapName); |
| 81 |
return Reflect[trapName].apply(Reflect, arguments); |
| 82 |
}; |
| 83 |
}); |
| 84 |
|
| 85 |
t.test('proxy of object', function (st) { |
| 86 |
var handler = new Handler(); |
| 87 |
var proxy = new Proxy({ lastIndex: 0 }, handler); |
| 88 |
|
| 89 |
st.equal(isRegex(proxy), false, 'proxy of plain object is not regex'); |
| 90 |
st.deepEqual(handler.trapCalls, ['getOwnPropertyDescriptor'], 'no unexpected proxy traps were triggered'); |
| 91 |
st.end(); |
| 92 |
}); |
| 93 |
|
| 94 |
t.test('proxy of RegExp instance', function (st) { |
| 95 |
var handler = new Handler(); |
| 96 |
var proxy = new Proxy(/a/, handler); |
| 97 |
|
| 98 |
st.equal(isRegex(proxy), false, 'proxy of RegExp instance is not regex'); |
| 99 |
st.deepEqual(handler.trapCalls, ['getOwnPropertyDescriptor'], 'no unexpected proxy traps were triggered'); |
| 100 |
st.end(); |
| 101 |
}); |
| 102 |
|
| 103 |
t.end(); |
| 104 |
}); |