CondExpression_test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. var assert = require("assert"),
  3. CondExpression = require("../../../../lib/pipeline/expressions/CondExpression"),
  4. Expression = require("../../../../lib/pipeline/expressions/Expression");
  5. module.exports = {
  6. "CondExpression": {
  7. "constructor()": {
  8. "should not throw an Error when constructing without args": function testConstructor(){
  9. assert.doesNotThrow(function(){
  10. new CondExpression();
  11. });
  12. },
  13. "should throw Error when constructing with 1 arg": function testConstructor1(){
  14. assert.throws(function(){
  15. new CondExpression(1);
  16. });
  17. }
  18. },
  19. "#getOpName()": {
  20. "should return the correct op name; $cond": function testOpName(){
  21. assert.equal(new CondExpression().getOpName(), "$cond");
  22. }
  23. },
  24. "#evaluateInternal()": {
  25. "array style": {
  26. "should fail if there aren't enough arguments": function() {
  27. assert.throws(function(){
  28. Expression.parseOperand({$cond:[1,2]}, {});
  29. })
  30. },
  31. "should fail if there are too many arguments": function() {
  32. assert.throws(function(){
  33. Expression.parseOperand({$cond:[1, 2, 3, 4]}, {});
  34. })
  35. },
  36. "should evaluate boolean expression as true, then return 1; [ true === true, 1, 0 ]": function () {
  37. assert.strictEqual(Expression.parseOperand({$cond: [ true, 1, 0 ]}, {}).evaluateInternal({}), 1);
  38. },
  39. "should evaluate boolean expression as false, then return 0; [ false === true, 1, 0 ]": function () {
  40. assert.strictEqual(Expression.parseOperand({$cond: [ false, 1, 0 ]}, {}).evaluateInternal({}), 0);
  41. },
  42. "should fail when the 'if' position is empty": function(){
  43. assert.throws(function(){
  44. Expression.parseOperand({$cond:[undefined, 2, 3]}, {});
  45. })
  46. },
  47. "should fail when the 'then' position is empty": function(){
  48. assert.throws(function(){
  49. Expression.parseOperand({$cond:[1, undefined, 3]}, {});
  50. })
  51. },
  52. "should fail when the 'else' position is empty": function(){
  53. assert.throws(function(){
  54. Expression.parseOperand({$cond:[1, 2, undefined]}, {});
  55. })
  56. }
  57. },
  58. "object style": {
  59. beforeEach: function(){
  60. this.shouldFail = function(expr) {
  61. assert.throws(function(){
  62. Expression.parseOperand(expr, {});
  63. });
  64. }
  65. },
  66. "should fail because the $cond is missing": function(){
  67. this.shouldFail({$zoot:[true, 1, 0 ]}, {});
  68. },
  69. "should fail because of missing if": function(){
  70. this.shouldFail({$cond:{xif:1, then:2, else:3}});
  71. },
  72. "should fail because of missing then": function(){
  73. this.shouldFail({$cond:{if:1, xthen:2, else:3}});
  74. },
  75. "should fail because of missing else": function(){
  76. this.shouldFail({$cond:{if:1, then:2, xelse:3}});
  77. },
  78. "should fail because of empty if": function(){
  79. this.shouldFail({$cond:{if:undefined, then:2, else:3}});
  80. },
  81. "should fail because of empty then": function(){
  82. this.shouldFail({$cond:{if:1, then:undefined, else:3}});
  83. },
  84. "should fail because of empty else": function(){
  85. this.shouldFail({$cond:{if:1, then:2, else:undefined}});
  86. },
  87. "should fail because of mystery args": function(){
  88. this.shouldFail({$cond:{if:1, then:2, else:3, zoot:4}});
  89. },
  90. "should evaluate true": function(){
  91. assert.strictEqual(
  92. Expression.parseOperand({$cond:{ if: true, then: 1, else: 0}}, {}).evaluate({}),
  93. 1);
  94. },
  95. "should evaluate true even with mixed up args": function(){
  96. assert.strictEqual(
  97. Expression.parseOperand({$cond:{ else: 0, then: 1, if: "$a" }}, {}).evaluate({$a: 1}),
  98. 1);
  99. },
  100. "should evaluate false": function(){
  101. assert.strictEqual(
  102. Expression.parseOperand({$cond:{ if: "$a", then: 0, else: 1}}, {}).evaluate({$a: 0}),
  103. 1);
  104. },
  105. "should evaluate false even with mixed up args": function() {
  106. assert.strictEqual(
  107. Expression.parseOperand({$cond: { else: 1, then: 0, if: "$a"}}, {}).evaluate({$a: 0}),
  108. 1);
  109. }
  110. }
  111. }
  112. }
  113. };
  114. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);