SetEqualsExpression.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. var assert = require("assert"),
  3. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  4. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  5. SetEqualsExpression = require("../../../../lib/pipeline/expressions/SetEqualsExpression"),
  6. Expression = require("../../../../lib/pipeline/expressions/Expression");
  7. function errMsg(expr, args, tree, expected, result) {
  8. return "for expression " + expr +
  9. " with argument " + args +
  10. " full tree: " + JSON.stringify(tree) +
  11. " expected: " + expected +
  12. " result: " + result;
  13. }
  14. module.exports = {
  15. "SetEqualsExpression": {
  16. "constructor()": {
  17. "should not throw Error when constructing without args": function testConstructor() {
  18. assert.doesNotThrow(function() {
  19. new SetEqualsExpression();
  20. });
  21. },
  22. "should throw Error when constructing with args": function testConstructor() {
  23. assert.throws(function() {
  24. new SetEqualsExpression("someArg");
  25. });
  26. }
  27. },
  28. "#getOpName()": {
  29. "should return the correct op name; $setEquals": function testOpName() {
  30. assert.equal(new SetEqualsExpression().getOpName(), "$setEquals");
  31. }
  32. },
  33. "#evaluateInternal()": {
  34. "Should fail if array1 is not an array": function testArg1() {
  35. var array1 = "not an array",
  36. array2 = [6, 7, 8, 9],
  37. input = [array1,array2],
  38. idGenerator = new VariablesIdGenerator(),
  39. vps = new VariablesParseState(idGenerator),
  40. expr = Expression.parseExpression("$setEquals", input, vps);
  41. assert.throws(function() {
  42. expr.evaluate({});
  43. });
  44. },
  45. "Should fail if array2 is not an array": function testArg2() {
  46. var array1 = [1, 2, 3, 4],
  47. array2 = "not an array",
  48. input = [array1,array2],
  49. idGenerator = new VariablesIdGenerator(),
  50. vps = new VariablesParseState(idGenerator),
  51. expr = Expression.parseExpression("$setEquals", input, vps);
  52. assert.throws(function() {
  53. expr.evaluate({});
  54. });
  55. },
  56. "Should fail if both are not an array": function testArg1andArg2() {
  57. var array1 = "not an array",
  58. array2 = "not an array",
  59. input = [array1,array2],
  60. idGenerator = new VariablesIdGenerator(),
  61. vps = new VariablesParseState(idGenerator),
  62. expr = Expression.parseExpression("$setEquals", input, vps);
  63. assert.throws(function() {
  64. expr.evaluate({});
  65. });
  66. },
  67. "Should pass and array1 should equal array2": function testBasicAssignment(){
  68. var array1 = [1, 2, 3, 5, 4],
  69. array2 = [1, 3, 2, 4, 5],
  70. input = [array1,array2],
  71. idGenerator = new VariablesIdGenerator(),
  72. vps = new VariablesParseState(idGenerator),
  73. expr = Expression.parseExpression("$setEquals", input, vps),
  74. result = expr.evaluate({}),
  75. expected = true,
  76. msg = errMsg("$setEquals", input, expr.serialize(false), expected, result);
  77. assert.equal(result, expected, msg);
  78. },
  79. "Should pass and array1 should not equal array2": function testBasicAssignment(){
  80. var array1 = [1, 2, 3, 4],
  81. array2 = [1, 3, 2, 4, 5],
  82. input = [array1,array2],
  83. idGenerator = new VariablesIdGenerator(),
  84. vps = new VariablesParseState(idGenerator),
  85. expr = Expression.parseExpression("$setEquals", input, vps),
  86. result = expr.evaluate({}),
  87. expected = false,
  88. msg = errMsg("$setEquals", input, expr.serialize(false), expected, result);
  89. assert.equal(result, expected, msg);
  90. }
  91. }
  92. }
  93. };
  94. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);