AddToSetAccumulator.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. var assert = require("assert"),
  3. AddToSetAccumulator = require("../../../../lib/pipeline/accumulators/AddToSetAccumulator");
  4. var createAccumulator = function createAccumulator() {
  5. return new AddToSetAccumulator();
  6. };
  7. //TODO: refactor these test cases using Expression.parseOperand() or something because these could be a whole lot cleaner...
  8. module.exports = {
  9. "AddToSetAccumulator": {
  10. "constructor()": {
  11. "should error if called with args": function testArgsGivenToCtor() {
  12. assert.throws(function() {
  13. new AddToSetAccumulator('arg');
  14. });
  15. },
  16. "should construct object with set property": function testCtorAssignsSet() {
  17. var acc = new AddToSetAccumulator();
  18. assert.notEqual(acc.set, null);
  19. assert.notEqual(acc.set, undefined);
  20. }
  21. },
  22. "#getFactory()": {
  23. "should return the constructor for this class": function factoryIsConstructor(){
  24. assert.strictEqual(new AddToSetAccumulator().getFactory(), AddToSetAccumulator);
  25. }
  26. },
  27. "#processInternal()" : {
  28. "should add input to set": function testAddsToSet() {
  29. var acc = createAccumulator();
  30. acc.processInternal(5);
  31. var value = acc.getValue();
  32. assert.deepEqual(JSON.stringify(value), JSON.stringify([5]));
  33. }
  34. },
  35. "#getValue()": {
  36. "should return empty array": function testEmptySet() {
  37. var acc = new createAccumulator();
  38. var value = acc.getValue();
  39. assert.equal((value instanceof Array), true);
  40. assert.equal(value.length, 0);
  41. },
  42. "should return array with one element that equals 5": function test5InSet() {
  43. var acc = createAccumulator();
  44. acc.processInternal(5);
  45. acc.processInternal(5);
  46. var value = acc.getValue();
  47. assert.deepEqual(JSON.stringify(value), JSON.stringify([5]));
  48. },
  49. "should produce value that is an array of multiple elements": function testMultipleItems() {
  50. var acc = createAccumulator();
  51. acc.processInternal(5);
  52. acc.processInternal({key: "value"});
  53. var value = acc.getValue();
  54. assert.deepEqual(JSON.stringify(value), JSON.stringify([5, {key: "value"}]));
  55. },
  56. "should return array with one element that is an object containing a key/value pair": function testKeyValue() {
  57. var acc = createAccumulator();
  58. acc.processInternal({key: "value"});
  59. var value = acc.getValue();
  60. assert.deepEqual(JSON.stringify(value), JSON.stringify([{key: "value"}]));
  61. },
  62. "should coalesce different instances of equivalent objects": function testGetValue_() {
  63. var acc = createAccumulator();
  64. acc.processInternal({key: "value"});
  65. acc.processInternal({key: "value"});
  66. var value = acc.getValue();
  67. assert.deepEqual(JSON.stringify(value), JSON.stringify([{key: "value"}]));
  68. }
  69. }
  70. }
  71. };
  72. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);