CondExpression.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Error when constructing without args": function testConstructor(){
  9. assert.doesNotThrow(function(){
  10. new CondExpression();
  11. });
  12. }
  13. },
  14. "#getOpName()": {
  15. "should return the correct op name; $cond": function testOpName(){
  16. assert.equal(new CondExpression().getOpName(), "$cond");
  17. }
  18. },
  19. "#getFactory()": {
  20. "should return the constructor for this class": function factoryIsConstructor(){
  21. assert.strictEqual(new CondExpression().getFactory(), undefined);
  22. }
  23. },
  24. "#evaluate()": {
  25. "should evaluate boolean expression as true, then return 1; [ true === true, 1, 0 ]": function testStuff(){
  26. assert.strictEqual(Expression.parseOperand({$cond:[ true === true, 1, 0 ]}).evaluate({}), 1);
  27. },
  28. "should evaluate boolean expression as false, then return 0; [ false === true, 1, 0 ]": function testStuff(){
  29. assert.strictEqual(Expression.parseOperand({$cond:[ false === true, 1, 0 ]}).evaluate({}), 0);
  30. },
  31. "should evaluate boolean expression as true, then return 1; [ (true === true) && true, 1, 0 ]": function testStuff(){
  32. assert.strictEqual(Expression.parseOperand({$cond:[ (true === true) && true , 1, 0 ]}).evaluate({}), 1);
  33. },
  34. "should evaluate boolean expression as false, then return 0; [ (false === true) && true, 1, 0 ]": function testStuff(){
  35. assert.strictEqual(Expression.parseOperand({$cond:[ (false === true) && true, 1, 0 ]}).evaluate({}), 0);
  36. },
  37. "should evaluate complex boolean expression as true, then return 1; [ ( 1 > 0 ) && (( 'a' == 'b' ) || ( 3 <= 5 )), 1, 0 ]": function testStuff(){
  38. assert.strictEqual(Expression.parseOperand({$cond:[ ( 1 > 0 ) && (( 'a' == 'b' ) || ( 3 <= 5 )), 1, 0 ]}).evaluate({}), 1);
  39. },
  40. }
  41. }
  42. };
  43. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);