is.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. var assert = require('assert');
  22. var util = require('../../');
  23. suite('is');
  24. test('util.isArray', function () {
  25. assert.equal(true, util.isArray([]));
  26. assert.equal(true, util.isArray(Array()));
  27. assert.equal(true, util.isArray(new Array()));
  28. assert.equal(true, util.isArray(new Array(5)));
  29. assert.equal(true, util.isArray(new Array('with', 'some', 'entries')));
  30. assert.equal(false, util.isArray({}));
  31. assert.equal(false, util.isArray({ push: function() {} }));
  32. assert.equal(false, util.isArray(/regexp/));
  33. assert.equal(false, util.isArray(new Error()));
  34. assert.equal(false, util.isArray(Object.create(Array.prototype)));
  35. });
  36. test('util.isRegExp', function () {
  37. assert.equal(true, util.isRegExp(/regexp/));
  38. assert.equal(true, util.isRegExp(RegExp()));
  39. assert.equal(true, util.isRegExp(new RegExp()));
  40. assert.equal(false, util.isRegExp({}));
  41. assert.equal(false, util.isRegExp([]));
  42. assert.equal(false, util.isRegExp(new Date()));
  43. assert.equal(false, util.isRegExp(Object.create(RegExp.prototype)));
  44. });
  45. test('util.isDate', function () {
  46. assert.equal(true, util.isDate(new Date()));
  47. assert.equal(true, util.isDate(new Date(0)));
  48. assert.equal(false, util.isDate(Date()));
  49. assert.equal(false, util.isDate({}));
  50. assert.equal(false, util.isDate([]));
  51. assert.equal(false, util.isDate(new Error()));
  52. assert.equal(false, util.isDate(Object.create(Date.prototype)));
  53. });
  54. test('util.isError', function () {
  55. assert.equal(true, util.isError(new Error()));
  56. assert.equal(true, util.isError(new TypeError()));
  57. assert.equal(true, util.isError(new SyntaxError()));
  58. assert.equal(false, util.isError({}));
  59. assert.equal(false, util.isError({ name: 'Error', message: '' }));
  60. assert.equal(false, util.isError([]));
  61. assert.equal(true, util.isError(Object.create(Error.prototype)));
  62. });
  63. test('util._extend', function () {
  64. assert.deepEqual(util._extend({a:1}), {a:1});
  65. assert.deepEqual(util._extend({a:1}, []), {a:1});
  66. assert.deepEqual(util._extend({a:1}, null), {a:1});
  67. assert.deepEqual(util._extend({a:1}, true), {a:1});
  68. assert.deepEqual(util._extend({a:1}, false), {a:1});
  69. assert.deepEqual(util._extend({a:1}, {b:2}), {a:1, b:2});
  70. assert.deepEqual(util._extend({a:1, b:2}, {b:3}), {a:1, b:3});
  71. });
  72. test('util.isBuffer', function () {
  73. assert.equal(true, util.isBuffer(new Buffer(4)));
  74. assert.equal(true, util.isBuffer(Buffer(4)));
  75. assert.equal(true, util.isBuffer(new Buffer(4)));
  76. assert.equal(true, util.isBuffer(new Buffer([1, 2, 3, 4])));
  77. assert.equal(false, util.isBuffer({}));
  78. assert.equal(false, util.isBuffer([]));
  79. assert.equal(false, util.isBuffer(new Error()));
  80. assert.equal(false, util.isRegExp(new Date()));
  81. assert.equal(true, util.isBuffer(Object.create(Buffer.prototype)));
  82. });