SubstrExpression_test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. module.exports = {
  11. "SubstrExpression": {
  12. "constructor()": {
  13. "should not throw Error when constructing without args": function testConstructor() {
  14. assert.doesNotThrow(function() {
  15. new SubstrExpression();
  16. });
  17. }
  18. },
  19. "#getOpName()": {
  20. "should return the correct op name; $substr": function testOpName() {
  21. assert.equal(new SubstrExpression().getOpName(), "$substr");
  22. }
  23. },
  24. "evaluate": {
  25. "before": function before() {
  26. this.run = function run() {
  27. var idGenerator = new VariablesIdGenerator(),
  28. vps = new VariablesParseState(idGenerator),
  29. spec = this.spec(),
  30. expectedResult = this.expectedResult,
  31. expression = Expression.parseOperand(spec, vps);
  32. assert.deepEqual(constify(spec), expressionToJson(expression));
  33. assert.equal(expectedResult, expression.evaluate({}));
  34. };
  35. this.str = undefined;
  36. this.offset = undefined;
  37. this.length = undefined;
  38. this.expectedResult = undefined;
  39. this.spec = function spec() {return {$substr:[this.str, this.offset, this.length]}; };
  40. },
  41. "FullNull": function FullNull() {
  42. this.str = "a\0b";
  43. this.offset = 0;
  44. this.length = 3;
  45. this.expectedResult = this.str;
  46. this.run();
  47. },
  48. "BeginAtNull": function BeginAtNull() {
  49. this.str = "a\0b";
  50. this.offset = 1;
  51. this.length = 2;
  52. this.expectedResult = "\0b";
  53. this.run();
  54. },
  55. "EndAtNull": function EndAtNull() {
  56. this.str = "a\0b";
  57. this.offset = 0;
  58. this.length = 2;
  59. this.expectedResult = "a\0";
  60. this.run();
  61. },
  62. "DropBeginningNull": function DropBeginningNull() {
  63. this.str = "\0b";
  64. this.offset = 1;
  65. this.length = 1;
  66. this.expectedResult = "b";
  67. this.run();
  68. },
  69. "DropEndingNull": function DropEndingNull() {
  70. this.str = "a\0";
  71. this.offset = 0;
  72. this.length = 1;
  73. this.expectedResult = "a";
  74. this.run();
  75. },
  76. }
  77. }
  78. };
  79. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);