SubstrExpression.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var assert = require("assert"),
  3. SubstrExpression = require("../../../../lib/pipeline/expressions/SubstrExpression"),
  4. Expression = require("../../../../lib/pipeline/expressions/Expression");
  5. module.exports = {
  6. "SubstrExpression": {
  7. "constructor()": {
  8. "should not throw Error when constructing without args": function testConstructor(){
  9. assert.doesNotThrow(function(){
  10. new SubstrExpression();
  11. });
  12. }
  13. },
  14. "#getOpName()": {
  15. "should return the correct op name; $substr": function testOpName(){
  16. assert.equal(new SubstrExpression().getOpName(), "$substr");
  17. }
  18. },
  19. "#evaluate()": {
  20. "Should fail if no end argument is given": function testMissing3rdArg(){
  21. var s = "mystring",
  22. start = 0,
  23. end = s.length;
  24. assert.throws(function(){Expression.parseOperand({$substr:["$s", "$start"]}).evaluate({s:s, start:start});});
  25. },
  26. "Should return entire string when called with 0 and length": function testWholeString(){
  27. var s = "mystring",
  28. start = 0,
  29. end = s.length;
  30. assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end}), "mystring");
  31. },
  32. "Should return entire string less the last character when called with 0 and length-1": function testLastCharacter(){
  33. var s = "mystring",
  34. start = 0,
  35. end = s.length;
  36. assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end-1}), "mystrin");
  37. },
  38. "Should return empty string when 0 and 0 are given as indexes": function test00Indexes(){
  39. var s = "mystring",
  40. start = 0,
  41. end = 0;
  42. assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end}), "");
  43. },
  44. "Should first character when 0 and 1 are given as indexes": function testFirstCharacter(){
  45. var s = "mystring",
  46. start = 0,
  47. end = 1;
  48. assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end}), "m");
  49. },
  50. "Should return empty string when empty string is given": function testEmptyString(){
  51. var s = "",
  52. start = 0,
  53. end = 0;
  54. assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end}), "");
  55. },
  56. "Should fail if end is before begin": function testUnorderedIndexes(){
  57. var s = "mystring",
  58. start = s.length,
  59. end = 0;
  60. assert.throws(function(){Expression.parseOperand({$substr:["$s", "$start"]}).evaluate({s:s, start:start, end:end});});
  61. },
  62. "Should fail if end is greater than length": function testIndexTooLarge(){
  63. var s = "mystring",
  64. start = 0,
  65. end = s.length+1;
  66. assert.throws(function(){Expression.parseOperand({$substr:["$s", "$start"]}).evaluate({s:s, start:start, end:end});});
  67. }
  68. }
  69. }
  70. };
  71. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);