SubstrExpression_test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. var assert = require("assert"),
  3. SubstrExpression = require("../../../../lib/pipeline/expressions/SubstrExpression"),
  4. Expression = require("../../../../lib/pipeline/expressions/Expression"),
  5. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  6. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  7. utils = require("./utils"),
  8. constify = utils.constify,
  9. expressionToJson = utils.expressionToJson;
  10. // Mocha one-liner to make these tests self-hosted
  11. if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
  12. var TestBase = function TestBase(overrides) {
  13. //NOTE: DEVIATION FROM MONGO: using this base class to make things easier to initialize
  14. for (var key in overrides)
  15. this[key] = overrides[key];
  16. },
  17. ExpectedResultBase = (function() {
  18. var klass = function ExpectedResultBase() {
  19. base.apply(this, arguments);
  20. }, base = TestBase, proto = klass.prototype = Object.create(base.prototype);
  21. proto.run = function(){
  22. var specElement = this.spec(),
  23. idGenerator = new VariablesIdGenerator(),
  24. vps = new VariablesParseState(idGenerator),
  25. expr = Expression.parseOperand(specElement, vps);
  26. assert.deepEqual(constify(specElement), expressionToJson(expr));
  27. assert.deepEqual(this.expectedResult, expr.evaluate({}));
  28. };
  29. proto.spec = function() { return {$substr:[this.str, this.offset, this.length]}; };
  30. return klass;
  31. })();
  32. exports.SubstrExpression = {
  33. "constructor()": {
  34. "should construct instance": function() {
  35. assert(new SubstrExpression() instanceof SubstrExpression);
  36. assert(new SubstrExpression() instanceof Expression);
  37. },
  38. "should error if given args": function() {
  39. assert.throws(function() {
  40. new SubstrExpression("bad stuff");
  41. });
  42. },
  43. },
  44. "#getOpName()": {
  45. "should return the correct op name; $substr": function() {
  46. assert.equal(new SubstrExpression().getOpName(), "$substr");
  47. },
  48. },
  49. "evaluate": {
  50. "should return full string (if contains null)": function FullNull() {
  51. new ExpectedResultBase({
  52. str: "a\0b",
  53. offset: 0,
  54. length: 3,
  55. get expectedResult(){ return this.str; },
  56. }).run();
  57. },
  58. "should return tail of string (if begin at null)": function BeginAtNull() {
  59. new ExpectedResultBase({
  60. str: "a\0b",
  61. offset: 1,
  62. length: 2,
  63. expectedResult: "\0b",
  64. }).run();
  65. },
  66. "should return head of string (if end at null)": function EndAtNull() {
  67. new ExpectedResultBase({
  68. str: "a\0b",
  69. offset: 0,
  70. length: 2,
  71. expectedResult: "a\0",
  72. }).run();
  73. },
  74. "should return tail of string (if head has null) ": function DropBeginningNull() {
  75. new ExpectedResultBase({
  76. str: "\0b",
  77. offset: 1,
  78. length: 1,
  79. expectedResult: "b",
  80. }).run();
  81. },
  82. "should return head of string (if tail has null)": function DropEndingNull() {
  83. new ExpectedResultBase({
  84. str: "a\0",
  85. offset: 0,
  86. length: 1,
  87. expectedResult: "a",
  88. }).run();
  89. },
  90. },
  91. };