forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonig.test.ts
More file actions
111 lines (99 loc) · 4.13 KB
/
onig.test.ts
File metadata and controls
111 lines (99 loc) · 4.13 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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import * as fs from 'fs';
import * as path from 'path';
import * as durations from 'durations';
import { IGrammarRegistration, Resolver, ILanguageRegistration } from './resolver';
import { getOnigasm, getOniguruma } from '../onigLibs';
import { Registry, StackElement } from '../main';
declare module 'durations';
describe.skip('Compare OnigLibs outputs', () => {
let registrations = getVSCodeRegistrations();;
if (!registrations) {
console.log('vscode repo ot found, skipping OnigLibs tests');
return;
}
let onigurumaResolver = new Resolver(registrations.grammarRegistrations, registrations.languageRegistrations, getOniguruma(), 'oniguruma');
let onigasmResolver = new Resolver(registrations.grammarRegistrations, registrations.languageRegistrations, getOnigasm(), 'onigasm');
const fixturesDir = path.join(__dirname, '../../test-cases/onigtests/fixtures');
const fixturesFiles = fs.readdirSync(fixturesDir);
for (let fixturesFile of fixturesFiles) {
let testFilePath = path.join(fixturesDir, fixturesFile);
let scopeName = onigurumaResolver.findScopeByFilename(fixturesFile);
addTest(scopeName, testFilePath, new Registry(onigurumaResolver), new Registry(onigasmResolver));
}
});
async function addTest(scopeName: string, filePath: string, onigurumaRegistry: Registry, onigasmRegistry: Registry) {
(<any>it(scopeName + '/' + path.basename(filePath), async () => {
const fileContent = fs.readFileSync(filePath).toString();
let lines = fileContent.match(/[^\r\n]+/g);
let prevState1: StackElement = null;
let prevState2: StackElement = null;
let grammar1 = await onigurumaRegistry.loadGrammar(scopeName);
let grammar2 = await onigasmRegistry.loadGrammar(scopeName);
let stopWatch1 = durations.stopwatch();
let stopWatch2 = durations.stopwatch();
for (let i = 0; i < lines.length; i++) {
stopWatch1.start();
let t1 = grammar1.tokenizeLine(lines[i], prevState1);
stopWatch1.stop();
stopWatch2.start();
let t2 = grammar2.tokenizeLine(lines[i], prevState2);
stopWatch2.stop();
assert.deepEqual(t2.tokens, t1.tokens, `Difference at line ${i}: ${lines[i]}`);
prevState1 = t1.ruleStack;
prevState2 = t2.ruleStack;
}
console.log(`Oniguruma: ${stopWatch1.format()}, Onigasm: ${stopWatch2.format()} (${Math.round(stopWatch2.duration().micros() * 10 / stopWatch1.duration().micros()) / 10}x slower)`);
})).timeout(1000000);
}
function getVSCodeRegistrations(): { grammarRegistrations: IGrammarRegistration[], languageRegistrations: ILanguageRegistration[] } {
const grammarRegistrations: IGrammarRegistration[] = [];
const languageRegistrations: ILanguageRegistration[] = [];
const extensionsPath = path.join(__dirname, '../../../vscode/extensions');
if (!fs.existsSync(extensionsPath)) {
return null;
}
const extDirs = fs.readdirSync(extensionsPath);
for (let ext of extDirs) {
try {
let packageJSONPath = path.join(extensionsPath, ext, 'package.json');
if (!fs.existsSync(packageJSONPath)) {
continue;
}
let packageJSON = JSON.parse(fs.readFileSync(packageJSONPath).toString());
let contributes = packageJSON['contributes'];
if (contributes) {
let grammars = contributes['grammars'];
if (Array.isArray(grammars)) {
for (let grammar of grammars) {
let registration: IGrammarRegistration = {
scopeName: grammar.scopeName,
path: path.join(extensionsPath, ext, grammar.path),
language: grammar.language,
embeddedLanguages: grammar.embeddedLanguages
};
grammarRegistrations.push(registration);
}
}
let languages = contributes['languages'];
if (Array.isArray(languages)) {
for (let language of languages) {
let registration: ILanguageRegistration = {
id: language.id,
filenames: language.filenames,
extensions: language.extensions
};
languageRegistrations.push(registration);
}
}
}
} catch (e) {
// i
}
}
return { grammarRegistrations, languageRegistrations };
}