LastAccumulator.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. var assert = require("assert"),
  3. LastAccumulator = require("../../../../lib/pipeline/accumulators/LastAccumulator");
  4. // Mocha one-liner to make these tests self-hosted
  5. if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
  6. exports.LastAccumulator = {
  7. ".constructor()": {
  8. "should create instance of Accumulator": function() {
  9. assert(new LastAccumulator() instanceof LastAccumulator);
  10. },
  11. "should throw error if called with args": function() {
  12. assert.throws(function() {
  13. new LastAccumulator(123);
  14. });
  15. },
  16. },
  17. ".create()": {
  18. "should return an instance of the accumulator": function() {
  19. assert(LastAccumulator.create() instanceof LastAccumulator);
  20. },
  21. },
  22. "#process()": {
  23. "should return undefined if no inputs evaluated": function testNone() {
  24. var acc = LastAccumulator.create();
  25. assert.strictEqual(acc.getValue(), undefined);
  26. },
  27. "should return value for one input": function testOne() {
  28. var acc = LastAccumulator.create();
  29. acc.process(5);
  30. assert.strictEqual(acc.getValue(), 5);
  31. },
  32. "should return missing for one missing input": function testMissing() {
  33. var acc = LastAccumulator.create();
  34. acc.process(undefined);
  35. assert.strictEqual(acc.getValue(), undefined);
  36. },
  37. "should return last of two inputs": function testTwo() {
  38. var acc = LastAccumulator.create();
  39. acc.process(5);
  40. acc.process(7);
  41. assert.strictEqual(acc.getValue(), 7);
  42. },
  43. "should return last of two inputs (even if last is missing)": function testFirstMissing() {
  44. var acc = LastAccumulator.create();
  45. acc.process(7);
  46. acc.process(undefined);
  47. assert.strictEqual(acc.getValue(), undefined);
  48. },
  49. },
  50. "#getValue()": {
  51. "should get value the same for shard and router": function() {
  52. var acc = LastAccumulator.create();
  53. assert.strictEqual(acc.getValue(false), acc.getValue(true));
  54. acc.process(123);
  55. assert.strictEqual(acc.getValue(false), acc.getValue(true));
  56. },
  57. },
  58. "#reset()": {
  59. "should reset to missing": function() {
  60. var acc = LastAccumulator.create();
  61. assert.strictEqual(acc.getValue(), undefined);
  62. acc.process(123);
  63. assert.notEqual(acc.getValue(), undefined);
  64. acc.reset();
  65. assert.strictEqual(acc.getValue(), undefined);
  66. assert.strictEqual(acc.getValue(true), undefined);
  67. },
  68. },
  69. "#getOpName()": {
  70. "should return the correct op name; $last": function() {
  71. assert.equal(new LastAccumulator().getOpName(), "$last");
  72. },
  73. },
  74. };
  75. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);