AddToSetAccumulator.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var assert = require("assert"),
  3. AddToSetAccumulator = require("../../../../lib/pipeline/accumulators/AddToSetAccumulator");
  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. var testData = {
  7. nil: null,
  8. bF: false, bT: true,
  9. numI: 123, numF: 123.456,
  10. str: "TesT! mmm π",
  11. obj: {foo:{bar:"baz"}},
  12. arr: [1, 2, 3, [4, 5, 6]],
  13. date: new Date(),
  14. re: /foo/gi,
  15. };
  16. //TODO: refactor these test cases using Expression.parseOperand() or something because these could be a whole lot cleaner...
  17. exports.AddToSetAccumulator = {
  18. ".constructor()": {
  19. "should create instance of Accumulator": function() {
  20. assert(new AddToSetAccumulator() instanceof AddToSetAccumulator);
  21. },
  22. "should error if called with args": function() {
  23. assert.throws(function() {
  24. new AddToSetAccumulator(123);
  25. });
  26. }
  27. },
  28. ".create()": {
  29. "should return an instance of the accumulator": function() {
  30. assert(AddToSetAccumulator.create() instanceof AddToSetAccumulator);
  31. }
  32. },
  33. "#process()": {
  34. "should add input to set": function() {
  35. var acc = AddToSetAccumulator.create();
  36. acc.process(testData);
  37. assert.deepEqual(acc.getValue(), [testData]);
  38. },
  39. "should add input iff not already in set": function() {
  40. var acc = AddToSetAccumulator.create();
  41. acc.process(testData);
  42. acc.process(testData);
  43. assert.deepEqual(acc.getValue(), [testData]);
  44. },
  45. "should merge input into set": function() {
  46. var acc = AddToSetAccumulator.create();
  47. acc.process(testData);
  48. acc.process([testData, 42], true);
  49. assert.deepEqual(acc.getValue(), [42, testData]);
  50. },
  51. },
  52. "#getValue()": {
  53. "should return empty set initially": function() {
  54. var acc = new AddToSetAccumulator.create();
  55. var value = acc.getValue();
  56. assert.equal((value instanceof Array), true);
  57. assert.equal(value.length, 0);
  58. },
  59. "should return set of added items": function() {
  60. var acc = AddToSetAccumulator.create(),
  61. expected = [
  62. 42,
  63. {foo:1, bar:2},
  64. {bar:2, foo:1},
  65. testData
  66. ];
  67. expected.forEach(function(input){
  68. acc.process(input);
  69. });
  70. assert.deepEqual(acc.getValue(), expected);
  71. },
  72. }
  73. };