You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.4KB

  1. var assert = require("assert"),
  2. vows = require("vows"),
  3. Scanner = require("./../mustache").Scanner;
  4. vows.describe("Mustache.Scanner").addBatch({
  5. "A Scanner": {
  6. "for an empty string": {
  7. topic: new Scanner(""),
  8. "should be at the end of the string": function () {
  9. var scanner = new Scanner("");
  10. assert(scanner.eos());
  11. }
  12. },
  13. "for a non-empty string": {
  14. topic: "a b c",
  15. "when calling scan": {
  16. "when the regexp matches the entire string": {
  17. topic: function (string) {
  18. var scanner = new Scanner(string);
  19. var match = scanner.scan(/a b c/);
  20. this.callback(scanner, match);
  21. },
  22. "it should return the entire string": function (scanner, match) {
  23. assert.equal(match, scanner.string);
  24. },
  25. "it should be at the end of the string": function (scanner, match) {
  26. assert(scanner.eos());
  27. }
  28. }, // when the regexp matches the entire string
  29. "when the regexp matches": {
  30. "at the 0th index": {
  31. topic: function (string) {
  32. var scanner = new Scanner(string);
  33. var match = scanner.scan(/a/);
  34. this.callback(scanner, match);
  35. },
  36. "it should return the portion of the string that was matched": function (scanner, match) {
  37. assert.equal(match, "a");
  38. },
  39. "it should advance the internal pointer the length of the match": function (scanner, match) {
  40. assert.equal(scanner.pos, 1);
  41. }
  42. }, // at the 0th index
  43. "at some index other than 0": {
  44. topic: function (string) {
  45. var scanner = new Scanner(string);
  46. var match = scanner.scan(/b/);
  47. this.callback(scanner, match);
  48. },
  49. "it should return null": function (scanner, match) {
  50. assert.equal(match, null);
  51. },
  52. "it should not advance the internal pointer": function (scanner, match) {
  53. assert.equal(scanner.pos, 0);
  54. }
  55. } // at some index other than 0
  56. }, // when the regexp matches
  57. "when the regexp doesn't match": {
  58. topic: function (string) {
  59. var scanner = new Scanner(string);
  60. var match = scanner.scan(/z/);
  61. this.callback(scanner, match);
  62. },
  63. "it should return null": function (scanner, match) {
  64. assert.equal(match, null);
  65. },
  66. "it should not advance the internal pointer": function (scanner, match) {
  67. assert.equal(scanner.pos, 0);
  68. }
  69. }
  70. }, // when calling scan
  71. "when calling scanUntil": {
  72. "when the regexp matches": {
  73. "at the 0th index": {
  74. topic: function (string) {
  75. var scanner = new Scanner(string);
  76. var match = scanner.scanUntil(/a/);
  77. this.callback(scanner, match);
  78. },
  79. "it should return null": function (scanner, match) {
  80. assert.equal(match, null)
  81. },
  82. "it should not advance the internal pointer": function (scanner, match) {
  83. assert.equal(scanner.pos, 0);
  84. }
  85. },
  86. "at index 2": {
  87. topic: function (string) {
  88. var scanner = new Scanner(string);
  89. var match = scanner.scanUntil(/b/);
  90. this.callback(scanner, match);
  91. },
  92. "it should return the portion of the string it scanned": function (scanner, match) {
  93. assert.equal(match, "a ");
  94. },
  95. "it should advance the internal pointer the length of the match": function (scanner, match) {
  96. assert.equal(scanner.pos, 2);
  97. }
  98. }
  99. }, // when the regexp matches
  100. "when the regexp doesn't match": {
  101. topic: function (string) {
  102. var scanner = new Scanner(string);
  103. var match = scanner.scanUntil(/z/);
  104. this.callback(scanner, match);
  105. },
  106. "it should return the entire string": function (scanner, match) {
  107. assert.equal(match, scanner.string);
  108. },
  109. "it should be at the end of the string": function (scanner, match) {
  110. assert(scanner.eos());
  111. }
  112. } // when the regexp doesn't match
  113. } // when calling scanUntil
  114. } // for a non-empty string
  115. }
  116. }).export(module);