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,4 +1,4 @@
import { transformText } from './text-split';
import { transformText } from './text-transform';
/**
* to Camel Case

View File

@ -3,8 +3,13 @@ import * as assert from 'assert';
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
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';
/*
suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.');
@ -13,3 +18,29 @@ suite('Extension Test Suite', () => {
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,60 +1,134 @@
import { TestCaseGroup } from "../type-definition/test-case-type";
enum SkipReason {
/** 空字符串 */
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> = [
const testGroups: Array<TestCaseGroup> = [
{
input:
''
,
isSkip: true,
skipReason: SkipReason.EMPTY_STRING
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.`
,
isSkip: true,
skipReason: SkipReason.STRING_TOO_LONG
transformText: '',
output: {
camelCase: '',
pascalCase: '',
},
},
// 输入包含换行
{
title: 'enter input',
input:
`How do you\bdo?
How do you\tdo!
`
,
transformText: '',
output: {
camelCase: '',
pascalCase: '',
},
},
// 非英文字符,特殊字符
{
title: 'Chinese input',
input:
'今天是星期日'
,
isSkip: true,
skipReason: SkipReason.NOT_CONTAIN_LETTERS
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',
@ -63,49 +137,25 @@ const testCase: Array<TestCase> = [
'TestCase',
'TEST_CASE',
],
isSkip: false,
splitResult: [
''
],
transformText: 'test|case',
output: {
camelCase: 'testCase',
pascalCase: 'TestCase',
}
},
},
{
title: 'Normal input (pine-apple)',
input: [
' A nice day! ',
' a_nice_day! ',
' ANiceDay! ',
' A_NICE_DAY! ',
' A_Nice_DaY ',
' A-NiCe_Day ',
' A----NiCe_Day_-_-- ',
],
isSkip: false,
splitResult: [
''
'pineApple',
],
transformText: 'pine|apple',
output: {
camelCase: ' aNiceDay! ',
pascalCase: ' ANiceDay! ',
}
},
{
input: [
' A niCe-Day-',
' A niceDay',
],
isSkip: false,
splitResult: [
''
],
output: {
camelCase: ' a niceDay',
pascalCase: ' A NiceDay',
}
camelCase: 'pineApple',
pascalCase: 'PineApple',
},
},
{
title: 'Normal input (have-a-nice-day!)',
input: [
'Have a nice day!',
'have a nice day!',
@ -118,66 +168,96 @@ const testCase: Array<TestCase> = [
'HaveANiceDay!',
'haveANiceDay!',
],
isSkip: false,
splitResult: ['have', 'a', 'nice', 'day', '!'],
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. '
,
isSkip: false,
splitResult: [],
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:
'🥰 a-cup/_of Coffee🍻,-_please!. '
[
` &quot;You&quot; (or &quot;Your&quot;) shall mean an individual or Legal Entity`,
` exercising permissions granted by this License.`
].join('\n')
,
isSkip: false,
splitResult: [],
transformText: ' &|quot|;|you|&|quot|;|(|or|&|quot|;|your|&|quot|;)|shall|mean|an|individual|or|legal|entity|\n|exercising|permissions|granted|by|this|license|.',
output: {
camelCase: '',
pascalCase: '',
}
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...
],
},
{
input:
' NHDAs--+90-usz&* '
,
isSkip: false,
splitResult: [],
output: {
camelCase: '',
pascalCase: '',
}
group: 'Extreme case verification',
testTitle: 'Extreme case verification (极端情况验证)',
cases: [
// 极端情况
// add more cases...
],
},
{
input:
'--担心你鸿dAf_=coffee—_— '
,
isSkip: false,
splitResult: [],
output: {
camelCase: '',
pascalCase: '',
}
},
{
input:
'fsdi_sdacsaf+desd'
,
isSkip: false,
splitResult: [],
output: {
camelCase: '',
pascalCase: '',
}
group: 'Xxxx verification',
testTitle: 'Xxxx verification (xxx验证)',
cases: [
// Description
// {
// title: 'Normal input ( ...)',
// input: '',
// transformText: '',
// output: {
// camelCase: '',
// pascalCase: ''
// }
// },
// add more cases...
],
},
// add more cases...
];
export default testGroups;

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
}
};