AddToSetAccumulator_test.js 2.2 KB

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