CursorDocumentSource.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. var assert = require("assert"),
  3. CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
  4. Cursor = require("../../../../lib/Cursor");
  5. module.exports = {
  6. "CursorDocumentSource": {
  7. "constructor(data)": {
  8. "should fail if CursorWithContext is not provided": function(){
  9. assert.throws(function(){
  10. var cds = new CursorDocumentSource();
  11. });
  12. },
  13. "should get a accept a CursorWithContext and set it internally": function(){
  14. var cwc = new CursorDocumentSource.CursorWithContext();
  15. cwc._cursor = new Cursor( [] );
  16. var cds = new CursorDocumentSource(cwc);
  17. assert.ok(cds._cursorWithContext);
  18. }
  19. },
  20. "#eof": {
  21. "should return true if the cursor is empty": function(){
  22. var cwc = new CursorDocumentSource.CursorWithContext();
  23. cwc._cursor = new Cursor( [] );
  24. var cds = new CursorDocumentSource(cwc);
  25. assert.equal(cds.eof(), true);
  26. },
  27. "should return false if the cursor is non-empty": function(){
  28. var cwc = new CursorDocumentSource.CursorWithContext();
  29. cwc._cursor = new Cursor( [1,2,3] );
  30. var cds = new CursorDocumentSource(cwc);
  31. assert.equal(cds.eof(), false);
  32. }
  33. },
  34. "#advance": {
  35. "should return true if the cursor was advanced": function(){
  36. var cwc = new CursorDocumentSource.CursorWithContext();
  37. cwc._cursor = new Cursor( [1,2,3] );
  38. var cds = new CursorDocumentSource(cwc);
  39. assert.equal(cds.advance(), true);
  40. },
  41. "should return false if the cursor is empty": function(){
  42. var cwc = new CursorDocumentSource.CursorWithContext();
  43. cwc._cursor = new Cursor( [1,2,3] );
  44. var cds = new CursorDocumentSource(cwc);
  45. cds.advance();cds.advance();cds.advance();
  46. assert.equal(cds.advance(), false);
  47. }
  48. },
  49. "#getCurrent": {
  50. "should return the current cursor value": function(){
  51. var cwc = new CursorDocumentSource.CursorWithContext();
  52. cwc._cursor = new Cursor( [1,2,3,4] );
  53. var cds = new CursorDocumentSource(cwc);
  54. assert.equal(cds.getCurrent(), 1);
  55. cds.advance();
  56. assert.equal(cds.getCurrent(), 2);
  57. cds.advance();
  58. assert.equal(cds.getCurrent(), 3);
  59. cds.advance();
  60. assert.equal(cds.getCurrent(), 4);
  61. cds.advance();
  62. assert.equal(cds.getCurrent(), undefined);
  63. }
  64. },
  65. "#dispose": {
  66. "should empty the current cursor": function(){
  67. var cwc = new CursorDocumentSource.CursorWithContext();
  68. cwc._cursor = new Cursor( [1,2,3] );
  69. var cds = new CursorDocumentSource(cwc);
  70. assert.equal(cds.getCurrent(), 1);
  71. cds.advance();
  72. assert.equal(cds.getCurrent(), 2);
  73. cds.dispose();
  74. assert.equal(cds.advance(), false);
  75. assert.equal(cds.eof(), true);
  76. }
  77. }
  78. }
  79. };
  80. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();