AddExpression.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var assert = require("assert"),
  2. AddExpression = require("../../../../lib/pipeline/expressions/AddExpression"),
  3. ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression");
  4. //TODO: refactor these test cases using Expression.parseOperand() or something because these could be a whole lot cleaner...
  5. module.exports = {
  6. "AddExpression": {
  7. "constructor()": {
  8. "should not throw Error when constructing without args": function testConstructor(){
  9. assert.doesNotThrow(function(){
  10. new AddExpression();
  11. });
  12. }
  13. },
  14. "#getOpName()": {
  15. "should return the correct op name; $add": function testOpName(){
  16. assert.equal(new AddExpression().getOpName(), "$add");
  17. }
  18. },
  19. "#getFactory()": {
  20. "should return the constructor for this class": function factoryIsConstructor(){
  21. assert.equal((new AddExpression()).getFactory(), AddExpression);
  22. }
  23. },
  24. "#evaluate()": {
  25. "should return the operand if null document is given": function nullDocument(){
  26. var expr = new AddExpression();
  27. expr.addOperand(new ConstantExpression(2));
  28. assert.equal(expr.evaluate(null), 2);
  29. },
  30. "should return 0 if no operands were given": function noOperands(){
  31. var expr = new AddExpression();
  32. assert.equal(expr.evaluate({}), 0);
  33. },
  34. "should throw Error if a Date operand was given": function date(){
  35. var expr = new AddExpression();
  36. expr.addOperand(new ConstantExpression(new Date()));
  37. assert.throws(function(){
  38. expr.evaluate({});
  39. });
  40. },
  41. "should throw Error if a String operand was given": function string(){
  42. var expr = new AddExpression();
  43. expr.addOperand(new ConstantExpression(""));
  44. assert.throws(function(){
  45. expr.evaluate({});
  46. });
  47. },
  48. "should throw Error if a Boolean operand was given": function bool() {
  49. var expr = new AddExpression();
  50. expr.addOperand(new ConstantExpression(true));
  51. assert.throws(function() {
  52. expr.evaluate({});
  53. });
  54. },
  55. "should pass thru a single number": function number() {
  56. var expr = new AddExpression(),
  57. input = 123,
  58. expected = 123;
  59. expr.addOperand(new ConstantExpression(input));
  60. assert.equal(expr.evaluate({}), expected);
  61. },
  62. "should pass thru a single null": function nullSupport() {
  63. var expr = new AddExpression(),
  64. input = null,
  65. expected = 0;
  66. expr.addOperand(new ConstantExpression(input));
  67. assert.equal(expr.evaluate({}), expected);
  68. },
  69. "should pass thru a single undefined": function undefinedSupport() {
  70. var expr = new AddExpression(),
  71. input,
  72. expected = 0;
  73. expr.addOperand(new ConstantExpression(input));
  74. assert.equal(expr.evaluate({}), expected);
  75. },
  76. "should add two numbers": function numbers() {
  77. var expr = new AddExpression(),
  78. inputs = [1, 5],
  79. expected = 6;
  80. inputs.forEach(function(input) {
  81. expr.addOperand(new ConstantExpression(input));
  82. });
  83. assert.equal(expr.evaluate({}), expected);
  84. },
  85. "should add a number and a null": function numberAndNull() {
  86. var expr = new AddExpression(),
  87. inputs = [1, null],
  88. expected = 1;
  89. inputs.forEach(function(input) {
  90. expr.addOperand(new ConstantExpression(input));
  91. });
  92. assert.equal(expr.evaluate({}), expected);
  93. },
  94. "should add a number and an undefined": function numberAndUndefined() {
  95. var expr = new AddExpression(),
  96. inputs = [1, undefined],
  97. expected = 1;
  98. inputs.forEach(function(input) {
  99. expr.addOperand(new ConstantExpression(input));
  100. });
  101. assert.equal(expr.evaluate({}), expected);
  102. }
  103. }
  104. }
  105. };
  106. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);