SubstrExpression.js 2.9 KB

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