LetExpression_test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. var assert = require("assert"),
  3. LetExpression = require("../../../../lib/pipeline/expressions/LetExpression"),
  4. ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression"),
  5. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
  6. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  7. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  8. Expression = require("../../../../lib/pipeline/expressions/Expression");
  9. module.exports = {
  10. "LetExpression": {
  11. beforeEach: function() {
  12. this.vps = new VariablesParseState(new VariablesIdGenerator());
  13. },
  14. "constructor()": {
  15. "should throw an Error when constructing without args": function () {
  16. assert.throws(function () {
  17. new LetExpression();
  18. });
  19. },
  20. "should throw Error when constructing with one arg": function () {
  21. assert.throws(function () {
  22. new LetExpression(1);
  23. });
  24. },
  25. "should not throw when constructing with two args": function () {
  26. assert.doesNotThrow(function () {
  27. new LetExpression(1, 2);
  28. });
  29. },
  30. "#parse()": {
  31. beforeEach: function(){
  32. var self = this;
  33. this.dieForTheRightReason = function(expr, regex) {
  34. var self = this;
  35. assert.throws(function () {
  36. Expression.parseOperand(expr, self.vps);
  37. }, regex);
  38. }
  39. },
  40. "should throw if $let isn't in expr": function () {
  41. this.dieForTheRightReason({$xlet: ['$$a', 1]}, /15999/);
  42. },
  43. "should throw if the $let expression isn't an object": function () {
  44. this.dieForTheRightReason({$let: "this is not an object"}, /16874/);
  45. },
  46. "should throw if the $let expression is an array": function () {
  47. this.dieForTheRightReason({$let: [1, 2, 3]}, /16874/);
  48. },
  49. "should throw if there is no vars parameter to $let": function () {
  50. this.dieForTheRightReason({$let: {noVars: 1}}, /16876/);
  51. },
  52. "should throw if there is no input parameter to $let": function () {
  53. this.dieForTheRightReason({$let: {vars: 1, noIn: 2}}, /16877/);
  54. },
  55. "should throw if any of the arguments to $let are not 'in' or 'var'": function () {
  56. this.dieForTheRightReason({$let: {vars: 1, in: 2, zoot:3}}, /16875/);
  57. },
  58. "should throw if the var name is not writable": function () {
  59. this.dieForTheRightReason({$let: {vars: ["$$bad$$"], in: 2}}, /16867/);
  60. },
  61. "should return a Let expression": function () {
  62. var x = Expression.parseOperand({$let: {vars: ["a"], in: 2}}, this.vps);
  63. assert(x instanceof LetExpression);
  64. assert(x._subExpression instanceof ConstantExpression);
  65. assert(x._subExpression.getValue() == 2);
  66. assert(x._variables[0].a instanceof ConstantExpression);
  67. assert(x._variables[0].a.getValue()[0] === 'a');
  68. },
  69. "should show we collect multiple vars": function() {
  70. var x = Expression.parseOperand({$let: {vars: ["a", "b", "c"], in: 2}}, this.vps);
  71. assert.deepEqual(x._variables[0].a.getValue(), ['a','b','c']);
  72. assert.deepEqual(x._variables[1].b.getValue(), ['a','b','c']);
  73. assert.deepEqual(x._variables[2].c.getValue(), ['a','b','c']);
  74. },
  75. "should show we require vars to be wrapped in an array.": function () {
  76. var self = this;
  77. assert.throws(function () {
  78. Expression.parseOperand({$let: {vars: "a", in: 2}}, self.vps);
  79. }, /TypeError: Object a has no method 'forEach'/);
  80. }
  81. },
  82. "#optimize()": {
  83. "should optimize subexpressions if there are no variables": function () {
  84. assert(false);
  85. },
  86. "should optimize variables": function () {
  87. assert(false);
  88. },
  89. "should optimize subexpressions if there are variables": function () {
  90. assert(false);
  91. }
  92. },
  93. "#serialize()": {
  94. "should serialize variables and the subexpression": function () {
  95. assert(false);
  96. }
  97. },
  98. "#evaluateInternal()": {
  99. "should preform the evaluation for variables and the subexpression": function () {
  100. assert(false);
  101. }
  102. }
  103. }
  104. }
  105. };
  106. if (!module.parent)(new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);