forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacterStream.test.ts
More file actions
112 lines (95 loc) · 3.82 KB
/
characterStream.test.ts
File metadata and controls
112 lines (95 loc) · 3.82 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as assert from 'assert';
// tslint:disable-next-line:import-name
import Char from 'typescript-char';
import { CharacterStream } from '../../client/language/characterStream';
import { TextIterator } from '../../client/language/textIterator';
import { ICharacterStream } from '../../client/language/types';
// tslint:disable-next-line:max-func-body-length
suite('Language.CharacterStream', () => {
test('Iteration (string)', async () => {
const content = 'some text';
const cs = new CharacterStream(content);
testIteration(cs, content);
});
test('Iteration (iterator)', async () => {
const content = 'some text';
const cs = new CharacterStream(new TextIterator(content));
testIteration(cs, content);
});
test('Positioning', async () => {
const content = 'some text';
const cs = new CharacterStream(content);
assert.equal(cs.position, 0);
cs.advance(1);
assert.equal(cs.position, 1);
cs.advance(1);
assert.equal(cs.position, 2);
cs.advance(2);
assert.equal(cs.position, 4);
cs.advance(-3);
assert.equal(cs.position, 1);
cs.advance(-3);
assert.equal(cs.position, 0);
cs.advance(100);
assert.equal(cs.position, content.length);
});
test('Characters', async () => {
const content = 'some \ttext "" \' \' \n text \r\n more text';
const cs = new CharacterStream(content);
for (let i = 0; i < content.length; i += 1) {
assert.equal(cs.currentChar, content.charCodeAt(i));
assert.equal(cs.nextChar, i < content.length - 1 ? content.charCodeAt(i + 1) : 0);
assert.equal(cs.prevChar, i > 0 ? content.charCodeAt(i - 1) : 0);
assert.equal(cs.lookAhead(2), i < content.length - 2 ? content.charCodeAt(i + 2) : 0);
assert.equal(cs.lookAhead(-2), i > 1 ? content.charCodeAt(i - 2) : 0);
const ch = content.charCodeAt(i);
const isLineBreak = ch === Char.LineFeed || ch === Char.CarriageReturn;
assert.equal(cs.isAtWhiteSpace(), ch === Char.Tab || ch === Char.Space || isLineBreak);
assert.equal(cs.isAtLineBreak(), isLineBreak);
assert.equal(cs.isAtString(), ch === Char.SingleQuote || ch === Char.DoubleQuote);
cs.moveNext();
}
});
test('Skip', async () => {
const content = 'some \ttext "" \' \' \n text \r\n more text';
const cs = new CharacterStream(content);
cs.skipWhitespace();
assert.equal(cs.position, 0);
cs.skipToWhitespace();
assert.equal(cs.position, 4);
cs.skipToWhitespace();
assert.equal(cs.position, 4);
cs.skipWhitespace();
assert.equal(cs.position, 6);
cs.skipLineBreak();
assert.equal(cs.position, 6);
cs.skipToEol();
assert.equal(cs.position, 18);
cs.skipLineBreak();
assert.equal(cs.position, 19);
});
});
function testIteration(cs: ICharacterStream, content: string) {
assert.equal(cs.position, 0);
assert.equal(cs.length, content.length);
assert.equal(cs.isEndOfStream(), false);
for (let i = -2; i < content.length + 2; i += 1) {
const ch = cs.charCodeAt(i);
if (i < 0 || i >= content.length) {
assert.equal(ch, 0);
} else {
assert.equal(ch, content.charCodeAt(i));
}
}
for (let i = 0; i < content.length; i += 1) {
assert.equal(cs.isEndOfStream(), false);
assert.equal(cs.position, i);
assert.equal(cs.currentChar, content.charCodeAt(i));
cs.moveNext();
}
assert.equal(cs.isEndOfStream(), true);
assert.equal(cs.position, content.length);
}