IfNullExpression_test.js 1.7 KB

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