UNPKG

1.72 kBJavaScriptView Raw
1"use strict";
2
3var Parser = require("../lib/text-parser");
4
5describe('Module "Parser"', function() {
6 function chk( text, func, expected ) {
7 expect(Parser({
8 text: text,
9 grammar: {start: func},
10 state: { out: '' }
11 }).out).toBe(expected);
12 }
13
14 it('should "eatRegex"', function() {
15 chk("Hello world! We are 666 people here.",
16 function( stream, state ) {
17 if (stream.eatRegex(/[a-z]+/gi)) {
18 state.out += 'T';
19 return;
20 }
21 if (stream.eatRegex(/[0-9]+/g)) {
22 state.out += 'N';
23 return;
24 }
25 state.out += stream.next();
26 },
27 "T T! T T N T T.");
28 });
29
30
31 it('should "eat" text', function() {
32 chk("I say YesNo, but also No ... and maybe Yes again!",
33 function( stream, state ) {
34 var res = stream.eat("Yes", "No");
35 if (res) state.out += res;
36 else stream.next();
37 return undefined;
38 },
39 "YesNoNoYes");
40 });
41
42 it('should "eatUntilChar', function() {
43 chk("I say YesNo, but also No ... and maybe Yes again!",
44 function( stream, state ) {
45 state.out = stream.eatUntilChar(",");
46 return null;
47 },
48 "I say YesNo");
49 });
50
51 it('should "eatUntilText', function() {
52 chk("I say YesNo, but also No ... and maybe Yes again!",
53 function( stream, state ) {
54 state.out = stream.eatUntilText(", ");
55 return null;
56 },
57 "I say YesNo");
58 });
59});