CondExpression.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 throw Error when constructing without args": function testConstructor(){
  9. assert.throws(function(){
  10. new CondExpression();
  11. });
  12. },
  13. "should throw Error when constructing with 1 arg": function testConstructor1(){
  14. assert.throws(function(){
  15. new CondExpression({if:true === true});
  16. });
  17. },
  18. "should throw Error when constructing with 2 args": function testConstructor2(){
  19. assert.throws(function(){
  20. new CondExpression(true === true,1);
  21. });
  22. },
  23. "should now throw Error when constructing with 3 args": function testConstructor3(){
  24. assert.doesNotThrow(function(){
  25. //new CondExpression({$cond:[{"if":"true === true"},{"then":"1"},{"else":"0"}]});
  26. new CondExpression({$cond:[ true === true, 1, 0 ]});
  27. });
  28. },
  29. },
  30. "#getOpName()": {
  31. "should return the correct op name; $cond": function testOpName(){
  32. assert.equal(new CondExpression().getOpName(), "$cond");
  33. }
  34. },
  35. "#evaluateInternal()": {
  36. "should evaluate boolean expression as true, then return 1; [ true === true, 1, 0 ]": function testStuff(){
  37. assert.strictEqual(Expression.parseOperand({$cond:[ true === true, 1, 0 ]}).evaluateInternal({}), 1);
  38. },
  39. "should evaluate boolean expression as false, then return 0; [ false === true, 1, 0 ]": function testStuff(){
  40. assert.strictEqual(Expression.parseOperand({$cond:[ false === true, 1, 0 ]}).evaluateInternal({}), 0);
  41. },
  42. "should evaluate boolean expression as true, then return 1; [ (true === true) && true, 1, 0 ]": function testStuff(){
  43. assert.strictEqual(Expression.parseOperand({$cond:[ (true === true) && true , 1, 0 ]}).evaluateInternal({}), 1);
  44. },
  45. "should evaluate boolean expression as false, then return 0; [ (false === true) && true, 1, 0 ]": function testStuff(){
  46. assert.strictEqual(Expression.parseOperand({$cond:[ (false === true) && true, 1, 0 ]}).evaluateInternal({}), 0);
  47. },
  48. "should evaluate complex boolean expression as true, then return 1; [ ( 1 > 0 ) && (( 'a' == 'b' ) || ( 3 <= 5 )), 1, 0 ]": function testStuff(){
  49. assert.strictEqual(Expression.parseOperand({$cond:[ ( 1 > 0 ) && (( 'a' == 'b' ) || ( 3 <= 5 )), 1, 0 ]}).evaluate({}), 1);
  50. },
  51. }
  52. }
  53. };
  54. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);