CondExpression_test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // Mocha one-liner to make these tests self-hosted
  8. if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
  9. exports.CondExpression = {
  10. "constructor()": {
  11. "should not throw an Error when constructing without args": function testConstructor(){
  12. assert.doesNotThrow(function(){
  13. new CondExpression();
  14. });
  15. },
  16. "should throw Error when constructing with 1 arg": function testConstructor1(){
  17. assert.throws(function(){
  18. new CondExpression(1);
  19. });
  20. },
  21. },
  22. "#getOpName()": {
  23. "should return the correct op name; $cond": function testOpName(){
  24. assert.equal(new CondExpression().getOpName(), "$cond");
  25. },
  26. },
  27. "#evaluate()": {
  28. "array style": {
  29. "should fail if there aren't enough arguments": function() {
  30. assert.throws(function(){
  31. Expression.parseOperand({$cond:[1,2]}, {});
  32. });
  33. },
  34. "should fail if there are too many arguments": function() {
  35. assert.throws(function(){
  36. Expression.parseOperand({$cond:[1, 2, 3, 4]}, {});
  37. });
  38. },
  39. "should evaluate boolean expression as true, then return 1; [ true === true, 1, 0 ]": function () {
  40. assert.strictEqual(Expression.parseOperand({$cond: [ true, 1, 0 ]}, {}).evaluate({}), 1);
  41. },
  42. "should evaluate boolean expression as false, then return 0; [ false === true, 1, 0 ]": function () {
  43. assert.strictEqual(Expression.parseOperand({$cond: [ false, 1, 0 ]}, {}).evaluate({}), 0);
  44. },
  45. },
  46. "object style": {
  47. beforeEach: function(){
  48. this.shouldFail = function(expr) {
  49. assert.throws(function(){
  50. Expression.parseOperand(expr, {});
  51. });
  52. };
  53. this.vps = new VariablesParseState(new VariablesIdGenerator());
  54. },
  55. "should fail because of missing if": function(){
  56. this.shouldFail({$cond:{ then:2, else:3}});
  57. },
  58. "should fail because of missing then": function(){
  59. this.shouldFail({$cond:{if:1, else:3}});
  60. },
  61. "should fail because of missing else": function(){
  62. this.shouldFail({$cond:{if:1, then:2 }});
  63. },
  64. "should fail because of mystery args": function(){
  65. this.shouldFail({$cond:{if:1, then:2, else:3, zoot:4}});
  66. },
  67. "should evaluate true": function(){
  68. assert.strictEqual(
  69. Expression.parseOperand({$cond:{ if: true, then: 1, else: 0}}, {}).evaluate({}),
  70. 1);
  71. },
  72. "should evaluate true even with mixed up args": function(){
  73. assert.strictEqual(
  74. Expression.parseOperand({$cond:{ else: 0, then: 1, if: "$a" }}, this.vps).evaluate({a: 1}),
  75. 1);
  76. },
  77. "should evaluate false": function(){
  78. assert.strictEqual(
  79. Expression.parseOperand({$cond:{ if: "$a", then: 0, else: 1}}, this.vps).evaluate({a: 0}),
  80. 1);
  81. },
  82. "should evaluate false even with mixed up args": function() {
  83. assert.strictEqual(
  84. Expression.parseOperand({$cond: { else: 1, then: 0, if: "$a"}}, this.vps).evaluate({a: 0}),
  85. 1);
  86. },
  87. },
  88. },
  89. };