SubstrExpression.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. "#evaluateInternal()": {
  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() {
  25. Expression.parseOperand({
  26. $substr: ["$s", "$start"]
  27. }).evaluateInternal({
  28. s: s,
  29. start: start
  30. });
  31. });
  32. },
  33. "Should return entire string when called with 0 and length": function testWholeString() {
  34. var s = "mystring",
  35. start = 0,
  36. end = s.length;
  37. assert.strictEqual(Expression.parseOperand({
  38. $substr: ["$s", "$start", "$end"]
  39. }).evaluateInternal({
  40. s: s,
  41. start: start,
  42. end: end
  43. }), "mystring");
  44. },
  45. "Should return entire string less the last character when called with 0 and length-1": function testLastCharacter() {
  46. var s = "mystring",
  47. start = 0,
  48. end = s.length;
  49. assert.strictEqual(Expression.parseOperand({
  50. $substr: ["$s", "$start", "$end"]
  51. }).evaluateInternal({
  52. s: s,
  53. start: start,
  54. end: end - 1
  55. }), "mystrin");
  56. },
  57. "Should return empty string when 0 and 0 are given as indexes": function test00Indexes() {
  58. var s = "mystring",
  59. start = 0,
  60. end = 0;
  61. assert.strictEqual(Expression.parseOperand({
  62. $substr: ["$s", "$start", "$end"]
  63. }).evaluateInternal({
  64. s: s,
  65. start: start,
  66. end: end
  67. }), "");
  68. },
  69. "Should first character when 0 and 1 are given as indexes": function testFirstCharacter() {
  70. var s = "mystring",
  71. start = 0,
  72. end = 1;
  73. assert.strictEqual(Expression.parseOperand({
  74. $substr: ["$s", "$start", "$end"]
  75. }).evaluateInternal({
  76. s: s,
  77. start: start,
  78. end: end
  79. }), "m");
  80. },
  81. "Should return empty string when empty string is given": function testEmptyString() {
  82. var s = "",
  83. start = 0,
  84. end = 0;
  85. assert.strictEqual(Expression.parseOperand({
  86. $substr: ["$s", "$start", "$end"]
  87. }).evaluateInternal({
  88. s: s,
  89. start: start,
  90. end: end
  91. }), "");
  92. },
  93. "Should return the entire string if end is -1": function testIndexTooLarge() {
  94. var s = "mystring",
  95. start = 0,
  96. end = -1;
  97. assert.strictEqual(Expression.parseOperand({
  98. $substr: ["$s", "$start", "$end"]
  99. }).evaluateInternal({
  100. s: s,
  101. start: start,
  102. end: end
  103. }), "mystring");
  104. },
  105. "Should fail if end is before begin": function testUnorderedIndexes() {
  106. var s = "mystring",
  107. start = s.length,
  108. end = 0;
  109. assert.throws(function() {
  110. Expression.parseOperand({
  111. $substr: ["$s", "$start"]
  112. }).evaluateInternal({
  113. s: s,
  114. start: start,
  115. end: end
  116. });
  117. });
  118. },
  119. "Should fail if end is greater than length": function testIndexTooLarge() {
  120. var s = "mystring",
  121. start = 0,
  122. end = s.length + 1;
  123. assert.throws(function() {
  124. Expression.parseOperand({
  125. $substr: ["$s", "$start"]
  126. }).evaluateInternal({
  127. s: s,
  128. start: start,
  129. end: end
  130. });
  131. });
  132. },
  133. }
  134. }
  135. };
  136. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);