ArrayRunner_test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. if (!module.parent) return require.cache[__filename] = 0, (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);
  3. var assert = require("assert"),
  4. Runner = require("../../../lib/query/Runner"),
  5. ArrayRunner = require("../../../lib/query/ArrayRunner");
  6. module.exports = {
  7. "ArrayRunner": {
  8. "#constructor": {
  9. "should accept an array of data": function(){
  10. assert.doesNotThrow(function(){
  11. new ArrayRunner([1,2,3]);
  12. });
  13. },
  14. "should fail if not given an array": function(){
  15. assert.throws(function(){
  16. new ArrayRunner();
  17. });
  18. assert.throws(function(){
  19. new ArrayRunner(123);
  20. });
  21. }
  22. },
  23. "#getNext": {
  24. "should return the next item in the array": function(done){
  25. var ar = new ArrayRunner([1,2,3]);
  26. ar.getNext(function(err, out, state){
  27. assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
  28. assert.strictEqual(out, 1);
  29. ar.getNext(function(err, out, state){
  30. assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
  31. assert.strictEqual(out, 2);
  32. ar.getNext(function(err, out, state){
  33. assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
  34. assert.strictEqual(out, 3);
  35. done();
  36. });
  37. });
  38. });
  39. },
  40. "should return EOF if there is nothing left in the array": function(done){
  41. var ar = new ArrayRunner([1]);
  42. ar.getNext(function(err, out, state){
  43. assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
  44. assert.strictEqual(out, 1);
  45. ar.getNext(function(err, out, state){
  46. assert.strictEqual(state, Runner.RunnerState.RUNNER_EOF);
  47. assert.strictEqual(out, undefined);
  48. done();
  49. });
  50. });
  51. }
  52. },
  53. "#getInfo": {
  54. "should return nothing if explain flag is not set": function(){
  55. var ar = new ArrayRunner([1,2,3]);
  56. assert.strictEqual(ar.getInfo(), undefined);
  57. },
  58. "should return information about the runner if explain flag is set": function(){
  59. var ar = new ArrayRunner([1,2,3]);
  60. assert.deepEqual(ar.getInfo(true), {
  61. "type":"ArrayRunner",
  62. "nDocs":3,
  63. "position":0,
  64. "state": Runner.RunnerState.RUNNER_ADVANCED
  65. });
  66. }
  67. },
  68. "#reset": {
  69. "should clear out the runner": function(){
  70. var ar = new ArrayRunner([1,2,3]);
  71. ar.reset();
  72. assert.deepEqual(ar.getInfo(true), {
  73. "type":"ArrayRunner",
  74. "nDocs":0,
  75. "position":0,
  76. "state": Runner.RunnerState.RUNNER_DEAD
  77. });
  78. }
  79. }
  80. }
  81. };