index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /* globals window, document, HTMLElement */
  2. 'use strict';
  3. var test = require('tape');
  4. var is = require('../index.js');
  5. var forEach = require('foreach');
  6. var toStr = Object.prototype.toString;
  7. test('is.type', function (t) {
  8. var booleans = [true, false];
  9. forEach(booleans, function (boolean) {
  10. t.ok(is.type(boolean, 'boolean'), '"' + boolean + '" is a boolean');
  11. });
  12. var numbers = [1, 0 / 1, 0 / -1, NaN, Infinity, -Infinity];
  13. forEach(numbers, function (number) {
  14. t.ok(is.type(number, 'number'), '"' + number + '" is a number');
  15. });
  16. var objects = [{}, null, new Date()];
  17. forEach(objects, function (object) {
  18. t.ok(is.type(object, 'object'), '"' + object + '" is an object');
  19. });
  20. var strings = ['', 'abc'];
  21. forEach(strings, function (string) {
  22. t.ok(is.type(string, 'string'), '"' + string + '" is a string');
  23. });
  24. t.ok(is.type(undefined, 'undefined'), 'undefined is undefined');
  25. t.end();
  26. });
  27. test('is.undef', function (t) {
  28. t.ok(is.undef(), 'absent undefined is undefined');
  29. t.ok(is.undef(undefined), 'literal undefined is undefined');
  30. t.notOk(is.undef(null), 'null is not undefined');
  31. t.notOk(is.undef({}), 'object is not undefined');
  32. t.end();
  33. });
  34. test('is.defined', function (t) {
  35. t.notOk(is.defined(), 'undefined is not defined');
  36. t.ok(is.defined(null), 'null is defined');
  37. t.ok(is.defined({}), 'object is defined');
  38. t.end();
  39. });
  40. test('is.empty', function (t) {
  41. t.ok(is.empty(''), 'empty string is empty');
  42. t.ok(is.empty(Object('')), 'empty String object is empty');
  43. t.ok(is.empty([]), 'empty array is empty');
  44. t.ok(is.empty({}), 'empty object is empty');
  45. t.ok(is.empty(null), 'null is empty');
  46. t.ok(is.empty(), 'undefined is empty');
  47. t.ok(is.empty(undefined), 'undefined is empty');
  48. t.ok(is.empty(false), 'false is empty');
  49. t.ok(is.empty(0), '0 is empty');
  50. t.ok(is.empty(NaN), 'nan is empty');
  51. (function () {
  52. t.ok(is.empty(arguments), 'empty arguments is empty');
  53. }());
  54. t.notOk(is.empty({ a: 1 }), 'nonempty object is not empty');
  55. t.notOk(is.empty(true), 'true is not empty');
  56. t.notOk(is.empty(/a/g), 'regex is not empty');
  57. t.notOk(is.empty(new Date()), 'date is not empty');
  58. t.end();
  59. });
  60. test('is.equal', function (t) {
  61. t.test('primitives', function (pt) {
  62. var primitives = [true, false, undefined, null, '', 'foo', 0, Infinity, -Infinity];
  63. pt.plan(primitives.length);
  64. for (var i = 0; i < primitives.length; ++i) {
  65. pt.ok(is.equal(primitives[i], primitives[i]), 'primitives are equal to themselves: ' + primitives[i]);
  66. }
  67. pt.end();
  68. });
  69. t.test('arrays', function (at) {
  70. at.ok(is.equal([1, 2, 3], [1, 2, 3]), 'arrays are shallowly equal');
  71. at.ok(is.equal([1, 2, [3, 4]], [1, 2, [3, 4]]), 'arrays are deep equal');
  72. at.notOk(is.equal([1, 2, 3], [5, 2, 3]), 'inequal arrays are not equal');
  73. at.notOk(is.equal([1, 2], [2, 3]), 'inequal arrays are not equal');
  74. at.notOk(is.equal([1, 2, 3], [2, 3]), 'inequal length arrays are not equal');
  75. at.ok(is.equal([], []), 'empty arrays are equal');
  76. var arr = [1, 2];
  77. at.ok(is.equal(arr, arr), 'array is equal to itself');
  78. at.end();
  79. });
  80. t.test('dates', function (dt) {
  81. dt.plan(2);
  82. var now = new Date();
  83. dt.ok(is.equal(now, new Date(now.getTime())), 'two equal date objects are equal');
  84. setTimeout(function () {
  85. dt.notOk(is.equal(now, new Date()), 'two inequal date objects are not equal');
  86. dt.end();
  87. }, 10);
  88. });
  89. t.test('plain objects', function (ot) {
  90. ot.ok(is.equal({ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 }), 'objects are shallowly equal');
  91. ot.ok(is.equal({ a: { b: 1 } }, { a: { b: 1 } }), 'objects are deep equal');
  92. ot.notOk(is.equal({ a: 1 }, { a: 2 }), 'inequal objects are not equal');
  93. ot.end();
  94. });
  95. t.test('object instances', function (ot) {
  96. var F = function F() {
  97. this.foo = 'bar';
  98. };
  99. F.prototype = {};
  100. var G = function G() {
  101. this.foo = 'bar';
  102. };
  103. var f = new F();
  104. var g = new G();
  105. ot.ok(is.equal(f, f), 'the same object instances are equal');
  106. ot.ok(is.equal(f, new F()), 'two object instances are equal when the prototype and props are the same');
  107. ot.ok(is.equal(f, new G()), 'two object instances are equal when the prototype is not the same, but props are');
  108. g.bar = 'baz';
  109. ot.notOk(is.equal(f, g), 'object instances are not equal when the prototype and props are not the same');
  110. ot.notOk(is.equal(g, f), 'object instances are not equal when the prototype and props are not the same');
  111. ot.end();
  112. });
  113. t.test('functions', function (ft) {
  114. var F = function () {};
  115. F.prototype = {};
  116. var G = function () {};
  117. G.prototype = new Date();
  118. ft.notEqual(F.prototype, G.prototype, 'F and G have different prototypes');
  119. ft.notOk(is.equal(F, G), 'two functions are not equal when the prototype is not the same');
  120. var H = function () {};
  121. H.prototype = F.prototype;
  122. ft.equal(F.prototype, H.prototype, 'F and H have the same prototype');
  123. ft.ok(is.equal(F, H), 'two functions are equal when the prototype is the same');
  124. ft.end();
  125. });
  126. t.end();
  127. });
  128. test('is.hosted', function (t) {
  129. t.ok(is.hosted('a', { a: {} }), 'object is hosted');
  130. t.ok(is.hosted('a', { a: [] }), 'array is hosted');
  131. t.ok(is.hosted('a', { a: function () {} }), 'function is hosted');
  132. t.notOk(is.hosted('a', { a: true }), 'boolean value is not hosted');
  133. t.notOk(is.hosted('a', { a: false }), 'boolean value is not hosted');
  134. t.notOk(is.hosted('a', { a: 3 }), 'number value is not hosted');
  135. t.notOk(is.hosted('a', { a: undefined }), 'undefined value is not hosted');
  136. t.notOk(is.hosted('a', { a: 'abc' }), 'string value is not hosted');
  137. t.notOk(is.hosted('a', { a: null }), 'null value is not hosted');
  138. t.end();
  139. });
  140. test('is.instance', function (t) {
  141. t.ok(is.instance(new Date(), Date), 'new Date is instanceof Date');
  142. var F = function () {};
  143. t.ok(is.instance(new F(), F), 'new constructor is instanceof constructor');
  144. t.end();
  145. });
  146. test('is.nil', function (t) {
  147. var isNull = is.nil;
  148. t.equal(isNull, is['null'], 'is.nil is the same as is.null');
  149. t.ok(isNull(null), 'null is null');
  150. t.notOk(isNull(undefined), 'undefined is not null');
  151. t.notOk(isNull({}), 'object is not null');
  152. t.end();
  153. });
  154. test('is.args', function (t) {
  155. t.notOk(is.args([]), 'array is not arguments');
  156. (function () {
  157. t.ok(is.args(arguments), 'arguments is arguments');
  158. }());
  159. (function () {
  160. t.notOk(is.args(Array.prototype.slice.call(arguments)), 'sliced arguments is not arguments');
  161. }());
  162. var fakeOldArguments = {
  163. callee: function () {},
  164. length: 3
  165. };
  166. t.ok(is.args(fakeOldArguments), 'old-style arguments object is arguments');
  167. t.end();
  168. });
  169. test('is.args.empty', function (t) {
  170. t.notOk(is.args.empty([]), 'empty array is not empty arguments');
  171. (function () {
  172. t.ok(is.args.empty(arguments), 'empty arguments is empty arguments');
  173. }());
  174. (function () {
  175. t.notOk(is.args.empty(Array.prototype.slice.call(arguments)), 'empty sliced arguments is not empty arguments');
  176. }());
  177. t.end();
  178. });
  179. test('is.array', function (t) {
  180. t.ok(is.array([]), 'array is array');
  181. (function () {
  182. t.ok(is.array(Array.prototype.slice.call(arguments)), 'sliced arguments is array');
  183. }());
  184. t.end();
  185. });
  186. test('is.array.empty', function (t) {
  187. t.ok(is.array.empty([]), 'empty array is empty array');
  188. (function () {
  189. t.notOk(is.array.empty(arguments), 'empty arguments is not empty array');
  190. }());
  191. (function () {
  192. t.ok(is.array.empty(Array.prototype.slice.call(arguments)), 'empty sliced arguments is empty array');
  193. }());
  194. t.end();
  195. });
  196. test('is.isarraylike', function (t) {
  197. t.notOk(is.arraylike(), 'undefined is not array-like');
  198. t.notOk(is.arraylike(null), 'null is not array-like');
  199. t.notOk(is.arraylike(false), 'false is not array-like');
  200. t.notOk(is.arraylike(true), 'true is not array-like');
  201. t.ok(is.arraylike({ length: 0 }), 'object with zero length is array-like');
  202. t.ok(is.arraylike({ length: 1 }), 'object with positive length is array-like');
  203. t.notOk(is.arraylike({ length: -1 }), 'object with negative length is not array-like');
  204. t.notOk(is.arraylike({ length: NaN }), 'object with NaN length is not array-like');
  205. t.notOk(is.arraylike({ length: 'foo' }), 'object with string length is not array-like');
  206. t.notOk(is.arraylike({ length: '' }), 'object with empty string length is not array-like');
  207. t.ok(is.arraylike([]), 'array is array-like');
  208. (function () {
  209. t.ok(is.arraylike(arguments), 'empty arguments is array-like');
  210. }());
  211. (function () {
  212. t.ok(is.arraylike(arguments), 'nonempty arguments is array-like');
  213. }(1, 2, 3));
  214. t.end();
  215. });
  216. test('is.bool', function (t) {
  217. t.ok(is.bool(true), 'literal true is a boolean');
  218. t.ok(is.bool(false), 'literal false is a boolean');
  219. t.ok(is.bool(Object(true)), 'object true is a boolean');
  220. t.ok(is.bool(Object(false)), 'object false is a boolean');
  221. t.notOk(is.bool(), 'undefined is not a boolean');
  222. t.notOk(is.bool(null), 'null is not a boolean');
  223. t.end();
  224. });
  225. test('is.false', function (t) {
  226. var isFalse = is['false'];
  227. t.ok(isFalse(false), 'false is false');
  228. t.ok(isFalse(Object(false)), 'object false is false');
  229. t.notOk(isFalse(true), 'true is not false');
  230. t.notOk(isFalse(), 'undefined is not false');
  231. t.notOk(isFalse(null), 'null is not false');
  232. t.notOk(isFalse(''), 'empty string is not false');
  233. t.end();
  234. });
  235. test('is.true', function (t) {
  236. var isTrue = is['true'];
  237. t.ok(isTrue(true), 'true is true');
  238. t.ok(isTrue(Object(true)), 'object true is true');
  239. t.notOk(isTrue(false), 'false is not true');
  240. t.notOk(isTrue(), 'undefined is not true');
  241. t.notOk(isTrue(null), 'null is not true');
  242. t.notOk(isTrue(''), 'empty string is not true');
  243. t.end();
  244. });
  245. test('is.date', function (t) {
  246. t.ok(is.date(new Date()), 'new Date is date');
  247. t.notOk(is.date(), 'undefined is not date');
  248. t.notOk(is.date(null), 'null is not date');
  249. t.notOk(is.date(''), 'empty string is not date');
  250. var nowTS = (new Date()).getTime();
  251. t.notOk(is.date(nowTS), 'timestamp is not date');
  252. var F = function () {};
  253. F.prototype = new Date();
  254. t.notOk(is.date(new F()), 'Date subtype is not date');
  255. t.end();
  256. });
  257. test('is.date.valid', function (t) {
  258. t.ok(is.date.valid(new Date()), 'new Date() is a valid date');
  259. t.notOk(is.date.valid(new Date('')), 'new Date("") is not a valid date');
  260. t.end();
  261. });
  262. test('is.element', function (t) {
  263. t.notOk(is.element(), 'undefined is not element');
  264. t.test('when HTMLElement exists', { skip: typeof HTMLElement === 'undefined' }, function (st) {
  265. var element = document.createElement('div');
  266. st.ok(is.element(element), 'HTMLElement is element');
  267. st.notOk(is.element({ nodeType: 1 }), 'object with nodeType is not element');
  268. st.end();
  269. });
  270. t.end();
  271. });
  272. test('is.error', function (t) {
  273. var err = new Error('foo');
  274. t.ok(is.error(err), 'Error is error');
  275. t.notOk(is.error({}), 'object is not error');
  276. var objWithErrorToString = {
  277. toString: function () {
  278. return '[object Error]';
  279. }
  280. };
  281. t.equal(String(objWithErrorToString), toStr.call(new Error()), 'obj has Error\'s toString');
  282. t.notOk(is.error(objWithErrorToString), 'object with Error\'s toString is not error');
  283. t.end();
  284. });
  285. test('is.fn', function (t) {
  286. t.equal(is['function'], is.fn, 'alias works');
  287. t.ok(is.fn(function () {}), 'function is function');
  288. if (typeof window !== 'undefined') {
  289. // in IE7/8, typeof alert === 'object'
  290. t.ok(is.fn(window.alert), 'window.alert is function');
  291. }
  292. t.notOk(is.fn({}), 'object is not function');
  293. t.notOk(is.fn(null), 'null is not function');
  294. t.end();
  295. });
  296. test('is.number', function (t) {
  297. t.ok(is.number(0), 'positive zero is number');
  298. t.ok(is.number(0 / -1), 'negative zero is number');
  299. t.ok(is.number(3), 'three is number');
  300. t.ok(is.number(NaN), 'NaN is number');
  301. t.ok(is.number(Infinity), 'infinity is number');
  302. t.ok(is.number(-Infinity), 'negative infinity is number');
  303. t.ok(is.number(Object(42)), 'object number is number');
  304. t.notOk(is.number(), 'undefined is not number');
  305. t.notOk(is.number(null), 'null is not number');
  306. t.notOk(is.number(true), 'true is not number');
  307. t.end();
  308. });
  309. test('is.infinite', function (t) {
  310. t.ok(is.infinite(Infinity), 'positive infinity is infinite');
  311. t.ok(is.infinite(-Infinity), 'negative infinity is infinite');
  312. t.notOk(is.infinite(NaN), 'NaN is not infinite');
  313. t.notOk(is.infinite(0), 'a number is not infinite');
  314. t.end();
  315. });
  316. test('is.decimal', function (t) {
  317. t.ok(is.decimal(1.1), 'decimal is decimal');
  318. t.notOk(is.decimal(0), 'zero is not decimal');
  319. t.notOk(is.decimal(1), 'integer is not decimal');
  320. t.notOk(is.decimal(NaN), 'NaN is not decimal');
  321. t.notOk(is.decimal(Infinity), 'Infinity is not decimal');
  322. t.end();
  323. });
  324. test('is.divisibleBy', function (t) {
  325. t.ok(is.divisibleBy(4, 2), '4 is divisible by 2');
  326. t.ok(is.divisibleBy(4, 2), '4 is divisible by 2');
  327. t.ok(is.divisibleBy(0, 1), '0 is divisible by 1');
  328. t.ok(is.divisibleBy(Infinity, 1), 'infinity is divisible by anything');
  329. t.ok(is.divisibleBy(1, Infinity), 'anything is divisible by infinity');
  330. t.ok(is.divisibleBy(Infinity, Infinity), 'infinity is divisible by infinity');
  331. t.notOk(is.divisibleBy(1, 0), '1 is not divisible by 0');
  332. t.notOk(is.divisibleBy(NaN, 1), 'NaN is not divisible by 1');
  333. t.notOk(is.divisibleBy(1, NaN), '1 is not divisible by NaN');
  334. t.notOk(is.divisibleBy(NaN, NaN), 'NaN is not divisible by NaN');
  335. t.notOk(is.divisibleBy(1, 3), '1 is not divisible by 3');
  336. t.end();
  337. });
  338. test('is.integer', function (t) {
  339. t.ok(is.integer(0), '0 is integer');
  340. t.ok(is.integer(3), '3 is integer');
  341. t.notOk(is.integer(1.1), '1.1 is not integer');
  342. t.notOk(is.integer(NaN), 'NaN is not integer');
  343. t.notOk(is.integer(Infinity), 'infinity is not integer');
  344. t.notOk(is.integer(null), 'null is not integer');
  345. t.notOk(is.integer(), 'undefined is not integer');
  346. t.end();
  347. });
  348. test('is.maximum', function (t) {
  349. t.ok(is.maximum(3, [3, 2, 1]), '3 is maximum of [3,2,1]');
  350. t.ok(is.maximum(3, [1, 2, 3]), '3 is maximum of [1,2,3]');
  351. t.ok(is.maximum(4, [1, 2, 3]), '4 is maximum of [1,2,3]');
  352. t.ok(is.maximum('c', ['a', 'b', 'c']), 'c is maximum of [a,b,c]');
  353. t.notOk(is.maximum(2, [1, 2, 3]), '2 is not maximum of [1,2,3]');
  354. var nanError = new TypeError('NaN is not a valid value');
  355. t['throws'](function () { return is.maximum(NaN); }, nanError, 'throws when first value is NaN');
  356. var error = new TypeError('second argument must be array-like');
  357. t['throws'](function () { return is.maximum(2, null); }, error, 'throws when second value is not array-like');
  358. t['throws'](function () { return is.maximum(2, {}); }, error, 'throws when second value is not array-like');
  359. t.end();
  360. });
  361. test('is.minimum', function (t) {
  362. t.ok(is.minimum(1, [1, 2, 3]), '1 is minimum of [1,2,3]');
  363. t.ok(is.minimum(0, [1, 2, 3]), '0 is minimum of [1,2,3]');
  364. t.ok(is.minimum('a', ['a', 'b', 'c']), 'a is minimum of [a,b,c]');
  365. t.notOk(is.minimum(2, [1, 2, 3]), '2 is not minimum of [1,2,3]');
  366. var nanError = new TypeError('NaN is not a valid value');
  367. t['throws'](function () { return is.minimum(NaN); }, nanError, 'throws when first value is NaN');
  368. var error = new TypeError('second argument must be array-like');
  369. t['throws'](function () { return is.minimum(2, null); }, error, 'throws when second value is not array-like');
  370. t['throws'](function () { return is.minimum(2, {}); }, error, 'throws when second value is not array-like');
  371. t.end();
  372. });
  373. test('is.nan', function (t) {
  374. t.ok(is.nan(NaN), 'NaN is not a number');
  375. t.ok(is.nan('abc'), 'string is not a number');
  376. t.ok(is.nan(true), 'boolean is not a number');
  377. t.ok(is.nan({}), 'object is not a number');
  378. t.ok(is.nan([]), 'array is not a number');
  379. t.ok(is.nan(function () {}), 'function is not a number');
  380. t.notOk(is.nan(0), 'zero is a number');
  381. t.notOk(is.nan(3), 'three is a number');
  382. t.notOk(is.nan(1.1), '1.1 is a number');
  383. t.notOk(is.nan(Infinity), 'infinity is a number');
  384. t.end();
  385. });
  386. test('is.even', function (t) {
  387. t.ok(is.even(0), 'zero is even');
  388. t.ok(is.even(2), 'two is even');
  389. t.ok(is.even(Infinity), 'infinity is even');
  390. t.notOk(is.even(1), '1 is not even');
  391. t.notOk(is.even(), 'undefined is not even');
  392. t.notOk(is.even(null), 'null is not even');
  393. t.notOk(is.even(NaN), 'NaN is not even');
  394. t.end();
  395. });
  396. test('is.odd', function (t) {
  397. t.ok(is.odd(1), 'zero is odd');
  398. t.ok(is.odd(3), 'two is odd');
  399. t.ok(is.odd(Infinity), 'infinity is odd');
  400. t.notOk(is.odd(0), '0 is not odd');
  401. t.notOk(is.odd(2), '2 is not odd');
  402. t.notOk(is.odd(), 'undefined is not odd');
  403. t.notOk(is.odd(null), 'null is not odd');
  404. t.notOk(is.odd(NaN), 'NaN is not odd');
  405. t.end();
  406. });
  407. test('is.ge', function (t) {
  408. t.ok(is.ge(3, 2), '3 is greater than 2');
  409. t.notOk(is.ge(2, 3), '2 is not greater than 3');
  410. t.ok(is.ge(3, 3), '3 is greater than or equal to 3');
  411. t.ok(is.ge('abc', 'a'), 'abc is greater than a');
  412. t.ok(is.ge('abc', 'abc'), 'abc is greater than or equal to abc');
  413. t.notOk(is.ge('a', 'abc'), 'a is not greater than abc');
  414. t.notOk(is.ge(Infinity, 0), 'infinity is not greater than anything');
  415. t.notOk(is.ge(0, Infinity), 'anything is not greater than infinity');
  416. var error = new TypeError('NaN is not a valid value');
  417. t['throws'](function () { return is.ge(NaN, 2); }, error, 'throws when first value is NaN');
  418. t['throws'](function () { return is.ge(2, NaN); }, error, 'throws when second value is NaN');
  419. t.end();
  420. });
  421. test('is.gt', function (t) {
  422. t.ok(is.gt(3, 2), '3 is greater than 2');
  423. t.notOk(is.gt(2, 3), '2 is not greater than 3');
  424. t.notOk(is.gt(3, 3), '3 is not greater than 3');
  425. t.ok(is.gt('abc', 'a'), 'abc is greater than a');
  426. t.notOk(is.gt('abc', 'abc'), 'abc is not greater than abc');
  427. t.notOk(is.gt('a', 'abc'), 'a is not greater than abc');
  428. t.notOk(is.gt(Infinity, 0), 'infinity is not greater than anything');
  429. t.notOk(is.gt(0, Infinity), 'anything is not greater than infinity');
  430. var error = new TypeError('NaN is not a valid value');
  431. t['throws'](function () { return is.gt(NaN, 2); }, error, 'throws when first value is NaN');
  432. t['throws'](function () { return is.gt(2, NaN); }, error, 'throws when second value is NaN');
  433. t.end();
  434. });
  435. test('is.le', function (t) {
  436. t.ok(is.le(2, 3), '2 is lesser than or equal to 3');
  437. t.notOk(is.le(3, 2), '3 is not lesser than or equal to 2');
  438. t.ok(is.le(3, 3), '3 is lesser than or equal to 3');
  439. t.ok(is.le('a', 'abc'), 'a is lesser than or equal to abc');
  440. t.ok(is.le('abc', 'abc'), 'abc is lesser than or equal to abc');
  441. t.notOk(is.le('abc', 'a'), 'abc is not lesser than or equal to a');
  442. t.notOk(is.le(Infinity, 0), 'infinity is not lesser than or equal to anything');
  443. t.notOk(is.le(0, Infinity), 'anything is not lesser than or equal to infinity');
  444. var error = new TypeError('NaN is not a valid value');
  445. t['throws'](function () { return is.le(NaN, 2); }, error, 'throws when first value is NaN');
  446. t['throws'](function () { return is.le(2, NaN); }, error, 'throws when second value is NaN');
  447. t.end();
  448. });
  449. test('is.lt', function (t) {
  450. t.ok(is.lt(2, 3), '2 is lesser than 3');
  451. t.notOk(is.lt(3, 2), '3 is not lesser than 2');
  452. t.notOk(is.lt(3, 3), '3 is not lesser than 3');
  453. t.ok(is.lt('a', 'abc'), 'a is lesser than abc');
  454. t.notOk(is.lt('abc', 'abc'), 'abc is not lesser than abc');
  455. t.notOk(is.lt('abc', 'a'), 'abc is not lesser than a');
  456. t.notOk(is.lt(Infinity, 0), 'infinity is not lesser than anything');
  457. t.notOk(is.lt(0, Infinity), 'anything is not lesser than infinity');
  458. var error = new TypeError('NaN is not a valid value');
  459. t['throws'](function () { return is.lt(NaN, 2); }, error, 'throws when first value is NaN');
  460. t['throws'](function () { return is.lt(2, NaN); }, error, 'throws when second value is NaN');
  461. t.end();
  462. });
  463. test('is.within', function (t) {
  464. t.test('throws on NaN', function (st) {
  465. var nanError = new TypeError('NaN is not a valid value');
  466. st['throws'](function () { return is.within(NaN, 0, 0); }, nanError, 'throws when first value is NaN');
  467. st['throws'](function () { return is.within(0, NaN, 0); }, nanError, 'throws when second value is NaN');
  468. st['throws'](function () { return is.within(0, 0, NaN); }, nanError, 'throws when third value is NaN');
  469. st.end();
  470. });
  471. t.test('throws on non-number', function (st) {
  472. var error = new TypeError('all arguments must be numbers');
  473. st['throws'](function () { return is.within('', 0, 0); }, error, 'throws when first value is string');
  474. st['throws'](function () { return is.within(0, '', 0); }, error, 'throws when second value is string');
  475. st['throws'](function () { return is.within(0, 0, ''); }, error, 'throws when third value is string');
  476. st['throws'](function () { return is.within({}, 0, 0); }, error, 'throws when first value is object');
  477. st['throws'](function () { return is.within(0, {}, 0); }, error, 'throws when second value is object');
  478. st['throws'](function () { return is.within(0, 0, {}); }, error, 'throws when third value is object');
  479. st['throws'](function () { return is.within(null, 0, 0); }, error, 'throws when first value is null');
  480. st['throws'](function () { return is.within(0, null, 0); }, error, 'throws when second value is null');
  481. st['throws'](function () { return is.within(0, 0, null); }, error, 'throws when third value is null');
  482. st['throws'](function () { return is.within(undefined, 0, 0); }, error, 'throws when first value is undefined');
  483. st['throws'](function () { return is.within(0, undefined, 0); }, error, 'throws when second value is undefined');
  484. st['throws'](function () { return is.within(0, 0, undefined); }, error, 'throws when third value is undefined');
  485. st.end();
  486. });
  487. t.ok(is.within(2, 1, 3), '2 is between 1 and 3');
  488. t.ok(is.within(0, -1, 1), '0 is between -1 and 1');
  489. t.ok(is.within(2, 0, Infinity), 'infinity always returns true');
  490. t.ok(is.within(2, Infinity, 2), 'infinity always returns true');
  491. t.ok(is.within(Infinity, 0, 1), 'infinity always returns true');
  492. t.notOk(is.within(2, -1, -1), '2 is not between -1 and 1');
  493. t.end();
  494. });
  495. test('is.object', function (t) {
  496. t.ok(is.object({}), 'object literal is object');
  497. t.notOk(is.object(), 'undefined is not an object');
  498. t.notOk(is.object(null), 'null is not an object');
  499. t.notOk(is.object(true), 'true is not an object');
  500. t.notOk(is.object(''), 'string is not an object');
  501. t.notOk(is.object(NaN), 'NaN is not an object');
  502. t.notOk(is.object(Object), 'object constructor is not an object');
  503. t.notOk(is.object(function () {}), 'function is not an object');
  504. t.test('Symbols', { skip: typeof Symbol !== 'function' }, function (st) {
  505. st.notOk(is.object(Symbol('foo')), 'symbol is not an object');
  506. st.end();
  507. });
  508. t.end();
  509. });
  510. test('is.primitive', function (t) {
  511. t.notOk(is.primitive({}), 'object literal is not a primitive');
  512. t.notOk(is.primitive([]), 'array is not a primitive');
  513. t.ok(is.primitive(), 'undefined is a primitive');
  514. t.ok(is.primitive(null), 'null is a primitive');
  515. t.ok(is.primitive(true), 'true is a primitive');
  516. t.ok(is.primitive(''), 'string is a primitive');
  517. t.ok(is.primitive(NaN), 'NaN is a primitive');
  518. t.notOk(is.primitive(Object), 'object constructor is not a primitive');
  519. t.notOk(is.primitive(function () {}), 'function is not a primitive');
  520. t.test('Symbols', { skip: typeof Symbol !== 'function' }, function (st) {
  521. st.ok(is.primitive(Symbol('foo')), 'symbol is a primitive');
  522. st.end();
  523. });
  524. t.end();
  525. });
  526. test('is.hash', function (t) {
  527. t.ok(is.hash({}), 'empty object literal is hash');
  528. t.ok(is.hash({ 1: 2, a: 'b' }), 'object literal is hash');
  529. t.notOk(is.hash(), 'undefined is not a hash');
  530. t.notOk(is.hash(null), 'null is not a hash');
  531. t.notOk(is.hash(new Date()), 'date is not a hash');
  532. t.notOk(is.hash(Object('')), 'string object is not a hash');
  533. t.notOk(is.hash(''), 'string literal is not a hash');
  534. t.notOk(is.hash(Object(0)), 'number object is not a hash');
  535. t.notOk(is.hash(1), 'number literal is not a hash');
  536. t.notOk(is.hash(true), 'true is not a hash');
  537. t.notOk(is.hash(false), 'false is not a hash');
  538. t.notOk(is.hash(Object(false)), 'boolean obj is not hash');
  539. t.notOk(is.hash(false), 'literal false is not hash');
  540. t.notOk(is.hash(true), 'literal true is not hash');
  541. t.test('commonJS environment', { skip: typeof module === 'undefined' }, function (st) {
  542. st.ok(is.hash(module.exports), 'module.exports is a hash');
  543. st.end();
  544. });
  545. t.test('browser stuff', { skip: typeof window === 'undefined' }, function (st) {
  546. st.notOk(is.hash(window), 'window is not a hash');
  547. st.notOk(is.hash(document.createElement('div')), 'element is not a hash');
  548. st.end();
  549. });
  550. t.test('node stuff', { skip: typeof process === 'undefined' }, function (st) {
  551. st.notOk(is.hash(global), 'global is not a hash');
  552. st.notOk(is.hash(process), 'process is not a hash');
  553. st.end();
  554. });
  555. t.end();
  556. });
  557. test('is.regexp', function (t) {
  558. t.ok(is.regexp(/a/g), 'regex literal is regex');
  559. t.ok(is.regexp(new RegExp('a', 'g')), 'regex object is regex');
  560. t.notOk(is.regexp(), 'undefined is not regex');
  561. t.notOk(is.regexp(function () {}), 'function is not regex');
  562. t.notOk(is.regexp('/a/g'), 'string regex is not regex');
  563. t.end();
  564. });
  565. test('is.string', function (t) {
  566. t.ok(is.string('foo'), 'string literal is string');
  567. t.ok(is.string(Object('foo')), 'string object is string');
  568. t.notOk(is.string(), 'undefined is not string');
  569. t.notOk(is.string(String), 'string constructor is not string');
  570. var F = function () {};
  571. F.prototype = Object('');
  572. t.notOk(is.string(F), 'string subtype is not string');
  573. t.end();
  574. });
  575. test('is.base64', function (t) {
  576. t.ok(is.base64('wxyzWXYZ/+=='), 'string is base64 encoded');
  577. t.ok(is.base64(''), 'zero length string is base64 encoded');
  578. t.notOk(is.base64('wxyzWXYZ123/+=='), 'string length not a multiple of four is not base64 encoded');
  579. t.notOk(is.base64('wxyzWXYZ1234|]=='), 'string with invalid characters is not base64 encoded');
  580. t.notOk(is.base64('wxyzWXYZ1234==/+'), 'string with = not at end is not base64 encoded');
  581. t.notOk(is.base64('wxyzWXYZ1234/==='), 'string ending with === is not base64 encoded');
  582. t.end();
  583. });
  584. test('is.hex', function (t) {
  585. t.ok(is.hex('abcdABCD1234'), 'string is hex encoded');
  586. t.ok(is.hex(''), 'zero length string is hex encoded');
  587. t.notOk(is.hex('wxyzWXYZ1234/+=='), 'string with invalid characters is not hex encoded');
  588. t.end();
  589. });
  590. test('is.symbol', function (t) {
  591. t.test('not symbols', function (st) {
  592. var notSymbols = [true, false, null, undefined, {}, [], function () {}, 42, NaN, Infinity, /a/g, '', 0, -0, new Error('error')];
  593. forEach(notSymbols, function (notSymbol) {
  594. st.notOk(is.symbol(notSymbol), notSymbol + ' is not symbol');
  595. });
  596. st.end();
  597. });
  598. t.test('symbols', { skip: typeof Symbol !== 'function' }, function (st) {
  599. st.ok(is.symbol(Symbol('foo')), 'Symbol("foo") is symbol');
  600. var notKnownSymbols = ['length', 'name', 'arguments', 'caller', 'prototype', 'for', 'keyFor'];
  601. var symbolKeys = Object.getOwnPropertyNames(Symbol).filter(function (name) {
  602. return notKnownSymbols.indexOf(name) < 0;
  603. });
  604. forEach(symbolKeys, function (symbolKey) {
  605. st.ok(is.symbol(Symbol[symbolKey]), symbolKey + ' is symbol');
  606. });
  607. st.end();
  608. });
  609. t.end();
  610. });