forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththemeTest.ts
More file actions
360 lines (307 loc) · 10.4 KB
/
themeTest.ts
File metadata and controls
360 lines (307 loc) · 10.4 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import * as fs from 'fs';
import * as path from 'path';
import { IEmbeddedLanguagesMap, Thenable } from '../main';
import { tokenizeWithTheme, IThemedToken, IThemedTokenScopeExplanation } from './themedTokenizer';
import { ThemeData } from './themes.test';
import { Resolver } from './resolver';
interface IExpected {
[theme: string]: IExpectedTokenization[];
}
interface IExpectedPatch {
[theme: string]: IExpectedTokenizationPatch[];
}
export interface IExpectedTokenization {
content: string;
color: string;
_r: string;
_t: string;
}
interface IExpectedTokenizationPatch {
index: number;
content: string;
color: string;
newColor: string;
}
export class ThemeTest {
private static _readFile(filename: string): string {
try {
return fs.readFileSync(filename).toString('utf8');
} catch (err) {
return null;
}
}
private static _readJSONFile<T>(filename: string): T {
try {
return JSON.parse(this._readFile(filename));
} catch (err) {
return null;
}
}
private readonly tests: SingleThemeTest[];
private readonly THEMES_TEST_PATH: string;
public readonly testName: string;
// private readonly contents: string;
// private readonly initialScopeName: string;
// private readonly initialLanguage: number;
// private readonly embeddedLanguages: IEmbeddedLanguagesMap;
// private readonly expected: IExpected;
// private readonly expectedPatch: IExpectedPatch;
constructor(THEMES_TEST_PATH: string, testFile: string, resolver: Resolver) {
this.THEMES_TEST_PATH = THEMES_TEST_PATH;
const TEST_FILE_PATH = path.join(THEMES_TEST_PATH, 'tests', testFile);
const testFileContents = ThemeTest._readFile(TEST_FILE_PATH);
const EXPECTED_FILE_PATH = path.join(THEMES_TEST_PATH, 'tests', testFile + '.result');
const testFileExpected = ThemeTest._readJSONFile<IExpected>(EXPECTED_FILE_PATH);
const EXPECTED_PATCH_FILE_PATH = path.join(THEMES_TEST_PATH, 'tests', testFile + '.result.patch');
const testFileExpectedPatch = ThemeTest._readJSONFile<IExpectedPatch>(EXPECTED_PATCH_FILE_PATH);
// Determine the language
let language = resolver.findLanguageByExtension(path.extname(testFile)) || resolver.findLanguageByFilename(testFile);
if (!language) {
throw new Error('Could not determine language for ' + testFile);
}
let grammar = resolver.findGrammarByLanguage(language);
let embeddedLanguages: IEmbeddedLanguagesMap = Object.create(null);
if (grammar.embeddedLanguages) {
for (let scopeName in grammar.embeddedLanguages) {
embeddedLanguages[scopeName] = resolver.language2id[grammar.embeddedLanguages[scopeName]];
}
}
// console.log(testFileExpected);
// console.log(testFileExpectedPatch);
this.tests = [];
for (let themeName in testFileExpected) {
this.tests.push(new SingleThemeTest(
themeName,
testFile,
testFileContents,
grammar.scopeName,
resolver.language2id[language],
embeddedLanguages,
testFileExpected[themeName],
testFileExpectedPatch ? testFileExpectedPatch[themeName] : []
));
}
this.testName = testFile + '-' + resolver.getOnigLibName();
// this.contents = testFileContents;
// this.initialScopeName = grammar.scopeName;
// this.initialLanguage = resolver.language2id[language];
// this.embeddedLanguages = embeddedLanguages;
// this.expected = testFileExpected;
// this.expectedPatch = testFileExpectedPatch;
// assertTokenizationForThemes(test, themeDatas);
}
public evaluate(themeDatas: ThemeData[]): Promise<any> {
let testsMap: { [themeName: string]: SingleThemeTest; } = {};
for (let i = 0; i < this.tests.length; i++) {
testsMap[this.tests[i].themeName] = this.tests[i];
}
return Promise.all(themeDatas.map(data => testsMap[data.themeName].evaluate(data)));
}
private _getDiffPageData(): IDiffPageData[] {
return this.tests.map(t => t.getDiffPageData());
}
public hasDiff(): boolean {
for (let i = 0; i < this.tests.length; i++) {
let test = this.tests[i];
if (test.patchedDiff && test.patchedDiff.length > 0) {
return true;
}
}
return false;
}
public writeDiffPage(): void {
let r = `<html><head>`;
r += `\n<link rel="stylesheet" type="text/css" href="../diff.css"/>`;
r += `\n<meta charset="utf-8">`;
r += `\n</head><body>`;
r += `\n<script>var allData = "${new Buffer(JSON.stringify(this._getDiffPageData())).toString('base64')}";</script>`;
r += `\n<script type="text/javascript" src="../diff.js"></script>`;
r += `\n</body></html>`;
fs.writeFileSync(path.join(this.THEMES_TEST_PATH, 'tests', this.testName + '.diff.html'), r);
}
}
interface IActualCanonicalToken {
content: string;
color: string;
scopes: IThemedTokenScopeExplanation[];
}
interface IExpectedCanonicalToken {
oldIndex: number;
content: string;
color: string;
_r: string;
_t: string;
}
interface ITokenizationDiff {
oldIndex: number;
oldToken: IExpectedTokenization;
newToken: IActualCanonicalToken;
}
interface IDiffPageData {
testContent: string;
themeName: string;
backgroundColor: string;
actual: IThemedToken[];
expected: IExpectedTokenization[];
diff: ITokenizationDiff[];
patchedExpected: IExpectedTokenization[];
patchedDiff: ITokenizationDiff[];
}
class SingleThemeTest {
public readonly themeName: string;
private readonly testName: string;
private readonly contents: string;
private readonly initialScopeName: string;
private readonly initialLanguage: number;
private readonly embeddedLanguages: IEmbeddedLanguagesMap;
private readonly expected: IExpectedTokenization[];
private readonly patchedExpected: IExpectedTokenization[];
private readonly expectedPatch: IExpectedTokenizationPatch[];
private backgroundColor: string;
public actual: IThemedToken[];
public diff: ITokenizationDiff[];
public patchedDiff: ITokenizationDiff[];
constructor(
themeName: string,
testName: string,
contents: string,
initialScopeName: string,
initialLanguage: number,
embeddedLanguages: IEmbeddedLanguagesMap,
expected: IExpectedTokenization[],
expectedPatch: IExpectedTokenizationPatch[],
) {
this.themeName = themeName;
this.testName = testName;
this.contents = contents;
this.initialScopeName = initialScopeName;
this.initialLanguage = initialLanguage;
this.embeddedLanguages = embeddedLanguages;
this.expected = expected;
this.expectedPatch = expectedPatch;
this.patchedExpected = [];
let patchIndex = this.expectedPatch.length - 1;
for (let i = this.expected.length - 1; i >= 0; i--) {
let expectedElement = this.expected[i];
let content = expectedElement.content;
while (patchIndex >= 0 && i === this.expectedPatch[patchIndex].index) {
let patch = this.expectedPatch[patchIndex];
let patchContentIndex = content.lastIndexOf(patch.content);
let afterContent = content.substr(patchContentIndex + patch.content.length);
if (afterContent.length > 0) {
this.patchedExpected.unshift({
_r: expectedElement._r,
_t: expectedElement._t,
content: afterContent,
color: expectedElement.color
});
}
this.patchedExpected.unshift({
_r: expectedElement._r,
_t: expectedElement._t,
content: patch.content,
color: patch.newColor
});
content = content.substr(0, patchContentIndex);
patchIndex--;
}
if (content.length > 0) {
this.patchedExpected.unshift({
_r: expectedElement._r,
_t: expectedElement._t,
content: content,
color: expectedElement.color
});
}
}
this.backgroundColor = null;
this.actual = null;
this.diff = null;
this.patchedDiff = null;
}
public evaluate(themeData: ThemeData): Thenable<void> {
this.backgroundColor = themeData.theme.settings[0].settings.background;
return this._tokenizeWithThemeAsync(themeData).then(res => {
this.actual = res;
this.diff = SingleThemeTest.computeThemeTokenizationDiff(this.actual, this.expected);
this.patchedDiff = SingleThemeTest.computeThemeTokenizationDiff(this.actual, this.patchedExpected);
});
}
public getDiffPageData(): IDiffPageData {
return {
testContent: this.contents,
themeName: this.themeName,
backgroundColor: this.backgroundColor,
actual: this.actual,
expected: this.expected,
diff: this.diff,
patchedExpected: this.patchedExpected,
patchedDiff: this.patchedDiff
};
}
private _tokenizeWithThemeAsync(themeData: ThemeData): Thenable<IThemedToken[]> {
return themeData.registry.loadGrammarWithEmbeddedLanguages(this.initialScopeName, this.initialLanguage, this.embeddedLanguages).then(grammar => {
return tokenizeWithTheme(themeData.theme, themeData.registry.getColorMap(), this.contents, grammar);
});
}
private static computeThemeTokenizationDiff(_actual: IThemedToken[], _expected: IExpectedTokenization[]): ITokenizationDiff[] {
let canonicalTokens: string[] = [];
for (let i = 0, len = _actual.length; i < len; i++) {
let explanation = _actual[i].explanation;
for (let j = 0, lenJ = explanation.length; j < lenJ; j++) {
canonicalTokens.push(explanation[j].content);
}
}
let actual: IActualCanonicalToken[] = [];
for (let i = 0, len = _actual.length; i < len; i++) {
let item = _actual[i];
for (let j = 0, lenJ = item.explanation.length; j < lenJ; j++) {
actual.push({
content: item.explanation[j].content,
color: item.color,
scopes: item.explanation[j].scopes
});
}
}
let expected: IExpectedCanonicalToken[] = [];
for (let i = 0, len = _expected.length, canonicalIndex = 0; i < len; i++) {
let item = _expected[i];
let content = item.content;
while (content.length > 0) {
expected.push({
oldIndex: i,
content: canonicalTokens[canonicalIndex],
color: item.color,
_t: item._t,
_r: item._r
});
content = content.substr(canonicalTokens[canonicalIndex].length);
canonicalIndex++;
}
}
if (actual.length !== expected.length) {
throw new Error('Content mismatch');
}
let diffs: ITokenizationDiff[] = [];
for (let i = 0, len = actual.length; i < len; i++) {
let expectedItem = expected[i];
let actualItem = actual[i];
let contentIsInvisible = /^\s+$/.test(expectedItem.content);
if (contentIsInvisible) {
continue;
}
if (actualItem.color.substr(0, 7) !== expectedItem.color) {
diffs.push({
oldIndex: expectedItem.oldIndex,
oldToken: expectedItem,
newToken: actualItem
});
}
}
return diffs;
}
}