ConcatExpression_test.js 3.2 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. module.exports = {
  8. "ConcatExpression": {
  9. beforeEach: function(){
  10. this.vps = new VariablesParseState(new VariablesIdGenerator());
  11. },
  12. "constructor()": {
  13. "should not throw Error when constructing without args": function testConstructor(){
  14. assert.doesNotThrow(function(){
  15. new ConcatExpression();
  16. });
  17. },
  18. "should throw Error when constructing with args": function testConstructor(){
  19. assert.throws(function(){
  20. new ConcatExpression("should die");
  21. });
  22. }
  23. },
  24. "#getOpName()": {
  25. "should return the correct op name; $concat": function testOpName(){
  26. assert.equal(new ConcatExpression().getOpName(), "$concat");
  27. }
  28. },
  29. "#evaluate()": {
  30. "should return empty string if no operands were given; {$concat:[]}": function testEmpty(){
  31. assert.equal(Expression.parseOperand({$concat:[]}, this.vps).evaluate(), "");
  32. },
  33. "should return mystring if operands are my string; {$concat:[my, string]}": function testConcat(){
  34. assert.equal(Expression.parseOperand({$concat:["my", "string"]}, this.vps).evaluate(), "mystring");
  35. },
  36. "should return mystring if operands are my and $a; {$concat:[my,$a]}": function testFieldPath(){
  37. assert.equal(Expression.parseOperand({$concat:["my","$a"]}, this.vps).evaluate({a:"string"}), "mystring");
  38. },
  39. //NOTE: DEVIATION FROM MONGO: I can't find the test cases for $concat. The following test
  40. // causes an exception as will any other non-string operand. I don't know the authority for
  41. // defining the behavior.
  42. // "should return null if an operand evaluates to null; {$concat:[my,$a]}": function testNull(){
  43. // var a = Expression.parseOperand({$concat:["my","$a"]}, this.vps);
  44. // var b = a.evaluate({a:null});
  45. // assert.equal(b, null);
  46. // },
  47. "should throw if an operand is a null": function testNull(){
  48. assert.throws(function(){
  49. Expression.parseOperand({$concat:["my","$a"]}, this.vps).evaluate({a:null});
  50. });
  51. },
  52. "should throw if an operand is a null (2)": function testNull(){
  53. assert.throws(function(){
  54. Expression.parseOperand({$concat:[null,"$a"]}, this.vps).evaluate({a:"hello"});
  55. });
  56. },
  57. "should throw if an operand is a number": function testNull(){
  58. assert.throws(function(){
  59. Expression.parseOperand({$concat:["my","$a"]}, this.vps).evaluate({a:100});
  60. });
  61. },
  62. "should throw if an operand is a date": function testNull(){
  63. assert.throws(function(){
  64. Expression.parseOperand({$concat:["my","$a"]}, this.vps).evaluate({a:new Date()});
  65. });
  66. },
  67. "should throw if an operand is a boolean": function testNull(){
  68. assert.throws(function(){
  69. Expression.parseOperand({$concat:["my","$a"]}, this.vps).evaluate({a:true});
  70. });
  71. }
  72. }
  73. }
  74. };
  75. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);