SetIsSubsetExpression.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. var assert = require("assert"),
  3. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  4. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  5. SetIsSubsetExpression = require("../../../../lib/pipeline/expressions/SetIsSubsetExpression"),
  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. "SetIsSubsetExpression": {
  16. "constructor()": {
  17. "should not throw Error when constructing without args": function testConstructor() {
  18. assert.doesNotThrow(function() {
  19. new SetIsSubsetExpression();
  20. });
  21. },
  22. "should throw Error when constructing with args": function testConstructor() {
  23. assert.throws(function() {
  24. new SetIsSubsetExpression("someArg");
  25. });
  26. }
  27. },
  28. "#getOpName()": {
  29. "should return the correct op name; $setIsSubset": function testOpName() {
  30. assert.equal(new SetIsSubsetExpression().getOpName(), "$setIsSubset");
  31. }
  32. },
  33. "#evaluateInternal()": {
  34. beforeEach: function() {
  35. this.compare = function(array1, array2, expected) {
  36. var input = [array1, array2],
  37. idGenerator = new VariablesIdGenerator(),
  38. vps = new VariablesParseState(idGenerator),
  39. expr = Expression.parseExpression("$setIsSubset", input, vps),
  40. result = expr.evaluate({}),
  41. msg = errMsg("$setIsSubset", input, expr.serialize(false), expected, result);
  42. assert.equal(result, expected, msg);
  43. }
  44. },
  45. "Should fail if array1 is not an array": function testArg1() {
  46. var array1 = "not an array",
  47. array2 = [6, 7, 8, 9],
  48. input = [array1,array2],
  49. idGenerator = new VariablesIdGenerator(),
  50. vps = new VariablesParseState(idGenerator),
  51. expr = Expression.parseExpression("$setIsSubset", input, vps);
  52. assert.throws(function () {
  53. expr.evaluate({});
  54. });
  55. },
  56. "Should fail if array2 is not an array": function testArg2() {
  57. var array1 = [1, 2, 3, 4],
  58. array2 = "not an array",
  59. input = [array1,array2],
  60. idGenerator = new VariablesIdGenerator(),
  61. vps = new VariablesParseState(idGenerator),
  62. expr = Expression.parseExpression("$setIsSubset", input, vps);
  63. assert.throws(function () {
  64. expr.evaluate({});
  65. });
  66. },
  67. "Should fail if both are not an array": function testArg1andArg2() {
  68. var array1 = "not an array",
  69. array2 = "not an array",
  70. input = [array1,array2],
  71. idGenerator = new VariablesIdGenerator(),
  72. vps = new VariablesParseState(idGenerator),
  73. expr = Expression.parseExpression("$setIsSubset", input, vps);
  74. assert.throws(function () {
  75. expr.evaluate({});
  76. });
  77. },
  78. "Should pass and return a true": function(){
  79. this.compare([2, 3], [1, 2, 3, 4, 5], true);
  80. },
  81. "Should pass and return false": function() {
  82. this.compare([1, 2, 3, 4, 5], [7, 8, 9], false);
  83. },
  84. "Should return false when the 1st array is not empty and the 2nd array is": function() {
  85. this.compare([1, 2, 3, 4, 5], [], false);
  86. },
  87. "Should return true if an 1st array is empty and the 2nd is not": function () {
  88. this.compare([],[1, 2, 3, 4, 5], true);
  89. },
  90. "Should return true if both are empty": function () {
  91. this.compare([],[],true);
  92. },
  93. "should not consider a non-nested source array the same as a nested object array": function() {
  94. this.compare([1], [[1]], false);
  95. },
  96. "should not consider a nested source the same as an unnested object array": function(){
  97. this.compare([[1]], [1], false);
  98. },
  99. "should ignore dups in the source": function(){
  100. this.compare([1,2,1], [1,2], true);
  101. }
  102. }
  103. }
  104. };
  105. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);