CondExpression_test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. },
  43. "object style": {
  44. beforeEach: function(){
  45. this.shouldFail = function(expr) {
  46. assert.throws(function(){
  47. Expression.parseOperand(expr, {});
  48. });
  49. }
  50. },
  51. "should fail because the $cond is missing": function(){
  52. this.shouldFail({$zoot:[true, 1, 0 ]}, {});
  53. },
  54. "should fail because of missing if": function(){
  55. this.shouldFail({$cond:{xif:1, then:2, else:3}});
  56. },
  57. "should fail because of missing then": function(){
  58. this.shouldFail({$cond:{if:1, xthen:2, else:3}});
  59. },
  60. "should fail because of missing else": function(){
  61. this.shouldFail({$cond:{if:1, then:2, xelse:3}});
  62. },
  63. "should fail because of mystery args": function(){
  64. this.shouldFail({$cond:{if:1, then:2, else:3, zoot:4}});
  65. },
  66. "should evaluate true": function(){
  67. assert.strictEqual(
  68. Expression.parseOperand({$cond:{ if: $a, then: 1, else: 0}}, {}).evaluate({$a: 1}),
  69. 1);
  70. },
  71. "should evaluate true even with mixed up args": function(){
  72. assert.strictEqual(
  73. Expression.parseOperand({$cond:{ else: 0, then: 1, if: $a }}, {}).evaluate({$a: 1}),
  74. 1);
  75. },
  76. "should evaluate false": function(){
  77. assert.strictEqual(
  78. Expression.parseOperand({$cond:{ if: $a, then: 0, else: 1}}, {}).evaluate({$a: 0}),
  79. 1);
  80. },
  81. "should evaluate false even with mixed up args": function() {
  82. assert.strictEqual(
  83. Expression.parseOperand({$cond: { else: 1, then: 0, if: $a}}, {}).evaluate({$a: 0}),
  84. 1);
  85. }
  86. }
  87. }
  88. }
  89. };
  90. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);