test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. var test = require('tape');
  3. var isRegex = require('./');
  4. var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
  5. test('not regexes', function (t) {
  6. t.notOk(isRegex(), 'undefined is not regex');
  7. t.notOk(isRegex(null), 'null is not regex');
  8. t.notOk(isRegex(false), 'false is not regex');
  9. t.notOk(isRegex(true), 'true is not regex');
  10. t.notOk(isRegex(42), 'number is not regex');
  11. t.notOk(isRegex('foo'), 'string is not regex');
  12. t.notOk(isRegex([]), 'array is not regex');
  13. t.notOk(isRegex({}), 'object is not regex');
  14. t.notOk(isRegex(function () {}), 'function is not regex');
  15. t.end();
  16. });
  17. test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
  18. var regex = /a/g;
  19. var fakeRegex = {
  20. toString: function () { return String(regex); },
  21. valueOf: function () { return regex; }
  22. };
  23. fakeRegex[Symbol.toStringTag] = 'RegExp';
  24. t.notOk(isRegex(fakeRegex), 'fake RegExp with @@toStringTag "RegExp" is not regex');
  25. t.end();
  26. });
  27. test('regexes', function (t) {
  28. t.ok(isRegex(/a/g), 'regex literal is regex');
  29. t.ok(isRegex(new RegExp('a', 'g')), 'regex object is regex');
  30. t.end();
  31. });
  32. test('does not mutate regexes', function (t) {
  33. t.test('lastIndex is a marker object', function (st) {
  34. var regex = /a/;
  35. var marker = {};
  36. regex.lastIndex = marker;
  37. st.equal(regex.lastIndex, marker, 'lastIndex is the marker object');
  38. st.ok(isRegex(regex), 'is regex');
  39. st.equal(regex.lastIndex, marker, 'lastIndex is the marker object after isRegex');
  40. st.end();
  41. });
  42. t.test('lastIndex is nonzero', function (st) {
  43. var regex = /a/;
  44. regex.lastIndex = 3;
  45. st.equal(regex.lastIndex, 3, 'lastIndex is 3');
  46. st.ok(isRegex(regex), 'is regex');
  47. st.equal(regex.lastIndex, 3, 'lastIndex is 3 after isRegex');
  48. st.end();
  49. });
  50. t.end();
  51. });