/test/should-node.js

https://github.com/mansoor-s/Should-Node · JavaScript · 805 lines · 615 code · 185 blank · 5 comment · 2 complexity · 8c48f9166373c85029b897127366e5f8 MD5 · raw file

  1. var assert = require('assert'),
  2. vows = require('vows'),
  3. util = require('util');
  4. require('../index.js').setAlias('must');
  5. var no_errors = [
  6. function checkInstantiation() {
  7. var obj = new Object;
  8. var could = obj.must;
  9. could.must.be.an.instanceof(Object);
  10. },
  11. function shouldNotBeNull() {
  12. this.should.not.be.null({});
  13. },
  14. function shouldBeNull() {
  15. this.should.be.null(null);
  16. },
  17. function shouldBeATypeof() {
  18. var obj = [];
  19. obj.should.be.a.typeof('array');
  20. },
  21. function shouldNotBeATypeof() {
  22. var obj = 'hello';
  23. obj.should.not.be.a.typeof('object');
  24. },
  25. function shouldBeAnObject() {
  26. var obj = {};
  27. obj.should.be.an.object;
  28. },
  29. function shouldBeAObject() {
  30. var obj = new Object;
  31. obj.should.be.a.object;
  32. },
  33. function shouldNotBeAnObject() {
  34. var obj = "hello";
  35. obj.should.not.be.an.object;
  36. },
  37. function shouldNotBeAObject() {
  38. var obj = "world";
  39. obj.should.not.be.a.object;
  40. },
  41. function shouldBeAFunction() {
  42. var func = function() {};
  43. func.should.be.a.function;
  44. },
  45. function shouldNotBeAFunction() {
  46. var func = 'robot';
  47. func.should.not.be.a.function;
  48. },
  49. function shouldBeAString() {
  50. var string = 'theory';
  51. string.should.be.a.string;
  52. },
  53. function shouldNotBeAString() {
  54. var string = 123;
  55. string.should.not.be.a.string;
  56. },
  57. function shouldBeANumber() {
  58. var num = 567;
  59. num.should.be.a.number;
  60. },
  61. function shouldNotBeANumber() {
  62. var str = 'orange';
  63. str.should.not.be.a.number;
  64. },
  65. function shouldBeABoolean() {
  66. var bool = true;
  67. bool.should.be.a.boolean;
  68. },
  69. function shouldNotBeABoolean() {
  70. var obj = {};
  71. obj.should.not.be.a.boolean;
  72. },
  73. function shouldBeDefined() {
  74. var obj = {};
  75. this.should.be.defined(obj);
  76. },
  77. function shouldNotBeDefined() {
  78. var obj;
  79. this.should.not.be.defined(obj);
  80. },
  81. function shouldBeUndefined() {
  82. var obj;
  83. this.should.be.undefined(obj);
  84. },
  85. function shouldNotBeUndefined() {
  86. var obj = {};
  87. this.should.not.be.undefined(obj);
  88. },
  89. function shouldBeAnArray() {
  90. var obj = [];
  91. obj.should.be.an.array;
  92. },
  93. function shouldBeAArray() {
  94. var obj = [];
  95. obj.should.be.a.array;
  96. },
  97. function shouldNotBeAnArray() {
  98. var obj = 'greets';
  99. obj.should.not.be.an.array;
  100. },
  101. function shouldNotBeAArray() {
  102. var obj = 'greets';
  103. obj.should.not.be.a.array;
  104. },
  105. function shouldBeNan() {
  106. var str = 'earth';
  107. str.should.be.nan;
  108. },
  109. function shouldBeANan() {
  110. var str = "oranges";
  111. str.should.be.a.nan;
  112. },
  113. function shouldNotBeNan() {
  114. var num = "123";
  115. num.should.not.be.nan;
  116. },
  117. function shouldNotBeANan() {
  118. var num = "";
  119. num.should.not.be.nan;
  120. },
  121. function shouldBeAnInstanceof() {
  122. var Obj = function() { this.foo = "bar"; };
  123. var obj = new Obj();
  124. obj.should.be.an.instanceof(Obj);
  125. },
  126. function shouldBeAInstanceof() {
  127. var Obj = function() { this.foo = "bar"; };
  128. var obj = new Obj();
  129. obj.should.be.an.instanceof(Obj);
  130. },
  131. function shouldNotBeAnInstanceof() {
  132. var Obj = function() { this.foo = "bar"; };
  133. var obj = new Array();
  134. obj.should.not.be.an.instanceof(Obj);
  135. },
  136. function shouldNotBeAInstanceof() {
  137. var Obj = function() { this.foo = "bar"; };
  138. var obj = new Array();
  139. obj.should.not.be.a.instanceof(Obj);
  140. },
  141. function shouldHaveProperty() {
  142. var obj = { foo: "bar" };
  143. obj.should.have.property('foo');
  144. },
  145. function shouldNotHaveProperty() {
  146. var obj = { foo: "bar" };
  147. obj.should.not.have.property('moo');
  148. },
  149. function shouldHaveOwnproperty() {
  150. var Obj = function() {
  151. this.foo = 'bar';
  152. };
  153. Obj.prototype.earth = 'planet';
  154. var obj = new Obj;
  155. obj.should.have.ownProperty('foo');
  156. },
  157. function shouldNotHaveOwnproperty() {
  158. var Obj = function() {
  159. this.foo = 'bar';
  160. };
  161. Obj.prototype.earth = 'planet';
  162. var obj = new Obj;
  163. obj.should.not.have.ownProperty('earth');
  164. },
  165. function shouldBeEmpty() {
  166. var array = [];
  167. array.should.be.empty;
  168. },
  169. function shouldNotBeEmpty() {
  170. var array = [1,2];
  171. array.should.not.be.empty;
  172. },
  173. function shouldHaveValue() {
  174. var array = [1,2];
  175. array.should.have.value(2);
  176. },
  177. function shouldNotHaveValue() {
  178. var array = [1,3];
  179. array.should.not.have.value(2);
  180. },
  181. function obj_shouldHaveKey() {
  182. var obj = {aKey: 'unlock'};
  183. obj.should.have.key('aKey');
  184. },
  185. function array_shouldHaveKey() {
  186. var array = [];
  187. array['some_key'] = "some value";
  188. array.should.have.key('some_key');
  189. },
  190. function obj_shouldNotHaveKey() {
  191. var obj = {aKey: 'unlock'};
  192. obj.should.not.have.key('a_fake_key');
  193. },
  194. function array_shouldNotHaveKey() {
  195. var array = [];
  196. array['some_key'] = "some value";
  197. array.should.not.have.key('some_other_key');
  198. },
  199. function shouldHaveExactly() {
  200. var array = [1,32,23,32];
  201. array.should.have.exactly(4).elements;
  202. },
  203. function shouldNotHaveExactly() {
  204. var array = [1,32,23,32];
  205. array.should.not.have.exactly(9).elements;
  206. },
  207. function obj_shouldHaveExactly() {
  208. var obj = {a: 'abc', b: 123};
  209. obj.should.have.exactly(2).properties;
  210. },
  211. function obj_shouldNotHaveExactly() {
  212. var obj = {hello: 'world'};
  213. obj.should.not.have.exactly(9).properties;
  214. },
  215. function shouldHaveLessthan() {
  216. var array = [1,32,23,32];
  217. array.should.have.lessthan(20).elements;
  218. },
  219. function shouldNotHaveLessthan() {
  220. var array = [1,32,23,32];
  221. array.should.not.have.lessthan(3).elements;
  222. },
  223. function obj_shouldHaveLessthan() {
  224. var obj = {a: 'abc', b: 123};
  225. obj.should.have.lessthan(5).properties;
  226. },
  227. function obj_shouldNotHaveLessthan() {
  228. var obj = {hello: 'world'};
  229. obj.should.not.have.lessthan(1).properties;
  230. },
  231. function shouldHaveMorethan() {
  232. var array = [1,32,23,32];
  233. array.should.have.morethan(2).elements;
  234. },
  235. function shouldNotHaveMorethan() {
  236. var array = [1,32,23,32];
  237. array.should.not.have.morethan(6).elements;
  238. },
  239. function obj_shouldHaveMorethan() {
  240. var obj = {a: 'abc', b: 123};
  241. obj.should.have.morethan(0).properties;
  242. },
  243. function obj_shouldNotHaveMorethan() {
  244. var obj = {hello: 'world', 'bye': 'thanks for all the fish'};
  245. obj.should.not.have.morethan(2).properties;
  246. },
  247. function shouldBeFalse() {
  248. this.should.be.false(false);
  249. },
  250. function shouldNotBeFalse() {
  251. this.should.not.be.false(true);
  252. },
  253. function shouldBeTrue() {
  254. this.should.be.true(true);
  255. },
  256. function shouldNotBeTrue() {
  257. this.should.not.be.true(false);
  258. },
  259. function obj_shouldEqual() {
  260. var obj1 = {propAlpha: 55, propBeta: 'green'};
  261. var obj2 = {propAlpha: 55, propBeta: 'green'};
  262. obj1.should.equal(obj2);
  263. },
  264. function obj_shouldNotEqual() {
  265. var obj1 = {propAlpha: 55, propBeta: 'green'};
  266. var obj2 = {propAlpha: 1, propBeta: 'yellow'};
  267. obj1.should.not.equal(obj2);
  268. },
  269. function array_shouldEqual() {
  270. var array1 = [55,'green'];
  271. var array2 = [55, 'green'];
  272. array1.should.equal(array2);
  273. },
  274. function array_shouldNotEqual() {
  275. var array1 = ['green'];
  276. var array2 = [55, 'green'];
  277. array1.should.not.equal(array2);
  278. },
  279. function str_shouldEqual() {
  280. ('FOOO').should.equal('FOOO');
  281. },
  282. function str_shouldNotEqual() {
  283. ('FOOO').should.not.equal('BAAR');
  284. },
  285. function num_shouldEqual() {
  286. (66).should.equal(66);
  287. },
  288. function num_shouldNotEqual() {
  289. (66).should.not.equal(33);
  290. },
  291. function nan_shouldEqual() {
  292. Number.NaN.should.equal(NaN);
  293. },
  294. function nan_shouldNotEqual() {
  295. Number.NaN.should.not.equal(2);
  296. },
  297. function regexp_shouldEqual() {
  298. /[a-z]/.should.equal(/[a-z]/);
  299. },
  300. function regexp_shouldNotEqual() {
  301. /[a-z]/.should.not.equal(/[1-9]/);
  302. }
  303. ];
  304. /****************************************************************************************
  305. * expect_errors
  306. *
  307. *****************************************************************************************/
  308. var expect_errors = [
  309. function shouldNotBeNull() {
  310. this.should.not.be.null(null);
  311. },
  312. function shouldBeNull() {
  313. this.should.be.null({});
  314. },
  315. function shouldBeATypeof() {
  316. var obj = [];
  317. obj.should.be.a.typeof('string');
  318. },
  319. function shouldNotBeATypeof() {
  320. var obj = 'hello';
  321. obj.should.not.be.a.typeof('string');
  322. },
  323. function shouldBeAnObject() {
  324. var obj = "hello";
  325. obj.should.be.an.object;
  326. },
  327. function shouldBeAObject() {
  328. var obj = "world";
  329. obj.should.be.a.object;
  330. },
  331. function shouldNotBeAnObject() {
  332. var obj = {apples: 'oranges'};
  333. obj.should.not.be.an.object;
  334. },
  335. function shouldNotBeAObject() {
  336. var obj = new Object();
  337. obj.should.not.be.a.object;
  338. },
  339. function shouldBeAFunction() {
  340. var func = 'superstring';
  341. func.should.be.a.function;
  342. },
  343. function shouldNotBeAFunction() {
  344. var func = function() {};
  345. func.should.not.be.a.function;
  346. },
  347. function shouldBeAString() {
  348. var num = 123;
  349. num.should.be.a.string;
  350. },
  351. function shouldNotBeAString() {
  352. var string = 'theory';
  353. string.should.not.be.a.string;
  354. },
  355. function shouldBeANumber() {
  356. var str = 'orange';
  357. str.should.be.a.number;
  358. },
  359. function shouldNotBeANumber() {
  360. var num = 567;
  361. num.should.not.be.a.number;
  362. },
  363. function shouldNotBeABoolean() {
  364. var bool = true;
  365. bool.should.not.be.a.boolean;
  366. },
  367. function shouldBeABoolean() {
  368. var obj = {};
  369. obj.should.be.a.boolean;
  370. },
  371. function shouldBeDefined() {
  372. var obj;
  373. this.should.be.defined(obj);
  374. },
  375. function shouldNotBeDefined() {
  376. var obj = {};
  377. this.should.not.be.defined(obj);
  378. },
  379. function shouldBeUndefined() {
  380. var obj = {};
  381. this.should.be.undefined(obj);
  382. },
  383. function shouldNotBeUndefined() {
  384. var obj;
  385. this.should.not.be.undefined(obj);
  386. },
  387. function shouldBeAnArray() {
  388. var obj = 'greets';
  389. obj.should.be.an.array;
  390. },
  391. function shouldBeAArray() {
  392. var obj = 'greets';
  393. obj.should.be.a.array;
  394. },
  395. function shouldNotBeAnArray() {
  396. var obj = [];
  397. obj.should.not.be.an.array;
  398. },
  399. function shouldNotBeAArray() {
  400. var obj = [];
  401. obj.should.not.be.a.array;
  402. },
  403. function shouldBeNan() {
  404. var str = '123';
  405. str.should.be.nan;
  406. },
  407. function shouldBeANan() {
  408. var num = 42;
  409. num.should.be.a.nan;
  410. },
  411. function shouldNotBeNan() {
  412. var num = 'saturn';
  413. num.should.not.be.nan;
  414. },
  415. function shouldNotBeANan() {
  416. var num = 'mars';
  417. num.should.not.be.nan;
  418. },
  419. function shouldBeAnInstanceof() {
  420. var Obj = function() { this.foo = "bar"; };
  421. var obj = new Array();
  422. obj.should.be.an.instanceof(Obj);
  423. },
  424. function shouldBeAInstanceof() {
  425. var Obj = function() { this.foo = "bar"; };
  426. var obj = new Array();
  427. obj.should.be.an.instanceof(Obj);
  428. },
  429. function shouldNotBeAnInstanceof() {
  430. var Obj = function() { this.foo = "bar"; };
  431. var obj = new Obj();
  432. obj.should.not.be.an.instanceof(Obj);
  433. },
  434. function shouldNotBeAInstanceof() {
  435. var Obj = function() { this.foo = "bar"; };
  436. var obj = new Obj();
  437. obj.should.not.be.a.instanceof(Obj);
  438. },
  439. function shouldHaveProperty() {
  440. var obj = { foo: "bar" };
  441. obj.should.have.property('moo');
  442. },
  443. function shouldNotHaveProperty() {
  444. var obj = { foo: "bar" };
  445. obj.should.not.have.property('foo');
  446. },
  447. function shouldHaveOwnproperty() {
  448. var Obj1 = function() {
  449. this.foo = 'bar';
  450. };
  451. Obj1.prototype.earth = 'planet';
  452. var obj = new Obj1;
  453. obj.should.have.ownProperty('earth');
  454. },
  455. function shouldNotHaveOwnproperty() {
  456. var Obj1 = function() {
  457. this.foo = 'bar';
  458. };
  459. Obj1.prototype.earth = 'planet';
  460. var obj = new Obj1;
  461. obj.should.not.have.ownProperty('foo');
  462. },
  463. function shouldBeEmpty() {
  464. var array = [1,2];
  465. array.should.be.empty;
  466. },
  467. function shouldBeEmpty() {
  468. var array = [];
  469. array.should.not.be.empty;
  470. },
  471. function shouldHaveValue() {
  472. var array = [1,2];
  473. array.should.have.value(4);
  474. },
  475. function shouldNotHaveValue() {
  476. var array = [1,2];
  477. array.should.not.have.value(2);
  478. },
  479. function obj_shouldHaveKey() {
  480. var obj = {aKey: 'unlock'};
  481. obj.should.have.key('foo');
  482. },
  483. function array_shouldHaveKey() {
  484. var array = [];
  485. array['some_key'] = "some value";
  486. array.should.have.key('foo');
  487. },
  488. function obj_shouldNotHaveKey() {
  489. var obj = {aKey: 'unlock'};
  490. obj.should.not.have.key('aKey');
  491. },
  492. function array_shouldNotHaveKey() {
  493. var array = [];
  494. array['some_key'] = "some value";
  495. array.should.not.have.key('some_key');
  496. },
  497. function shouldHaveExactly() {
  498. var array = [1,32,23,32];
  499. array.should.have.exactly(2).elements;
  500. },
  501. function shouldNotHaveExactly() {
  502. var array = [1,32,23,32];
  503. array.should.not.have.exactly(4).elements;
  504. },
  505. function obj_shouldHaveExactly() {
  506. var obj = {a: 'abc', b: 123};
  507. obj.should.have.exactly(9).elements;
  508. },
  509. function obj_shouldNotHaveExactly() {
  510. var obj = {hello: 'world'};
  511. obj.should.not.have.exactly(1).elements;
  512. },
  513. function shouldHaveLessthan() {
  514. var array = [1,32,23,32];
  515. array.should.have.lessthan(2).elements;
  516. },
  517. function shouldNotHaveLessthan() {
  518. var array = [1,32,23,32];
  519. array.should.not.have.lessthan(10).elements;
  520. },
  521. function obj_shouldHaveLessthan() {
  522. var obj = {a: 'abc', b: 123};
  523. obj.should.have.lessthan(2).properties;
  524. },
  525. function obj_shouldNotHaveLessthan() {
  526. var obj = {hello: 'world'};
  527. obj.should.not.have.lessthan(2).properties;
  528. },
  529. function shouldHaveMorethan() {
  530. var array = [1,32,23,32];
  531. array.should.have.morethan(10).elements;
  532. },
  533. function shouldNotHaveMorethan() {
  534. var array = [1,32,23,32];
  535. array.should.not.have.morethan(2).elements;
  536. },
  537. function obj_shouldHaveMorethan() {
  538. var obj = {a: 'abc', b: 123};
  539. obj.should.have.morethan(8).properties;
  540. },
  541. function obj_shouldNotHaveMorethan() {
  542. var obj = {hello: 'world', 'bye': 'thanks for all the fish'};
  543. obj.should.not.have.morethan(1).properties;
  544. },
  545. function shouldBeFalse() {
  546. this.should.be.false(true);
  547. },
  548. function shouldNotBeFalse() {
  549. this.should.not.be.false(false);
  550. },
  551. function shouldBeTrue() {
  552. this.should.be.true(false);
  553. },
  554. function shouldNotBeTrue() {
  555. this.should.not.be.true(true);
  556. },
  557. function obj_shouldEqual() {
  558. var obj1 = {propAlpha: 55, propBeta: 'green'};
  559. var obj2 = {propBeta: 'pink'};
  560. obj1.should.equal(obj2);
  561. },
  562. function obj_shouldNotEqual() {
  563. var obj1 = {propAlpha: 55, propBeta: 'green'};
  564. var obj2 = {propAlpha: 55, propBeta: 'green'};
  565. obj1.should.not.equal(obj2);
  566. },
  567. function array_shouldEqual() {
  568. var array1 = [55,'green'];
  569. var array2 = [1,];
  570. array1.should.equal(array2);
  571. },
  572. function array_shouldNotEqual() {
  573. var array1 = [55, 'green'];
  574. var array2 = [55, 'green'];
  575. array1.should.not.equal(array2);
  576. },
  577. function str_shouldEqual() {
  578. ('FOOO').should.equal('food');
  579. },
  580. function str_shouldNotEqual() {
  581. ('FOOO').should.not.equal('FOOO');
  582. },
  583. function num_shouldEqual() {
  584. (66).should.equal(0);
  585. },
  586. function num_shouldNotEqual() {
  587. (66).should.not.equal(66);
  588. },
  589. function nan_shouldEqual() {
  590. Number.NaN.should.equal(0);
  591. },
  592. function nan_shouldNotEqual() {
  593. Number.NaN.should.not.equal(NaN);
  594. },
  595. function regexp_shouldEqual() {
  596. /[a-z]/.should.equal(/[1-5]/);
  597. },
  598. function regexp_shouldNotEqual() {
  599. /[a-z]/.should.not.equal(/[a-z]/);
  600. }
  601. ];
  602. function does_not_throw(array) {
  603. array.forEach(function(elem, pos) {
  604. assert.doesNotThrow(elem, function error_handler(err) {
  605. console.log('error at: ' + pos);
  606. console.log(err.message);
  607. console.log(elem.toString());
  608. //process.exit(1);
  609. },
  610. 'Unexpected Error << The best kind!'
  611. );
  612. });
  613. }
  614. function throws_errors(array) {
  615. array.forEach(function(elem, pos) {
  616. assert.throws(elem);
  617. });
  618. }
  619. does_not_throw(no_errors);
  620. throws_errors(expect_errors);