CondExpression_test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. if (!module.parent) return require.cache[__filename] = 0, (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);
  3. var assert = require("assert"),
  4. CondExpression = require("../../../../lib/pipeline/expressions/CondExpression"),
  5. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  6. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  7. Expression = require("../../../../lib/pipeline/expressions/Expression");
  8. exports.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. "#evaluate()": {
  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 ]}, {}).evaluate({}), 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 ]}, {}).evaluate({}), 0);
  43. },
  44. },
  45. "object style": {
  46. beforeEach: function(){
  47. this.shouldFail = function(expr) {
  48. assert.throws(function(){
  49. Expression.parseOperand(expr, {});
  50. });
  51. };
  52. this.vps = new VariablesParseState(new VariablesIdGenerator());
  53. },
  54. "should fail because of missing if": function(){
  55. this.shouldFail({$cond:{ then:2, else:3}});
  56. },
  57. "should fail because of missing then": function(){
  58. this.shouldFail({$cond:{if:1, else:3}});
  59. },
  60. "should fail because of missing else": function(){
  61. this.shouldFail({$cond:{if:1, then:2 }});
  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: true, then: 1, else: 0}}, {}).evaluate({}),
  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" }}, this.vps).evaluate({a: 1}),
  74. 1);
  75. },
  76. "should evaluate false": function(){
  77. assert.strictEqual(
  78. Expression.parseOperand({$cond:{ if: "$a", then: 0, else: 1}}, this.vps).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"}}, this.vps).evaluate({a: 0}),
  84. 1);
  85. },
  86. },
  87. },
  88. };