IfNullExpression.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. var assert = require("assert"),
  3. IfNullExpression = require("../../../../lib/pipeline/expressions/IfNullExpression"),
  4. Expression = require("../../../../lib/pipeline/expressions/Expression");
  5. module.exports = {
  6. "IfNullExpression": {
  7. "constructor()": {
  8. "should not throw Error when constructing without args": function() {
  9. assert.doesNotThrow(function () {
  10. new IfNullExpression();
  11. });
  12. },
  13. "should throw Error when constructing with args": function () {
  14. assert.throws(function () {
  15. new IfNullExpression();
  16. });
  17. }
  18. },
  19. "#getOpName()": {
  20. "should return the correct op name; $ifNull": function() {
  21. assert.equal(new IfNullExpression().getOpName(), "$ifNull");
  22. }
  23. },
  24. "#evaluateInternal()": {
  25. beforeEach: function () {
  26. this.expr = {$ifNull:["$a", "$b"]};
  27. this.parsed = Expression.parseOperand(this.expr, {});
  28. },
  29. "should return the left hand side if the left hand side is not null or undefined": function() {
  30. assert.strictEqual(this.parsed.evaluateInternal({a: 1, b: 2}), 1);
  31. },
  32. "should return the right hand side if the left hand side is null": function() {
  33. assert.strictEqual(this.parsed.evaluateInternal({a: null, b: 2}), 2);
  34. },
  35. "should return the right hand side if the left hand side is undefined": function() {
  36. assert.strictEqual(this.parsed.evaluateInternal({b: 2}), 2);
  37. }
  38. }
  39. }
  40. };
  41. if (!module.parent)(new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);