|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- var assert = require("assert"),
- vows = require("vows"),
- Scanner = require("./../mustache").Scanner;
-
- vows.describe("Mustache.Scanner").addBatch({
- "A Scanner": {
- "for an empty string": {
- topic: new Scanner(""),
- "should be at the end of the string": function () {
- var scanner = new Scanner("");
- assert(scanner.eos());
- }
- },
- "for a non-empty string": {
- topic: "a b c",
- "when calling scan": {
- "when the regexp matches the entire string": {
- topic: function (string) {
- var scanner = new Scanner(string);
- var match = scanner.scan(/a b c/);
- this.callback(scanner, match);
- },
- "it should return the entire string": function (scanner, match) {
- assert.equal(match, scanner.string);
- },
- "it should be at the end of the string": function (scanner, match) {
- assert(scanner.eos());
- }
- }, // when the regexp matches the entire string
- "when the regexp matches": {
- "at the 0th index": {
- topic: function (string) {
- var scanner = new Scanner(string);
- var match = scanner.scan(/a/);
- this.callback(scanner, match);
- },
- "it should return the portion of the string that was matched": function (scanner, match) {
- assert.equal(match, "a");
- },
- "it should advance the internal pointer the length of the match": function (scanner, match) {
- assert.equal(scanner.pos, 1);
- }
- }, // at the 0th index
- "at some index other than 0": {
- topic: function (string) {
- var scanner = new Scanner(string);
- var match = scanner.scan(/b/);
- this.callback(scanner, match);
- },
- "it should return the empty string": function (scanner, match) {
- assert.equal(match, "");
- },
- "it should not advance the internal pointer": function (scanner, match) {
- assert.equal(scanner.pos, 0);
- }
- } // at some index other than 0
- }, // when the regexp matches
- "when the regexp doesn't match": {
- topic: function (string) {
- var scanner = new Scanner(string);
- var match = scanner.scan(/z/);
- this.callback(scanner, match);
- },
- "it should return the empty string": function (scanner, match) {
- assert.equal(match, "");
- },
- "it should not advance the internal pointer": function (scanner, match) {
- assert.equal(scanner.pos, 0);
- }
- }
- }, // when calling scan
- "when calling scanUntil": {
- "when the regexp matches": {
- "at the 0th index": {
- topic: function (string) {
- var scanner = new Scanner(string);
- var match = scanner.scanUntil(/a/);
- this.callback(scanner, match);
- },
- "it should return the empty string": function (scanner, match) {
- assert.equal(match, "")
- },
- "it should not advance the internal pointer": function (scanner, match) {
- assert.equal(scanner.pos, 0);
- }
- },
- "at index 2": {
- topic: function (string) {
- var scanner = new Scanner(string);
- var match = scanner.scanUntil(/b/);
- this.callback(scanner, match);
- },
- "it should return the portion of the string it scanned": function (scanner, match) {
- assert.equal(match, "a ");
- },
- "it should advance the internal pointer the length of the match": function (scanner, match) {
- assert.equal(scanner.pos, 2);
- }
- }
- }, // when the regexp matches
- "when the regexp doesn't match": {
- topic: function (string) {
- var scanner = new Scanner(string);
- var match = scanner.scanUntil(/z/);
- this.callback(scanner, match);
- },
- "it should return the entire string": function (scanner, match) {
- assert.equal(match, scanner.string);
- },
- "it should be at the end of the string": function (scanner, match) {
- assert(scanner.eos());
- }
- } // when the regexp doesn't match
- } // when calling scanUntil
- } // for a non-empty string
- }
- }).export(module);
|