CondExpression_test.js 4.3 KB

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