SetUnionExpression.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. var assert = require("assert"),
  3. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  4. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  5. SetUnionExpression = require("../../../../lib/pipeline/expressions/SetUnionExpression"),
  6. Expression = require("../../../../lib/pipeline/expressions/Expression");
  7. function errMsg(expr, args, tree, expected, result) {
  8. return "for expression " + expr +
  9. " with argument " + args +
  10. " full tree: " + JSON.stringify(tree) +
  11. " expected: " + expected +
  12. " result: " + result;
  13. }
  14. module.exports = {
  15. "SetUnionExpression": {
  16. "constructor()": {
  17. "should not throw Error when constructing without args": function testConstructor() {
  18. assert.doesNotThrow(function() {
  19. new SetUnionExpression();
  20. });
  21. },
  22. "should throw Error when constructing with args": function testConstructor() {
  23. assert.throws(function() {
  24. new SetUnionExpression("someArg");
  25. });
  26. }
  27. },
  28. "#getOpName()": {
  29. "should return the correct op name; $setUnion": function testOpName() {
  30. assert.equal(new SetUnionExpression().getOpName(), "$setUnion");
  31. }
  32. },
  33. "#evaluateInternal()": {
  34. "Should fail if array1 is not an array": function testArg1() {
  35. var array1 = "not an array",
  36. array2 = [6, 7, 8, 9],
  37. input = [array1,array2],
  38. idGenerator = new VariablesIdGenerator(),
  39. vps = new VariablesParseState(idGenerator),
  40. expr = Expression.parseExpression("$setUnion", input, vps);
  41. assert.throws(function () {
  42. expr.evaluate({});
  43. });
  44. },
  45. "Should fail if array2 is not an array": function testArg2() {
  46. var array1 = [1, 2, 3, 4],
  47. array2 = "not an array",
  48. input = [array1,array2],
  49. idGenerator = new VariablesIdGenerator(),
  50. vps = new VariablesParseState(idGenerator),
  51. expr = Expression.parseExpression("$setUnion", input, vps);
  52. assert.throws(function () {
  53. expr.evaluate({});
  54. });
  55. },
  56. "Should fail if both are not an array": function testArg1andArg2() {
  57. var array1 = "not an array",
  58. array2 = "not an array",
  59. input = [array1,array2],
  60. idGenerator = new VariablesIdGenerator(),
  61. vps = new VariablesParseState(idGenerator),
  62. expr = Expression.parseExpression("$setUnion", input, vps);
  63. assert.throws(function () {
  64. expr.evaluate({});
  65. });
  66. },
  67. "Should pass and return [1, 2, 3, 4, 5, 8, 9]": function(){
  68. var array1 = [1, 2, 3, 9, 8],
  69. array2 = [1, 2, 3, 4, 5],
  70. input = [array1,array2],
  71. idGenerator = new VariablesIdGenerator(),
  72. vps = new VariablesParseState(idGenerator),
  73. expr = Expression.parseExpression("$setUnion", input, vps),
  74. result = expr.evaluate({}),
  75. expected = [1, 2, 3, 4, 5, 8, 9],
  76. msg = errMsg("$setUnion", input, expr.serialize(false), expected, result);
  77. assert.deepEqual(result, expected, msg);
  78. },
  79. "Should pass and return [1, 2, 3, 4, 5, 7]": function() {
  80. var array1 = [2, 4],
  81. array2 = [1, 2, 3, 4, 5],
  82. array3 = [7, 2, 1],
  83. array4 = [],
  84. input = [array1,array2,array3, array4],
  85. idGenerator = new VariablesIdGenerator(),
  86. vps = new VariablesParseState(idGenerator),
  87. expr = Expression.parseExpression("$setUnion", input, vps),
  88. result = expr.evaluate({}),
  89. expected = [1, 2, 3, 4, 5, 7],
  90. msg = errMsg("$setUnion", input, expr.serialize(false), expected, result);
  91. assert.deepEqual(result, expected, msg);
  92. },
  93. "Should pass and return [1, 2, 7]": function() {
  94. var array1 = [],
  95. array2 = [],
  96. array3 = [7, 2, 1],
  97. array4 = [],
  98. input = [array1,array2,array3, array4],
  99. idGenerator = new VariablesIdGenerator(),
  100. vps = new VariablesParseState(idGenerator),
  101. expr = Expression.parseExpression("$setUnion", input, vps),
  102. result = expr.evaluate({}),
  103. expected = [1, 2, 7],
  104. msg = errMsg("$setUnion", input, expr.serialize(false), expected, result);
  105. assert.deepEqual(result, expected, msg);
  106. },
  107. "Should recognize a null array and return null because of it": function(){
  108. var array1 = [1, 2, 3, 9, 8],
  109. array2 = null,
  110. input = [array1,array2],
  111. idGenerator = new VariablesIdGenerator(),
  112. vps = new VariablesParseState(idGenerator),
  113. expr = Expression.parseExpression("$setUnion", input, vps),
  114. result = expr.evaluate({}),
  115. expected = null,
  116. msg = errMsg("$setUnion", input, expr.serialize(false), expected, result);
  117. assert.deepEqual(result, expected, msg);
  118. },
  119. }
  120. }
  121. };
  122. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);