IfNullExpression_test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // Mocha one-liner to make these tests self-hosted
  9. if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
  10. exports.IfNullExpression = {
  11. "constructor()": {
  12. "should not throw Error when constructing without args": function() {
  13. assert.doesNotThrow(function () {
  14. new IfNullExpression();
  15. });
  16. },
  17. "should throw Error when constructing with args": function () {
  18. assert.throws(function () {
  19. new IfNullExpression(1);
  20. });
  21. },
  22. },
  23. "#getOpName()": {
  24. "should return the correct op name; $ifNull": function() {
  25. assert.equal(new IfNullExpression().getOpName(), "$ifNull");
  26. },
  27. },
  28. "#evaluate()": {
  29. beforeEach: function () {
  30. this.vps = new VariablesParseState(new VariablesIdGenerator());
  31. this.parsed = Expression.parseExpression("$ifNull", ["$a", "$b"], this.vps);
  32. },
  33. "should return the left hand side if the left hand side is not null or undefined": function() {
  34. assert.strictEqual(this.parsed.evaluate({a: 1, b: 2}), 1);
  35. },
  36. "should return the right hand side if the left hand side is null": function() {
  37. assert.strictEqual(this.parsed.evaluate({a: null, b: 2}), 2);
  38. },
  39. "should return the right hand side if the left hand side is undefined": function() {
  40. assert.strictEqual(this.parsed.evaluate({b: 2}), 2);
  41. },
  42. },
  43. };