add tests

This commit is contained in:
Benjamin Pasero
2025-02-19 12:42:36 +01:00
parent 2e1fbe4aeb
commit 52d6a94d3d
4 changed files with 47 additions and 6 deletions

View File

@@ -266,6 +266,7 @@ suite('vscode API - workspace-fs', () => {
// without setting
assert.strictEqual(await vscode.workspace.fs.decode(uri, Buffer.from('Hello World')), 'Hello World');
assert.strictEqual(await vscode.workspace.fs.decode(uri, Buffer.from('Hellö Wörld')), 'Hellö Wörld');
assert.strictEqual(await vscode.workspace.fs.decode(uri, new Uint8Array([0xEF, 0xBB, 0xBF, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100])), 'Hello World'); // UTF-8 with BOM
assert.strictEqual(await vscode.workspace.fs.decode(uri, new Uint8Array([0xFE, 0xFF, 0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100])), 'Hello World'); // UTF-16 BE with BOM
assert.strictEqual(await vscode.workspace.fs.decode(uri, new Uint8Array([0xFF, 0xFE, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0])), 'Hello World'); // UTF-16 LE with BOM
@@ -288,4 +289,43 @@ suite('vscode API - workspace-fs', () => {
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'utf8', vscode.ConfigurationTarget.Global);
}
});
test('fs.encode', async function () {
const uri = root.with({ path: posix.join(root.path, 'file.txt') });
// without setting
assert.strictEqual((await vscode.workspace.fs.encode(uri, 'Hello World')).toString(), 'Hello World');
// with encoding setting
try {
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'utf8bom', vscode.ConfigurationTarget.Global);
assert.ok(equalsUint8Array(await vscode.workspace.fs.encode(uri, 'Hello World'), new Uint8Array([0xEF, 0xBB, 0xBF, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100])));
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'utf16le', vscode.ConfigurationTarget.Global);
assert.ok(equalsUint8Array(await vscode.workspace.fs.encode(uri, 'Hello World'), new Uint8Array([0xFF, 0xFE, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0])));
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'utf16be', vscode.ConfigurationTarget.Global);
assert.ok(equalsUint8Array(await vscode.workspace.fs.encode(uri, 'Hello World'), new Uint8Array([0xFE, 0xFF, 0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100])));
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'cp1252', vscode.ConfigurationTarget.Global);
assert.ok(equalsUint8Array(await vscode.workspace.fs.encode(uri, 'Hellö Wörld'), new Uint8Array([72, 101, 108, 108, 0xF6, 32, 87, 0xF6, 114, 108, 100])));
} finally {
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'utf8', vscode.ConfigurationTarget.Global);
}
});
function equalsUint8Array(a: Uint8Array, b: Uint8Array): boolean {
if (a === b) {
return true;
}
if (a.byteLength !== b.byteLength) {
return false;
}
for (let i = 0; i < a.byteLength; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
});