ConcatExpression_test.js 2.8 KB

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