SubstrExpression_test.js 3.0 KB

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