Cursor.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. var assert = require("assert"),
  2. Cursor = require("../../lib/Cursor");
  3. module.exports = {
  4. "Cursor": {
  5. "constructor(data)": {
  6. "should throw an exception if it does not get a valid array or stream": function(){
  7. assert.throws(function(){
  8. var c = new Cursor();
  9. });
  10. assert.throws(function(){
  11. var c = new Cursor(5);
  12. });
  13. }
  14. },
  15. "#ok": {
  16. "should return true if there is still data in the array": function(){
  17. var c = new Cursor([1,2,3,4,5]);
  18. assert.equal(c.ok(), true);
  19. },
  20. "should return false if there is no data left in the array": function(){
  21. var c = new Cursor([]);
  22. assert.equal(c.ok(), false);
  23. },
  24. "should return true if there is no data left in the array, but there is still a current value": function(){
  25. var c = new Cursor([1,2]);
  26. c.advance();
  27. c.advance();
  28. assert.equal(c.ok(), true);
  29. c.advance();
  30. assert.equal(c.ok(), false);
  31. }
  32. // ,
  33. // "should return true if there is still data in the stream": function(){
  34. //
  35. // },
  36. // "should return false if there is no data left in the stream": function(){
  37. //
  38. // }
  39. },
  40. "#advance": {
  41. "should return true if there is still data in the array": function(){
  42. var c = new Cursor([1,2,3,4,5]);
  43. assert.equal(c.advance(), true);
  44. },
  45. "should return false if there is no data left in the array": function(){
  46. var c = new Cursor([1]);
  47. c.advance();
  48. assert.equal(c.advance(), false);
  49. },
  50. "should update the current object to the next item in the array": function(){
  51. var c = new Cursor([1,"2"]);
  52. c.advance();
  53. assert.strictEqual(c.current(), 1);
  54. c.advance();
  55. assert.strictEqual(c.current(), "2");
  56. c.advance();
  57. assert.strictEqual(c.current(), undefined);
  58. }
  59. //, "should return true if there is still data in the stream": function(){
  60. //
  61. // },
  62. // "should return false if there is no data left in the stream": function(){
  63. //
  64. // },
  65. // "should update the current object to the next item in the stream": function(){
  66. //
  67. // }
  68. },
  69. "#current": {
  70. "should return the first value if the cursor has not been advanced yet": function(){
  71. var c = new Cursor([1,2,3,4,5]);
  72. assert.equal(c.current(), 1);
  73. },
  74. "should return the first value if the cursor has been advanced once": function(){
  75. var c = new Cursor([1,2,3,4,5]);
  76. c.advance();
  77. assert.equal(c.current(), 1);
  78. }
  79. }
  80. }
  81. };
  82. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();