Skip to content

Commit aadeb4e

Browse files
zhaogeshiqiWang0
authored andcommitted
feat: update contribute ts and update unit test
1 parent 56df21f commit aadeb4e

File tree

6 files changed

+128
-26
lines changed

6 files changed

+128
-26
lines changed

src/_.contribution.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { languages } from './fillers/monaco-editor-core';
6+
import { editor, languages } from './fillers/monaco-editor-core';
77

88
interface ILang extends languages.ILanguageExtensionPoint {
99
loader: () => Promise<ILangImpl>;
@@ -39,11 +39,6 @@ class LazyLanguageLoader {
3939
this._lazyLoadPromiseReject = reject;
4040
});
4141
}
42-
43-
public whenLoaded(): Promise<ILangImpl> {
44-
return this._lazyLoadPromise;
45-
}
46-
4742
public load(): Promise<ILangImpl> {
4843
if (!this._loadingTriggered) {
4944
this._loadingTriggered = true;
@@ -58,22 +53,24 @@ class LazyLanguageLoader {
5853

5954
export async function loadLanguage(languageId: string): Promise<void> {
6055
await LazyLanguageLoader.getOrCreate(languageId).load();
56+
// trigger tokenizer creation by instantiating a model
57+
const model = editor.createModel('', languageId);
58+
model.dispose();
6159
}
6260

6361
export function registerLanguage(def: ILang): void {
6462
const languageId = def.id;
65-
6663
languageDefinitions[languageId] = def;
6764
languages.register(def);
6865

6966
const lazyLanguageLoader = LazyLanguageLoader.getOrCreate(languageId);
70-
71-
languages.setMonarchTokensProvider(
72-
languageId,
73-
lazyLanguageLoader.whenLoaded().then((mod) => mod.language)
74-
);
75-
76-
languages.onLanguage(languageId, async () => {
67+
languages.registerTokensProviderFactory(languageId, {
68+
create: async (): Promise<languages.IMonarchLanguage> => {
69+
const mod = await lazyLanguageLoader.load();
70+
return mod.language;
71+
}
72+
});
73+
languages.onLanguageEncountered(languageId, async () => {
7774
const mod = await lazyLanguageLoader.load();
7875
languages.setLanguageConfiguration(languageId, mod.conf);
7976
});

src/languages/hive/hive.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import './hive.contribution';
7+
8+
import { postfixTokenClass, TokenClassConsts } from '../../common/constants';
69
import { testTokenization } from '../../test/testRunner';
7-
import { TokenClassConsts, postfixTokenClass } from '../../common/constants';
810

911
testTokenization('hivesql', [
1012
// Comments

src/test/testRunner.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import '../all.contributions';
7-
import { loadLanguage } from '../_.contribution';
7+
88
import * as assert from 'assert';
9+
10+
import { loadLanguage } from '../_.contribution';
911
import { editor } from '../fillers/monaco-editor-core';
1012

1113
export interface IRelaxedToken {
@@ -59,5 +61,10 @@ function runTest(languageId: string, test: ITestItem[]): void {
5961
};
6062
});
6163

62-
assert.deepStrictEqual(actual, test);
64+
try {
65+
assert.deepStrictEqual(actual, test);
66+
} catch (err) {
67+
console.error(`❌ Tokenization mismatch for ${languageId}:`);
68+
throw err;
69+
}
6370
}

test/all.js

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ const requirejs = require('requirejs');
22
const jsdom = require('jsdom');
33
const glob = require('fast-glob');
44
const path = require('path');
5+
const Mocha = require('mocha');
56

67
requirejs.config({
7-
baseUrl: '',
8+
baseUrl: path.join(__dirname, '../'),
89
paths: {
10+
vs: 'node_modules/monaco-editor/dev/vs',
911
'vs/css': 'test/css.mock',
1012
'vs/nls': 'test/nls.mock',
11-
'out/amd/fillers/monaco-editor-core': 'out/amd/fillers/monaco-editor-core-amd',
12-
vs: 'node_modules/monaco-editor/dev/vs'
13+
'out/amd/fillers/monaco-editor-core': 'out/amd/fillers/monaco-editor-core-amd'
1314
},
1415
nodeRequire: require
1516
});
@@ -24,6 +25,9 @@ global.document.queryCommandSupported = function () {
2425
};
2526
global.UIEvent = tmp.window.UIEvent;
2627
global.define = requirejs.define;
28+
29+
// 添加完整的DOM环境支持
30+
global.Element = tmp.window.Element;
2731
global.window = {
2832
location: {},
2933
navigator: tmp.window.navigator,
@@ -32,8 +36,18 @@ global.window = {
3236
matches: false,
3337
addEventListener: function () {}
3438
};
35-
}
39+
},
40+
setInterval: setInterval,
41+
clearInterval: clearInterval,
42+
setTimeout: setTimeout,
43+
clearTimeout: clearTimeout,
44+
document: tmp.window.document,
45+
Element: tmp.window.Element
3646
};
47+
if (!document.body) {
48+
const body = document.createElement('body');
49+
document.appendChild(body);
50+
}
3751

3852
requirejs(
3953
['./test/setup'],
@@ -52,10 +66,37 @@ requirejs(
5266
requirejs(
5367
files.map((f) => f.replace(/\.js$/, '')),
5468
function () {
55-
run(); // We can launch the tests!
69+
// 初始化Mocha
70+
const mocha = new Mocha({
71+
ui: 'bdd',
72+
reporter: 'spec',
73+
timeout: 5000
74+
});
75+
76+
// 手动添加测试到Mocha的suite
77+
const Suite = require('mocha/lib/suite');
78+
const Test = require('mocha/lib/test');
79+
80+
// 创建一个根suite
81+
const rootSuite = new Suite('Root Suite');
82+
mocha.suite.addSuite(rootSuite);
83+
84+
// 添加存储的测试
85+
if (global._pendingTests && global._pendingTests.length > 0) {
86+
global._pendingTests.forEach(function (test) {
87+
const mochaTest = new Test(test.name, test.fn);
88+
rootSuite.addTest(mochaTest);
89+
});
90+
}
91+
92+
// 运行测试
93+
mocha.run(function (failures) {
94+
process.exit(failures ? 1 : 0);
95+
});
5696
},
5797
function (err) {
58-
console.log(err);
98+
console.log('Error loading test files:', err);
99+
process.exit(1);
59100
}
60101
);
61102
},

test/setup.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ define('vs/nls', [], {
1010
localize: function () {
1111
return 'NO_LOCALIZATION_FOR_YOU';
1212
},
13+
localize2: function () {
14+
return 'NO_LOCALIZATION_FOR_YOU';
15+
},
1316
getConfiguredDefaultLocale: function () {
1417
return undefined;
1518
}
@@ -18,8 +21,8 @@ define('vs/nls', [], {
1821
localize: function () {
1922
return 'NO_LOCALIZATION_FOR_YOU';
2023
},
21-
localize2: function () {
22-
return 'NO_LOCALIZATION_FOR_YOU';
24+
localize2: function (key, message) {
25+
return { value: 'NO_LOCALIZATION_FOR_YOU', original: message };
2326
},
2427
load: function (name, req, load) {
2528
load({});
@@ -29,3 +32,52 @@ define('vs/nls', [], {
2932
define(['vs/editor/editor.main'], function (api) {
3033
global.monaco = api;
3134
});
35+
36+
// 定义Mocha全局函数
37+
global.test = function (name, fn) {
38+
// 检查函数是否返回Promise(包括编译后的异步函数)
39+
const isPromiseFunction =
40+
fn &&
41+
typeof fn === 'function' &&
42+
((fn.constructor && fn.constructor.name === 'AsyncFunction') ||
43+
fn.toString().includes('__awaiter') ||
44+
fn.toString().includes('__generator') ||
45+
fn.toString().includes('Promise'));
46+
47+
if (isPromiseFunction) {
48+
// 异步函数需要包装为Mocha能理解的格式
49+
const wrappedFn = function (done) {
50+
const result = fn();
51+
if (result && typeof result.then === 'function') {
52+
result
53+
.then(() => {
54+
if (done) done();
55+
})
56+
.catch((err) => {
57+
if (done) done(err);
58+
});
59+
} else {
60+
if (done) done();
61+
}
62+
};
63+
global._pendingTests = global._pendingTests || [];
64+
global._pendingTests.push({ name, fn: wrappedFn });
65+
} else {
66+
global._pendingTests = global._pendingTests || [];
67+
global._pendingTests.push({ name, fn });
68+
}
69+
};
70+
71+
global.describe =
72+
global.describe ||
73+
function (name, fn) {
74+
global._pendingSuites = global._pendingSuites || [];
75+
global._pendingSuites.push({ name, fn });
76+
};
77+
78+
global.it =
79+
global.it ||
80+
function (name, fn) {
81+
global._pendingTests = global._pendingTests || [];
82+
global._pendingTests.push({ name, fn });
83+
};

tsconfig.amd.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"module": "amd",
55
"outDir": "out/amd/",
66
"target": "es5",
7-
"noEmit": false
7+
"noEmit": false,
8+
"sourceMap": true,
9+
"allowJs": true,
10+
"checkJs": false
811
}
912
}

0 commit comments

Comments
 (0)