ConstantExpression_test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression"),
  5. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  6. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  7. DepsTracker = require("../../../../lib/pipeline/DepsTracker");
  8. exports.ConstantExpression = {
  9. ".constructor()": {
  10. "should accept one argument": function () {
  11. new ConstantExpression(5);
  12. },
  13. "should not accept 0 arguments": function () {
  14. assert.throws(function () {
  15. new ConstantExpression();
  16. });
  17. },
  18. "should not accept 2 arguments": function () {
  19. assert.throws(function () {
  20. new ConstantExpression(1, 2);
  21. });
  22. }
  23. },
  24. ".parse()": {
  25. "should create an expression from a json element": function testCreateFromBsonElement() {
  26. var idGenerator = new VariablesIdGenerator(),
  27. vps = new VariablesParseState(idGenerator),
  28. expression = ConstantExpression.parse("foo", vps);
  29. assert.deepEqual("foo", expression.evaluate({}));
  30. }
  31. },
  32. ".create()": {
  33. "should create an expression": function testCreate() {
  34. assert(ConstantExpression.create() instanceof ConstantExpression);
  35. }
  36. //SKIPPED: testCreateFronBsonElement
  37. },
  38. "#optimize()": {
  39. "should not optimize anything": function testOptimize() {
  40. var expr = new ConstantExpression(5);
  41. assert.strictEqual(expr, expr.optimize());
  42. }
  43. },
  44. "#addDependencies()": {
  45. "should return nothing": function testDependencies() {
  46. var expr = ConstantExpression.create(5),
  47. deps = new DepsTracker();
  48. expr.addDependencies(deps);
  49. assert.deepEqual(deps.fields, {});
  50. assert.strictEqual(deps.needWholeDocument, false);
  51. assert.strictEqual(deps.needTextScore, false);
  52. }
  53. },
  54. //TODO: AddToBsonObj
  55. //TODO: AddToBsonArray
  56. "#evaluate()": {
  57. "should do what comes natural with an int": function () {
  58. var c = 567;
  59. var expr = new ConstantExpression(c);
  60. assert.deepEqual(expr.evaluate(), c);
  61. },
  62. "should do what comes natural with a float": function () {
  63. var c = 567.123;
  64. var expr = new ConstantExpression(c);
  65. assert.deepEqual(expr.evaluate(), c);
  66. },
  67. "should do what comes natural with a String": function () {
  68. var c = "Quoth the raven";
  69. var expr = new ConstantExpression(c);
  70. assert.deepEqual(expr.evaluate(), c);
  71. },
  72. "should do what comes natural with a date": function () {
  73. var c = new Date();
  74. var expr = new ConstantExpression(c);
  75. assert.deepEqual(expr.evaluate(), c);
  76. }
  77. }
  78. };