IfNullExpression_test.js 1.8 KB

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