1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

实现最基础的 Windows <-> Unix 风格互转逻辑;添加基础测试用例并通过

This commit is contained in:
程序员小墨 2024-12-14 22:00:12 +08:00
parent 6248f3d0cd
commit 6fce88f88a
4 changed files with 163 additions and 6 deletions

View File

@ -1,6 +1,60 @@
import { SupportPathFormat } from "./types/SupportPathFormatType";
/** / */
const LEFT_SLASH = '/';
/** \ */
const RIGHT_SLASH = '\\';
/** \\ */
const DOUBLE_RIGHT_SLASH = '\\\\';
export function pathConversion(SupportPathFormat: SupportPathFormat, input: string) {
// TODO
export function pathConversion(targetPathType: SupportPathFormat, input: string): string {
let resultPath;
let isSeperator = false;
switch (targetPathType) {
case SupportPathFormat.Windows:
// 将其中的 / 替换为 \
resultPath = Array.from(input).map((char: string) => {
if (char !== LEFT_SLASH) {
// 当前字符不是 /
isSeperator = false;
return char;
} else {
// 当前字符是 /
if (!isSeperator) {
// 上一字符不是 /
isSeperator = true;
return RIGHT_SLASH; // 替换成 \
}
// 上一字符是 /
return '';
}
}).join('');
break;
case SupportPathFormat.Unix:
// 将其中的 \\ 和 \ 替换为 /
resultPath = Array.from(input).map((char: string) => {
if (char !== RIGHT_SLASH) {
// 当前字符不是 \
isSeperator = false;
return char;
} else {
// 当前字符是 \
if (!isSeperator) {
// 上一字符不是 \
isSeperator = true;
return LEFT_SLASH; // 替换成 /
}
// 上一字符是 \
return '';
}
}).join('');
break;
// case SupportPathFormat.WindowsGitBash:
// break;
default:
return input;
}
return resultPath;
}

View File

@ -116,8 +116,8 @@ suite('Extension Test: run path convert test case', () => {
for (const input of inputList) {
// console.log('input', '->' + input + '<-');
// 验证转换
assert.strictEqual(testCase.output.Windows, pathConversion(SupportPathFormat.Windows, input), 'Windows path format test failed.');
assert.strictEqual(testCase.output.Unix, pathConversion(SupportPathFormat.Unix, input), 'Unix path format test failed.');
assert.strictEqual(testCase.output.Windows.unEscape, pathConversion(SupportPathFormat.Windows, input), 'Windows path format test failed.');
assert.strictEqual(testCase.output.Unix.unEscape, pathConversion(SupportPathFormat.Unix, input), 'Unix path format test failed.');
}
});
}

View File

@ -1,4 +1,101 @@
import { PathTestCaseGroup } from "./types/PathTestCaseType";
export const pathConvertTestGroups: Array<PathTestCaseGroup> = [
{
group: 'Normal Path Format Convert',
testTitle: 'Normal Path Format Convert (常规路径风格转换)',
cases: [
{
title: 'Windows 风格',
input: // E:\Project\variable-conversion-vscode-extension
'E:\\Project\\variable-conversion-vscode-extension',
output: {
Windows: {
unEscape: // E:\Project\variable-conversion-vscode-extension
'E:\\Project\\variable-conversion-vscode-extension',
escape: // E:\\Project\\variable-conversion-vscode-extension
'E:\\\\Project\\\\variable-conversion-vscode-extension',
},
Unix: {
unEscape:
'E:/Project/variable-conversion-vscode-extension',
escape: // E:\/Project\/variable-conversion-vscode-extension
'E:\\/Project\\/variable-conversion-vscode-extension',
},
},
},
{
title: 'Unix 风格',
input: '/home/user/file.txt',
output: {
Windows: {
unEscape: // \home\user\file.txt
'\\home\\user\\file.txt',
escape: // \\home\\user\\file.txt
'\\\\home\\\\user\\\\file.txt',
},
Unix: {
unEscape:
'/home/user/file.txt',
escape: // \/home\/user\/file.txt
'\\/home\\/user\\/file.txt',
},
},
},
{
title: 'Windows (Git Bash) 风格',
input: '/c/Users/test/file.txt',
output: {
Windows: {
unEscape: // \c\Users\test/file.txt
'\\c\\Users\\test\\file.txt',
escape: // \\c\\Users\\test\\file.txt
'\\\\c\\\\Users\\\\test\\\\file.txt',
// TODO need to transform to ↓
/*
unEscape: // C:\Users\test\file.txt
'C:\\Users\\test\\file.txt',
escape: // C:\\Users\\test\\file.txt
'C:\\\\Users\\\\test\\\\file.txt',
*/
},
Unix: {
unEscape:
'/c/Users/test/file.txt',
escape: // \/c\/Users\/test\/file.txt
'\\/c\\/Users\\/test\\/file.txt',
},
},
},
// TODO
// Windows 局域网主机名风格
// \\ComputerName
// 路径带空格
// /home/user/hello world.txt
// /home/user/hello world.txt
// and more ...
// {
// title: '',
// input: //
// '',
// output: {
// Windows: {
// unEscape: //
// '',
// escape: //
// '',
// },
// Unix: {
// unEscape: //
// '',
// escape: //
// '',
// },
// },
// },
],
},
];

View File

@ -1,3 +1,4 @@
import exp from "constants";
export type PathTestCaseGroup = {
group: string
@ -5,11 +6,16 @@ export type PathTestCaseGroup = {
cases: Array<PathTestCase>
};
export type PathTestOutputResult = {
unEscape: string;
escape: string;
};
export type PathTestCase = {
title: string
input: string | Array<string>
output: {
Windows: string
Unix: string
Windows: PathTestOutputResult
Unix: PathTestOutputResult
}
};