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