Cursor.js 2.5 KB

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