ConcatExpression_test.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. var assert = require("assert"),
  3. ConcatExpression = require("../../../../lib/pipeline/expressions/ConcatExpression"),
  4. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  5. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  6. Expression = require("../../../../lib/pipeline/expressions/Expression");
  7. // Mocha one-liner to make these tests self-hosted
  8. 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));
  9. exports.ConcatExpression = {
  10. beforeEach: function() {
  11. this.vps = new VariablesParseState(new VariablesIdGenerator());
  12. },
  13. "constructor()": {
  14. "should not throw Error when constructing without args": function() {
  15. assert.doesNotThrow(function() {
  16. new ConcatExpression();
  17. });
  18. },
  19. "should throw Error when constructing with args": function() {
  20. assert.throws(function() {
  21. new ConcatExpression("should die");
  22. });
  23. },
  24. },
  25. "#getOpName()": {
  26. "should return the correct op name; $concat": function() {
  27. assert.equal(new ConcatExpression().getOpName(), "$concat");
  28. },
  29. },
  30. "#evaluate()": {
  31. "should return empty string if no operands were given; {$concat:[]}": function() {
  32. var expr = Expression.parseOperand({$concat:[]}, this.vps);
  33. assert.equal(expr.evaluate(), "");
  34. },
  35. "should return mystring if operands are my string; {$concat:[my, string]}": function() {
  36. var expr = Expression.parseOperand({$concat:["my", "string"]}, this.vps);
  37. assert.equal(expr.evaluate(), "mystring");
  38. },
  39. "should return mystring if operands are my and $a; {$concat:[my,$a]}": function() {
  40. var expr = Expression.parseOperand({$concat:["my","$a"]}, this.vps);
  41. assert.equal(expr.evaluate({a:"string"}), "mystring");
  42. },
  43. "should return null if an operand evaluates to null; {$concat:[my,$a]}": function() {
  44. var expr = Expression.parseOperand({$concat:["my","$a"]}, this.vps);
  45. assert.equal(expr.evaluate({a:null}), null);
  46. },
  47. "should return null if an operand evaluates to undefined; {$concat:[my,$a]}": function() {
  48. var expr = Expression.parseOperand({$concat:["my","$a"]}, this.vps);
  49. assert.equal(expr.evaluate({a:undefined}), null);
  50. },
  51. "should throw if an operand is a number": function() {
  52. var expr = Expression.parseOperand({$concat:["my","$a"]}, this.vps);
  53. assert.throws(function() {
  54. expr.evaluate({a:100});
  55. });
  56. },
  57. "should throw if an operand is a date": function() {
  58. var expr = Expression.parseOperand({$concat:["my","$a"]}, this.vps);
  59. assert.throws(function() {
  60. expr.evaluate({a:new Date()});
  61. });
  62. },
  63. "should throw if an operand is a boolean": function() {
  64. var expr = Expression.parseOperand({$concat:["my","$a"]}, this.vps);
  65. assert.throws(function() {
  66. expr.evaluate({a:true});
  67. });
  68. },
  69. },
  70. };