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

编写测试用例

This commit is contained in:
程序员小墨 2024-04-02 21:21:47 +08:00
parent 37c2442505
commit 4b046a7d14
6 changed files with 450 additions and 324 deletions

View File

@ -1,10 +1,10 @@
import { transformText } from './text-split'; import { transformText } from './text-transform';
/** /**
* to Camel Case * to Camel Case
* *
* @param {string} str user selection * @param {string} str user selection
* @returns * @returns
* @since 2024-03-28 * @since 2024-03-28
*/ */
export function toCamelCase(str: string): string { export function toCamelCase(str: string): string {
@ -19,9 +19,9 @@ export function toCamelCase(str: string): string {
/** /**
* to Pascal Case * to Pascal Case
* *
* @param {string} str user selection * @param {string} str user selection
* @returns * @returns
* @since 2024-03-28 * @since 2024-03-28
*/ */
export function toPascalCase(str: string): string { export function toPascalCase(str: string): string {
@ -30,9 +30,9 @@ export function toPascalCase(str: string): string {
/** /**
* to Upper Case * to Upper Case
* *
* @param {string} str user selection * @param {string} str user selection
* @returns * @returns
* @since 2024-03-28 * @since 2024-03-28
*/ */
export function toUpperCase(str: string): string { export function toUpperCase(str: string): string {
@ -41,9 +41,9 @@ export function toUpperCase(str: string): string {
/** /**
* to Lower Case * to Lower Case
* *
* @param {string} str user selection * @param {string} str user selection
* @returns * @returns
* @since 2024-03-28 * @since 2024-03-28
*/ */
export function toLowerCase(str: string): string { export function toLowerCase(str: string): string {

View File

@ -1,56 +1,56 @@
const logDebugInfo = false; const logDebugInfo = false;
/** /**
* *
* *
* @param str * @param str
* @since 2024-04-02 * @since 2024-04-02
*/ */
export function transformText(input: string): string { export function transformText(input: string): string {
logDebugInfo && console.log('input ', '->' + input + '<-'); logDebugInfo && console.log('input ', '->' + input + '<-');
// 记录首尾空格 // 记录首尾空格
const leadingSpaces = input.match(/^ +/); const leadingSpaces = input.match(/^ +/);
const trailingSpaces = input.match(/ +$/); const trailingSpaces = input.match(/ +$/);
// 去除首尾空格 // 去除首尾空格
input = input.trim(); input = input.trim();
// 使用正则表达式匹配中英文字母、连字符、下划线和空格 // 使用正则表达式匹配中英文字母、连字符、下划线和空格
let result = input.replace(/([A-Za-z\-_ ]+)/g, (match: string) => { let result = input.replace(/([A-Za-z\-_ ]+)/g, (match: string) => {
// 替换连字符为 '|' (如有多个则合并) // 替换连字符为 '|' (如有多个则合并)
match = match.replace(/[-_ ]+/g, '|'); match = match.replace(/[-_ ]+/g, '|');
// 拆分连续的小写字母和大写字母为多个单词 // 拆分连续的小写字母和大写字母为多个单词
match = match.replace(/([a-z])([A-Z])/g, '$1|$2'); match = match.replace(/([a-z])([A-Z])/g, '$1|$2');
// 分割 // 分割
let words = match.split('|'); let words = match.split('|');
// 处理特殊情况,如 'ENFADADO' 不应该被拆分 // 处理特殊情况,如 'ENFADADO' 不应该被拆分
words = words.map(word => { words = words.map(word => {
if (word.toUpperCase() === word && word.length > 1) { if (word.toUpperCase() === word && word.length > 1) {
return word.toLowerCase(); return word.toLowerCase();
} }
return word.replace(/([A-Z])/g, '|$1').toLowerCase(); return word.replace(/([A-Z])/g, '|$1').toLowerCase();
}); });
// 重新组合单词 // 重新组合单词
return '|' + words.join('|') + '|'; return '|' + words.join('|') + '|';
}); });
// 如果有多个 | 将其合并 // 如果有多个 | 将其合并
result = result.replace(/[\|]+/g, '|'); result = result.replace(/[\|]+/g, '|');
// 如果首尾有 | 将其替换掉 // 如果首尾有 | 将其替换掉
result = result.replace(/(^[\|]+|[\|]+$)/g, ''); result = result.replace(/(^[\|]+|[\|]+$)/g, '');
// 还原首尾空格 // 还原首尾空格
// result = (leadingSpaces ? (leadingSpaces[0] + '|') : '') + result + (trailingSpaces ? ('|' + trailingSpaces[0]) : ''); // result = (leadingSpaces ? (leadingSpaces[0] + '|') : '') + result + (trailingSpaces ? ('|' + trailingSpaces[0]) : '');
result = (leadingSpaces ? leadingSpaces[0] : '') + result + (trailingSpaces ? trailingSpaces[0] : ''); result = (leadingSpaces ? leadingSpaces[0] : '') + result + (trailingSpaces ? trailingSpaces[0] : '');
logDebugInfo && console.log('output ', '->' + result + '<-'); logDebugInfo && console.log('output ', '->' + result + '<-');
return result; return result;
} }

View File

@ -3,8 +3,13 @@ import * as assert from 'assert';
// You can import and use all API from the 'vscode' module // You can import and use all API from the 'vscode' module
// as well as import your extension to test it // as well as import your extension to test it
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import testGroups from './test-case';
import { TestCase, TestCaseGroup } from '../type-definition/test-case-type';
import { transformText } from '../main-code/text-transform';
import { toCamelCase, toPascalCase } from '../main-code/text-conversion';
// import * as myExtension from '../../extension'; // import * as myExtension from '../../extension';
/*
suite('Extension Test Suite', () => { suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.'); vscode.window.showInformationMessage('Start all tests.');
@ -13,3 +18,29 @@ suite('Extension Test Suite', () => {
assert.strictEqual(-1, [1, 2, 3].indexOf(0)); assert.strictEqual(-1, [1, 2, 3].indexOf(0));
}); });
}); });
*/
suite('Extension Test: run test case', () => {
vscode.window.showInformationMessage('Start all tests.');
// transform to camel case
const groups: Array<TestCaseGroup> = testGroups;
for (const testGroup of groups) {
const testTitle = testGroup.testTitle;
const testCases: Array<TestCase> = testGroup.cases;
for (const testCase of testCases) {
test(testTitle + ' - ' + testCase.title, () => {
const inputList = Array.isArray(testCase.input) ? testCase.input : [testCase.input];
for (const input of inputList) {
// 验证 transformText
assert.strictEqual(testCase.transformText, transformText(input));
// 验证转换
assert.strictEqual(testCase.output.camelCase, toCamelCase(input));
assert.strictEqual(testCase.output.pascalCase, toPascalCase(input));
}
});
}
}
});

View File

@ -1,183 +1,263 @@
import { TestCaseGroup } from "../type-definition/test-case-type";
const testGroups: Array<TestCaseGroup> = [
{
group: 'Input validation',
testTitle: 'Input validation (输入有效性验证)',
cases: [
// 输入为空
{
title: 'empty input',
input: '',
transformText: '',
output: {
camelCase: '',
pascalCase: '',
},
},
{
title: 'empty input (contains space)',
input: ' ',
transformText: ' ',
output: {
camelCase: ' ',
pascalCase: ' ',
},
},
// 输入长文本
{
title: 'long text input',
input:
`China's factory activity expanded in March after five consecutive months of contraction, an official survey revealed on Sunday, adding to a run of indicators that suggest the stabilization of the world's second-largest economy.`
,
transformText: '',
output: {
camelCase: '',
pascalCase: '',
},
},
// 输入包含换行
{
title: 'enter input',
input:
`How do you\bdo?
How do you\tdo!
`
,
transformText: '',
output: {
camelCase: '',
pascalCase: '',
},
},
// 非英文字符,特殊字符
{
title: 'Chinese input',
input:
'今天是星期日'
,
transformText: '',
output: {
camelCase: '今天是星期日',
pascalCase: '今天是星期日',
},
},
{
title: 'Special character with emoji input',
input:
'🥰 a-cup/_of Coffee🍻,-_please!. '
,
transformText: '',
output: {
camelCase: '',
pascalCase: '',
},
},
{
title: 'Random input',
input:
' NHDAs--+90-usz&* '
,
transformText: '',
output: {
camelCase: '',
pascalCase: '',
},
},
{
title: 'Mixed input 1',
input:
'--担心你鸿dAf_=coffee—_— '
,
transformText: '',
output: {
camelCase: '',
pascalCase: '',
},
},
{
title: 'Mixed input 2',
input:
'fsdi_sdacsaf+desd'
,
transformText: '',
output: {
camelCase: '',
pascalCase: '',
},
},
// add more cases...
],
},
{
group: 'Functional verification',
testTitle: 'Functional verification (功能验证)',
cases: [
// 正常输入
{
title: 'Normal input (foo-bar)',
input: [
'foo--bar',
'-foo -bar',
'__foo - _bar-__',
],
transformText: 'foo|bar',
output: {
camelCase: 'fooBar',
pascalCase: 'FooBar',
},
},
{
title: 'Normal input (test-case)',
input: [
'test case',
'test_case',
'test-case',
'testCase',
'TestCase',
'TEST_CASE',
],
transformText: 'test|case',
output: {
camelCase: 'testCase',
pascalCase: 'TestCase',
},
},
{
title: 'Normal input (pine-apple)',
input: [
'pineApple',
],
transformText: 'pine|apple',
output: {
camelCase: 'pineApple',
pascalCase: 'PineApple',
},
},
{
title: 'Normal input (have-a-nice-day!)',
input: [
'Have a nice day!',
'have a nice day!',
'Have-a-nice-day!',
'have-a-nice-day!',
'HAVE-A-NICE-DAY!',
'Have_a_nice_day!',
'have_a_nice_day!',
'HAVE_A_NICE_DAY!',
'HaveANiceDay!',
'haveANiceDay!',
],
transformText: 'have|a|nice|day|!',
output: {
camelCase: 'haveANiceDay!',
pascalCase: 'HaveANiceDay!',
},
},
{
title: 'Normal input (a-nice-day!)',
input: [
' A nice day! ',
' a_nice_day! ',
' ANiceDay! ',
' A_NICE_DAY! ',
' A_Nice_DaY! ',
' A-NiCe_Day! ',
' A----NiCe_Day_-_!-- ',
],
transformText: ' a|nice|day|! ',
output: {
camelCase: ' aNiceDay! ',
pascalCase: ' ANiceDay! ',
},
},
{
title: 'Normal input (a-nice-day)',
input: [
' A niCe-Day-',
' A niceDay',
],
transformText: ' a|nice|day',
output: {
camelCase: ' a niceDay',
pascalCase: ' A NiceDay',
},
},
{
title: 'Normal input (Julius-Caesar, William-Shakespeare, ...)',
input:
' Julius_Caesar, William_Shakespeare, Albert_Einstein, Marie_Curie, WolfgangAmadeusMozart, Vincent-van-Gogh. '
,
transformText: ' julius|caesar|,|william|shakespeare|,|albert|einstein|,|marie|curie|,|wolfgang|amadeus|mozart|,|vincent|van|gogh|. ',
output: {
camelCase: '',
pascalCase: '',
},
},
{
title: 'Normal input (&quot;You&quot; (or &quot;Your&quot;) ...)',
input:
[
` &quot;You&quot; (or &quot;Your&quot;) shall mean an individual or Legal Entity`,
` exercising permissions granted by this License.`
].join('\n')
,
transformText: ' &|quot|;|you|&|quot|;|(|or|&|quot|;|your|&|quot|;)|shall|mean|an|individual|or|legal|entity|\n|exercising|permissions|granted|by|this|license|.',
output: {
camelCase: ' &quot;You&quot; (or &quot;Your&quot;) shall mean an individual or Legal Entity\n exercising permissions granted by this License.',
pascalCase: ' &quot;You&quot; (or &quot;Your&quot;) shall mean an individual or Legal Entity\n exercising permissions granted by this License.',
},
},
// add more cases...
],
},
{
group: 'Extreme case verification',
testTitle: 'Extreme case verification (极端情况验证)',
cases: [
// 极端情况
enum SkipReason { // add more cases...
/** 空字符串 */
EMPTY_STRING,
/** 字符串过长 */
STRING_TOO_LONG,
/** 不包含英文字母 */
NOT_CONTAIN_LETTERS,
}
type TestSkipCase = {
/** 当 input 不是规范的输入时isSkip 为 true */
isSkip: true
skipReason: SkipReason
};
type TestReplaceCase = {
isSkip: false
splitResult: Array<string>
output: {
camelCase: string
pascalCase: string
}
};
type TestCase = (TestSkipCase | TestReplaceCase) & {
input: string | Array<string>
};
/*
Have a nice day!
foo--bar
foo -bar
pineApple
*/
const testCase: Array<TestCase> = [
{
input:
''
,
isSkip: true,
skipReason: SkipReason.EMPTY_STRING
},
{
input:
`China's factory activity expanded in March after five consecutive months of contraction, an official survey revealed on Sunday, adding to a run of indicators that suggest the stabilization of the world's second-largest economy.`
,
isSkip: true,
skipReason: SkipReason.STRING_TOO_LONG
},
{
input:
'今天是星期日'
,
isSkip: true,
skipReason: SkipReason.NOT_CONTAIN_LETTERS
},
{
input: [
'test case',
'test_case',
'test-case',
'testCase',
'TestCase',
'TEST_CASE',
], ],
isSkip: false, },
splitResult: [ {
'' group: 'Xxxx verification',
testTitle: 'Xxxx verification (xxx验证)',
cases: [
// Description
// {
// title: 'Normal input ( ...)',
// input: '',
// transformText: '',
// output: {
// camelCase: '',
// pascalCase: ''
// }
// },
// add more cases...
], ],
output: {
camelCase: 'testCase',
pascalCase: 'TestCase',
}
},
{
input: [
' A nice day! ',
' a_nice_day! ',
' ANiceDay! ',
' A_NICE_DAY! ',
' A_Nice_DaY ',
' A-NiCe_Day ',
' A----NiCe_Day_-_-- ',
],
isSkip: false,
splitResult: [
''
],
output: {
camelCase: ' aNiceDay! ',
pascalCase: ' ANiceDay! ',
}
},
{
input: [
' A niCe-Day-',
' A niceDay',
],
isSkip: false,
splitResult: [
''
],
output: {
camelCase: ' a niceDay',
pascalCase: ' A NiceDay',
}
},
{
input: [
'Have a nice day!',
'have a nice day!',
'Have-a-nice-day!',
'have-a-nice-day!',
'HAVE-A-NICE-DAY!',
'Have_a_nice_day!',
'have_a_nice_day!',
'HAVE_A_NICE_DAY!',
'HaveANiceDay!',
'haveANiceDay!',
],
isSkip: false,
splitResult: ['have', 'a', 'nice', 'day', '!'],
output: {
camelCase: ' a niceDay',
pascalCase: ' A NiceDay',
}
},
{
input:
' Julius_Caesar, William_Shakespeare, Albert_Einstein, Marie_Curie, WolfgangAmadeusMozart, Vincent-van-Gogh. '
,
isSkip: false,
splitResult: [],
output: {
camelCase: '',
pascalCase: '',
}
},
{
input:
'🥰 a-cup/_of Coffee🍻,-_please!. '
,
isSkip: false,
splitResult: [],
output: {
camelCase: '',
pascalCase: '',
}
},
{
input:
' NHDAs--+90-usz&* '
,
isSkip: false,
splitResult: [],
output: {
camelCase: '',
pascalCase: '',
}
},
{
input:
'--担心你鸿dAf_=coffee—_— '
,
isSkip: false,
splitResult: [],
output: {
camelCase: '',
pascalCase: '',
}
},
{
input:
'fsdi_sdacsaf+desd'
,
isSkip: false,
splitResult: [],
output: {
camelCase: '',
pascalCase: '',
}
}, },
// add more cases...
]; ];
export default testGroups;

View File

@ -1,82 +1,82 @@
export enum SupportCase { export enum SupportCase {
/** /**
* / * /
* Camel Case * Camel Case
* e.g. fooBar * e.g. fooBar
* *
* @alias: camelCase / CamelCase / camel_case / CAMEL_CASE * @alias: camelCase / CamelCase / camel_case / CAMEL_CASE
* @since 2024-04-02 * @since 2024-04-02
*/ */
CAMEL_CASE, CAMEL_CASE,
/** /**
* / * /
* Pascal Case * Pascal Case
* e.g. FooBar * e.g. FooBar
* *
* @alias: pascalCase / PascalCase / pascal_case / PASCAL_CASE * @alias: pascalCase / PascalCase / pascal_case / PASCAL_CASE
* @since 2024-04-02 * @since 2024-04-02
*/ */
PASCAL_CASE, PASCAL_CASE,
/** /**
* / * /
* Kebab Case / Spinal Case * Kebab Case / Spinal Case
* e.g. foo-bar * e.g. foo-bar
* *
* @alias: kebabCase / KebabCase / kebab_case / KEBAB_CASE * @alias: kebabCase / KebabCase / kebab_case / KEBAB_CASE
* spinalCase / SpinalCase / spinal_case / SPINAL_CASE * spinalCase / SpinalCase / spinal_case / SPINAL_CASE
* @since 2024-04-02 * @since 2024-04-02
*/ */
KEBAB_CASE, KEBAB_CASE,
/** /**
* 线 / * 线 /
* Snake Case * Snake Case
* e.g. foo_bar * e.g. foo_bar
* *
* @alias: snakeCase / SnakeCase / snake_case / SNAKE_CASE * @alias: snakeCase / SnakeCase / snake_case / SNAKE_CASE
* @since 2024-04-02 * @since 2024-04-02
*/ */
SNAKE_CASE, SNAKE_CASE,
/** /**
* 线 * 线
* Snake Upper Case * Snake Upper Case
* e.g. FOO_BAR * e.g. FOO_BAR
* *
* @alias: snakeUpperCase / SnakeUpperCase / snake_upper_case / SNAKE_UPPER_CASE * @alias: snakeUpperCase / SnakeUpperCase / snake_upper_case / SNAKE_UPPER_CASE
* @since 2024-04-02 * @since 2024-04-02
*/ */
SNAKE_UPPER_CASE, SNAKE_UPPER_CASE,
/** /**
* *
* Upper Case * Upper Case
* e.g. FOO_BAR / FOOBAR * e.g. FOO_BAR / FOOBAR
* *
* @alias: upperCase / UpperCase / upper_case / UPPER_CASE * @alias: upperCase / UpperCase / upper_case / UPPER_CASE
* @since 2024-04-02 * @since 2024-04-02
*/ */
UPPER_CASE, UPPER_CASE,
/** /**
* *
* Lower Case * Lower Case
* e.g. foo_bar / foobar * e.g. foo_bar / foobar
* *
* @alias: lowerCase / LowerCase / lower_case / LOWER_CASE * @alias: lowerCase / LowerCase / lower_case / LOWER_CASE
* @since 2024-04-02 * @since 2024-04-02
*/ */
LOWER_CASE, LOWER_CASE,
/** /**
* *
* Camel Snake Case * Camel Snake Case
* e.g. Foo_Bar * e.g. Foo_Bar
* *
* @alias: camelSnakeCase / CamelSnakeCase / camel_snake_case / CAMEL_SNAKE_CASE * @alias: camelSnakeCase / CamelSnakeCase / camel_snake_case / CAMEL_SNAKE_CASE
* @since 2024-04-02 * @since 2024-04-02
*/ */
CAMEL_SNAKE_CASE, CAMEL_SNAKE_CASE,
} }

View File

@ -0,0 +1,15 @@
export type TestCaseGroup = {
group: string
testTitle: string
cases: Array<TestCase>
};
export type TestCase = {
title: string
input: string | Array<string>
transformText: string
output: {
camelCase: string
pascalCase: string
}
};