AddExpression.js 3.7 KB

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