AndExpression_test.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. "use strict";
  2. var assert = require("assert"),
  3. AndExpression = require("../../../../lib/pipeline/expressions/AndExpression"),
  4. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  5. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  6. CoerceToBoolExpression = require("../../../../lib/pipeline/expressions/CoerceToBoolExpression"),
  7. ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression"),
  8. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
  9. Expression = require("../../../../lib/pipeline/expressions/Expression");
  10. module.exports = {
  11. "AndExpression": {
  12. beforeEach: function() {
  13. this.vps = new VariablesParseState(new VariablesIdGenerator());
  14. },
  15. "constructor()": {
  16. "should not throw Error when constructing without args": function testConstructor(){
  17. assert.doesNotThrow(function(){
  18. new AndExpression();
  19. });
  20. },
  21. "should throw Error when constructing with args": function testConstructor(){
  22. assert.throws(function(){
  23. new AndExpression(1);
  24. });
  25. }
  26. },
  27. "#getOpName()": {
  28. "should return the correct op name; $and": function testOpName(){
  29. assert.equal(new AndExpression().getOpName(), "$and");
  30. }
  31. },
  32. "#evaluate()": {
  33. "should return true if no operands were given; {$and:[]}": function testEmpty(){
  34. assert.equal(Expression.parseOperand({$and:[]},this.vps).evaluate(), true);
  35. },
  36. "should return true if operands is one true; {$and:[true]}": function testTrue(){
  37. assert.equal(Expression.parseOperand({$and:[true]},this.vps).evaluate(), true);
  38. },
  39. "should return false if operands is one false; {$and:[false]}": function testFalse(){
  40. assert.equal(Expression.parseOperand({$and:[false]},this.vps).evaluate(), false);
  41. },
  42. "should return true if operands are true and true; {$and:[true,true]}": function testTrueTrue(){
  43. assert.equal(Expression.parseOperand({$and:[true,true]},this.vps).evaluate(), true);
  44. },
  45. "should return false if operands are true and false; {$and:[true,false]}": function testTrueFalse(){
  46. assert.equal(Expression.parseOperand({$and:[true,false]},this.vps).evaluate(), false);
  47. },
  48. "should return false if operands are false and true; {$and:[false,true]}": function testFalseTrue(){
  49. assert.equal(Expression.parseOperand({$and:[false,true]},this.vps).evaluate(), false);
  50. },
  51. "should return false if operands are false and false; {$and:[false,false]}": function testFalseFalse(){
  52. assert.equal(Expression.parseOperand({$and:[false,false]},this.vps).evaluate(), false);
  53. },
  54. "should return true if operands are true, true, and true; {$and:[true,true,true]}": function testTrueTrueTrue(){
  55. assert.equal(Expression.parseOperand({$and:[true,true,true]},this.vps).evaluate(), true);
  56. },
  57. "should return false if operands are true, true, and false; {$and:[true,true,false]}": function testTrueTrueFalse(){
  58. assert.equal(Expression.parseOperand({$and:[true,true,false]},this.vps).evaluate(), false);
  59. },
  60. "should return false if operands are 0 and 1; {$and:[0,1]}": function testZeroOne(){
  61. assert.equal(Expression.parseOperand({$and:[0,1]},this.vps).evaluate(), false);
  62. },
  63. "should return false if operands are 1 and 2; {$and:[1,2]}": function testOneTwo(){
  64. assert.equal(Expression.parseOperand({$and:[1,2]},this.vps).evaluate(), true);
  65. },
  66. "should return true if operand is a path String to a truthy value; {$and:['$a']}": function testFieldPath(){
  67. assert.equal(Expression.parseOperand({$and:['$a']},this.vps).evaluate({a:1}), true);
  68. }
  69. },
  70. "#optimize()": {
  71. "should optimize a constant expression to a constant; {$and:[1]} == true": function testOptimizeConstantExpression(){
  72. var a = Expression.parseOperand({$and:[1]}, this.vps).optimize();
  73. assert.equal(a.operands.length, 0, "The operands should have been optimized away");
  74. assert.equal(a.evaluateInternal(), true);
  75. },
  76. "should not optimize a non-constant expression; {$and:['$a']}": function testNonConstant(){
  77. var a = Expression.parseOperand({$and:['$a']}, this.vps).optimize();
  78. assert.equal(a.operands[0]._fieldPath.fieldNames.length, 2);
  79. assert.deepEqual(a.operands[0]._fieldPath.fieldNames[0], "CURRENT");
  80. assert.deepEqual(a.operands[0]._fieldPath.fieldNames[1], "a");
  81. },
  82. "should not optimize an expression ending with a non-constant. {$and:[1,'$a']};": function testConstantNonConstant(){
  83. var a = Expression.parseOperand({$and:[1,'$a']}, this.vps).optimize();
  84. assert(a instanceof CoerceToBoolExpression);
  85. assert(a.expression instanceof FieldPathExpression);
  86. assert.equal(a.expression._fieldPath.fieldNames.length, 2);
  87. assert.equal(a.expression._fieldPath.fieldNames[0], "CURRENT");
  88. assert.equal(a.expression._fieldPath.fieldNames[1], "a");
  89. },
  90. "should optimize an expression with a path and a '1'; {$and:['$a',1]}": function testNonConstantOne(){
  91. var a = Expression.parseOperand({$and:['$a', 1]}, this.vps).optimize();
  92. // The 1 should be removed as it is redundant.
  93. assert(a instanceof CoerceToBoolExpression, "The result should be forced to a boolean");
  94. // This is the '$a' which cannot be optimized.
  95. assert.equal(a.expression._fieldPath.fieldNames.length, 2);
  96. assert.equal(a.expression._fieldPath.fieldNames[0], "CURRENT");
  97. assert.equal(a.expression._fieldPath.fieldNames[1], "a");
  98. },
  99. "should optimize an expression with a field path and a '0'; {$and:['$a',0]}": function testNonConstantZero(){
  100. var a = Expression.parseOperand({$and:['$a',0]}, this.vps).optimize();
  101. assert.equal(a.operands.length, 0, "The operands should have been optimized away");
  102. assert.equal(a.evaluateInternal(), false, "The 0 operand should have been converted to false");
  103. },
  104. "should optimize an expression with two field paths and '1'; {$and:['$a','$b',1]}": function testNonConstantNonConstantOne(){
  105. var a = Expression.parseOperand({$and:['$a', '$b', 1]}, this.vps).optimize();
  106. assert.equal(a.operands.length, 2, "Two operands should remain.");
  107. // This is the '$a' which cannot be optimized.
  108. assert.deepEqual(a.operands[0]._fieldPath.fieldNames.length, 2);
  109. assert.deepEqual(a.operands[0]._fieldPath.fieldNames[0], "CURRENT");
  110. assert.deepEqual(a.operands[0]._fieldPath.fieldNames[1], "a");
  111. // This is the '$b' which cannot be optimized.
  112. assert.deepEqual(a.operands[1]._fieldPath.fieldNames.length, 2);
  113. assert.deepEqual(a.operands[1]._fieldPath.fieldNames[0], "CURRENT");
  114. assert.deepEqual(a.operands[1]._fieldPath.fieldNames[1], "b");
  115. },
  116. "should optimize an expression with two field paths and '0'; {$and:['$a','$b',0]}": function testNonConstantNonConstantZero(){
  117. var a = Expression.parseOperand({$and:['$a', '$b', 0]}, this.vps).optimize();
  118. assert(a instanceof ConstantExpression, "With that trailing false, we know the result...");
  119. assert.equal(a.operands.length, 0, "The operands should have been optimized away");
  120. assert.equal(a.evaluateInternal(), false);
  121. },
  122. "should optimize an expression with '0', '1', and a field path; {$and:[0,1,'$a']}": function testZeroOneNonConstant(){
  123. var a = Expression.parseOperand({$and:[0,1,'$a']}, this.vps).optimize();
  124. assert(a instanceof ConstantExpression);
  125. assert.equal(a.evaluateInternal(), false);
  126. },
  127. "should optimize an expression with '1', '1', and a field path; {$and:[1,1,'$a']}": function testOneOneNonConstant(){
  128. var a = Expression.parseOperand({$and:[1,1,'$a']}, this.vps).optimize();
  129. assert(a instanceof CoerceToBoolExpression);
  130. assert(a.expression instanceof FieldPathExpression);
  131. assert.equal(a.expression._fieldPath.fieldNames.length, 2);
  132. assert.equal(a.expression._fieldPath.fieldNames[0], "CURRENT");
  133. assert.equal(a.expression._fieldPath.fieldNames[1], "a");
  134. },
  135. "should optimize nested $and expressions properly and optimize out values evaluating to true; {$and:[1,{$and:[1]},'$a','$b']}": function testNested(){
  136. var a = Expression.parseOperand({$and:[1,{$and:[1]},'$a','$b']}, this.vps).optimize();
  137. assert.equal(a.operands.length, 2)
  138. assert(a.operands[0] instanceof FieldPathExpression);
  139. assert(a.operands[1] instanceof FieldPathExpression);
  140. },
  141. "should optimize nested $and expressions containing a nested value evaluating to false; {$and:[1,{$and:[1]},'$a','$b']}": function testNested(){
  142. //assert.deepEqual(Expression.parseOperand({$and:[1,{$and:[{$and:[0]}]},'$a','$b']}, this.vps).optimize().toJSON(true), {$const:false});
  143. var a = Expression.parseOperand({$and:[1,{$and:[{$and:[0]}]},'$a','$b']}, this.vps).optimize();
  144. assert(a instanceof ConstantExpression);
  145. assert.equal(a.evaluateInternal(), false);
  146. },
  147. "should optimize when the constants are on the right of the operand list. The rightmost is true": function(){
  148. // 1, "x", and 1 are all true. They should be optimized away.
  149. var a = Expression.parseOperand({$and:['$a', 1, "x", 1]}, this.vps).optimize();
  150. assert(a instanceof CoerceToBoolExpression);
  151. assert(a.expression instanceof FieldPathExpression);
  152. assert.equal(a.expression._fieldPath.fieldNames.length, 2);
  153. assert.equal(a.expression._fieldPath.fieldNames[0], "CURRENT");
  154. assert.equal(a.expression._fieldPath.fieldNames[1], "a");
  155. },
  156. "should optimize when the constants are on the right of the operand list. The rightmost is false": function(){
  157. // 1, "x", and 1 are all true. They should be optimized away.
  158. var a = Expression.parseOperand({$and:['$a', 1, "x", 0]}, this.vps).optimize();
  159. assert(a instanceof ConstantExpression, "The rightmost false kills it all");
  160. assert.equal(a.evaluateInternal(), false);
  161. }
  162. }
  163. }
  164. };
  165. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);