CoerceToBoolExpression.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. var assert = require("assert"),
  3. CoerceToBoolExpression = require("../../../../lib/pipeline/expressions/CoerceToBoolExpression"),
  4. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
  5. ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression");
  6. module.exports = {
  7. "CoerceToBoolExpression": {
  8. "constructor()": {
  9. "should throw Error if no args": function construct(){
  10. assert.throws(function(){
  11. new CoerceToBoolExpression();
  12. });
  13. },
  14. "should throw Error if more than 1 arg": function construct(){
  15. assert.throws(function(){
  16. var a = b = "foo";
  17. new CoerceToBoolExpression(a,b);
  18. });
  19. },
  20. "should not throw Error if 1 arg": function construct(){
  21. assert.doesNotThrow(function(){
  22. var a = "foo";
  23. new CoerceToBoolExpression(a);
  24. });
  25. },
  26. },
  27. "#evaluate()": {
  28. "should return true if nested expression is coerced to true; {$const:5}": function testEvaluateTrue(){
  29. var expr = new CoerceToBoolExpression(new ConstantExpression(5));
  30. assert.equal(expr.evaluateInternal({}), true);
  31. },
  32. "should return false if nested expression is coerced to false; {$const:0}": function testEvaluateFalse(){
  33. var expr = new CoerceToBoolExpression(new ConstantExpression(0));
  34. assert.equal(expr.evaluateInternal({}), false);
  35. }
  36. },
  37. /**
  38. * These tests should just work after the FieldPathExpression Stuff is ported.
  39. **/
  40. "#toJSON()": {
  41. "should serialize as $and which will coerceToBool; '$foo'": function(){
  42. var expr = new CoerceToBoolExpression(new FieldPathExpression('foo'));
  43. assert.deepEqual(expr.toJSON(), {$and:['$foo']});
  44. }
  45. },
  46. "#addDependencies()": {
  47. "should forward dependencies of nested expression": function testDependencies(){
  48. var nested = new FieldPathExpression.create("a.b"),
  49. expr = new CoerceToBoolExpression(nested),
  50. deps = new DepsTracker();
  51. expr.addDependencies(deps);
  52. assert.equal(Object.keys(deps).length, 1);
  53. assert.ok(deps['a.b']);
  54. }
  55. }
  56. }
  57. };
  58. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);