-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext-reader-test.js
176 lines (164 loc) · 5.81 KB
/
text-reader-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { TextReader } from "../simple-implementations/text-reader.js"
describe(".readChar", () => {
it("returns char and advances index", () => {
const reader = new TextReader("hello world");
let result = reader.readChar();
expect(result).toEqual("h");
expect(reader.index).toEqual(1);
});
});
describe(".peekChar", () => {
it("returns char and does not advance index", () => {
const reader = new TextReader("hello world");
let result = reader.peekChar();
expect(result).toEqual("h");
expect(reader.index).toEqual(0);
});
});
describe(".validate", () => {
it("returns true if validated and advances index", () => {
const reader = new TextReader("hello world");
let result = reader.validate("hel");
expect(result).toBeTruthy();
expect(reader.index).toEqual(3);
});
it("returns true if validated and advances index", () => {
const reader = new TextReader("hello world");
let result = reader.validate("wor");
expect(result).not.toBeTruthy();
expect(reader.index).toEqual(0);
});
});
describe(".peekValidate", () => {
it("returns false if not validated and does not advance index", function () {
const reader = new TextReader("hello world");
let result = reader.peekValidate("hel");
expect(result).toBeTruthy();
expect(reader.index).toEqual(0);
});
it("returns false if not validated and does not advance index", function () {
const reader = new TextReader("hello world");
let result = reader.peekValidate("wor");
expect(result).not.toBeTruthy();
expect(reader.index).toEqual(0);
});
});
describe(".readUntil", () => {
it("returns until match and advances index (char)", function () {
const reader = new TextReader("hello world");
let { result, match } = reader.readUntil("w");
expect(result).toEqual("hello ");
expect(reader.index).toEqual(6);
});
it("returns until match and advances index (string)", function () {
const reader = new TextReader("hello world");
let { result, match } = reader.readUntil("rl");
expect(result).toEqual("hello wo");
expect(reader.index).toEqual(8);
});
it("returns until first match and advances index (char)", function () {
const reader = new TextReader("hello world");
let { result, match } = reader.readUntil("l", "r");
expect(match).toEqual("l");
expect(result).toEqual("he");
expect(reader.index).toEqual(2);
});
it("returns until first match and advances index (param order)", function () {
const reader = new TextReader("hello world");
let { result, match } = reader.readUntil("d", "r");
expect(match).toEqual("r");
expect(result).toEqual("hello wo");
expect(reader.index).toEqual(8);
});
it("returns until first match and advances index (string)", function () {
const reader = new TextReader("hello world");
let { result, match } = reader.readUntil("lo", "rl");
expect(match).toEqual("lo");
expect(result).toEqual("hel");
expect(reader.index).toEqual(3);
});
it("returns null and rest of text if not found", function () {
const reader = new TextReader("hello world");
let { result } = reader.readUntil("z", ".");
expect(result).toEqual("hello world");
expect(reader.index).toEqual(11);
});
});
describe(".peekUntil", () => {
it("returns until match and does not advance index (char)", function () {
const reader = new TextReader("hello world");
let { result } = reader.peekUntil("w");
expect(result).toEqual("hello ");
expect(reader.index).toEqual(0);
});
it("returns until match and does not advance index (string)", function () {
const reader = new TextReader("hello world");
let { result } = reader.peekUntil("rl");
expect(result).toEqual("hello wo");
expect(reader.index).toEqual(0);
});
it("returns until first match and does not advance index (char)", function () {
const reader = new TextReader("hello world");
let { result, match } = reader.peekUntil("l", "r");
expect(match).toEqual("l");
expect(result).toEqual("he");
expect(reader.index).toEqual(0);
});
it("returns until first match and does not advance index (param order)", function () {
const reader = new TextReader("hello world");
let { result, match } = reader.peekUntil("d", "r");
expect(match).toEqual("r");
expect(result).toEqual("hello wo");
expect(reader.index).toEqual(0);
});
it("returns until first match and does not advance index (string)", function () {
const reader = new TextReader("hello world");
let { result, match } = reader.peekUntil("lo", "rl");
expect(match).toEqual("lo");
expect(result).toEqual("hel");
expect(reader.index).toEqual(0);
});
it("returns null and rest of text if not found", function () {
const reader = new TextReader("hello world");
let { result } = reader.peekUntil("z", ".");
expect(result).toEqual("hello world");
expect(reader.index).toEqual(0);
});
});
describe(".readToEnd", () => {
it("returns string until character and advances index", function () {
const reader = new TextReader("hello world");
let result = reader.readToEnd();
expect(result).toEqual("hello world");
expect(reader.index).toEqual(11);
});
});
describe(".peekToEnd", () => {
it("returns string until character and advances index", function () {
const reader = new TextReader("hello world");
let result = reader.peekToEnd();
expect(result).toEqual("hello world");
expect(reader.index).toEqual(0);
});
});
describe(".readWhiteSpace", () => {
it("returns string until non-whitespace and advances index", () => {
const reader = new TextReader(" h");
let result = reader.readWhiteSpace();
expect(result).toEqual(" ");
expect(reader.index).toEqual(3);
});
[
String.fromCharCode(9),
String.fromCharCode(10),
String.fromCharCode(13),
String.fromCharCode(32)
].forEach(test => {
it("returns should read as whiteSpace", () => {
const reader = new TextReader(test);
let result = reader.readWhiteSpace();
expect(result).toEqual(test);
expect(reader.index).toEqual(1);
});
});
});