IfNullExpression_test.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. this.vars = new Variables(2);
  32. this.vars.setValue(0, "a");
  33. this.vars.setValue(1, "b");
  34. this.makeParsed = function(a, b) {
  35. return Expression.parseExpression("$ifNull", [a, b], this.vps);
  36. }
  37. },
  38. "should return the left hand side if the left hand side is not null or undefined": function() {
  39. //assert.strictEqual(this.parsed.evaluate(this.vars), 1);
  40. assert.strictEqual(this.makeParsed(1, 2).evaluate(this.vars), 1);
  41. },
  42. "should return the right hand side if the left hand side is null": function() {
  43. //assert.strictEqual(this.parsed.evaluate({a: null, b: 2}), 2);
  44. assert.strictEqual(this.makeParsed(null, 2).evaluate(this.vars), 2);
  45. },
  46. "should return the right hand side if the left hand side is undefined": function() {
  47. //assert.strictEqual(this.parsed.evaluate({b: 2}), 2);
  48. assert.strictEqual(this.makeParsed(undefined, 2).evaluate(this.vars), 2);
  49. }
  50. }
  51. }
  52. };
  53. if (!module.parent)(new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);